Skip to content

Commit

Permalink
Write unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Mar 4, 2024
1 parent d8fc897 commit f68d939
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 49 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache" displayDetailsOnTestsThatTriggerDeprecations="true">
<coverage>
<report>
<html outputDirectory="/tmp/pop-pdf-cc" lowUpperBound="35" highLowerBound="70"/>
Expand Down
8 changes: 4 additions & 4 deletions src/Document/Page/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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[] = [
Expand Down
78 changes: 39 additions & 39 deletions src/Document/Page/Text/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -174,71 +174,71 @@ 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;
}

/**
* Get start Y
*
* @return ?int
* @return int|float|null
*/
public function getStartY(): ?int
public function getStartY(): int|float|null
{
return $this->startY;
}

/**
* Get edge X boundary
*
* @return ?int
* @return int|float|null
*/
public function getEdgeX(): ?int
public function getEdgeX(): int|float|null
{
return $this->edgeX;
}

/**
* Get edge Y boundary
*
* @return ?int
* @return int|float|null
*/
public function getEdgeY(): ?int
public function getEdgeY(): int|float|null
{
return $this->edgeY;
}

/**
* Get current X
*
* @return ?int
* @return int|float|null
*/
public function getCurrentX(): ?int
public function getCurrentX(): int|float|null
{
return $this->currentX;
}

/**
* Get current Y
*
* @return ?int
* @return int|float|null
*/
public function getCurrentY(): ?int
public function getCurrentY(): int|float|null
{
return $this->currentY;
}

/**
* 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,
Expand Down Expand Up @@ -507,4 +507,4 @@ public function hasOrphanIndex(): bool
return ($this->orphanIndex !== null);
}

}
}
4 changes: 2 additions & 2 deletions tests/Document/Page/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -220,4 +220,4 @@ public function testGetPartialStreamWithCharWrap()
$this->assertStringContainsString(")Tj", $stream);
}

}
}
22 changes: 22 additions & 0 deletions tests/Document/StyleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Pop\Pdf\Test\Document;

use Pop\Pdf\Document\Style;
use PHPUnit\Framework\TestCase;

class StyleTest extends TestCase
{

public function testGettersAndSetters()
{
$style = Style::create('bold', 'Arial,Bold', 12);
$this->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());
}

}
Loading

0 comments on commit f68d939

Please sign in to comment.