PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.21
ووسلام – همگام سازی ووکامرس و باسلام v1.10.21
1.10.21 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 All 54 releases
sync-basalam / tests / Services / FetchCommissionTest.php

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

163 lines 4.9 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 RuntimeException;
7 use SyncBasalam\Config\Endpoints;
8 use SyncBasalam\Services\ApiServiceManager;
9 use SyncBasalam\Services\Products\FetchCommission;
10
11 class FetchCommissionTest extends TestCase
12 {
13 private $apiService;
14
15 protected function setUp(): void
16 {
17 FetchCommission::resetProductCommissionCache();
18
19 $this->apiService = new class {
20 public array $requestedUrls = [];
21 public array $response = [
22 'status_code' => 200,
23 'body' => '{}',
24 ];
25 public ?\Throwable $exception = null;
26
27 public function get($url): array
28 {
29 $this->requestedUrls[] = $url;
30
31 if ($this->exception !== null) {
32 throw $this->exception;
33 }
34
35 return $this->response;
36 }
37 };
38
39 $apiService = $this->apiService;
40 $GLOBALS['sync_basalam_test_container'] = new class($apiService) {
41 private $apiService;
42
43 public function __construct($apiService)
44 {
45 $this->apiService = $apiService;
46 }
47
48 public function get(string $service)
49 {
50 if ($service !== ApiServiceManager::class) {
51 throw new RuntimeException('Unexpected service requested: ' . $service);
52 }
53
54 return $this->apiService;
55 }
56 };
57 }
58
59 protected function tearDown(): void
60 {
61 FetchCommission::resetProductCommissionCache();
62 unset($GLOBALS['sync_basalam_test_container']);
63 }
64
65 public function testProductCommissionUsesProductEndpointAndReadsEffectiveCommission(): void
66 {
67 $this->apiService->response['body'] = json_encode([
68 'commission_data' => [
69 'commission_percent' => 4,
70 ],
71 'category_commission_data' => [
72 'commission_percent' => 6.5,
73 ],
74 ]);
75
76 self::assertSame(4, FetchCommission::fetchProductCommission(57983220));
77 self::assertSame(
78 ['https://core.basalam.com/api_v2/commission/products/57983220/percent'],
79 $this->apiService->requestedUrls
80 );
81 }
82
83 public function testProductCommissionIsFetchedOncePerProductDuringTheRequest(): void
84 {
85 $productId = 57983221;
86 $this->apiService->response['body'] = json_encode([
87 'commission_data' => [
88 'commission_percent' => 4,
89 ],
90 ]);
91
92 self::assertSame(4, FetchCommission::fetchProductCommission($productId));
93 self::assertSame(4, FetchCommission::fetchProductCommission($productId));
94 self::assertSame(
95 [sprintf(Endpoints::PRODUCT_COMMISSION, $productId)],
96 $this->apiService->requestedUrls
97 );
98 }
99
100 public function testCategoryCommissionKeepsTheExistingCreateFlow(): void
101 {
102 $this->apiService->response['body'] = json_encode([
103 'commission_data' => [
104 'commission_percent' => 6.5,
105 ],
106 ]);
107
108 self::assertSame(6.5, FetchCommission::fetchCategoryCommission([11, 22, 33]));
109 self::assertSame(
110 [Endpoints::COMMISSION . '?product.category.level1=11&product.category.level2=22&product.category.level3=33'],
111 $this->apiService->requestedUrls
112 );
113 }
114
115 /**
116 * @dataProvider invalidProductIdProvider
117 */
118 public function testInvalidProductIdReturnsZeroWithoutRequest($productId): void
119 {
120 self::assertSame(0, FetchCommission::fetchProductCommission($productId));
121 self::assertSame([], $this->apiService->requestedUrls);
122 }
123
124 public function invalidProductIdProvider(): array
125 {
126 return [
127 'null' => [null],
128 'empty string' => [''],
129 'non numeric string' => ['not-an-id'],
130 'zero' => [0],
131 'negative integer' => [-10],
132 ];
133 }
134
135 /**
136 * @dataProvider invalidResponseProvider
137 */
138 public function testInvalidProductCommissionResponseReturnsZero($body): void
139 {
140 $this->apiService->response['body'] = $body;
141
142 self::assertSame(0, FetchCommission::fetchProductCommission(57983220));
143 }
144
145 public function invalidResponseProvider(): array
146 {
147 return [
148 'malformed JSON' => ['not-json'],
149 'empty response' => [''],
150 'missing commission data' => [json_encode([])],
151 'missing commission percent' => [json_encode(['commission_data' => []])],
152 'non numeric percent' => [json_encode(['commission_data' => ['commission_percent' => 'unknown']])],
153 ];
154 }
155
156 public function testProductCommissionRequestFailureReturnsZero(): void
157 {
158 $this->apiService->exception = new RuntimeException('Network failure');
159
160 self::assertSame(0, FetchCommission::fetchProductCommission(57983220));
161 }
162 }
163