ObjectReflectorTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /*
  3. * This file is part of object-reflector.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace SebastianBergmann\ObjectReflector;
  12. use PHPUnit\Framework\TestCase;
  13. use SebastianBergmann\ObjectReflector\TestFixture\ChildClass;
  14. use SebastianBergmann\ObjectReflector\TestFixture\ClassWithIntegerAttributeName;
  15. /**
  16. * @covers SebastianBergmann\ObjectReflector\ObjectReflector
  17. */
  18. class ObjectReflectorTest extends TestCase
  19. {
  20. /**
  21. * @var ObjectReflector
  22. */
  23. private $objectReflector;
  24. protected function setUp()/*: void */
  25. {
  26. $this->objectReflector = new ObjectReflector;
  27. }
  28. public function testReflectsAttributesOfObject()/*: void */
  29. {
  30. $o = new ChildClass;
  31. $this->assertEquals(
  32. [
  33. 'privateInChild' => 'private',
  34. 'protectedInChild' => 'protected',
  35. 'publicInChild' => 'public',
  36. 'undeclared' => 'undeclared',
  37. 'SebastianBergmann\ObjectReflector\TestFixture\ParentClass::privateInParent' => 'private',
  38. 'SebastianBergmann\ObjectReflector\TestFixture\ParentClass::protectedInParent' => 'protected',
  39. 'SebastianBergmann\ObjectReflector\TestFixture\ParentClass::publicInParent' => 'public',
  40. ],
  41. $this->objectReflector->getAttributes($o)
  42. );
  43. }
  44. public function testReflectsAttributeWithIntegerName()/*: void */
  45. {
  46. $o = new ClassWithIntegerAttributeName;
  47. $this->assertEquals(
  48. [
  49. 1 => 2
  50. ],
  51. $this->objectReflector->getAttributes($o)
  52. );
  53. }
  54. public function testRaisesExceptionWhenPassedArgumentIsNotAnObject()/*: void */
  55. {
  56. $this->expectException(InvalidArgumentException::class);
  57. $this->objectReflector->getAttributes(null);
  58. }
  59. }