File: ReflectionConstantTest.php

package info (click to toggle)
php-symfony-polyfill 1.33.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,544 kB
  • sloc: php: 127,238; makefile: 69
file content (164 lines) | stat: -rw-r--r-- 5,905 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Polyfill\Tests\Php84;

use PHPUnit\Framework\TestCase;

const EXAMPLE = 'Foo';

class ExampleNonStringable
{
    protected $value;

    public function __construct(string $value)
    {
        $this->value = $value;
    }
}

class ExampleStringable extends ExampleNonStringable
{
    public function __toString(): string
    {
        return 'ExampleStringable (value='.$this->value.')';
    }
}

/**
 * @author Daniel Scherzer <daniel.e.scherzer@gmail.com>
 */
class ReflectionConstantTest extends TestCase
{
    public function testMissingConstant()
    {
        $this->expectException(\ReflectionException::class);
        $this->expectExceptionMessage('Constant "missing" does not exist');
        new \ReflectionConstant('missing');
    }

    public function testClassConstant()
    {
        $this->assertTrue(\defined('ReflectionFunction::IS_DEPRECATED'));

        $this->expectException(\ReflectionException::class);
        $this->expectExceptionMessage('Constant "ReflectionClass::IS_DEPRECATED" does not exist');
        new \ReflectionConstant('ReflectionClass::IS_DEPRECATED');
    }

    public function testBuiltInConstant()
    {
        $constant = new \ReflectionConstant('E_ERROR');
        $this->assertSame('E_ERROR', $constant->name);
        $this->assertSame('E_ERROR', $constant->getName());
        $this->assertSame('', $constant->getNamespaceName());
        $this->assertSame('E_ERROR', $constant->getShortName());
        $this->assertSame(\E_ERROR, $constant->getValue());
        $this->assertFalse($constant->isDeprecated());
        $this->assertSame("Constant [ <persistent> int E_ERROR ] { 1 }\n", (string) $constant);
    }

    public function testUserConstant()
    {
        \define('TESTING', 123);

        $constant = new \ReflectionConstant('TESTING');
        $this->assertSame('TESTING', $constant->name);
        $this->assertSame('TESTING', $constant->getName());
        $this->assertSame('', $constant->getNamespaceName());
        $this->assertSame('TESTING', $constant->getShortName());
        $this->assertSame(TESTING, $constant->getValue());
        $this->assertFalse($constant->isDeprecated());
        $this->assertSame("Constant [ int TESTING ] { 123 }\n", (string) $constant);
    }

    public function testNamespacedConstant()
    {
        $constant = new \ReflectionConstant(EXAMPLE::class);
        $this->assertSame(EXAMPLE::class, $constant->name);
        $this->assertSame(EXAMPLE::class, $constant->getName());
        $this->assertSame(__NAMESPACE__, $constant->getNamespaceName());
        $this->assertSame('EXAMPLE', $constant->getShortName());
        $this->assertSame(EXAMPLE, $constant->getValue());
        $this->assertFalse($constant->isDeprecated());
        $this->assertSame("Constant [ string Symfony\\Polyfill\\Tests\\Php84\\EXAMPLE ] { Foo }\n", (string) $constant);
    }

    public function testDeprecated()
    {
        $constant = new \ReflectionConstant('MT_RAND_PHP');
        $this->assertSame('MT_RAND_PHP', $constant->name);
        $this->assertSame('MT_RAND_PHP', $constant->getName());
        $this->assertSame('', $constant->getNamespaceName());
        $this->assertSame('MT_RAND_PHP', $constant->getShortName());
        $this->assertSame(1, $constant->getValue());
        if (\PHP_VERSION_ID >= 80300) {
            $this->assertTrue($constant->isDeprecated());
            $this->assertSame("Constant [ <persistent, deprecated> int MT_RAND_PHP ] { 1 }\n", (string) $constant);
        } else {
            $this->assertFalse($constant->isDeprecated());
            $this->assertSame("Constant [ <persistent> int MT_RAND_PHP ] { 1 }\n", (string) $constant);
        }
    }

    public function testNonStringable()
    {
        if (\PHP_VERSION_ID < 80100) {
            $this->markTestSkipped('Constants can only be objects since PHP 8.1');
        }
        $value = new ExampleNonStringable('Testing');
        \define('NonStringable', $value);

        $constant = new \ReflectionConstant('NonStringable');

        // No error version of expectException()
        try {
            (string) $constant;
            $this->fail('Error should be thrown');
        } catch (\Error $e) {
            $this->assertSame("Object of class Symfony\Polyfill\Tests\Php84\ExampleNonStringable could not be converted to string", $e->getMessage());
        }
    }

    public function testStringable()
    {
        if (\PHP_VERSION_ID < 80100) {
            $this->markTestSkipped('Constants can only be objects since PHP 8.1');
        }
        $value = new ExampleStringable('Testing');
        \define('IsStringable', $value);

        $constant = new \ReflectionConstant('IsStringable');

        $this->assertSame("Constant [ Symfony\Polyfill\Tests\Php84\ExampleStringable IsStringable ] { ExampleStringable (value=Testing) }\n", (string) $constant);
    }

    public function testSerialization()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage("Serialization of 'ReflectionConstant' is not allowed");

        $r = new \ReflectionConstant('PHP_VERSION');
        serialize($r);
    }

    public function testUnserialization()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage("Unserialization of 'ReflectionConstant' is not allowed");
        unserialize(
            'O:18:"ReflectionConstant":4:{s:4:"name";'.
            "s:11:\"PHP_VERSION\";s:25:\"\0ReflectionConstant\0value\";s:6:\"8.3.19\";".
            "s:30:\"\0ReflectionConstant\0deprecated\";b:0;".
            "s:30:\"\0ReflectionConstant\0persistent\";b:1;}"
        );
    }
}