ReflectionConstantTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2018 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. \define('Psy\\Test\\Reflection\\SOME_CONSTANT', 'yep');
  13. class ReflectionConstantTest extends \PHPUnit\Framework\TestCase
  14. {
  15. public function testConstruction()
  16. {
  17. $refl = new ReflectionConstant_('Psy\\Test\\Reflection\\SOME_CONSTANT');
  18. $this->assertFalse($refl->getDocComment());
  19. $this->assertEquals('Psy\\Test\\Reflection\\SOME_CONSTANT', $refl->getName());
  20. $this->assertEquals('Psy\\Test\\Reflection', $refl->getNamespaceName());
  21. $this->assertEquals('yep', $refl->getValue());
  22. $this->assertTrue($refl->inNamespace());
  23. $this->assertEquals('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->assertEquals('PHP_VERSION', $refl->getName());
  30. $this->assertEquals('PHP_VERSION', (string) $refl);
  31. $this->assertEquals(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->assertEquals($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. /**
  61. * @expectedException \InvalidArgumentException
  62. */
  63. public function testUnknownConstantThrowsException()
  64. {
  65. new ReflectionConstant_('UNKNOWN_CONSTANT');
  66. }
  67. public function testExport()
  68. {
  69. $ret = ReflectionConstant_::export('Psy\\Test\\Reflection\\SOME_CONSTANT', true);
  70. $this->assertEquals($ret, 'Constant [ string Psy\\Test\\Reflection\\SOME_CONSTANT ] { yep }');
  71. }
  72. public function testExportOutput()
  73. {
  74. $this->expectOutputString("Constant [ string Psy\\Test\\Reflection\\SOME_CONSTANT ] { yep }\n");
  75. ReflectionConstant_::export('Psy\\Test\\Reflection\\SOME_CONSTANT', false);
  76. }
  77. public function testGetFileName()
  78. {
  79. $refl = new ReflectionConstant_('Psy\\Test\\Reflection\\SOME_CONSTANT');
  80. $this->assertNull($refl->getFileName());
  81. }
  82. /**
  83. * @expectedException \RuntimeException
  84. * @dataProvider notYetImplemented
  85. */
  86. public function testNotYetImplemented($method)
  87. {
  88. $refl = new ReflectionConstant_('Psy\\Test\\Reflection\\SOME_CONSTANT');
  89. $refl->$method();
  90. }
  91. public function notYetImplemented()
  92. {
  93. return [
  94. ['getStartLine'],
  95. ['getEndLine'],
  96. ];
  97. }
  98. }