MirrorTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\Util;
  11. use Psy\Reflection\ReflectionClassConstant;
  12. use Psy\Reflection\ReflectionConstant_;
  13. use Psy\Reflection\ReflectionNamespace;
  14. use Psy\Util\Mirror;
  15. class MirrorTest extends \Psy\Test\TestCase
  16. {
  17. const FOO = 1;
  18. private $bar = 2;
  19. private static $baz = 3;
  20. public function aPublicMethod()
  21. {
  22. // nada
  23. }
  24. public function testMirror()
  25. {
  26. $refl = Mirror::get('sort');
  27. $this->assertInstanceOf(\ReflectionFunction::class, $refl);
  28. $refl = Mirror::get(self::class);
  29. $this->assertInstanceOf(\ReflectionClass::class, $refl);
  30. $refl = Mirror::get($this);
  31. $this->assertInstanceOf(\ReflectionObject::class, $refl);
  32. $refl = Mirror::get($this, 'FOO');
  33. if (\version_compare(\PHP_VERSION, '7.1.0', '>=')) {
  34. $this->assertInstanceOf(\ReflectionClassConstant::class, $refl);
  35. } else {
  36. $this->assertInstanceOf(ReflectionClassConstant::class, $refl);
  37. }
  38. $refl = Mirror::get('PHP_VERSION');
  39. $this->assertInstanceOf(ReflectionConstant_::class, $refl);
  40. $refl = Mirror::get($this, 'bar');
  41. $this->assertInstanceOf(\ReflectionProperty::class, $refl);
  42. $refl = Mirror::get($this, 'baz');
  43. $this->assertInstanceOf(\ReflectionProperty::class, $refl);
  44. $refl = Mirror::get($this, 'aPublicMethod');
  45. $this->assertInstanceOf(\ReflectionMethod::class, $refl);
  46. $refl = Mirror::get($this, 'baz', Mirror::STATIC_PROPERTY);
  47. $this->assertInstanceOf(\ReflectionProperty::class, $refl);
  48. $refl = Mirror::get('Psy\\Test\\Util');
  49. $this->assertInstanceOf(ReflectionNamespace::class, $refl);
  50. // This is both a namespace and a class, so let's make sure it gets the class:
  51. $refl = Mirror::get('Psy\\CodeCleaner');
  52. $this->assertInstanceOf(\ReflectionClass::class, $refl);
  53. }
  54. public function testMirrorThrowsExceptions()
  55. {
  56. $this->expectException(\RuntimeException::class);
  57. Mirror::get($this, 'notAMethod');
  58. $this->fail();
  59. }
  60. /**
  61. * @dataProvider invalidArguments
  62. */
  63. public function testMirrorThrowsInvalidArgumentExceptions($value)
  64. {
  65. $this->expectException(\InvalidArgumentException::class);
  66. Mirror::get($value);
  67. $this->fail();
  68. }
  69. public function invalidArguments()
  70. {
  71. return [
  72. ['not_a_function_or_class'],
  73. [[]],
  74. [1],
  75. ];
  76. }
  77. }