Skip to content

Commit e08576e

Browse files
committed
Writing tests
1 parent 2036533 commit e08576e

16 files changed

+126
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ composer.lock
33
vendor
44
.php-cs-fixer.cache
55
.phpunit.result.cache
6+
tests/Fixtures/Saloon

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
},
2727
"require-dev": {
2828
"friendsofphp/php-cs-fixer": "^3.5",
29+
"league/flysystem": "^3.0",
2930
"pestphp/pest": "^1.21",
3031
"spatie/ray": "^1.33"
3132
},

src/Exceptions/DirectoryNotFoundException.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Sammyjo20\Saloon\Exceptions;
44

5-
use Exception;
6-
7-
class DirectoryNotFoundException extends Exception
5+
class DirectoryNotFoundException extends SaloonException
86
{
97
/**
108
* Constructor

src/Exceptions/OAuthConfigValidationException.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Sammyjo20\Saloon\Exceptions;
44

5-
use \Exception;
6-
7-
class OAuthConfigValidationException extends Exception
5+
class OAuthConfigValidationException extends SaloonException
86
{
97
//
108
}

src/Exceptions/UnableToCreateDirectoryException.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Sammyjo20\Saloon\Exceptions;
44

5-
use Exception;
6-
7-
class UnableToCreateDirectoryException extends Exception
5+
class UnableToCreateDirectoryException extends SaloonException
86
{
97
/**
108
* Constructor

src/Exceptions/UnableToCreateFileException.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Sammyjo20\Saloon\Exceptions;
44

5-
use Exception;
6-
7-
class UnableToCreateFileException extends Exception
5+
class UnableToCreateFileException extends SaloonException
86
{
97
/**
108
* Constructor

src/Http/SaloonRequest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ abstract class SaloonRequest implements SaloonRequestInterface
5252
*/
5353
private ?SaloonConnector $loadedConnector = null;
5454

55+
/**
56+
* Denotes if the request is being used to record a fixture.
57+
*
58+
* @var bool
59+
*/
60+
protected bool $isRecordingFixture = false;
61+
5562
/**
5663
* Define the endpoint for the request.
5764
*
@@ -176,6 +183,39 @@ public function traitExistsOnConnector(string $trait): bool
176183
return array_key_exists($trait, class_uses($this->getConnector()));
177184
}
178185

186+
/**
187+
* Set if the request is being used to record a fixture.
188+
*
189+
* @param bool $value
190+
* @return $this
191+
*/
192+
public function setIsRecordingFixture(bool $value): static
193+
{
194+
$this->isRecordingFixture = $value;
195+
196+
return $this;
197+
}
198+
199+
/**
200+
* Get if the request is recording a fixture.
201+
*
202+
* @return bool
203+
*/
204+
public function isRecordingFixture(): bool
205+
{
206+
return $this->isRecordingFixture;
207+
}
208+
209+
/**
210+
* Get if the request is not recording a fixture.
211+
*
212+
* @return bool
213+
*/
214+
public function isNotRecordingFixture(): bool
215+
{
216+
return ! $this->isRecordingFixture();
217+
}
218+
179219
/**
180220
* Dynamically proxy other methods to the connector.
181221
*

src/Managers/RequestManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ private function createResponse(array $requestOptions, Response $response, Reque
278278
// If we are mocking, we should record the request and response on the mock manager,
279279
// so we can run assertions on the responses.
280280

281-
if ($this->isMocking()) {
281+
if ($this->isMocking() && $request->isNotRecordingFixture()) {
282282
$response->setMocked(true);
283283
$this->mockClient->recordResponse($response);
284284
}

src/Traits/ManagesGuzzle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ private function bootHandlers(HandlerStack $handlerStack): HandlerStack
105105
// we will register the fixture recorder middleware.
106106

107107
if (is_null($mockResponse) && $mockObject instanceof Fixture) {
108+
$this->request->setIsRecordingFixture(true);
109+
108110
$handlerStack->push(new FixtureRecorderMiddleware($mockObject), 'saloonFixtureMiddleware');
109111
}
110112
}

tests/Feature/MockRequestTest.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use League\Flysystem\Filesystem;
4+
use League\Flysystem\Local\LocalFilesystemAdapter;
35
use Sammyjo20\Saloon\Http\Fixture;
46
use Sammyjo20\Saloon\Http\MockResponse;
57
use Sammyjo20\Saloon\Clients\MockClient;
@@ -12,6 +14,15 @@
1214
use Sammyjo20\Saloon\Tests\Fixtures\Connectors\QueryParameterConnector;
1315
use Sammyjo20\Saloon\Tests\Fixtures\Requests\DifferentServiceUserRequest;
1416
use Sammyjo20\Saloon\Tests\Fixtures\Requests\QueryParameterConnectorRequest;
17+
use function PHPUnit\Framework\assertFileDoesNotExist;
18+
use function PHPUnit\Framework\assertFileExists;
19+
20+
$filesystem = new Filesystem(new LocalFilesystemAdapter('tests/Fixtures/Saloon'));
21+
22+
beforeEach(function () use ($filesystem) {
23+
$filesystem->deleteDirectory('/');
24+
$filesystem->createDirectory('/');
25+
});
1526

1627
test('a request can be mocked with a sequence', function () {
1728
$mockClient = new MockClient([
@@ -224,19 +235,24 @@ function (SaloonRequest $request): MockResponse {
224235
expect($sequenceResponse->json())->toEqual(['request_class' => UserRequest::class]);
225236
});
226237

227-
test('you can tell the mock client to record requests and it will store recorded requests as a fixture', function () {
238+
test('you can use a fixture mock response and it will record the request for future use', function () use ($filesystem) {
228239
$mockClient = new MockClient([
229240
UserRequest::class => MockResponse::fixture('user'),
230-
ErrorRequest::class => MockResponse::fixture('error'),
231241
]);
232242

233-
$response = UserRequest::make()->send($mockClient);
243+
expect($filesystem->fileExists('user.json'))->toBeFalse();
244+
245+
$responseA = UserRequest::make()->send($mockClient);
234246

235-
$errorResponse = ErrorRequest::make()->send($mockClient);
247+
expect($responseA->isMocked())->toBeFalse();
248+
expect($responseA->status())->toEqual(200);
249+
250+
expect($filesystem->fileExists('user.json'))->toBeTrue();
236251

237-
dd($errorResponse->status());
252+
$responseB = UserRequest::make()->send($mockClient);
238253

239-
dd($response->json());
254+
expect($responseB->isMocked())->toBeTrue();
255+
expect($responseB->status())->toEqual(200);
240256
});
241257

242258
// Tests: Make sure the fixture works with all the mocking types including closures

0 commit comments

Comments
 (0)