|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Polyfill\Tests\Php84; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +const EXAMPLE = 'Foo'; |
| 17 | + |
| 18 | +class ExampleNonStringable |
| 19 | +{ |
| 20 | + protected $value; |
| 21 | + |
| 22 | + public function __construct(string $value) |
| 23 | + { |
| 24 | + $this->value = $value; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +class ExampleStringable extends ExampleNonStringable |
| 29 | +{ |
| 30 | + public function __toString(): string |
| 31 | + { |
| 32 | + return 'ExampleStringable (value='.$this->value.')'; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Daniel Scherzer <[email protected]> |
| 38 | + */ |
| 39 | +class ReflectionConstantTest extends TestCase |
| 40 | +{ |
| 41 | + public function testMissingConstant() |
| 42 | + { |
| 43 | + $this->expectException(\ReflectionException::class); |
| 44 | + $this->expectExceptionMessage('Constant "missing" does not exist'); |
| 45 | + new \ReflectionConstant('missing'); |
| 46 | + } |
| 47 | + |
| 48 | + public function testClassConstant() |
| 49 | + { |
| 50 | + $this->assertTrue(\defined('ReflectionFunction::IS_DEPRECATED')); |
| 51 | + |
| 52 | + $this->expectException(\ReflectionException::class); |
| 53 | + $this->expectExceptionMessage('Constant "ReflectionClass::IS_DEPRECATED" does not exist'); |
| 54 | + new \ReflectionConstant('ReflectionClass::IS_DEPRECATED'); |
| 55 | + } |
| 56 | + |
| 57 | + public function testBuiltInConstant() |
| 58 | + { |
| 59 | + $constant = new \ReflectionConstant('E_ERROR'); |
| 60 | + $this->assertSame('E_ERROR', $constant->name); |
| 61 | + $this->assertSame('E_ERROR', $constant->getName()); |
| 62 | + $this->assertSame('', $constant->getNamespaceName()); |
| 63 | + $this->assertSame('E_ERROR', $constant->getShortName()); |
| 64 | + $this->assertSame(\E_ERROR, $constant->getValue()); |
| 65 | + $this->assertFalse($constant->isDeprecated()); |
| 66 | + $this->assertSame("Constant [ <persistent> int E_ERROR ] { 1 }\n", (string) $constant); |
| 67 | + } |
| 68 | + |
| 69 | + public function testUserConstant() |
| 70 | + { |
| 71 | + \define('TESTING', 123); |
| 72 | + |
| 73 | + $constant = new \ReflectionConstant('TESTING'); |
| 74 | + $this->assertSame('TESTING', $constant->name); |
| 75 | + $this->assertSame('TESTING', $constant->getName()); |
| 76 | + $this->assertSame('', $constant->getNamespaceName()); |
| 77 | + $this->assertSame('TESTING', $constant->getShortName()); |
| 78 | + $this->assertSame(TESTING, $constant->getValue()); |
| 79 | + $this->assertFalse($constant->isDeprecated()); |
| 80 | + $this->assertSame("Constant [ int TESTING ] { 123 }\n", (string) $constant); |
| 81 | + } |
| 82 | + |
| 83 | + public function testNamespacedConstant() |
| 84 | + { |
| 85 | + $constant = new \ReflectionConstant(EXAMPLE::class); |
| 86 | + $this->assertSame(EXAMPLE::class, $constant->name); |
| 87 | + $this->assertSame(EXAMPLE::class, $constant->getName()); |
| 88 | + $this->assertSame(__NAMESPACE__, $constant->getNamespaceName()); |
| 89 | + $this->assertSame('EXAMPLE', $constant->getShortName()); |
| 90 | + $this->assertSame(EXAMPLE, $constant->getValue()); |
| 91 | + $this->assertFalse($constant->isDeprecated()); |
| 92 | + $this->assertSame("Constant [ string Symfony\\Polyfill\\Tests\\Php84\\EXAMPLE ] { Foo }\n", (string) $constant); |
| 93 | + } |
| 94 | + |
| 95 | + public function testDeprecated() |
| 96 | + { |
| 97 | + $constant = new \ReflectionConstant('MT_RAND_PHP'); |
| 98 | + $this->assertSame('MT_RAND_PHP', $constant->name); |
| 99 | + $this->assertSame('MT_RAND_PHP', $constant->getName()); |
| 100 | + $this->assertSame('', $constant->getNamespaceName()); |
| 101 | + $this->assertSame('MT_RAND_PHP', $constant->getShortName()); |
| 102 | + $this->assertSame(1, $constant->getValue()); |
| 103 | + if (\PHP_VERSION_ID >= 80300) { |
| 104 | + $this->assertTrue($constant->isDeprecated()); |
| 105 | + $this->assertSame("Constant [ <persistent, deprecated> int MT_RAND_PHP ] { 1 }\n", (string) $constant); |
| 106 | + } else { |
| 107 | + $this->assertFalse($constant->isDeprecated()); |
| 108 | + $this->assertSame("Constant [ <persistent> int MT_RAND_PHP ] { 1 }\n", (string) $constant); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public function testNonStringable() |
| 113 | + { |
| 114 | + if (\PHP_VERSION_ID < 80100) { |
| 115 | + $this->markTestSkipped('Constants can only be objects since PHP 8.1'); |
| 116 | + } |
| 117 | + $value = new ExampleNonStringable('Testing'); |
| 118 | + \define('NonStringable', $value); |
| 119 | + |
| 120 | + $constant = new \ReflectionConstant('NonStringable'); |
| 121 | + |
| 122 | + // No error version of expectException() |
| 123 | + try { |
| 124 | + (string) $constant; |
| 125 | + $this->fail('Error should be thrown'); |
| 126 | + } catch (\Error $e) { |
| 127 | + $this->assertSame("Object of class Symfony\Polyfill\Tests\Php84\ExampleNonStringable could not be converted to string", $e->getMessage()); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public function testStringable() |
| 132 | + { |
| 133 | + if (\PHP_VERSION_ID < 80100) { |
| 134 | + $this->markTestSkipped('Constants can only be objects since PHP 8.1'); |
| 135 | + } |
| 136 | + $value = new ExampleStringable('Testing'); |
| 137 | + \define('IsStringable', $value); |
| 138 | + |
| 139 | + $constant = new \ReflectionConstant('IsStringable'); |
| 140 | + |
| 141 | + $this->assertSame("Constant [ Symfony\Polyfill\Tests\Php84\ExampleStringable IsStringable ] { ExampleStringable (value=Testing) }\n", (string) $constant); |
| 142 | + } |
| 143 | + |
| 144 | + public function testSerialization() |
| 145 | + { |
| 146 | + $this->expectException(\Exception::class); |
| 147 | + $this->expectExceptionMessage("Serialization of 'ReflectionConstant' is not allowed"); |
| 148 | + |
| 149 | + $r = new \ReflectionConstant('PHP_VERSION'); |
| 150 | + serialize($r); |
| 151 | + } |
| 152 | + |
| 153 | + public function testUnserialization() |
| 154 | + { |
| 155 | + $this->expectException(\Exception::class); |
| 156 | + $this->expectExceptionMessage("Unserialization of 'ReflectionConstant' is not allowed"); |
| 157 | + unserialize( |
| 158 | + 'O:18:"ReflectionConstant":4:{s:4:"name";'. |
| 159 | + "s:11:\"PHP_VERSION\";s:25:\"\0ReflectionConstant\0value\";s:6:\"8.3.19\";". |
| 160 | + "s:30:\"\0ReflectionConstant\0deprecated\";b:0;". |
| 161 | + "s:30:\"\0ReflectionConstant\0persistent\";b:1;}" |
| 162 | + ); |
| 163 | + } |
| 164 | +} |
0 commit comments