PluginProbe
Packeta / trunk
Packeta vtrunk
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / tests / Module / Checkout / CheckoutStorageTest.php

CheckoutStorageTest.php in Packeta trunk, at tests/Module/Checkout/CheckoutStorageTest.php

267 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Tests\Module\Checkout;
6
7 use Packetery\Module\Checkout\CheckoutStorage;
8 use Packetery\Module\Framework\WcAdapter;
9 use Packetery\Module\Framework\WpAdapter;
10 use Packetery\Module\Transients;
11 use Packetery\Nette\Http\Request;
12 use PHPUnit\Framework\MockObject\MockObject;
13 use PHPUnit\Framework\TestCase;
14 use ReflectionClass;
15
16 class CheckoutStorageTest extends TestCase {
17 private WpAdapter&MockObject $wpAdapterMock;
18
19 private function createCheckoutStorage(): CheckoutStorage {
20 $this->wpAdapterMock = $this->createMock( WpAdapter::class );
21
22 return new CheckoutStorage(
23 $this->createMock( Request::class ),
24 $this->wpAdapterMock,
25 $this->createMock( WcAdapter::class )
26 );
27 }
28
29 public function testIsKeyPresentInSavedDataButNotInPostData(): void {
30 $checkoutStorage = $this->createCheckoutStorage();
31
32 $reflection = new ReflectionClass( $checkoutStorage );
33
34 $privateIsKeyPresentInSavedDataButNotInPostData = $reflection->getMethod( 'isKeyPresentInSavedDataButNotInPostData' );
35 $privateIsKeyPresentInSavedDataButNotInPostData->setAccessible( true );
36
37 $this->assertTrue(
38 $privateIsKeyPresentInSavedDataButNotInPostData->invoke(
39 $checkoutStorage,
40 [ 'key1' => 'value1' ],
41 [ 'key2' => 'value2' ],
42 'key2'
43 ),
44 'Key is not present in checkoutData but present in savedCarrierData.'
45 );
46
47 $this->assertFalse(
48 $privateIsKeyPresentInSavedDataButNotInPostData->invoke(
49 $checkoutStorage,
50 [
51 'key1' => 'value1',
52 'key2' => 'value2',
53 ],
54 [ 'key2' => 'value2' ],
55 'key2'
56 ),
57 'Key is present in both checkoutData and savedCarrierData.'
58 );
59
60 $this->assertFalse(
61 $privateIsKeyPresentInSavedDataButNotInPostData->invoke(
62 $checkoutStorage,
63 [ 'key2' => '' ],
64 [ 'key2' => '' ],
65 'key2'
66 ),
67 'Key is present in both, but both are empty or not set.'
68 );
69
70 $this->assertTrue(
71 $privateIsKeyPresentInSavedDataButNotInPostData->invoke(
72 $checkoutStorage,
73 [],
74 [ 'key2' => 'value2' ],
75 'key2'
76 ),
77 'Key is missing in checkoutData but present in savedCarrierData.'
78 );
79
80 $this->assertFalse(
81 $privateIsKeyPresentInSavedDataButNotInPostData->invoke(
82 $checkoutStorage,
83 [ 'key2' => 'value2' ],
84 [],
85 'key2'
86 ),
87 'Key is present in checkoutData but missing in savedCarrierData.'
88 );
89
90 $this->assertFalse(
91 $privateIsKeyPresentInSavedDataButNotInPostData->invoke(
92 $checkoutStorage,
93 [],
94 [],
95 'key2'
96 ),
97 'Key is missing in both arrays.'
98 );
99 }
100
101 public function testMigrateGuestSessionToUserSession(): void {
102 $dummyGuestSessionId = 'dummy_guest_session_123';
103 $oldTransientId = Transients::CHECKOUT_DATA_PREFIX . $dummyGuestSessionId;
104 $dummyTransientData = [
105 'shipping_method_1' => [
106 'point_id' => '123',
107 'carrier_id' => '456',
108 ],
109 ];
110
111 $checkoutStorage = $this->createCheckoutStorage();
112
113 $this->wpAdapterMock->expects( $this->once() )
114 ->method( 'getTransient' )
115 ->with( $oldTransientId )
116 ->willReturn( $dummyTransientData );
117
118 $this->wpAdapterMock->expects( $this->once() )
119 ->method( 'setTransient' );
120
121 $this->wpAdapterMock->expects( $this->once() )
122 ->method( 'deleteTransient' )
123 ->with( $oldTransientId );
124
125 $checkoutStorage->migrateGuestSessionToUserSession( $dummyGuestSessionId );
126 }
127
128 public function testMigrateGuestSessionToUserSessionNoDataSaved(): void {
129 $dummyGuestSessionId = 'dummy_guest_session_123';
130 $oldTransientId = Transients::CHECKOUT_DATA_PREFIX . $dummyGuestSessionId;
131 $checkoutStorage = $this->createCheckoutStorage();
132
133 $this->wpAdapterMock->expects( $this->once() )
134 ->method( 'getTransient' )
135 ->with( $oldTransientId )
136 ->willReturn( false );
137
138 $this->wpAdapterMock->expects( $this->never() )
139 ->method( 'setTransient' );
140
141 $this->wpAdapterMock->expects( $this->never() )
142 ->method( 'deleteTransient' );
143
144 $checkoutStorage->migrateGuestSessionToUserSession( $dummyGuestSessionId );
145 }
146
147 public static function transientDataProvider(): array {
148 return [
149 'do not set, delete v1' => [
150 'transientData' => [],
151 ],
152 'do not set, delete v2' => [
153 'transientData' => 123,
154 ],
155 'do not set, delete v3' => [
156 'transientData' => 123.45,
157 ],
158 'do not set, delete v4' => [
159 'transientData' => 'foo',
160 ],
161 'do not set, delete v5' => [
162 'transientData' => '',
163 ],
164 'do not set, delete v6' => [
165 'transientData' => new \stdClass(),
166 ],
167 'do not set, delete v7' => [
168 'transientData' => null,
169 ],
170 'do not set, delete v8' => [
171 'transientData' => true,
172 ],
173 ];
174 }
175
176 /**
177 * @dataProvider transientDataProvider
178 */
179 public function testMigrateGuestSessionToUserSessionEmptyOrWrongTypeDataSaved( $transientData ): void {
180 $dummyGuestSessionId = 'dummy_guest_session_123';
181 $oldTransientId = Transients::CHECKOUT_DATA_PREFIX . $dummyGuestSessionId;
182 $checkoutStorage = $this->createCheckoutStorage();
183
184 $this->wpAdapterMock->expects( $this->once() )
185 ->method( 'getTransient' )
186 ->with( $oldTransientId )
187 ->willReturn( $transientData );
188
189 $this->wpAdapterMock->expects( $this->never() )
190 ->method( 'setTransient' );
191
192 $this->wpAdapterMock->expects( $this->once() )
193 ->method( 'deleteTransient' )
194 ->with( $oldTransientId );
195
196 $checkoutStorage->migrateGuestSessionToUserSession( $dummyGuestSessionId );
197 }
198
199 public static function validateDataStructureProvider(): array {
200 return [
201 'valid structure' => [
202 'data' => [
203 'shipping_method_1' => [
204 'point_id' => '123',
205 'carrier_id' => '456',
206 ],
207 'shipping_method_2' => [
208 'address_validated' => true,
209 'street' => 'Main Street',
210 ],
211 ],
212 'expected' => true,
213 ],
214 'empty array' => [
215 'data' => [],
216 'expected' => false,
217 ],
218 'not an array' => [
219 'data' => 'not an array',
220 'expected' => false,
221 ],
222 'numeric key in top level' => [
223 'data' => [
224 0 => [
225 'point_id' => '123',
226 ],
227 ],
228 'expected' => false,
229 ],
230 'value not an array' => [
231 'data' => [
232 'shipping_method_1' => 'not an array',
233 ],
234 'expected' => false,
235 ],
236 'numeric key in nested array' => [
237 'data' => [
238 'shipping_method_1' => [
239 0 => 'numeric key',
240 ],
241 ],
242 'expected' => false,
243 ],
244 'mixed valid and invalid' => [
245 'data' => [
246 'shipping_method_1' => [
247 'point_id' => '123',
248 ],
249 0 => [
250 'carrier_id' => '456',
251 ],
252 ],
253 'expected' => false,
254 ],
255 ];
256 }
257
258 /**
259 * @dataProvider validateDataStructureProvider
260 */
261 public function testValidateDataStructure( $data, bool $expected ): void {
262 $checkoutStorage = $this->createCheckoutStorage();
263
264 $this->assertSame( $checkoutStorage->validateDataStructure( $data ), $expected );
265 }
266 }
267