From f68d939bbbe2008f2601ce5e03aee8d4f0ccb56a Mon Sep 17 00:00:00 2001 From: Nick Sagona Date: Mon, 4 Mar 2024 15:03:39 -0600 Subject: [PATCH] Write unit tests --- phpunit.xml | 2 +- src/Document/Page/Text.php | 8 ++-- src/Document/Page/Text/Stream.php | 78 +++++++++++++++---------------- tests/Document/Page/TextTest.php | 4 +- tests/Document/StyleTest.php | 22 +++++++++ tests/DocumentTest.php | 66 ++++++++++++++++++++++++-- 6 files changed, 131 insertions(+), 49 deletions(-) create mode 100644 tests/Document/StyleTest.php diff --git a/phpunit.xml b/phpunit.xml index 2bedbea..8d837c5 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,5 @@ - + diff --git a/src/Document/Page/Text.php b/src/Document/Page/Text.php index 7e6bfd4..f7a38a9 100644 --- a/src/Document/Page/Text.php +++ b/src/Document/Page/Text.php @@ -147,7 +147,7 @@ public function setString(string $string): Text { if (function_exists('mb_strlen')) { if (mb_strlen($string, 'UTF-8') < strlen($string)) { - $string = utf8_decode($string); + $string = mb_convert_encoding($string, 'UTF-8', mb_detect_encoding($string)); } } $this->string = $string; @@ -167,11 +167,11 @@ public function setStrings(array $strings): Text if ($value instanceof Text) { $v = $value->getString(); if (mb_strlen($v, 'UTF-8') < strlen($v)) { - return utf8_decode($v); + return mb_convert_encoding($v, 'UTF-8', mb_detect_encoding($v)); } $value->setString($v); } else if (mb_strlen($value, 'UTF-8') < strlen($value)) { - $value = utf8_decode($value); + $value = mb_convert_encoding($value, 'UTF-8', mb_detect_encoding($value)); } return $value; }, $strings); @@ -192,7 +192,7 @@ public function addStringWithOffset(string $string, int $offset = 0): Text { if (function_exists('mb_strlen')) { if (mb_strlen($string, 'UTF-8') < strlen($string)) { - $string = utf8_decode($string); + $string = mb_convert_encoding($string, 'UTF-8', mb_detect_encoding($string)); } } $this->stringsWithOffsets[] = [ diff --git a/src/Document/Page/Text/Stream.php b/src/Document/Page/Text/Stream.php index 37bd8c6..8e0ef39 100644 --- a/src/Document/Page/Text/Stream.php +++ b/src/Document/Page/Text/Stream.php @@ -30,38 +30,38 @@ class Stream /** * Start X - * @var ?int + * @var int|float|null */ - protected ?int $startX = null; + protected int|float|null $startX = null; /** * Start Y - * @var ?int + * @var int|float|null */ - protected ?int $startY = null; + protected int|float|null $startY = null; /** * Edge X boundary * @var ?int */ - protected ?int $edgeX = null; + protected int|float|null $edgeX = null; /** * Edge Y boundary - * @var ?int + * @var int|float|null */ - protected ?int $edgeY = null; + protected int|float|null $edgeY = null; /** * Current X - * @var ?int + * @var int|float|null */ - protected ?int $currentX = null; + protected int|float|null $currentX = null; /** * Current Y - * @var ?int + * @var int|float|null */ - protected ?int $currentY = null; + protected int|float|null $currentY = null; /** * Text streams @@ -102,10 +102,10 @@ public function __construct(int $startX, int $startY, int $edgeX, ?int $edgeY = /** * Set start X * - * @param int $startX + * @param int|float $startX * @return Stream */ - public function setStartX(int $startX): Stream + public function setStartX(int|float $startX): Stream { $this->startX = $startX; return $this; @@ -114,10 +114,10 @@ public function setStartX(int $startX): Stream /** * Set start Y * - * @param int $startY + * @param int|float $startY * @return Stream */ - public function setStartY(int $startY): Stream + public function setStartY(int|float $startY): Stream { $this->startY = $startY; return $this; @@ -126,10 +126,10 @@ public function setStartY(int $startY): Stream /** * Set edge X boundary * - * @param int $edgeX + * @param int|float $edgeX * @return Stream */ - public function setEdgeX(int $edgeX): Stream + public function setEdgeX(int|float $edgeX): Stream { $this->edgeX = $edgeX; return $this; @@ -138,10 +138,10 @@ public function setEdgeX(int $edgeX): Stream /** * Set edge Y boundary * - * @param int $edgeY + * @param int|float $edgeY * @return Stream */ - public function setEdgeY(int $edgeY): Stream + public function setEdgeY(int|float $edgeY): Stream { $this->edgeY = $edgeY; return $this; @@ -150,10 +150,10 @@ public function setEdgeY(int $edgeY): Stream /** * Set current X * - * @param int $currentX + * @param int|float $currentX * @return Stream */ - public function setCurrentX(int $currentX): Stream + public function setCurrentX(int|float $currentX): Stream { $this->currentX = $currentX; return $this; @@ -162,10 +162,10 @@ public function setCurrentX(int $currentX): Stream /** * Set current Y * - * @param int $currentY + * @param int|float $currentY * @return Stream */ - public function setCurrentY(int $currentY): Stream + public function setCurrentY(int|float $currentY): Stream { $this->currentY = $currentY; return $this; @@ -174,9 +174,9 @@ public function setCurrentY(int $currentY): Stream /** * Get start X * - * @return ?int + * @return int|float|null */ - public function getStartX(): ?int + public function getStartX(): int|float|null { return $this->startX; } @@ -184,9 +184,9 @@ public function getStartX(): ?int /** * Get start Y * - * @return ?int + * @return int|float|null */ - public function getStartY(): ?int + public function getStartY(): int|float|null { return $this->startY; } @@ -194,9 +194,9 @@ public function getStartY(): ?int /** * Get edge X boundary * - * @return ?int + * @return int|float|null */ - public function getEdgeX(): ?int + public function getEdgeX(): int|float|null { return $this->edgeX; } @@ -204,9 +204,9 @@ public function getEdgeX(): ?int /** * Get edge Y boundary * - * @return ?int + * @return int|float|null */ - public function getEdgeY(): ?int + public function getEdgeY(): int|float|null { return $this->edgeY; } @@ -214,9 +214,9 @@ public function getEdgeY(): ?int /** * Get current X * - * @return ?int + * @return int|float|null */ - public function getCurrentX(): ?int + public function getCurrentX(): int|float|null { return $this->currentX; } @@ -224,9 +224,9 @@ public function getCurrentX(): ?int /** * Get current Y * - * @return ?int + * @return int|float|null */ - public function getCurrentY(): ?int + public function getCurrentY(): int|float|null { return $this->currentY; } @@ -234,11 +234,11 @@ public function getCurrentY(): ?int /** * Add text to the stream * - * @param string $string - * @param ?int $y + * @param string $string + * @param int|float|null $y * @return Stream */ - public function addText(string $string, ?int $y = null): Stream + public function addText(string $string, int|float|null $y = null): Stream { $this->streams[] = [ 'string' => $string, @@ -507,4 +507,4 @@ public function hasOrphanIndex(): bool return ($this->orphanIndex !== null); } -} \ No newline at end of file +} diff --git a/tests/Document/Page/TextTest.php b/tests/Document/Page/TextTest.php index 1431afb..cbb4da5 100644 --- a/tests/Document/Page/TextTest.php +++ b/tests/Document/Page/TextTest.php @@ -19,7 +19,7 @@ public function testSetMbString() { $text = new Text("mb string åèä test", 12); $this->assertTrue($text->hasString()); - $this->assertEquals(18, strlen($text->getString())); + $this->assertEquals(18, mb_strlen($text->getString())); } public function testSetStrings() @@ -220,4 +220,4 @@ public function testGetPartialStreamWithCharWrap() $this->assertStringContainsString(")Tj", $stream); } -} \ No newline at end of file +} diff --git a/tests/Document/StyleTest.php b/tests/Document/StyleTest.php new file mode 100644 index 0000000..7ee5f00 --- /dev/null +++ b/tests/Document/StyleTest.php @@ -0,0 +1,22 @@ +assertTrue($style->hasName()); + $this->assertTrue($style->hasFont()); + $this->assertTrue($style->hasSize()); + $this->assertEquals('bold', $style->getName()); + $this->assertEquals('Arial,Bold', $style->getFont()); + $this->assertEquals(12, $style->getSize()); + } + +} diff --git a/tests/DocumentTest.php b/tests/DocumentTest.php index 2e479a7..00055c5 100644 --- a/tests/DocumentTest.php +++ b/tests/DocumentTest.php @@ -5,16 +5,25 @@ use Pop\Pdf\Document; use Pop\Pdf\Document\Page; use Pop\Pdf\Document\Font; +use Pop\Pdf\Document\Style; use Pop\Pdf\Document\Form; use PHPUnit\Framework\TestCase; class DocumentTest extends TestCase { - public function testConstructor() + public function testConstructor1() { - $doc = new Document(new Page(Page::LETTER)); + $doc = new Document(new Page(Page::LETTER), null, new Style('normal')); $this->assertInstanceOf('Pop\Pdf\Document', $doc); + $this->assertTrue($doc->hasStyles()); + } + + public function testConstructor2() + { + $doc = new Document(new Page(Page::LETTER), null, [new Style('normal')]); + $this->assertInstanceOf('Pop\Pdf\Document', $doc); + $this->assertTrue($doc->hasStyles()); } public function testSetOrigin() @@ -121,6 +130,13 @@ public function testAddFont2() $this->assertEquals(1, $doc->getNumberOfFonts()); } + public function testAddFonts() + { + $doc = new Document(); + $doc->addFonts([new Font('Arial')]); + $this->assertEquals(1, $doc->getNumberOfFonts()); + } + public function testAddFontWithEmbedFont() { $doc = new Document(); @@ -140,6 +156,18 @@ public function testEmbedFont() $this->assertEquals(0, count($doc->getImportedFonts())); } + public function testEmbedFonts() + { + $doc = new Document(); + $doc->embedFonts([new Font(__DIR__ . '/tmp/fonts/times.ttf')]); + $this->assertInstanceOf('Pop\Pdf\Build\Font\AbstractFont', $doc->getFont('Times-Bold')->getParsedFont()); + $this->assertEquals(2, count($doc->getFont('Times-Bold')->getParsedFont()->getWidthsForGlyphs([0, 1]))); + $this->assertEquals(33.0, ceil($doc->getFont('Times-Bold')->getParsedFont()->getStringWidth('Hello World', 12))); + $this->assertEquals(1, $doc->getNumberOfFonts()); + $this->assertFalse($doc->hasImportedFonts()); + $this->assertEquals(0, count($doc->getImportedFonts())); + } + public function testEmbedFontWithStandardFont() { $doc = new Document(); @@ -165,6 +193,38 @@ public function testGetFontException() $this->assertInstanceOf('Pop\Pdf\Document\Font', $doc->getFont('Arial')); } + public function testCreateStyle() + { + $doc = new Document(); + $doc->createStyle('normal', 'Arial', 12); + $this->assertCount(1, $doc->getStyles()); + $this->assertTrue($doc->hasStyle('normal')); + } + + public function testAddStyle1() + { + $doc = new Document(); + $doc->addStyle(new Style('normal', 'Arial', 12)); + $this->assertCount(1, $doc->getStyles()); + $this->assertTrue($doc->hasStyle('normal')); + } + + public function testAddStyle2() + { + $doc = new Document(); + $doc->addStyle('normal'); + $this->assertCount(1, $doc->getStyles()); + $this->assertTrue($doc->hasStyle('normal')); + } + + public function testAddStyles() + { + $doc = new Document(); + $doc->addStyles([new Style('normal', 'Arial', 12)]); + $this->assertCount(1, $doc->getStyles()); + $this->assertTrue($doc->hasStyle('normal')); + } + public function testSetCurrentPage() { $doc = new Document(); @@ -206,4 +266,4 @@ public function testAddForm() $this->assertInstanceOf('Pop\Pdf\Document\Form', $doc->getForm('contact')); } -} \ No newline at end of file +}