From fba0df20c25c1a23286374a284a3e61839db7982 Mon Sep 17 00:00:00 2001 From: Steve McDougall Date: Thu, 11 Nov 2021 08:05:52 +0000 Subject: [PATCH] Adding view payload and sending xml options --- README.md | 52 +++++++++++++++++++- composer.json | 1 + phpstan.neon | 5 ++ src/Concerns/SendsXml.php | 40 +++++++++++++++ src/Concurrently.php | 28 ++++++++++- src/Request.php | 93 ++++++++++++++++++++++++++++++++++- tests/Feature/RequestTest.php | 14 ++++++ 7 files changed, 227 insertions(+), 6 deletions(-) create mode 100644 phpstan.neon create mode 100644 src/Concerns/SendsXml.php diff --git a/README.md b/README.md index 47fca92..24fa267 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,23 @@ When building your request to send, you can override the following: - Request Query Params using `withQuery(array $query)` - Request Path using `setPath(string $path)` -### Concurrent + +### Checking the payload + +I had a request in an issue to be able to see the request data for a request, so I have added a helper method called `payload` which will return whatever has been stored in the request `data` property. + +```php +$request = TestRequest::build() + ->withToken('foobar') + ->withData([ + 'title' => 'Build a package' + ]); + +$data = $request->payload(); // ['title' => 'Build a package'] +``` + + +### Concurrent Requests ```php $responses = \JustSteveKing\Transporter\Facades\Concurrently::build()->setRequests([ @@ -93,7 +109,7 @@ $responses[1]->json(); $responses[2]->json(); ``` -### Concurrency with Custom key +### Concurrency with a Custom key ```php $responses = \JustSteveKing\Transporter\Facades\Concurrently::build()->setRequests([ @@ -179,6 +195,38 @@ $responses = Concurrently::fake()->setRequests([ Which will return a response with the data you pass through to `withFakeData`, which internally will merge what is on the class with what you pass it. So you can build up an initial state of faked data per class. +### Sending XML + +Thanks to a fantastic suggestion by [@jessarcher](https://github.com/jessarcher) we can use a `Trait` to allow for easy use of XML in your requests. Using this as a trait makes a lot of sense as most APIs these days use JSON, so it is purely opt in. +To use this, simply use the trait on your request: + +```php +withXml( + xml: 'Send an XML Requetsfalse' +)->send(); +``` + ## Testing To run the tests in parallel: diff --git a/composer.json b/composer.json index b9aaf12..781e0ad 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,7 @@ "nunomaduro/collision": "^5.3", "orchestra/testbench": "^6.15", "pestphp/pest": "^1.20", + "phpstan/phpstan": "^1.1", "phpunit/phpunit": "^9.3", "spatie/laravel-ray": "^1.9" }, diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..ee11ef6 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,5 @@ +parameters: + paths: + - src + level: 9 + checkMissingIterableValueType: false diff --git a/src/Concerns/SendsXml.php b/src/Concerns/SendsXml.php new file mode 100644 index 0000000..2799569 --- /dev/null +++ b/src/Concerns/SendsXml.php @@ -0,0 +1,40 @@ +request + ->withHeaders( + headers: [ + 'Accept' => 'application/xml', + ], + )->withBody( + content: $xml, + contentType: 'application/xml', + ); + + return $this; + } + + /** + * @return Response + */ + public function send(): Response + { + return $this->request->send( + method: $this->method, + url: $this->getUrl(), + ); + } +} diff --git a/src/Concurrently.php b/src/Concurrently.php index f0f2c5e..b896170 100644 --- a/src/Concurrently.php +++ b/src/Concurrently.php @@ -20,16 +20,25 @@ class Concurrently protected bool $isFake = false; + /** + * @param HttpFactory $http + * @return void + */ public function __construct( private HttpFactory $http - ) { - } + ) { } + /** + * @return static + */ public function build(): static { return app(static::class); } + /** + * @return static + */ public function fake(): static { $concurrent = $this->build(); @@ -39,6 +48,10 @@ public function fake(): static return $concurrent; } + /** + * @param array $requests + * @return static + */ public function setRequests(array $requests): static { $this->requests = $requests; @@ -53,6 +66,10 @@ public function setRequests(array $requests): static return $this; } + /** + * @param Request $request + * @return static + */ public function add(Request $request): static { if ($request->getAs() !== null) { @@ -64,6 +81,9 @@ public function add(Request $request): static return $this; } + /** + * @return array + */ public function run(): array { if ($this->isFake) { @@ -75,6 +95,10 @@ public function run(): array ->toArray(); } + /** + * @param $pool + * @return array + */ private function buildRequestsPool($pool): array { $requests = []; diff --git a/src/Request.php b/src/Request.php index 4a4d3e9..49313e1 100644 --- a/src/Request.php +++ b/src/Request.php @@ -34,17 +34,25 @@ abstract class Request protected string $path; protected string $baseUrl; - protected ?string $as = null; + protected null|string $as = null; protected array $query = []; protected array $data = []; protected array $fakeData = []; protected int $status; + /** + * @param array $args + * @return static + */ public static function build(...$args): static { return app(static::class, $args); } + /** + * @param int $status + * @return static + */ public static function fake(int $status = Http::OK): static { $request = static::build(); @@ -55,6 +63,10 @@ public static function fake(int $status = Http::OK): static return $request; } + /** + * @param HttpFactory $http + * @return void + */ public function __construct(HttpFactory $http) { $this->request = $http->baseUrl( @@ -66,11 +78,18 @@ public function __construct(HttpFactory $http) ); } - public function getAs(): ?string + /** + * @return string|null + */ + public function getAs(): null|string { return $this->as; } + /** + * @param string|int $as + * @return static + */ public function as(string|int $as): static { $this->as = $as; @@ -78,6 +97,10 @@ public function as(string|int $as): static return $this; } + /** + * @param array $data + * @return static + */ public function withData(array $data): static { $this->data = array_merge($this->data, $data); @@ -85,6 +108,18 @@ public function withData(array $data): static return $this; } + /** + * @return array + */ + public function payload(): array + { + return $this->data; + } + + /** + * @param array $data + * @return static + */ public function withFakeData(array $data): static { $this->fakeData = array_merge($this->fakeData, $data); @@ -92,6 +127,10 @@ public function withFakeData(array $data): static return $this; } + /** + * @param array $query + * @return static + */ public function withQuery(array $query): static { $this->query = array_merge_recursive($this->query, $query); @@ -99,6 +138,10 @@ public function withQuery(array $query): static return $this; } + /** + * @return string + * @throws RuntimeException + */ public function getBaseUrl(): string { if (isset($this->baseUrl)) { @@ -114,11 +157,19 @@ public function getBaseUrl(): string ); } + /** + * @param string $baseUrl + * @return static + */ public function lockOn(string $baseUrl): static { return $this->setBaseUrl($baseUrl); } + /** + * @param string $baseUrl + * @return static + */ public function setBaseUrl(string $baseUrl): static { $this->baseUrl = $baseUrl; @@ -128,6 +179,9 @@ public function setBaseUrl(string $baseUrl): static return $this; } + /** + * @return Psr7Response + */ public function fakeResponse(): Psr7Response { return new Psr7Response( @@ -136,6 +190,10 @@ public function fakeResponse(): Psr7Response ); } + /** + * @param Pool $pool + * @return mixed + */ public function buildForConcurrent(Pool $pool): mixed { /** @@ -159,11 +217,17 @@ public function buildForConcurrent(Pool $pool): mixed }; } + /** + * @return Response + */ public function energize(): Response { return $this->send(); } + /** + * @return Response + */ public function send(): Response { if ($this->useFake) { @@ -183,6 +247,9 @@ public function send(): Response }; } + /** + * @return string + */ public function getUrl(): string { $url = (string) Str::of($this->path()) @@ -196,26 +263,43 @@ public function getUrl(): string return $url; } + /** + * @param PendingRequest $request + * @return void + */ protected function withRequest(PendingRequest $request): void { // do something with the initialized request } + /** + * @return string + */ protected function path(): string { return $this->path ?? ''; } + /** + * @return PendingRequest + */ public function getRequest(): PendingRequest { return $this->request; } + /** + * @return array + */ public function getQuery(): array { return $this->query; } + /** + * @param string $path + * @return static + */ public function setPath(string $path): static { $this->path = $path; @@ -223,6 +307,11 @@ public function setPath(string $path): static return $this; } + /** + * @param string $method + * @param array $parameters + * @return static + */ public function __call(string $method, array $parameters): static { if (method_exists($this->request, $method)) { diff --git a/tests/Feature/RequestTest.php b/tests/Feature/RequestTest.php index dcde60f..50114d3 100644 --- a/tests/Feature/RequestTest.php +++ b/tests/Feature/RequestTest.php @@ -375,3 +375,17 @@ $query['page']['size'] )->toBe(30); }); + +it('can get the request payload', function () { + $data = [ + 'pest' => 'test', + ]; + + expect( + PostRequest::build()->withData( + data: $data, + )->payload(), + )->toEqual( + expected: $data, + ); +});