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