PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.19
ووسلام – همگام سازی ووکامرس و باسلام v1.10.19
1.10.19 1.10.20 1.10.18 1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.2 1.9.1 1.9.0 1.8.8 1.8.5 1.8.6 All 53 releases
sync-basalam / tests / Services / VendorSyncPolicyTest.php

VendorSyncPolicyTest.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.19, at tests/Services/VendorSyncPolicyTest.php

228 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SyncBasalam\Tests\Services;
4
5 use PHPUnit\Framework\TestCase;
6 use SyncBasalam\Services\VendorSyncPolicy;
7
8 class VendorSyncPolicyTest extends TestCase
9 {
10 public function testActiveVendorAllowsNormalSync(): void
11 {
12 $now = 2_000_000;
13 $state = VendorSyncPolicy::stateFromVendorInfo(
14 [],
15 ['is_active' => true, 'status' => ['value' => 2987, 'name' => 'فعال']],
16 123,
17 $now
18 );
19
20 self::assertTrue($state['is_active']);
21 self::assertNull($state['inactive_since']);
22 self::assertSame(VendorSyncPolicy::MODE_ACTIVE, VendorSyncPolicy::resolveMode($state, $now));
23 }
24
25 public function testDisabledVendorStartsOneContinuousInactivePeriod(): void
26 {
27 $firstCheck = 2_000_000;
28 $state = VendorSyncPolicy::stateFromVendorInfo(
29 [],
30 ['is_active' => false, 'status' => ['id' => 2988, 'name' => 'غیرفعال']],
31 123,
32 $firstCheck
33 );
34
35 $secondCheck = $firstCheck + DAY_IN_SECONDS;
36 $state = VendorSyncPolicy::stateFromVendorInfo(
37 $state,
38 ['is_active' => false, 'status' => ['value' => 2988, 'name' => 'غیرفعال']],
39 123,
40 $secondCheck
41 );
42
43 self::assertFalse($state['is_active']);
44 self::assertSame($firstCheck, $state['inactive_since']);
45 self::assertSame(
46 VendorSyncPolicy::MODE_INACTIVE_LIMITED,
47 VendorSyncPolicy::resolveMode($state, $firstCheck + (21 * DAY_IN_SECONDS) - 1)
48 );
49 }
50
51 public function testExactlyTwentyOneDaysSuspendsAllProductSync(): void
52 {
53 $inactiveSince = 2_000_000;
54 $state = ['is_active' => false, 'inactive_since' => $inactiveSince];
55
56 self::assertSame(
57 VendorSyncPolicy::MODE_INACTIVE_LIMITED,
58 VendorSyncPolicy::resolveMode($state, $inactiveSince + (21 * DAY_IN_SECONDS) - 1)
59 );
60 self::assertSame(
61 VendorSyncPolicy::MODE_INACTIVE_SUSPENDED,
62 VendorSyncPolicy::resolveMode($state, $inactiveSince + (21 * DAY_IN_SECONDS))
63 );
64 }
65
66 /**
67 * @dataProvider unavailableVendorInfoProvider
68 */
69 public function testUnavailableVendorSignalsAreRecognized(array $vendorInfo): void
70 {
71 $state = VendorSyncPolicy::stateFromVendorInfo([], $vendorInfo, 123, 2_000_000);
72
73 self::assertFalse($state['is_active']);
74 self::assertSame(2_000_000, $state['inactive_since']);
75 self::assertSame(
76 VendorSyncPolicy::MODE_INACTIVE_LIMITED,
77 VendorSyncPolicy::resolveMode($state, 2_000_000)
78 );
79 }
80
81 public function unavailableVendorInfoProvider(): array
82 {
83 return [
84 'disabled status code wins over a contradictory active flag' => [
85 ['is_active' => true, 'status' => ['value' => 2988, 'name' => 'فعال']],
86 ],
87 'closed status code' => [
88 ['status' => ['id' => 3199, 'name' => 'بسته شده']],
89 ],
90 'Persian inactive status name' => [
91 ['status' => ['name' => 'غیرفعال توسط پشتیبانی']],
92 ],
93 'English closed status title' => [
94 ['status' => ['title' => 'Closed by support']],
95 ],
96 'string false active flag' => [
97 ['is_active' => 'false', 'status' => ['name' => 'Unknown']],
98 ],
99 ];
100 }
101
102 public function testReactivationClearsInactiveTimestamp(): void
103 {
104 $state = VendorSyncPolicy::stateFromVendorInfo(
105 [
106 'vendor_id' => 123,
107 'is_active' => false,
108 'inactive_since' => 1_000_000,
109 ],
110 ['is_active' => true, 'status' => ['value' => 2987, 'name' => 'فعال']],
111 123,
112 3_000_000
113 );
114
115 self::assertTrue($state['is_active']);
116 self::assertNull($state['inactive_since']);
117 }
118
119 public function testChangingVendorStartsANewInactivePeriod(): void
120 {
121 $state = VendorSyncPolicy::stateFromVendorInfo(
122 [
123 'vendor_id' => 123,
124 'is_active' => false,
125 'inactive_since' => 1_000_000,
126 ],
127 ['is_active' => false, 'status' => ['value' => 2988, 'name' => 'غیرفعال']],
128 456,
129 3_000_000
130 );
131
132 self::assertSame(456, $state['vendor_id']);
133 self::assertSame(3_000_000, $state['inactive_since']);
134 }
135
136 public function testActiveModePreservesTheCompleteUpdatePayload(): void
137 {
138 $payload = [
139 'id' => 11,
140 'name' => 'Product name',
141 'description' => 'Description',
142 'primary_price' => 120_000,
143 'stock' => 4,
144 'photos' => [1, 2],
145 ];
146
147 self::assertSame(
148 $payload,
149 VendorSyncPolicy::restrictUpdatePayloadForMode($payload, VendorSyncPolicy::MODE_ACTIVE)
150 );
151 }
152
153 public function testLimitedModeKeepsOnlyPriceAndStockFields(): void
154 {
155 $payload = [
156 'id' => 11,
157 'type' => 'variable',
158 'name' => 'Product name',
159 'description' => 'Description',
160 'primary_price' => 120_000,
161 'stock' => 0,
162 'photos' => [1, 2],
163 'variants' => [
164 [
165 'id' => 22,
166 'primary_price' => 90_000,
167 'stock' => 3,
168 'properties' => [['value' => 'red']],
169 ],
170 [
171 'id' => null,
172 'primary_price' => 80_000,
173 'stock' => 2,
174 'properties' => [['value' => 'blue']],
175 ],
176 ],
177 ];
178
179 $restricted = VendorSyncPolicy::restrictUpdatePayloadForMode(
180 $payload,
181 VendorSyncPolicy::MODE_INACTIVE_LIMITED
182 );
183
184 self::assertSame(['id', 'type', 'primary_price', 'stock', 'variants'], array_keys($restricted));
185 self::assertCount(1, $restricted['variants']);
186 self::assertSame(['id', 'primary_price', 'stock'], array_keys($restricted['variants'][0]));
187 self::assertSame(0, $restricted['stock']);
188 }
189
190 public function testSuspendedModeProducesNoMutationPayload(): void
191 {
192 self::assertSame([], VendorSyncPolicy::restrictUpdatePayloadForMode(
193 ['id' => 11, 'stock' => 2],
194 VendorSyncPolicy::MODE_INACTIVE_SUSPENDED
195 ));
196 }
197
198 public function testLimitedModeDropsEveryDisconnectedVariation(): void
199 {
200 $restricted = VendorSyncPolicy::restrictUpdatePayloadForMode(
201 [
202 'id' => 11,
203 'type' => 'variable',
204 'name' => 'Product name',
205 'variants' => [
206 ['id' => null, 'primary_price' => 80_000, 'stock' => 2],
207 ['primary_price' => 90_000, 'stock' => 3],
208 ],
209 ],
210 VendorSyncPolicy::MODE_INACTIVE_LIMITED
211 );
212
213 self::assertSame(['id' => 11, 'type' => 'variable'], $restricted);
214 }
215
216 public function testStatusRefreshBecomesDueAtTwentyFourHours(): void
217 {
218 $checkedAt = 2_000_000;
219 $state = ['vendor_id' => 123, 'checked_at' => $checkedAt];
220
221 self::assertFalse(VendorSyncPolicy::isRefreshDue($state, 123, $checkedAt + DAY_IN_SECONDS - 1));
222 self::assertTrue(VendorSyncPolicy::isRefreshDue($state, 123, $checkedAt + DAY_IN_SECONDS));
223 self::assertTrue(VendorSyncPolicy::isRefreshDue($state, 456, $checkedAt + 10));
224 self::assertTrue(VendorSyncPolicy::isRefreshDue($state, 123, $checkedAt - 1));
225 self::assertTrue(VendorSyncPolicy::isRefreshDue(['vendor_id' => 123], 123, $checkedAt));
226 }
227 }
228