Skip to content

Commit

Permalink
Adding view payload and sending xml options
Browse files Browse the repository at this point in the history
  • Loading branch information
JustSteveKing committed Nov 11, 2021
1 parent 8a1e2fa commit fba0df2
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 6 deletions.
52 changes: 50 additions & 2 deletions README.md
Expand Up @@ -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([
Expand All @@ -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([
Expand Down Expand Up @@ -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
<?php

declare(strict_types=1);

namespace App\Transporter\Requests;

use JustSteveKing\Transporter\Concerns\SendsXml;
use JustSteveKing\Transporter\Request;

class XmlRequest extends Request
{
use SendsXml;

protected string $method = 'POST';

protected string $path = '/your-endpoint';
}
```
Then all you need to do is call the methods:

```php
XmlRequest::build()->withXml(
xml: '<todo><name>Send an XML Requets</name><completed>false</completed></todo>'
)->send();
```

## Testing

To run the tests in parallel:
Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -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"
},
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
@@ -0,0 +1,5 @@
parameters:
paths:
- src
level: 9
checkMissingIterableValueType: false
40 changes: 40 additions & 0 deletions src/Concerns/SendsXml.php
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace JustSteveKing\Transporter\Concerns;

use Illuminate\Http\Client\Response;

trait SendsXml
{
/**
* @param string $xml
* @return static
*/
public function withXml(string $xml): static
{
$this->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(),
);
}
}
28 changes: 26 additions & 2 deletions src/Concurrently.php
Expand Up @@ -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();
Expand All @@ -39,6 +48,10 @@ public function fake(): static
return $concurrent;
}

/**
* @param array $requests
* @return static
*/
public function setRequests(array $requests): static
{
$this->requests = $requests;
Expand All @@ -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) {
Expand All @@ -64,6 +81,9 @@ public function add(Request $request): static
return $this;
}

/**
* @return array
*/
public function run(): array
{
if ($this->isFake) {
Expand All @@ -75,6 +95,10 @@ public function run(): array
->toArray();
}

/**
* @param $pool
* @return array
*/
private function buildRequestsPool($pool): array
{
$requests = [];
Expand Down

0 comments on commit fba0df2

Please sign in to comment.