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\Zed\Comment\Business\CommentFacade; |
||
9 | |||
10 | use Codeception\Test\Unit; |
||
11 | use Generated\Shared\DataBuilder\CommentBuilder; |
||
12 | use Generated\Shared\DataBuilder\CommentRequestBuilder; |
||
13 | use Generated\Shared\Transfer\CommentRequestTransfer; |
||
14 | use Generated\Shared\Transfer\CommentTransfer; |
||
15 | use Generated\Shared\Transfer\CustomerTransfer; |
||
16 | use Spryker\Shared\Kernel\Transfer\Exception\NullValueException; |
||
17 | use Spryker\Zed\Comment\Communication\Plugin\Comment\CustomerCommentAuthorValidationStrategyPlugin; |
||
18 | use SprykerTest\Zed\Comment\CommentBusinessTester; |
||
19 | |||
20 | /** |
||
21 | * Auto-generated group annotations |
||
22 | * |
||
23 | * @group SprykerTest |
||
24 | * @group Zed |
||
25 | * @group Comment |
||
26 | * @group Business |
||
27 | * @group CommentFacade |
||
28 | * @group Facade |
||
29 | * @group CommentFacadeValidateCommentAuthorTest |
||
30 | * Add your own group annotations below this line |
||
31 | */ |
||
32 | class CommentFacadeValidateCommentAuthorTest extends Unit |
||
33 | { |
||
34 | /** |
||
35 | * @uses \Spryker\Zed\Comment\CommentDependencyProvider::PLUGINS_COMMENT_AUTHOR_VALIDATOR_STRATEGY |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected const PLUGINS_COMMENT_AUTHOR_VALIDATOR_STRATEGY = 'PLUGINS_COMMENT_AUTHOR_VALIDATOR_STRATEGY'; |
||
40 | |||
41 | /** |
||
42 | * @uses \Spryker\Zed\Comment\Business\Validator\CustomerCommentValidator::GLOSSARY_KEY_COMMENT_ACCESS_DENIED |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected const GLOSSARY_KEY_COMMENT_ACCESS_DENIED = 'comment.validation.error.access_denied'; |
||
47 | |||
48 | /** |
||
49 | * @uses \Spryker\Zed\Comment\Business\Validator\CustomerCommentValidator::GLOSSARY_KEY_CUSTOMER_NOT_FOUND |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected const GLOSSARY_KEY_CUSTOMER_NOT_FOUND = 'comment.validation.error.customer_not_found'; |
||
54 | |||
55 | /** |
||
56 | * @var \SprykerTest\Zed\Comment\CommentBusinessTester |
||
57 | */ |
||
58 | protected CommentBusinessTester $tester; |
||
59 | |||
60 | /** |
||
61 | * @return void |
||
62 | */ |
||
63 | protected function setUp(): void |
||
64 | { |
||
65 | parent::setUp(); |
||
66 | |||
67 | $this->tester->setDependency(static::PLUGINS_COMMENT_AUTHOR_VALIDATOR_STRATEGY, [ |
||
68 | new CustomerCommentAuthorValidationStrategyPlugin(), |
||
69 | ]); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return void |
||
74 | */ |
||
75 | public function testReturnsNoErrorWhenIdCustomerOfExistingCustomerIsProvided(): void |
||
76 | { |
||
77 | // Arrange |
||
78 | $customerTransfer = $this->tester->haveCustomer(); |
||
79 | $commentRequestTransfer = (new CommentRequestBuilder()) |
||
80 | ->withComment([CommentTransfer::CUSTOMER => $customerTransfer]) |
||
81 | ->build(); |
||
82 | |||
83 | // Act |
||
84 | $commentValidationResponseTransfer = $this->tester->getFacade()->validateCommentAuthor($commentRequestTransfer); |
||
85 | |||
86 | // Assert |
||
87 | $this->assertTrue($commentValidationResponseTransfer->getIsSuccessful()); |
||
88 | $this->assertEmpty($commentValidationResponseTransfer->getMessages()); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return void |
||
93 | */ |
||
94 | public function testReturnsNoErrorWhenIdCustomerIsProvidedAndCommentHasIdCommentSet(): void |
||
95 | { |
||
96 | // Arrange |
||
97 | $customerTransfer = $this->tester->haveCustomer(); |
||
98 | |||
99 | $commentTransfer = (new CommentBuilder([ |
||
100 | CommentTransfer::CUSTOMER => $customerTransfer, |
||
101 | ]))->build(); |
||
102 | $commentThreadResponseTransfer = $this->tester->haveComment([ |
||
103 | CommentRequestTransfer::COMMENT => $commentTransfer->toArray(), |
||
104 | ]); |
||
105 | |||
106 | $commentTransfer = $commentThreadResponseTransfer->getCommentThreadOrFail()->getComments()->getIterator()->current(); |
||
107 | $commentRequestTransfer = (new CommentRequestBuilder()) |
||
108 | ->withComment($commentTransfer->toArray()) |
||
109 | ->build(); |
||
110 | |||
111 | // Act |
||
112 | $commentValidationResponseTransfer = $this->tester->getFacade()->validateCommentAuthor($commentRequestTransfer); |
||
113 | |||
114 | // Assert |
||
115 | $this->assertTrue($commentValidationResponseTransfer->getIsSuccessful()); |
||
116 | $this->assertEmpty($commentValidationResponseTransfer->getMessages()); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @return void |
||
121 | */ |
||
122 | public function testReturnsErrorWhenIdCustomerOfNonExistingCustomerIsProvided(): void |
||
123 | { |
||
124 | // Arrange |
||
125 | $commentRequestTransfer = (new CommentRequestBuilder()) |
||
126 | ->withComment([CommentTransfer::CUSTOMER => (new CustomerTransfer())->setIdCustomer(-1)]) |
||
127 | ->build(); |
||
128 | |||
129 | // Act |
||
130 | $commentValidationResponseTransfer = $this->tester->getFacade()->validateCommentAuthor($commentRequestTransfer); |
||
131 | |||
132 | // Assert |
||
133 | $this->assertFalse($commentValidationResponseTransfer->getIsSuccessful()); |
||
134 | $this->assertCount(1, $commentValidationResponseTransfer->getMessages()); |
||
135 | $this->assertSame( |
||
136 | static::GLOSSARY_KEY_CUSTOMER_NOT_FOUND, |
||
137 | $commentValidationResponseTransfer->getMessages()->getIterator()->current()->getValue(), |
||
138 | ); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @return void |
||
143 | */ |
||
144 | public function testReturnsErrorWhenProvidedCustomerIsNotAnAuthorOfUpdatedComment(): void |
||
145 | { |
||
146 | // Arrange |
||
147 | $customer1 = $this->tester->haveCustomer(); |
||
148 | $customer2 = $this->tester->haveCustomer(); |
||
149 | |||
150 | $commentTransfer = (new CommentBuilder([ |
||
151 | CommentTransfer::CUSTOMER => $customer1, |
||
152 | ]))->build(); |
||
153 | $commentThreadResponseTransfer = $this->tester->haveComment([ |
||
154 | CommentRequestTransfer::COMMENT => $commentTransfer->toArray(), |
||
155 | ]); |
||
156 | |||
157 | $commentTransfer = $commentThreadResponseTransfer->getCommentThreadOrFail()->getComments()->getIterator()->current(); |
||
158 | $commentRequestTransfer = (new CommentRequestBuilder()) |
||
159 | ->withComment($commentTransfer->setCustomer($customer2)->toArray()) |
||
160 | ->build(); |
||
161 | |||
162 | // Act |
||
163 | $commentValidationResponseTransfer = $this->tester->getFacade()->validateCommentAuthor($commentRequestTransfer); |
||
164 | |||
165 | // Assert |
||
166 | $this->assertFalse($commentValidationResponseTransfer->getIsSuccessful()); |
||
167 | $this->assertCount(1, $commentValidationResponseTransfer->getMessages()); |
||
168 | $this->assertSame( |
||
169 | static::GLOSSARY_KEY_COMMENT_ACCESS_DENIED, |
||
170 | $commentValidationResponseTransfer->getMessages()->getIterator()->current()->getValue(), |
||
171 | ); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @dataProvider throwsNullValueExceptionWhenRequiredPropertyIsNotSetDataProvider |
||
176 | * |
||
177 | * @param \Generated\Shared\Transfer\CommentRequestTransfer $commentRequestTransfer |
||
178 | * |
||
179 | * @return void |
||
180 | */ |
||
181 | public function testThrowsNullValueExceptionWhenRequiredPropertyIsNotSet(CommentRequestTransfer $commentRequestTransfer): void |
||
182 | { |
||
183 | // Assert |
||
184 | $this->expectException(NullValueException::class); |
||
185 | |||
186 | // Act |
||
187 | $this->tester->getFacade()->validateCommentAuthor($commentRequestTransfer); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @return array<string, list<\Generated\Shared\Transfer\CommentRequestTransfer>> |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
192 | */ |
||
193 | protected function throwsNullValueExceptionWhenRequiredPropertyIsNotSetDataProvider(): array |
||
194 | { |
||
195 | return [ |
||
196 | 'CommentRequestTransfer.comment property is not set' => [ |
||
197 | (new CommentRequestBuilder([CommentRequestTransfer::COMMENT => null]))->build(), |
||
198 | ], |
||
199 | 'CommentRequestTransfer.comment.customer property is not set' => [ |
||
200 | (new CommentRequestBuilder([ |
||
201 | CommentRequestTransfer::COMMENT => [ |
||
202 | CommentTransfer::CUSTOMER => null, |
||
203 | ]]))->build(), |
||
204 | ], |
||
205 | 'CommentRequestTransfer.comment.customer.idCustomer property is not set' => [ |
||
206 | (new CommentRequestBuilder([ |
||
207 | CommentRequestTransfer::COMMENT => [ |
||
208 | CommentTransfer::CUSTOMER => (new CustomerTransfer())->setIdCustomer(null), |
||
209 | ]]))->build(), |
||
210 | ], |
||
211 | ]; |
||
212 | } |
||
213 | } |
||
214 |