1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace ApiClients\Tools\ResourceTestUtilities; |
5
|
|
|
|
6
|
|
|
use ApiClients\Foundation\Resource\ResourceInterface; |
7
|
|
|
use Doctrine\Common\Inflector\Inflector; |
8
|
|
|
use Generator; |
9
|
|
|
use phpDocumentor\Reflection\DocBlock; |
10
|
|
|
use phpDocumentor\Reflection\DocBlockFactory; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
use ReflectionProperty; |
13
|
|
|
|
14
|
|
|
abstract class AbstractResourceTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
abstract function getClass(): string; |
|
|
|
|
17
|
|
|
abstract function getNamespace(): string; |
|
|
|
|
18
|
|
|
|
19
|
|
|
public function provideProperties(): Generator |
20
|
|
|
{ |
21
|
|
|
yield from $this->providePropertiesGenerator('compatible'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function providePropertiesIncompatible(): Generator |
25
|
|
|
{ |
26
|
|
|
yield from $this->providePropertiesGenerator('incompatible'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function providePropertiesGenerator(string $typeMethod): Generator |
30
|
|
|
{ |
31
|
|
|
foreach (Types::types() as $t) { |
32
|
|
|
var_export($t); |
33
|
|
|
} |
34
|
|
|
$class = new ReflectionClass($this->getClass()); |
35
|
|
|
|
36
|
|
|
$jsonTemplate = []; |
37
|
|
|
foreach ($class->getProperties() as $property) { |
38
|
|
|
$jsonTemplate[$property->getName()] = ''; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
foreach ($class->getProperties() as $property) { |
42
|
|
|
$method = Inflector::camelize($property->getName()); |
|
|
|
|
43
|
|
|
$docBlock = $this->getDocBlock($property->getDocComment()); |
44
|
|
|
|
45
|
|
|
$varTag = $docBlock->getTagsByName('var'); |
46
|
|
|
if (count($varTag) !== 1) { |
47
|
|
|
continue; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$varTag = $varTag[0]; |
51
|
|
|
if ($varTag->getType() === '') { |
52
|
|
|
continue; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (!Types::has($varTag->getType())) { |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$type = Types::get($varTag->getType()); |
60
|
|
|
yield from $this->generateTypeValues($type, $property, $typeMethod, $jsonTemplate); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function generateTypeValues(Type $type, ReflectionProperty $property, string $method, array $jsonTemplate): Generator |
65
|
|
|
{ |
66
|
|
|
$json = $jsonTemplate; |
67
|
|
|
foreach ($type->$method as $typeClass) { |
68
|
|
|
$methodType = Types::get(constant($typeClass . '::SCALAR')); |
69
|
|
|
foreach ($methodType->generate(25) as $value) { |
70
|
|
|
$json[$property->getName()] = $value; |
71
|
|
|
yield [ |
72
|
|
|
$property->getName(), // Name of the property to assign data to |
73
|
|
|
$method, // Method to call verifying that data |
74
|
|
|
$type, // The different types of data assiciated with this field |
75
|
|
|
$json, // JSON to use during testing |
76
|
|
|
$value, // Value to check against |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $docBlockContents |
84
|
|
|
* @return DocBlock |
85
|
|
|
*/ |
86
|
|
|
protected function getDocBlock(string $docBlockContents): DocBlock |
87
|
|
|
{ |
88
|
|
|
if (class_exists('phpDocumentor\Reflection\DocBlockFactory')) { |
89
|
|
|
return DocBlockFactory::createInstance()->create($docBlockContents); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return new DocBlock($docBlockContents); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @dataProvider provideProperties |
97
|
|
|
*/ |
98
|
|
|
public function testProperties(string $property, string $method, Type $type, array $json, mixed $value) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$class = $this->getClass(); |
101
|
|
|
$resource = $this->hydrate( |
102
|
|
|
str_replace( |
103
|
|
|
$this->getNamespace(), |
104
|
|
|
$this->getNamespace() . '\\Async', |
105
|
|
|
$class |
106
|
|
|
), |
107
|
|
|
$json, |
108
|
|
|
'Async' |
109
|
|
|
); |
110
|
|
|
$this->assertSame($value, $resource->{$method}()); |
111
|
|
|
$this->assertInternalType($type->scalar(), $resource->{$method}()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @dataProvider providePropertiesIncompatible |
116
|
|
|
*/ |
117
|
|
|
public function testPropertiesIncompatible(string $property, string $method, Type $type, array $json, mixed $value) |
118
|
|
|
{ |
119
|
|
|
var_export([$property, $method, $type, $json, $value]); |
120
|
|
|
$class = $this->getClass(); |
121
|
|
|
$resource = $this->hydrate( |
122
|
|
|
str_replace( |
123
|
|
|
$this->getNamespace(), |
124
|
|
|
$this->getNamespace() . '\\Async', |
125
|
|
|
$class |
126
|
|
|
), |
127
|
|
|
$json, |
128
|
|
|
'Async' |
129
|
|
|
); |
130
|
|
|
$this->assertSame($value, $resource->{$method}()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testInterface() |
134
|
|
|
{ |
135
|
|
|
$this->assertTrue( |
136
|
|
|
is_subclass_of( |
|
|
|
|
137
|
|
|
$this->getClass(), |
138
|
|
|
ResourceInterface::class |
139
|
|
|
) |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.