Chris@13
|
1 <?php
|
Chris@13
|
2
|
Chris@13
|
3 /*
|
Chris@13
|
4 * This file is part of Psy Shell.
|
Chris@13
|
5 *
|
Chris@13
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@13
|
7 *
|
Chris@13
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@13
|
9 * file that was distributed with this source code.
|
Chris@13
|
10 */
|
Chris@13
|
11
|
Chris@13
|
12 namespace Psy\Test\Reflection;
|
Chris@13
|
13
|
Chris@16
|
14 use Psy\Reflection\ReflectionConstant_;
|
Chris@16
|
15
|
Chris@17
|
16 \define('Psy\\Test\\Reflection\\SOME_CONSTANT', 'yep');
|
Chris@13
|
17
|
Chris@13
|
18 class ReflectionConstantTest extends \PHPUnit\Framework\TestCase
|
Chris@13
|
19 {
|
Chris@13
|
20 public function testConstruction()
|
Chris@13
|
21 {
|
Chris@16
|
22 $refl = new ReflectionConstant_('Psy\\Test\\Reflection\\SOME_CONSTANT');
|
Chris@13
|
23
|
Chris@16
|
24 $this->assertFalse($refl->getDocComment());
|
Chris@16
|
25 $this->assertEquals('Psy\\Test\\Reflection\\SOME_CONSTANT', $refl->getName());
|
Chris@16
|
26 $this->assertEquals('Psy\\Test\\Reflection', $refl->getNamespaceName());
|
Chris@16
|
27 $this->assertEquals('yep', $refl->getValue());
|
Chris@16
|
28 $this->assertTrue($refl->inNamespace());
|
Chris@16
|
29 $this->assertEquals('Psy\\Test\\Reflection\\SOME_CONSTANT', (string) $refl);
|
Chris@13
|
30 $this->assertNull($refl->getFileName());
|
Chris@16
|
31 }
|
Chris@16
|
32
|
Chris@16
|
33 public function testBuiltInConstant()
|
Chris@16
|
34 {
|
Chris@16
|
35 $refl = new ReflectionConstant_('PHP_VERSION');
|
Chris@16
|
36
|
Chris@16
|
37 $this->assertEquals('PHP_VERSION', $refl->getName());
|
Chris@16
|
38 $this->assertEquals('PHP_VERSION', (string) $refl);
|
Chris@16
|
39 $this->assertEquals(PHP_VERSION, $refl->getValue());
|
Chris@16
|
40 $this->assertFalse($refl->inNamespace());
|
Chris@16
|
41 $this->assertSame('', $refl->getNamespaceName());
|
Chris@16
|
42 }
|
Chris@16
|
43
|
Chris@16
|
44 /**
|
Chris@16
|
45 * @dataProvider magicConstants
|
Chris@16
|
46 */
|
Chris@16
|
47 public function testIsMagicConstant($name, $is)
|
Chris@16
|
48 {
|
Chris@16
|
49 $this->assertEquals($is, ReflectionConstant_::isMagicConstant($name));
|
Chris@16
|
50 }
|
Chris@16
|
51
|
Chris@16
|
52 public function magicConstants()
|
Chris@16
|
53 {
|
Chris@16
|
54 return [
|
Chris@16
|
55 ['__LINE__', true],
|
Chris@16
|
56 ['__FILE__', true],
|
Chris@16
|
57 ['__DIR__', true],
|
Chris@16
|
58 ['__FUNCTION__', true],
|
Chris@16
|
59 ['__CLASS__', true],
|
Chris@16
|
60 ['__TRAIT__', true],
|
Chris@16
|
61 ['__METHOD__', true],
|
Chris@16
|
62 ['__NAMESPACE__', true],
|
Chris@16
|
63 ['__COMPILER_HALT_OFFSET__', true],
|
Chris@16
|
64 ['PHP_VERSION', false],
|
Chris@16
|
65 ['PHP_EOL', false],
|
Chris@16
|
66 ['Psy\\Test\\Reflection\\SOME_CONSTANT', false],
|
Chris@16
|
67 ['What if it isn\'t even a valid constant name?', false],
|
Chris@16
|
68 ];
|
Chris@13
|
69 }
|
Chris@13
|
70
|
Chris@13
|
71 /**
|
Chris@13
|
72 * @expectedException \InvalidArgumentException
|
Chris@13
|
73 */
|
Chris@13
|
74 public function testUnknownConstantThrowsException()
|
Chris@13
|
75 {
|
Chris@16
|
76 new ReflectionConstant_('UNKNOWN_CONSTANT');
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 public function testExport()
|
Chris@16
|
80 {
|
Chris@16
|
81 $ret = ReflectionConstant_::export('Psy\\Test\\Reflection\\SOME_CONSTANT', true);
|
Chris@16
|
82 $this->assertEquals($ret, 'Constant [ string Psy\\Test\\Reflection\\SOME_CONSTANT ] { yep }');
|
Chris@16
|
83 }
|
Chris@16
|
84
|
Chris@16
|
85 public function testExportOutput()
|
Chris@16
|
86 {
|
Chris@16
|
87 $this->expectOutputString("Constant [ string Psy\\Test\\Reflection\\SOME_CONSTANT ] { yep }\n");
|
Chris@16
|
88 ReflectionConstant_::export('Psy\\Test\\Reflection\\SOME_CONSTANT', false);
|
Chris@16
|
89 }
|
Chris@16
|
90
|
Chris@16
|
91 public function testGetFileName()
|
Chris@16
|
92 {
|
Chris@16
|
93 $refl = new ReflectionConstant_('Psy\\Test\\Reflection\\SOME_CONSTANT');
|
Chris@16
|
94 $this->assertNull($refl->getFileName());
|
Chris@13
|
95 }
|
Chris@13
|
96
|
Chris@13
|
97 /**
|
Chris@13
|
98 * @expectedException \RuntimeException
|
Chris@13
|
99 * @dataProvider notYetImplemented
|
Chris@13
|
100 */
|
Chris@13
|
101 public function testNotYetImplemented($method)
|
Chris@13
|
102 {
|
Chris@16
|
103 $refl = new ReflectionConstant_('Psy\\Test\\Reflection\\SOME_CONSTANT');
|
Chris@13
|
104 $refl->$method();
|
Chris@13
|
105 }
|
Chris@13
|
106
|
Chris@13
|
107 public function notYetImplemented()
|
Chris@13
|
108 {
|
Chris@13
|
109 return [
|
Chris@13
|
110 ['getStartLine'],
|
Chris@13
|
111 ['getEndLine'],
|
Chris@13
|
112 ];
|
Chris@13
|
113 }
|
Chris@13
|
114 }
|