PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
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 2.0.9, at tests/Module/Checkout/CheckoutStorageTest.php

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