1 | <?php |
||||
2 | |||||
3 | /** |
||||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||||
6 | */ |
||||
7 | |||||
8 | namespace SprykerTest\Glue\GlueApplication\Rest\Response; |
||||
9 | |||||
10 | use Codeception\Test\Unit; |
||||
11 | use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceBuilder; |
||||
12 | use Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface; |
||||
13 | use Spryker\Glue\GlueApplication\Rest\ResourceRelationshipLoaderInterface; |
||||
14 | use Spryker\Glue\GlueApplication\Rest\Response\ResponseRelationship; |
||||
15 | use Spryker\Glue\GlueApplication\Rest\Response\ResponseRelationshipInterface; |
||||
16 | use Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\ResourceRelationshipPluginInterface; |
||||
17 | use SprykerTest\Glue\GlueApplication\Stub\RestRequest; |
||||
18 | |||||
19 | /** |
||||
20 | * @deprecated Will be removed without replacement. |
||||
21 | * |
||||
22 | * Auto-generated group annotations |
||||
23 | * |
||||
24 | * @group SprykerTest |
||||
25 | * @group Glue |
||||
26 | * @group GlueApplication |
||||
27 | * @group Rest |
||||
28 | * @group Response |
||||
29 | * @group ResponseRelationshipTest |
||||
30 | * |
||||
31 | * Add your own group annotations below this line |
||||
32 | */ |
||||
33 | class ResponseRelationshipTest extends Unit |
||||
34 | { |
||||
35 | /** |
||||
36 | * @var string |
||||
37 | */ |
||||
38 | protected const RESOURCE_TYPE_PRODUCT_ABSTRACT = 'product-abstract'; |
||||
39 | |||||
40 | /** |
||||
41 | * @var string |
||||
42 | */ |
||||
43 | protected const RESOURCE_TYPE_PRODUCT_CONCRETE = 'product-concrete'; |
||||
44 | |||||
45 | /** |
||||
46 | * @var string |
||||
47 | */ |
||||
48 | protected const RESOURCE_TYPE_IMAGE_SET = 'concrete-product-image-sets'; |
||||
49 | |||||
50 | /** |
||||
51 | * @var int |
||||
52 | */ |
||||
53 | protected const RESOURCE_ABSTRACT_PRODUCT_ID = 1; |
||||
54 | |||||
55 | /** |
||||
56 | * @var int |
||||
57 | */ |
||||
58 | protected const RESOURCE_IMAGE_SET_ID = 1; |
||||
59 | |||||
60 | /** |
||||
61 | * @var int |
||||
62 | */ |
||||
63 | protected const RESOURCE_CONCRETE_PRODUCT_ID = 2; |
||||
64 | |||||
65 | /** |
||||
66 | * @return void |
||||
67 | */ |
||||
68 | public function testLoadRelationshipsShouldIncludeRelationsByPlugin(): void |
||||
69 | { |
||||
70 | $restResponseBuilder = new RestResourceBuilder(); |
||||
71 | |||||
72 | $relationshipLoaderMock = $this->createRelationshipLoaderMock(); |
||||
73 | |||||
74 | $relationshipPluginMock = $this->createResourceRelationshipPluginMock(); |
||||
75 | |||||
76 | $relationshipPluginMock |
||||
77 | ->method('addResourceRelationships') |
||||
78 | ->willReturnCallback( |
||||
79 | function (array $resources, RestRequestInterface $restRequest) use ($restResponseBuilder): void { |
||||
80 | foreach ($resources as $resource) { |
||||
81 | $resource->addRelationship( |
||||
82 | $restResponseBuilder->createRestResource('related', 1), |
||||
83 | ); |
||||
84 | } |
||||
85 | }, |
||||
86 | ); |
||||
87 | |||||
88 | $relationshipLoaderMock |
||||
89 | ->method('load') |
||||
90 | ->willReturn([$relationshipPluginMock]); |
||||
91 | |||||
92 | $responseRelationship = $this->createResponseRelationship($relationshipLoaderMock); |
||||
93 | |||||
94 | $resource = $restResponseBuilder->createRestResource('tests', 1); |
||||
95 | |||||
96 | $restRequest = (new RestRequest())->createRestRequest(); |
||||
97 | |||||
98 | $responseRelationship->loadRelationships('tests', [$resource], $restRequest); |
||||
99 | |||||
100 | $actualRelationships = $resource->getRelationships()['related']; |
||||
101 | $this->assertCount(1, $actualRelationships); |
||||
102 | |||||
103 | $firstRelation = reset($actualRelationships); |
||||
104 | $this->assertSame('1', $firstRelation->getId()); |
||||
105 | $this->assertSame('related', $firstRelation->getType()); |
||||
106 | } |
||||
107 | |||||
108 | /** |
||||
109 | * @return void |
||||
110 | */ |
||||
111 | public function testProcessIncludedShouldReturnIncludedFromRelationships(): void |
||||
112 | { |
||||
113 | $responseRelationship = $this->createResponseRelationship(); |
||||
114 | |||||
115 | $restResponseBuilder = new RestResourceBuilder(); |
||||
116 | $resource = $restResponseBuilder->createRestResource('tests', 1); |
||||
117 | $resourceRelated = $restResponseBuilder->createRestResource('related', 1); |
||||
118 | $resource->addRelationship($resourceRelated); |
||||
119 | |||||
120 | $restRequest = (new RestRequest())->createRestRequest(); |
||||
121 | |||||
122 | $included = $responseRelationship->processIncluded([$resource], $restRequest); |
||||
123 | |||||
124 | $this->assertCount(1, $included); |
||||
125 | $this->assertSame('related', $included[0]->getType()); |
||||
126 | $this->assertSame('1', $included[0]->getId()); |
||||
127 | } |
||||
128 | |||||
129 | /** |
||||
130 | * @return void |
||||
131 | */ |
||||
132 | public function testProcessIncludedDoesntOverrideResourceWithRelationship(): void |
||||
133 | { |
||||
134 | // Arrange |
||||
135 | $restResourceBuilder = new RestResourceBuilder(); |
||||
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||||
136 | $restRequest = (new RestRequest())->createRestRequest(); |
||||
0 ignored issues
–
show
The class
SprykerTest\Glue\GlueApplication\Stub\RestRequest has been deprecated: Will be removed without replacement.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
137 | |||||
138 | $relationshipLoaderMock = $this->createRelationshipLoaderMock(); |
||||
139 | $responseRelationship = $this->createResponseRelationship($relationshipLoaderMock); |
||||
140 | |||||
141 | $resources = $this->createResourcesWithOverwritableRelations($restResourceBuilder); |
||||
142 | |||||
143 | // Act |
||||
144 | /** |
||||
145 | * @var \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface $includeAbstractProduct |
||||
146 | * @var \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface $includeImageSet |
||||
147 | * @var \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface $includeConcreteProduct |
||||
148 | */ |
||||
149 | [$includeAbstractProduct, $includeImageSet, $includeConcreteProduct] = $responseRelationship |
||||
150 | ->processIncluded($resources, $restRequest); |
||||
151 | |||||
152 | // Assert |
||||
153 | $this->assertSame(static::RESOURCE_TYPE_PRODUCT_ABSTRACT, $includeAbstractProduct->getType()); |
||||
154 | $this->assertEquals(static::RESOURCE_ABSTRACT_PRODUCT_ID, $includeAbstractProduct->getId()); |
||||
155 | |||||
156 | $this->assertSame(static::RESOURCE_TYPE_IMAGE_SET, $includeImageSet->getType()); |
||||
157 | $this->assertEquals(static::RESOURCE_IMAGE_SET_ID, $includeImageSet->getId()); |
||||
158 | |||||
159 | $this->assertSame(static::RESOURCE_TYPE_PRODUCT_CONCRETE, $includeConcreteProduct->getType()); |
||||
160 | $this->assertEquals(static::RESOURCE_CONCRETE_PRODUCT_ID, $includeConcreteProduct->getId()); |
||||
161 | |||||
162 | $concreteProductIncludesItemRelations = $includeConcreteProduct->getRelationships(); |
||||
163 | |||||
164 | $this->assertCount(2, $concreteProductIncludesItemRelations); |
||||
165 | |||||
166 | $this->assertArrayHasKey(static::RESOURCE_TYPE_PRODUCT_ABSTRACT, $concreteProductIncludesItemRelations); |
||||
167 | $this->assertCount(1, $concreteProductIncludesItemRelations[static::RESOURCE_TYPE_PRODUCT_ABSTRACT]); |
||||
168 | |||||
169 | $this->assertArrayHasKey(static::RESOURCE_TYPE_IMAGE_SET, $concreteProductIncludesItemRelations); |
||||
170 | $this->assertCount(1, $concreteProductIncludesItemRelations[static::RESOURCE_TYPE_IMAGE_SET]); |
||||
171 | } |
||||
172 | |||||
173 | /** |
||||
174 | * @param \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceBuilder $restResourceBuilder |
||||
175 | * |
||||
176 | * @return array<\Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface> |
||||
177 | */ |
||||
178 | protected function createResourcesWithOverwritableRelations(RestResourceBuilder $restResourceBuilder): array |
||||
179 | { |
||||
180 | $abstractProductResource = $restResourceBuilder->createRestResource( |
||||
181 | static::RESOURCE_TYPE_PRODUCT_ABSTRACT, |
||||
182 | static::RESOURCE_ABSTRACT_PRODUCT_ID, |
||||
183 | ); |
||||
184 | |||||
185 | $imageSetResource = $restResourceBuilder->createRestResource( |
||||
186 | static::RESOURCE_TYPE_IMAGE_SET, |
||||
187 | static::RESOURCE_IMAGE_SET_ID, |
||||
188 | ); |
||||
189 | |||||
190 | $concreteProduct = $restResourceBuilder->createRestResource( |
||||
191 | static::RESOURCE_TYPE_PRODUCT_CONCRETE, |
||||
192 | static::RESOURCE_CONCRETE_PRODUCT_ID, |
||||
193 | ); |
||||
194 | |||||
195 | $concreteProduct->addRelationship( |
||||
196 | $restResourceBuilder->createRestResource( |
||||
197 | static::RESOURCE_TYPE_PRODUCT_ABSTRACT, |
||||
198 | static::RESOURCE_ABSTRACT_PRODUCT_ID, |
||||
199 | ), |
||||
200 | ); |
||||
201 | |||||
202 | $concreteProduct->addRelationship( |
||||
203 | $restResourceBuilder->createRestResource( |
||||
204 | static::RESOURCE_TYPE_IMAGE_SET, |
||||
205 | static::RESOURCE_IMAGE_SET_ID, |
||||
206 | ), |
||||
207 | ); |
||||
208 | |||||
209 | $abstractProductResource->addRelationship($concreteProduct); |
||||
210 | |||||
211 | $imageSetResource->addRelationship( |
||||
212 | $restResourceBuilder->createRestResource( |
||||
213 | static::RESOURCE_TYPE_PRODUCT_CONCRETE, |
||||
214 | static::RESOURCE_CONCRETE_PRODUCT_ID, |
||||
215 | ), |
||||
216 | ); |
||||
217 | |||||
218 | return [$abstractProductResource, $imageSetResource]; |
||||
219 | } |
||||
220 | |||||
221 | /** |
||||
222 | * @return void |
||||
223 | */ |
||||
224 | public function testHasRelationshipShouldReturnTrueWhenIncludedOmitted(): void |
||||
225 | { |
||||
226 | $responseRelationship = $this->createResponseRelationship(); |
||||
227 | |||||
228 | $restRequest = (new RestRequest())->createRestRequest(); |
||||
229 | |||||
230 | $this->assertTrue($responseRelationship->hasRelationship('test', $restRequest)); |
||||
231 | } |
||||
232 | |||||
233 | /** |
||||
234 | * @param \Spryker\Glue\GlueApplication\Rest\ResourceRelationshipLoaderInterface|null $relationshipLoaderMock |
||||
235 | * |
||||
236 | * @return \Spryker\Glue\GlueApplication\Rest\Response\ResponseRelationshipInterface |
||||
237 | */ |
||||
238 | protected function createResponseRelationship( |
||||
239 | ?ResourceRelationshipLoaderInterface $relationshipLoaderMock = null |
||||
240 | ): ResponseRelationshipInterface { |
||||
241 | if (!$relationshipLoaderMock) { |
||||
242 | $relationshipLoaderMock = $this->createRelationshipLoaderMock(); |
||||
243 | } |
||||
244 | |||||
245 | return new ResponseRelationship($relationshipLoaderMock); |
||||
246 | } |
||||
247 | |||||
248 | /** |
||||
249 | * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Glue\GlueApplication\Rest\ResourceRelationshipLoaderInterface |
||||
250 | */ |
||||
251 | protected function createRelationshipLoaderMock(): ResourceRelationshipLoaderInterface |
||||
252 | { |
||||
253 | return $this->getMockBuilder(ResourceRelationshipLoaderInterface::class) |
||||
254 | ->setMethods(['load']) |
||||
255 | ->getMock(); |
||||
256 | } |
||||
257 | |||||
258 | /** |
||||
259 | * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\ResourceRelationshipPluginInterface |
||||
260 | */ |
||||
261 | protected function createResourceRelationshipPluginMock(): ResourceRelationshipPluginInterface |
||||
262 | { |
||||
263 | return $this->getMockBuilder(ResourceRelationshipPluginInterface::class) |
||||
264 | ->setMethods(['addResourceRelationships', 'getRelationshipResourceType']) |
||||
265 | ->getMock(); |
||||
266 | } |
||||
267 | } |
||||
268 |