-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReadAttributeTrait.php
57 lines (47 loc) · 1.71 KB
/
ReadAttributeTrait.php
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
<?php
namespace Enqueue\Test;
trait ReadAttributeTrait
{
public function readAttribute(object $object, string $attribute)
{
$refProperty = $this->getClassAttribute($object, $attribute);
$refProperty->setAccessible(true);
$value = $refProperty->getValue($object);
$refProperty->setAccessible(false);
return $value;
}
private function getClassAttribute(
object $object,
string $attribute,
?string $class = null,
): \ReflectionProperty {
if (null === $class) {
$class = $object::class;
}
try {
return new \ReflectionProperty($class, $attribute);
} catch (\ReflectionException $exception) {
$parentClass = get_parent_class($object);
if (false === $parentClass) {
throw $exception;
}
return $this->getClassAttribute($object, $attribute, $parentClass);
}
}
private function assertAttributeSame($expected, string $attribute, object $object): void
{
static::assertSame($expected, $this->readAttribute($object, $attribute));
}
private function assertAttributeEquals($expected, string $attribute, object $object): void
{
static::assertEquals($expected, $this->readAttribute($object, $attribute));
}
private function assertAttributeInstanceOf(string $expected, string $attribute, object $object): void
{
static::assertInstanceOf($expected, $this->readAttribute($object, $attribute));
}
private function assertAttributeCount(int $count, string $attribute, object $object): void
{
static::assertCount($count, $this->readAttribute($object, $attribute));
}
}