ReflectionHelperTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace DeepCopyTest\Reflection;
  3. use DeepCopy\Reflection\ReflectionHelper;
  4. use PHPUnit\Framework\TestCase;
  5. use ReflectionClass;
  6. use ReflectionProperty;
  7. /**
  8. * @covers \DeepCopy\Reflection\ReflectionHelper
  9. */
  10. class ReflectionHelperTest extends TestCase
  11. {
  12. public function test_it_retrieves_the_object_properties()
  13. {
  14. $child = new ReflectionHelperTestChild();
  15. $childReflectionClass = new ReflectionClass($child);
  16. $expectedProps = array(
  17. 'a1',
  18. 'a2',
  19. 'a3',
  20. 'a10',
  21. 'a11',
  22. 'a12',
  23. 'a20',
  24. 'a21',
  25. 'a22',
  26. 'a100',
  27. 'a101',
  28. 'a102',
  29. );
  30. $actualProps = ReflectionHelper::getProperties($childReflectionClass);
  31. $this->assertSame($expectedProps, array_keys($actualProps));
  32. }
  33. /**
  34. * @dataProvider provideProperties
  35. */
  36. public function test_it_can_retrieve_an_object_property($name)
  37. {
  38. $object = new ReflectionHelperTestChild();
  39. $property = ReflectionHelper::getProperty($object, $name);
  40. $this->assertInstanceOf(ReflectionProperty::class, $property);
  41. $this->assertSame($name, $property->getName());
  42. }
  43. public function provideProperties()
  44. {
  45. return [
  46. 'public property' => ['a10'],
  47. 'private property' => ['a102'],
  48. 'private property of ancestor' => ['a3']
  49. ];
  50. }
  51. /**
  52. * @expectedException \DeepCopy\Exception\PropertyException
  53. */
  54. public function test_it_cannot_retrieve_a_non_existent_prperty()
  55. {
  56. $object = new ReflectionHelperTestChild();
  57. ReflectionHelper::getProperty($object, 'non existent property');
  58. }
  59. }
  60. class ReflectionHelperTestParent
  61. {
  62. public $a1;
  63. protected $a2;
  64. private $a3;
  65. public $a10;
  66. protected $a11;
  67. private $a12;
  68. public static $a20;
  69. protected static $a21;
  70. private static $a22;
  71. }
  72. class ReflectionHelperTestChild extends ReflectionHelperTestParent
  73. {
  74. public $a1;
  75. protected $a2;
  76. private $a3;
  77. public $a100;
  78. protected $a101;
  79. private $a102;
  80. }