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