forked from laminas-api-tools/api-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableGatewayAbstractFactoryTest.php
280 lines (240 loc) · 10.9 KB
/
TableGatewayAbstractFactoryTest.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
declare(strict_types=1);
namespace LaminasTest\ApiTools;
use Interop\Container\ContainerInterface;
use Laminas\ApiTools\TableGatewayAbstractFactory;
use Laminas\Db\Adapter\Adapter as DbAdapter;
use Laminas\Db\Adapter\AdapterInterface as DbAdapterInterface;
use Laminas\Db\Adapter\Platform\PlatformInterface as DbPlatformInterface;
use Laminas\Db\ResultSet\HydratingResultSet;
use Laminas\Db\TableGateway\TableGateway;
use Laminas\Hydrator\ClassMethods;
use Laminas\Hydrator\ClassMethodsHydrator;
use Laminas\Hydrator\HydratorPluginManager;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use ReflectionException;
use ReflectionProperty;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Adapter\AdapterInterface;
use function class_exists;
class TableGatewayAbstractFactoryTest extends TestCase
{
use ProphecyTrait;
protected function setUp(): void
{
$this->services = $this->prophesize(ContainerInterface::class);
$this->factory = new TableGatewayAbstractFactory();
}
public function testWillNotCreateServiceWithoutAppropriateSuffix(): void
{
$this->services->has('config')->shouldNotBeCalled();
$this->assertFalse($this->factory->canCreate($this->services->reveal(), 'Foo'));
}
public function testWillNotCreateServiceIfConfigServiceIsMissing(): void
{
$this->services->has('config')->willReturn(false);
$this->services->get('config')->shouldNotBeCalled();
$this->assertFalse($this->factory->canCreate($this->services->reveal(), 'Foo\Table'));
}
public function testWillNotCreateServiceIfMissingApiToolsConfig(): void
{
$this->services->has('config')->willReturn(true);
$this->services->get('config')->willReturn([]);
$this->assertFalse($this->factory->canCreate($this->services->reveal(), 'Foo\Table'));
}
public function testWillNotCreateServiceIfMissingDbConnectedConfigSegment(): void
{
$this->services->has('config')->willReturn(true);
$this->services->get('config')->willReturn(['api-tools' => []]);
$this->assertFalse($this->factory->canCreate($this->services->reveal(), 'Foo\Table'));
}
public function testWillNotCreateServiceIfMissingServiceSubSegment(): void
{
$this->services->has('config')->willReturn(true);
$this->services->get('config')->willReturn(['api-tools' => ['db-connected' => []]]);
$this->assertFalse($this->factory->canCreate($this->services->reveal(), 'Foo\Table'));
}
public function testWillNotCreateServiceIfServiceSubSegmentIsInvalid(): void
{
$this->services->has('config')->willReturn(true);
$this->services->get('config')->willReturn(['api-tools' => ['db-connected' => ['Foo' => 'invalid']]]);
$this->assertFalse($this->factory->canCreate($this->services->reveal(), 'Foo\Table'));
}
public function testWillNotCreateServiceIfServiceSubSegmentDoesNotContainTableName(): void
{
$this->services->has('config')->willReturn(true);
$this->services->get('config')->willReturn(['api-tools' => ['db-connected' => ['Foo' => []]]]);
$this->assertFalse($this->factory->canCreate($this->services->reveal(), 'Foo\Table'));
}
public function testWillNotCreateServiceIfServiceSubSegmentDoesNotContainAdapterInformation(): void
{
$this->services->has('config')->willReturn(true);
$this->services->get('config')
->willReturn([
'api-tools' => [
'db-connected' => [
'Foo' => [
'table_name' => 'test',
],
],
],
]);
$this->services->has(DbAdapterInterface::class)->willReturn(false);
$this->services->has(AdapterInterface::class)->willReturn(false);
$this->services->has(DbAdapter::class)->willReturn(false);
$this->services->has(Adapter::class)->willReturn(false);
$this->assertFalse($this->factory->canCreate($this->services->reveal(), 'Foo\Table'));
}
public function testWillCreateServiceIfConfigContainsValidTableNameAndAdapterName(): void
{
$this->services->has('config')->willReturn(true);
$this->services->get('config')
->willReturn([
'api-tools' => [
'db-connected' => [
'Foo' => [
'table_name' => 'test',
'adapter_name' => 'FooAdapter',
],
],
],
]);
$this->services->has('FooAdapter')->willReturn(true);
$this->assertTrue($this->factory->canCreate($this->services->reveal(), 'Foo\Table'));
}
// phpcs:ignore Generic.Files.LineLength.TooLong
public function testWillCreateServiceIfConfigContainsValidTableNameNoAdapterNameAndServicesContainDefaultAdapter(): void
{
$this->services->has('config')->willReturn(true);
$this->services->get('config')
->willReturn([
'api-tools' => [
'db-connected' => [
'Foo' => [
'table_name' => 'test',
],
],
],
]);
$this->services->has(DbAdapterInterface::class)->willReturn(false);
$this->services->has(AdapterInterface::class)->willReturn(false);
$this->services->has(DbAdapter::class)->willReturn(true);
$this->assertTrue($this->factory->canCreate($this->services->reveal(), 'Foo\Table'));
}
/** @psalm-return array<string, array{0: class-string}> */
public function validConfig(): array
{
return [
'named_adapter' => ['Db\NamedAdapter'],
'default_adapter' => [DbAdapter::class],
];
}
/**
* @dataProvider validConfig
*/
public function testFactoryReturnsTableGatewayInstanceBasedOnConfiguration(string $adapterServiceName): void
{
$hydrator = $this->prophesize($this->getClassMethodsHydratorClassName())->reveal();
$hydrators = $this->prophesize(HydratorPluginManager::class);
$hydrators->get('ClassMethods')->willReturn($hydrator);
$this->services->get('HydratorManager')->willReturn($hydrators->reveal());
$platform = $this->prophesize(DbPlatformInterface::class);
$platform->getName()->willReturn('sqlite');
$adapter = $this->prophesize(DbAdapter::class);
$adapter->getPlatform()->willReturn($platform->reveal());
$this->services->has($adapterServiceName)->willReturn(true);
$this->services->get($adapterServiceName)->willReturn($adapter->reveal());
$config = [
'api-tools' => [
'db-connected' => [
'Foo' => [
'controller_service_name' => 'Foo\Controller',
'table_name' => 'foo',
'hydrator_name' => 'ClassMethods',
],
],
],
'api-tools-rest' => [
'Foo\Controller' => [
'entity_class' => TestAsset\Foo::class,
],
],
];
if ($adapterServiceName !== DbAdapter::class) {
$config['api-tools']['db-connected']['Foo']['adapter_name'] = $adapterServiceName;
}
$this->services->get('config')->willReturn($config);
$gateway = $this->factory->__invoke($this->services->reveal(), 'Foo\Table');
$this->assertInstanceOf(TableGateway::class, $gateway);
$this->assertEquals('foo', $gateway->getTable());
$this->assertSame($adapter->reveal(), $gateway->getAdapter());
$resultSet = $gateway->getResultSetPrototype();
$this->assertInstanceOf(HydratingResultSet::class, $resultSet);
$this->assertSame($hydrator, $resultSet->getHydrator());
$this->assertObjectPrototypeProperty($resultSet, TestAsset\Foo::class);
}
/**
* @dataProvider validConfig
*/
public function testFactoryReturnsTableGatewayInstanceBasedOnConfigurationWithoutLaminasRest(
string $adapterServiceName
): void {
$hydrator = $this->prophesize($this->getClassMethodsHydratorClassName())->reveal();
$hydrators = $this->prophesize(HydratorPluginManager::class);
$hydrators->get('ClassMethods')->willReturn($hydrator);
$this->services->get('HydratorManager')->willReturn($hydrators->reveal());
$platform = $this->prophesize(DbPlatformInterface::class);
$platform->getName()->willReturn('sqlite');
$adapter = $this->prophesize(DbAdapter::class);
$adapter->getPlatform()->willReturn($platform->reveal());
$this->services->has($adapterServiceName)->willReturn(true);
$this->services->get($adapterServiceName)->willReturn($adapter);
$config = [
'api-tools' => [
'db-connected' => [
'Foo' => [
'controller_service_name' => 'Foo\Controller',
'table_name' => 'foo',
'hydrator_name' => 'ClassMethods',
'entity_class' => TestAsset\Bar::class,
],
],
],
];
if ($adapterServiceName !== DbAdapter::class) {
$config['api-tools']['db-connected']['Foo']['adapter_name'] = $adapterServiceName;
}
$this->services->get('config')->willReturn($config);
$gateway = $this->factory->__invoke($this->services->reveal(), 'Foo\Table');
$this->assertInstanceOf(TableGateway::class, $gateway);
$this->assertEquals('foo', $gateway->getTable());
$this->assertSame($adapter->reveal(), $gateway->getAdapter());
$resultSet = $gateway->getResultSetPrototype();
$this->assertInstanceOf(HydratingResultSet::class, $resultSet);
$this->assertInstanceOf($this->getClassMethodsHydratorClassName(), $resultSet->getHydrator());
$this->assertObjectPrototypeProperty($resultSet, TestAsset\Bar::class);
}
/**
* Simple check whether we should use ClassMethodsHydrator from laminas-hydrator 3
* as ClassMethods from < 3.0.0 is deprecated and triggers an E_USER_DEPRECATED error
*
* @return string
*/
private function getClassMethodsHydratorClassName()
{
if (class_exists(ClassMethodsHydrator::class)) {
return ClassMethodsHydrator::class;
}
return ClassMethods::class;
}
/**
* @throws ReflectionException
*/
private function assertObjectPrototypeProperty(HydratingResultSet $resultSet, string $expectedClassName): void
{
$objectPrototypeProperty = new ReflectionProperty($resultSet, 'objectPrototype');
$objectPrototypeProperty->setAccessible(true);
$this->assertInstanceOf($expectedClassName, $objectPrototypeProperty->getValue($resultSet));
}
}