-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Sammyjo20/feature/xml
Added withXMLBody trait
- Loading branch information
Showing
7 changed files
with
139 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Sammyjo20\Saloon\Traits\Features; | ||
|
||
use Sammyjo20\Saloon\Exceptions\SaloonTraitExistsException; | ||
|
||
trait HasXMLBody | ||
{ | ||
/** | ||
* Add the required headers to send XML | ||
* | ||
* @return void | ||
* @throws SaloonTraitExistsException | ||
* @throws \Sammyjo20\Saloon\Exceptions\SaloonInvalidConnectorException | ||
*/ | ||
public function bootHasXMLBodyFeature() | ||
{ | ||
$this->addHeader('Accept', 'application/xml'); | ||
$this->addHeader('Content-Type', 'application/xml'); | ||
|
||
$this->addConfig('body', $this->defineXMLBody()); | ||
} | ||
|
||
/** | ||
* Define your XML body | ||
* | ||
* @return null | ||
*/ | ||
public function defineXMLBody() | ||
{ | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Sammyjo20\Saloon\Tests\Resources\Requests; | ||
|
||
use Sammyjo20\Saloon\Constants\Saloon; | ||
use Sammyjo20\Saloon\Http\SaloonRequest; | ||
use Sammyjo20\Saloon\Tests\Resources\Connectors\TestConnector; | ||
use Sammyjo20\Saloon\Traits\Features\HasXMLBody; | ||
|
||
class HasXMLRequest extends SaloonRequest | ||
{ | ||
use HasXMLBody; | ||
|
||
/** | ||
* Define the method that the request will use. | ||
* | ||
* @var string|null | ||
*/ | ||
protected ?string $method = Saloon::GET; | ||
|
||
/** | ||
* The connector. | ||
* | ||
* @var string|null | ||
*/ | ||
protected ?string $connector = TestConnector::class; | ||
|
||
/** | ||
* Define the endpoint for the request. | ||
* | ||
* @return string | ||
*/ | ||
public function defineEndpoint(): string | ||
{ | ||
return '/user'; | ||
} | ||
|
||
public function defineXMLBody() | ||
{ | ||
return '<xml></xml>'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
use Sammyjo20\Saloon\Managers\RequestManager; | ||
use Sammyjo20\Saloon\Tests\Resources\Requests\HasXMLRequest; | ||
|
||
test('with the hasXMLBody trait, you can pass in a string body response', function () { | ||
$request = new HasXMLRequest; | ||
|
||
$requestManager = new RequestManager($request); | ||
$requestManager->hydrate(); | ||
|
||
expect($requestManager->getHeaders())->toHaveKey('Accept', 'application/xml'); | ||
expect($requestManager->getHeaders())->toHaveKey('Content-Type', 'application/xml'); | ||
|
||
$request->addHandler('hasBodyHandler', function (callable $handler) { | ||
return function (RequestInterface $request, array $options) use ($handler) { | ||
expect($request->getBody()->getContents())->toEqual('<xml></xml>'); | ||
|
||
return $handler($request, $options); | ||
}; | ||
}); | ||
}); |