Skip to content

Commit 09ac9de

Browse files
Intercept PendingRequest (#34)
* implement 'body_format' setting for changing the bodyFormat of the requests. * Revert "implement 'body_format' setting for changing the bodyFormat of the requests." This reverts commit 81fd21b. * implement the intecept method. With this method you can change the headers and settings of the used PendingRequest object for the current cycle. * Small adjustments on the interceptor, tests and SCA * Adjust test --------- Co-authored-by: Vincent Boon <[email protected]>
1 parent 981ad1b commit 09ac9de

File tree

2 files changed

+101
-15
lines changed

2 files changed

+101
-15
lines changed

src/Client/Magento.php

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace JustBetter\MagentoClient\Client;
44

5+
use Closure;
56
use Generator;
67
use Illuminate\Http\Client\PendingRequest;
78
use Illuminate\Http\Client\Response;
@@ -16,6 +17,8 @@ class Magento
1617

1718
public ?string $storeCode = null;
1819

20+
public ?Closure $interceptor;
21+
1922
public function __construct(
2023
protected BuildsRequest $request
2124
) {
@@ -42,7 +45,7 @@ public function graphql(string $query, array $variables = []): Response
4245
$endpoint = config("magento.connections.{$this->connection}.graphql_path");
4346

4447
/** @var Response $response */
45-
$response = $this->request->build($this->connection)
48+
$response = $this->request()
4649
->when($this->storeCode !== null, fn (PendingRequest $request): PendingRequest => $request->withHeaders(['Store' => $this->storeCode]))
4750
->post($endpoint, [
4851
'query' => $query,
@@ -54,104 +57,103 @@ public function graphql(string $query, array $variables = []): Response
5457

5558
public function get(string $path, array $data = []): Response
5659
{
57-
/** @var Response $response */
58-
$response = $this->request->build($this->connection)->get($this->getUrl($path), $data);
60+
$response = $this->request()->get($this->getUrl($path), $data);
5961

6062
return $response;
6163
}
6264

6365
public function post(string $path, array $data = []): Response
6466
{
6567
/** @var Response $response */
66-
$response = $this->request->build($this->connection)->post($this->getUrl($path), $data);
68+
$response = $this->request()->post($this->getUrl($path), $data);
6769

6870
return $response;
6971
}
7072

7173
public function postAsync(string $path, array $data = []): Response
7274
{
7375
/** @var Response $response */
74-
$response = $this->request->build($this->connection)->post($this->getUrl($path, true), $data);
76+
$response = $this->request()->post($this->getUrl($path, true), $data);
7577

7678
return $response;
7779
}
7880

7981
public function postBulk(string $path, array $data = []): Response
8082
{
8183
/** @var Response $response */
82-
$response = $this->request->build($this->connection)->post($this->getUrl($path, true, true), $data);
84+
$response = $this->request()->post($this->getUrl($path, true, true), $data);
8385

8486
return $response;
8587
}
8688

8789
public function patch(string $path, array $data = []): Response
8890
{
8991
/** @var Response $response */
90-
$response = $this->request->build($this->connection)->patch($this->getUrl($path), $data);
92+
$response = $this->request()->patch($this->getUrl($path), $data);
9193

9294
return $response;
9395
}
9496

9597
public function patchAsync(string $path, array $data = []): Response
9698
{
9799
/** @var Response $response */
98-
$response = $this->request->build($this->connection)->patch($this->getUrl($path, true), $data);
100+
$response = $this->request()->patch($this->getUrl($path, true), $data);
99101

100102
return $response;
101103
}
102104

103105
public function patchBulk(string $path, array $data = []): Response
104106
{
105107
/** @var Response $response */
106-
$response = $this->request->build($this->connection)->patch($this->getUrl($path, true, true), $data);
108+
$response = $this->request()->patch($this->getUrl($path, true, true), $data);
107109

108110
return $response;
109111
}
110112

111113
public function put(string $path, array $data = []): Response
112114
{
113115
/** @var Response $response */
114-
$response = $this->request->build($this->connection)->put($this->getUrl($path), $data);
116+
$response = $this->request()->put($this->getUrl($path), $data);
115117

116118
return $response;
117119
}
118120

119121
public function putAsync(string $path, array $data = []): Response
120122
{
121123
/** @var Response $response */
122-
$response = $this->request->build($this->connection)->put($this->getUrl($path, true), $data);
124+
$response = $this->request()->put($this->getUrl($path, true), $data);
123125

124126
return $response;
125127
}
126128

127129
public function putBulk(string $path, array $data = []): Response
128130
{
129131
/** @var Response $response */
130-
$response = $this->request->build($this->connection)->put($this->getUrl($path, true, true), $data);
132+
$response = $this->request()->put($this->getUrl($path, true, true), $data);
131133

132134
return $response;
133135
}
134136

135137
public function delete(string $path, array $data = []): Response
136138
{
137139
/** @var Response $response */
138-
$response = $this->request->build($this->connection)->delete($this->getUrl($path), $data);
140+
$response = $this->request()->delete($this->getUrl($path), $data);
139141

140142
return $response;
141143
}
142144

143145
public function deleteAsync(string $path, array $data = []): Response
144146
{
145147
/** @var Response $response */
146-
$response = $this->request->build($this->connection)->delete($this->getUrl($path, true), $data);
148+
$response = $this->request()->delete($this->getUrl($path, true), $data);
147149

148150
return $response;
149151
}
150152

151153
public function deleteBulk(string $path, array $data = []): Response
152154
{
153155
/** @var Response $response */
154-
$response = $this->request->build($this->connection)->delete($this->getUrl($path, true, true), $data);
156+
$response = $this->request()->delete($this->getUrl($path, true, true), $data);
155157

156158
return $response;
157159
}
@@ -206,6 +208,25 @@ public function getUrl(string $path, bool $async = false, bool $bulk = false): s
206208
return implode('/', $options);
207209
}
208210

211+
public function intercept(Closure $callable): static
212+
{
213+
$this->interceptor = $callable;
214+
215+
return $this;
216+
}
217+
218+
protected function request(): PendingRequest
219+
{
220+
$request = $this->request->build($this->connection);
221+
222+
if (isset($this->interceptor)) {
223+
call_user_func($this->interceptor, $request);
224+
$this->interceptor = null;
225+
}
226+
227+
return $request;
228+
}
229+
209230
public static function fake(): void
210231
{
211232
config()->set('magento.connection', 'default');

tests/Client/ClientTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace JustBetter\MagentoClient\Tests\Client;
44

5+
use Illuminate\Http\Client\PendingRequest;
56
use Illuminate\Http\Client\Request;
67
use Illuminate\Support\Facades\Http;
78
use Illuminate\Support\Str;
@@ -504,4 +505,68 @@ public function test_it_can_set_store_code(): void
504505
return $request->url() == 'magento/rest/store/V1/products';
505506
});
506507
}
508+
509+
public function test_it_can_intersect_the_client_and_adjust_its_body_format(): void
510+
{
511+
Http::fake([
512+
'magento/rest/all/V1/products*' => Http::response(['items' => []]),
513+
]);
514+
515+
/** @var Magento $magento */
516+
$magento = app(Magento::class);
517+
518+
$result = $magento->intercept(function (PendingRequest $request) {
519+
$request->asForm();
520+
});
521+
522+
$this->assertInstanceOf(Magento::class, $result);
523+
524+
$response = $magento->get('products', [
525+
'searchCriteria[pageSize]' => 10,
526+
'searchCriteria[currentPage]' => 0,
527+
]);
528+
529+
$this->assertTrue($response->ok());
530+
$this->assertCount(0, $response->json('items'));
531+
532+
Http::assertSent(function (Request $request) {
533+
return $request->method() === 'GET' &&
534+
$request->header('Content-Type')[0] === 'application/x-www-form-urlencoded' &&
535+
$request->url() == 'magento/rest/all/V1/products?searchCriteria%5BpageSize%5D=10&searchCriteria%5BcurrentPage%5D=0';
536+
});
537+
}
538+
539+
public function test_it_resets_the_interceptor(): void
540+
{
541+
Http::fake([
542+
'magento/rest/all/V1/products' => Http::response([
543+
'product' => [
544+
'entity_id' => 1,
545+
'sku' => '::some-sku::',
546+
],
547+
]),
548+
]);
549+
550+
/** @var Magento $magento */
551+
$magento = app(Magento::class);
552+
553+
$magento->intercept(function (PendingRequest $request) {
554+
$request->withHeaders(['some-header' => '::test::']);
555+
})->post('products', [
556+
'product' => [
557+
'sku' => '::some-sku::',
558+
],
559+
]);
560+
561+
$magento->post('products', [
562+
'product' => [
563+
'sku' => '::some-sku::',
564+
],
565+
]);
566+
567+
Http::assertSentInOrder([
568+
fn (Request $request) => $request->hasHeader('some-header'),
569+
fn (Request $request) => ! $request->hasHeader('some-header'),
570+
]);
571+
}
507572
}

0 commit comments

Comments
 (0)