MirrorTest.php 2.2 KB

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