Skip to content

Commit

Permalink
Added unit tests for PDF HTML.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Jun 11, 2024
1 parent 5bed9ff commit cf439d8
Show file tree
Hide file tree
Showing 9 changed files with 550 additions and 12 deletions.
8 changes: 1 addition & 7 deletions src/Pdf/Html/HtmlLiChunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,8 @@ private function findFont(): ?PdfFont
while ($chunk instanceof AbstractHtmlChunk && !$chunk->hasStyle()) {
$chunk = $chunk->getParent();
}
if ($chunk instanceof AbstractHtmlChunk) {
$style = $chunk->getStyle();
if ($style instanceof HtmlStyle) {
return $style->getFont();
}
}

return null;
return $chunk?->getStyle()?->getFont();
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/Pdf/Html/HtmlParentChunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,14 @@ public function outputChildren(HtmlReport $report): void
*/
public function remove(AbstractHtmlChunk $child): static
{
if (\in_array($child, $this->children, true)) {
$child->setParent(null);
$this->children = \array_diff($this->children, [$child]);
$index = $this->indexOf($child);
if (-1 === $index) {
return $this;
}

$child->setParent(null);
unset($this->children[$index]);

return $this;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Pdf/Traits/PdfChartLegendTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@

use App\Pdf\Colors\PdfDrawColor;
use App\Pdf\Colors\PdfFillColor;
use App\Pdf\Interfaces\PdfChartInterface;
use fpdf\PdfRectangleStyle;

/**
* Trait to draw chart legends.
*
* @psalm-import-type ColorStringType from \App\Pdf\Interfaces\PdfChartInterface
* @psalm-import-type ColorStringType from PdfChartInterface
*
* @psalm-require-extends \App\Pdf\PdfDocument
*
* @psalm-require-implements \App\Pdf\Interfaces\PdfChartInterface
* @psalm-require-implements PdfChartInterface
*/
trait PdfChartLegendTrait
{
Expand Down
103 changes: 103 additions & 0 deletions tests/Pdf/Html/HtmlParentChunkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/*
* This file is part of the Calculation package.
*
* (c) bibi.nu <bibi@bibi.nu>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Tests\Pdf\Html;

use App\Pdf\Html\HtmlPageBreakChunk;
use App\Pdf\Html\HtmlParentChunk;
use App\Pdf\Html\HtmlTag;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(HtmlParentChunk::class)]
class HtmlParentChunkTest extends TestCase
{
public function testAdd(): void
{
$actual = new HtmlParentChunk('body');
$chunk = new HtmlPageBreakChunk('fake');
$actual->add($chunk);
self::assertCount(1, $actual);

$actual->add($chunk);
self::assertCount(1, $actual);
}

public function testDefault(): void
{
$actual = new HtmlParentChunk('body');
self::assertCount(0, $actual);
self::assertEmpty($actual->getChildren());
self::assertNull($actual->getParent());
self::assertNull($actual->findChild(HtmlTag::BOLD));
self::assertTrue($actual->isEmpty());
self::assertFalse($actual->isNewLine());

$chunk = new HtmlPageBreakChunk('fake');
self::assertSame(-1, $actual->indexOf($chunk));
}

public function testFind(): void
{
$parent = new HtmlParentChunk(HtmlTag::BODY->value);
$actual = $parent->findChild(HtmlTag::H1);
self::assertNull($actual);

$span = new HtmlParentChunk(HtmlTag::SPAN->value);
$parent->add($span);

$pageBreak = new HtmlPageBreakChunk(HtmlTag::PAGE_BREAK->value);
$parent->add($pageBreak);

$actual = $parent->findChild(HtmlTag::SPAN);
self::assertNotNull($actual);

$actual = $parent->findChild(HtmlTag::PAGE_BREAK);
self::assertNotNull($actual);

$actual = $parent->findChild(HtmlTag::H1);
self::assertNull($actual);
}

public function testFindRecursive(): void
{
$parent = new HtmlParentChunk(HtmlTag::BODY->value);
$actual = $parent->findChild(HtmlTag::BOLD);
self::assertNull($actual);

$h1 = new HtmlParentChunk(HtmlTag::H1->value);
$parent->add($h1);

$h2 = new HtmlParentChunk(HtmlTag::H2->value);
$h1->add($h2);

$actual = $parent->findChild(HtmlTag::H1);
self::assertNotNull($actual);

$actual = $parent->findChild(HtmlTag::H2);
self::assertNotNull($actual);
}

public function testRemove(): void
{
$actual = new HtmlParentChunk(HtmlTag::BODY->value);
$chunk = new HtmlPageBreakChunk(HtmlTag::PAGE_BREAK->value);

$actual->add($chunk);
self::assertCount(1, $actual);
$actual->remove($chunk);
self::assertCount(0, $actual);

$actual->remove($chunk);
self::assertCount(0, $actual);
}
}
121 changes: 121 additions & 0 deletions tests/Pdf/Html/HtmlParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/*
* This file is part of the Calculation package.
*
* (c) bibi.nu <bibi@bibi.nu>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Tests\Pdf\Html;

use App\Pdf\Html\HtmlBrChunk;
use App\Pdf\Html\HtmlLiChunk;
use App\Pdf\Html\HtmlOlChunk;
use App\Pdf\Html\HtmlPageBreakChunk;
use App\Pdf\Html\HtmlParentChunk;
use App\Pdf\Html\HtmlParser;
use App\Pdf\Html\HtmlTextChunk;
use App\Pdf\Html\HtmlUlChunk;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(HtmlParser::class)]
class HtmlParserTest extends TestCase
{
public function testBrChunk(): void
{
$parser = new HtmlParser('<body><br></body>');
$actual = $parser->parse();
self::assertInstanceOf(HtmlParentChunk::class, $actual);
self::assertNotEmpty($actual->getChildren());
self::assertCount(1, $actual);
$actual = $actual->getChildren()[0];
self::assertInstanceOf(HtmlBrChunk::class, $actual);
}

public function testEmpty(): void
{
$parser = new HtmlParser('');
$actual = $parser->parse();
self::assertNull($actual);
}

public function testEmptyBody(): void
{
$parser = new HtmlParser('<html><body></body></html>');
$actual = $parser->parse();
self::assertNull($actual);
}

public function testLiChunk(): void
{
$parser = new HtmlParser('<body><li></li></body>');
$actual = $parser->parse();
self::assertInstanceOf(HtmlParentChunk::class, $actual);
self::assertNotEmpty($actual->getChildren());
self::assertCount(1, $actual);
$actual = $actual->getChildren()[0];
self::assertInstanceOf(HtmlLiChunk::class, $actual);
}

public function testNoBody(): void
{
$parser = new HtmlParser('<html></html>');
$actual = $parser->parse();
self::assertNull($actual);
}

public function testOlChunk(): void
{
$parser = new HtmlParser('<body><ol></ol></body>');
$actual = $parser->parse();
self::assertInstanceOf(HtmlParentChunk::class, $actual);
self::assertNotEmpty($actual->getChildren());
self::assertCount(1, $actual);
$actual = $actual->getChildren()[0];
self::assertInstanceOf(HtmlOlChunk::class, $actual);
}

public function testPageBreakChunk(): void
{
$parser = new HtmlParser('<body><div class="page-break"></div></body>');
$actual = $parser->parse();
self::assertInstanceOf(HtmlParentChunk::class, $actual);
self::assertNotEmpty($actual->getChildren());
self::assertCount(1, $actual);
$actual = $actual->getChildren()[0];
self::assertInstanceOf(HtmlPageBreakChunk::class, $actual);
}

public function testTextChunk(): void
{
$parser = new HtmlParser('<body><p>My Text</p></body>');
$actual = $parser->parse();
self::assertInstanceOf(HtmlParentChunk::class, $actual);
self::assertNotEmpty($actual->getChildren());
self::assertCount(1, $actual);

$actual = $actual->getChildren()[0];
self::assertInstanceOf(HtmlParentChunk::class, $actual);
self::assertNotEmpty($actual->getChildren());
self::assertCount(1, $actual);

$actual = $actual->getChildren()[0];
self::assertInstanceOf(HtmlTextChunk::class, $actual);
}

public function testUlChunk(): void
{
$parser = new HtmlParser('<body><ul></ul></body>');
$actual = $parser->parse();
self::assertInstanceOf(HtmlParentChunk::class, $actual);
self::assertNotEmpty($actual->getChildren());
self::assertCount(1, $actual);
$actual = $actual->getChildren()[0];
self::assertInstanceOf(HtmlUlChunk::class, $actual);
}
}
63 changes: 63 additions & 0 deletions tests/Pdf/Traits/PdfCellTranslatorTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/*
* This file is part of the Calculation package.
*
* (c) bibi.nu <bibi@bibi.nu>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Tests\Pdf\Traits;

use App\Pdf\PdfColumn;
use App\Pdf\PdfDocument;
use App\Pdf\PdfTable;
use App\Pdf\Traits\PdfCellTranslatorTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\Translation\TranslatorInterface;

#[CoversClass(PdfCellTranslatorTrait::class)]
class PdfCellTranslatorTraitTest extends TestCase
{
/**
* @throws Exception
*/
public function testRender(): void
{
$document = new PdfDocument();
$translator = $this->createMock(TranslatorInterface::class);

$table = new class($document, $translator) extends PdfTable {
use PdfCellTranslatorTrait;

public function __construct(PdfDocument $parent, private readonly TranslatorInterface $translator)
{
parent::__construct($parent);
}

public function render(): bool
{
$this->addColumn(PdfColumn::left('', 10.0));
$this->startRow();
$this->addCellTrans('id');
$this->endRow();

return true;
}

public function getTranslator(): TranslatorInterface
{
return $this->translator;
}
};
$document->resetStyle()
->addPage();
$actual = $table->render();
self::assertTrue($actual);
}
}

0 comments on commit cf439d8

Please sign in to comment.