ReflectionConstantTest.php 3.4 KB

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