ObjectReflectorTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/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. namespace SebastianBergmann\ObjectReflector;
  11. use PHPUnit\Framework\TestCase;
  12. use SebastianBergmann\ObjectReflector\TestFixture\ChildClass;
  13. use SebastianBergmann\ObjectReflector\TestFixture\ClassWithIntegerAttributeName;
  14. /**
  15. * @covers \SebastianBergmann\ObjectReflector\ObjectReflector
  16. */
  17. final class ObjectReflectorTest extends TestCase
  18. {
  19. /**
  20. * @var ObjectReflector
  21. */
  22. private $objectReflector;
  23. protected function setUp(): void
  24. {
  25. $this->objectReflector = new ObjectReflector;
  26. }
  27. public function testReflectsAttributesOfObject(): void
  28. {
  29. $o = new ChildClass;
  30. $this->assertEquals(
  31. [
  32. 'privateInChild' => 'private',
  33. 'protectedInChild' => 'protected',
  34. 'publicInChild' => 'public',
  35. 'undeclared' => 'undeclared',
  36. 'SebastianBergmann\ObjectReflector\TestFixture\ParentClass::privateInParent' => 'private',
  37. 'SebastianBergmann\ObjectReflector\TestFixture\ParentClass::protectedInParent' => 'protected',
  38. 'SebastianBergmann\ObjectReflector\TestFixture\ParentClass::publicInParent' => 'public',
  39. ],
  40. $this->objectReflector->getAttributes($o)
  41. );
  42. }
  43. public function testReflectsAttributeWithIntegerName(): void
  44. {
  45. $o = new ClassWithIntegerAttributeName;
  46. $this->assertEquals(
  47. [
  48. 1 => 2,
  49. ],
  50. $this->objectReflector->getAttributes($o)
  51. );
  52. }
  53. public function testRaisesExceptionWhenPassedArgumentIsNotAnObject(): void
  54. {
  55. $this->expectException(InvalidArgumentException::class);
  56. $this->objectReflector->getAttributes(null);
  57. }
  58. }