Skip to content

Commit 6053be6

Browse files
committed
Add Font::uchr float-handling test
Add testUchrWithOutOfRangeFloat covering both the warning fix from the previous commit (e161d7c) for out-of-range floats, INF and NAN, as well as a check that floats in general are still cast to int (added in #623). Note that the previous commit wrongly assumed that the cast warning was introduced in PHP 8.1, but it was in fact introduced in PHP 8.5 only. Previous PHP versions silently cast an unrepresentable float to system dependent "weird" results (negative integers, 0-bytes). See https://wiki.php.net/rfc/warnings-php-8-5 for details. This change enables PHPUnit's failOnWarning option which will turn warnings into actual test failures - the test in this commit as well as the existing ParserTest::testIssue621 and ParserTest::testParseFile will fail now without the patch in e161d7c. Unrelated, minor convenience fix: this also increases the memory limit for unit tests to 1GB (from default 128MB) to avoid out of memory issues during the test run via phpunit.xml
1 parent e161d7c commit 6053be6

2 files changed

Lines changed: 62 additions & 18 deletions

File tree

phpunit.xml

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
3-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" cacheDirectory=".phpunit.cache" backupStaticProperties="false" displayDetailsOnTestsThatTriggerWarnings="true">
4-
<php>
5-
<ini name="error_reporting" value="-1"/>
6-
<ini name="zend.enable_gc" value="0"/>
7-
<ini name="error_reporting" value="-1"/>
8-
<ini name="intl.error_level" value="0"/>
9-
<ini name="display_errors" value="On"/>
10-
</php>
11-
<testsuites>
12-
<testsuite name="all">
13-
<directory>tests/PHPUnit</directory>
14-
</testsuite>
15-
</testsuites>
16-
<source>
17-
<include>
18-
<directory>src</directory>
19-
</include>
20-
</source>
3+
<phpunit
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
6+
backupGlobals="false"
7+
bootstrap="vendor/autoload.php"
8+
colors="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
cacheDirectory=".phpunit.cache"
12+
backupStaticProperties="false"
13+
displayDetailsOnTestsThatTriggerWarnings="true"
14+
failOnWarning="true">
15+
<php>
16+
<ini name="error_reporting" value="-1"/>
17+
<ini name="zend.enable_gc" value="0"/>
18+
<ini name="error_reporting" value="-1"/>
19+
<ini name="intl.error_level" value="0"/>
20+
<ini name="display_errors" value="On"/>
21+
<ini name="memory_limit" value="1G"/>
22+
</php>
23+
<testsuites>
24+
<testsuite name="all">
25+
<directory>tests/PHPUnit</directory>
26+
</testsuite>
27+
</testsuites>
28+
<source>
29+
<include>
30+
<directory>src</directory>
31+
</include>
32+
</source>
2133
</phpunit>

tests/PHPUnit/Unit/FontTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,36 @@ public function testDecodeTextIssue597(): void
6868
// compare result with expected value
6969
self::assertEquals('3cc2ab083e', bin2hex($result));
7070
}
71+
72+
/**
73+
* A CMap could contain oversized hex values. hexdec() then returns a float
74+
* larger than PHP_INT_MAX which cannot be cast to int. On PHP 8.5 this
75+
* cast raises a "not representable as int" warning.
76+
*
77+
* Since these values can not represent valid Unicode code points anyway,
78+
* it's safe to return Font::MISSING for them. This test checks that this
79+
* is the case.
80+
*
81+
* The test relies on PhpUnit's failOnWarning="true" in phpunit.xml:
82+
* a warning would error.
83+
*
84+
* @see https://github.com/smalot/pdfparser/pull/623
85+
* @see https://github.com/smalot/pdfparser/pull/825
86+
*/
87+
public function testUchrWithOutOfRangeFloat(): void
88+
{
89+
// a regular code point is still decoded
90+
$this->assertEquals('A', Font::uchr(0x41));
91+
92+
// a float that fits into an integer is still cast and decoded; this is
93+
// the reason uchr() accepts floats in the first place
94+
$this->assertEquals('A', Font::uchr(65.0));
95+
96+
// floats that do not fit into an integer can never be a valid code
97+
// point; the value below is produced by hexdec() of an oversized hex
98+
// string taken from samples/bugs/Issue621.pdf
99+
$this->assertEquals(Font::MISSING, Font::uchr(1.50646556872121E+28));
100+
$this->assertEquals(Font::MISSING, Font::uchr(\INF));
101+
$this->assertEquals(Font::MISSING, Font::uchr(\NAN));
102+
}
71103
}

0 commit comments

Comments
 (0)