Skip to content

Commit fb73709

Browse files
committed
Add tests for new tokenizer constant code
1 parent 7b972cb commit fb73709

5 files changed

Lines changed: 72 additions & 0 deletions

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
<directory>./src/Standards/Squiz/Tests/</directory>
3232
<directory>./src/Standards/Zend/Tests/</directory>
3333
</testsuite>
34+
<testsuite name="End2EndPhpt">
35+
<directory suffix=".phpt">tests/EndToEndPhpt/</directory>
36+
</testsuite>
3437
</testsuites>
3538

3639
<groups>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Detect when the value of a polyfilled PHP token collides with a value already used by an existing internal PHP token.
3+
--SKIPIF--
4+
<?php
5+
if (version_compare(PHP_VERSION, "8.4", ">=")) {
6+
echo "skip because tokens used in this test already exist in PHP 8.4 so we cannot test polyfilling them", PHP_EOL;
7+
}
8+
--FILE--
9+
<?php
10+
define('T_PUBLIC_SET', T_STRING);
11+
require('src/Util/Tokens.php');
12+
--EXPECTF--
13+
Fatal error: Uncaught Exception: Externally polyfilled tokenizer constant value collision detected! T_PUBLIC_SET has the same value as T_STRING in %s:%d
14+
Stack trace:
15+
#0 %s(%d): PHP_CodeSniffer\Util\Tokens::polyfillTokenizerConstants()
16+
#1 Standard input code(%d): require('...')
17+
#2 {main}
18+
thrown in %s on line %d
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Detect when two or more polyfilled PHP tokens have the same value.
3+
--SKIPIF--
4+
<?php
5+
if (version_compare(PHP_VERSION, "8.4", ">=")) {
6+
echo "skip because tokens used in this test already exist in PHP 8.4 so we cannot test polyfilling them", PHP_EOL;
7+
}
8+
--FILE--
9+
<?php
10+
define('T_PRIVATE_SET', 10000);
11+
define('T_PROTECTED_SET', 10000);
12+
define('T_PUBLIC_SET', 10000);
13+
require('src/Util/Tokens.php');
14+
--EXPECTF--
15+
Fatal error: Uncaught Exception: Externally polyfilled tokenizer constant value collision detected! T_PROTECTED_SET has the same value as T_PRIVATE_SET in %s:%d
16+
Stack trace:
17+
#0 %s(%d): PHP_CodeSniffer\Util\Tokens::polyfillTokenizerConstants()
18+
#1 Standard input code(%d): require('...')
19+
#2 {main}
20+
thrown in %s on line %d
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Detect when an external party defines a PHP token polyfill with a number that we would have used.
3+
--SKIPIF--
4+
<?php
5+
if (version_compare(PHP_VERSION, "8.4", ">=")) {
6+
echo "skip because tokens used in this test already exist in PHP 8.4 so we cannot test polyfilling them", PHP_EOL;
7+
}
8+
--FILE--
9+
<?php
10+
define('T_PUBLIC_SET', 135000);
11+
require('src/Util/Tokens.php');
12+
echo T_PRIVATE_SET, PHP_EOL; // ..0 is used, so this becomes ..1
13+
--EXPECT--
14+
135001
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Value collision with a non-tokenizer constant should not cause an error.
3+
--SKIPIF--
4+
<?php
5+
if (version_compare(PHP_VERSION, "8.4", ">=")) {
6+
echo "skip because tokens used in this test already exist in PHP 8.4 so we cannot test polyfilling them", PHP_EOL;
7+
}
8+
--FILE--
9+
<?php
10+
// Using T_STRING as a value because that would be a collision (and throw) if the constant name looked like a tokenizer constant.
11+
define('T_', T_STRING); // Too short
12+
define('ONE', T_STRING); // First character is not 'T'
13+
define('TWO', T_STRING); // Second character is not '_'
14+
require('src/Util/Tokens.php');
15+
echo 'No conflicts', PHP_EOL;
16+
--EXPECT--
17+
No conflicts

0 commit comments

Comments
 (0)