Skip to content

Commit

Permalink
Add InexistentExcelObjectException
Browse files Browse the repository at this point in the history
  • Loading branch information
sprain committed Oct 31, 2023
1 parent b8d58da commit 93a5ab2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
9 changes: 9 additions & 0 deletions lib/HtmlPhpExcel/Exception/InexistentExcelObjectException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Ticketpark\HtmlPhpExcel\Exception;

class InexistentExcelObjectException extends Exception
{

}
10 changes: 7 additions & 3 deletions lib/HtmlPhpExcel/HtmlPhpExcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use avadim\FastExcelWriter\Excel;
use Ticketpark\HtmlPhpExcel\Elements as HtmlPhpExcelElement;
use Ticketpark\HtmlPhpExcel\Exception\HtmlPhpExcelException;
use Ticketpark\HtmlPhpExcel\Exception\InexistentExcelObjectException;
use Ticketpark\HtmlPhpExcel\Parser\Parser;

class HtmlPhpExcel
Expand Down Expand Up @@ -103,18 +103,22 @@ public function process(?Excel $excel = null): self
public function download(string $filename): void
{
$filename = str_ireplace('.xlsx', '', $filename);
$this->excel->download($filename . '.xlsx');
$this->getExcelObject()->download($filename . '.xlsx');
}

public function save(string $filename): bool
{
$filename = str_ireplace('.xlsx', '', $filename);

return $this->excel->save($filename . '.xlsx');
return $this->getExcelObject()->save($filename . '.xlsx');
}

public function getExcelObject(): Excel
{
if (null === $this->excel) {
throw new InexistentExcelObjectException('You must run process() before handling the excel object. ');
}

return $this->excel;
}

Expand Down
27 changes: 26 additions & 1 deletion tests/HtmlPhpExcel/Tests/HtmlPhpExcelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use avadim\FastExcelWriter\Excel;
use PHPUnit\Framework\TestCase;
use Ticketpark\HtmlPhpExcel\Exception\InexistentExcelObjectException;
use Ticketpark\HtmlPhpExcel\HtmlPhpExcel;

/**
Expand Down Expand Up @@ -47,6 +48,30 @@ public function testSave()
public function testItReturnsExcelInstance()
{
$htmlphpexcel = new HtmlPhpExcel('<table></table>');
$this->assertInstanceOf(Excel::class, $htmlphpexcel->getExcelObject());
$this->assertInstanceOf(Excel::class, $htmlphpexcel->process()->getExcelObject());
}

public function testItThrowsExceptionIfProcessIsNotRunBeforeGettingExcelObject()
{
$this->expectException(InexistentExcelObjectException::class);

$htmlphpexcel = new HtmlPhpExcel('<table></table>');
$htmlphpexcel->getExcelObject();
}

public function testItThrowsExceptionIfProcessIsNotRunBeforeSave()
{
$this->expectException(InexistentExcelObjectException::class);

$htmlphpexcel = new HtmlPhpExcel('<table></table>');
$htmlphpexcel->save('foo');
}

public function testItThrowsExceptionIfProcessIsNotRunBeforeDownload()
{
$this->expectException(InexistentExcelObjectException::class);

$htmlphpexcel = new HtmlPhpExcel('<table></table>');
$htmlphpexcel->download('foo');
}
}

0 comments on commit 93a5ab2

Please sign in to comment.