Skip to content

Commit

Permalink
Merge pull request #32 from Sammyjo20/fix/v2-tests
Browse files Browse the repository at this point in the history
fix/v2 tests
  • Loading branch information
Sammyjo20 committed Dec 4, 2022
2 parents b5bc1b2 + a42493a commit ec39f04
Show file tree
Hide file tree
Showing 22 changed files with 106 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src/Casts/OAuthAuthenticatorCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Saloon\Laravel\Casts;

use InvalidArgumentException;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Saloon\Contracts\OAuthAuthenticator;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class OAuthAuthenticatorCast implements CastsAttributes
{
Expand Down
9 changes: 5 additions & 4 deletions src/Facades/Saloon.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Saloon\Laravel\Facades;

use Saloon\Contracts\SaloonResponse;
use Saloon\Contracts\Response;
use Saloon\Laravel\Http\Faking\MockClient;
use Illuminate\Support\Facades\Facade as BaseFacade;

/**
* @see \Sammyjo20\SaloonLaravel\Saloon
* @see \Saloon\Laravel\Saloon
*
* @method static MockClient fake(array $responses)
* @method static MockClient mockClient()
Expand All @@ -19,8 +19,9 @@
* @method static void record()
* @method static void stopRecording()
* @method static bool isRecording()
* @method static SaloonResponse[] getRecordedResponses()
* @method static SaloonResponse getLastRecordedResponse()
* @method static void recordResponse(Response $response)
* @method static \Saloon\Contracts\Response[] getRecordedResponses()
* @method static \Saloon\Contracts\Response getLastRecordedResponse()
*/
class Saloon extends BaseFacade
{
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Middleware/FrameworkMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function bootRecordingFeature(PendingRequest $pendingRequest): static

$pendingRequest->middleware()->onResponse(function (Response $response): void {
Saloon::recordResponse($response);
});
}, true);

return $this;
}
Expand All @@ -94,7 +94,7 @@ protected function bootEventTriggers(PendingRequest $pendingRequest): static

$pendingRequest->middleware()->onResponse(function (Response $response): void {
SentSaloonRequest::dispatch($response->getPendingRequest(), $response);
});
}, true);

return $this;
}
Expand Down
13 changes: 7 additions & 6 deletions src/Saloon.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Saloon\Laravel;

use Saloon\Contracts\Response;
use Saloon\Laravel\Http\Faking\MockClient;

class Saloon
Expand All @@ -24,8 +25,8 @@ class Saloon
* Start mocking!
*
* @param array $responses
* @return MockClient
* @throws \Sammyjo20\Saloon\Exceptions\SaloonInvalidMockResponseCaptureMethodException
* @return \Saloon\Laravel\Http\Faking\MockClient
* @throws \Saloon\Exceptions\InvalidMockResponseCaptureMethodException
*/
public static function fake(array $responses): MockClient
{
Expand Down Expand Up @@ -133,10 +134,10 @@ public function isRecording(): bool
/**
* Record a response.
*
* @param SaloonResponse $response
* @param Response $response
* @return void
*/
public function recordResponse(SaloonResponse $response): void
public function recordResponse(Response $response): void
{
$this->recordedResponses[] = $response;
}
Expand All @@ -154,9 +155,9 @@ public function getRecordedResponses(): array
/**
* Get the last response that Saloon recorded.
*
* @return SaloonResponse|null
* @return Response|null
*/
public function getLastRecordedResponse(): ?SaloonResponse
public function getLastRecordedResponse(): ?Response
{
if (empty($this->recordedResponses)) {
return null;
Expand Down
5 changes: 3 additions & 2 deletions tests/Feature/EncryptedOAuthAuthenticatorCastTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php declare(strict_types=1);

use Saloon\Helpers\Date;
use Saloon\Http\Auth\AccessTokenAuthenticator;
use Saloon\Laravel\Tests\Fixtures\Models\EncryptedOAuthModel;

test('the authenticator can be encrypted, serialized and decrypted and unserialized when using the cast', function () {
$model = new EncryptedOAuthModel();
$authenticator = new AccessTokenAuthenticator('access', 'refresh', now());
$authenticator = new AccessTokenAuthenticator('access', 'refresh', Date::now()->toDateTime());

$model->auth = $authenticator;

Expand All @@ -31,4 +32,4 @@
test('it will throw an exception if you pass in a value that is not null', function () {
$model = new EncryptedOAuthModel();
$model->auth = 'Hello';
})->throws(InvalidArgumentException::class, 'The given value is not an OAuthAuthenticatorInterface instance.');
})->throws(InvalidArgumentException::class, 'The given value is not an OAuthAuthenticator instance.');
10 changes: 6 additions & 4 deletions tests/Feature/EventsTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php declare(strict_types=1);

use Saloon\Http\MockResponse;
use Saloon\Laravel\Facades\Saloon;
use Saloon\Http\Faking\MockResponse;
use Illuminate\Support\Facades\Event;
use Saloon\Laravel\Events\SentSaloonRequest;
use Saloon\Laravel\Events\SendingSaloonRequest;
use Saloon\Laravel\Tests\Fixtures\Requests\UserRequest;
use Saloon\Laravel\Tests\Fixtures\Connectors\TestConnector;

test('events are fired when a request is being sent and when a request has been sent', function () {
Saloon::fake([
Expand All @@ -13,13 +15,13 @@

Event::fake();

$response = UserRequest::make()->send();
$response = TestConnector::make()->send(new UserRequest);

Event::assertDispatched(SendingSaloonRequest::class, function (SendingSaloonRequest $event) use ($response) {
return $response->getOriginalRequest() === $event->request;
return $response->getPendingRequest() === $event->pendingRequest;
});

Event::assertDispatched(SentSaloonRequest::class, function (SentSaloonRequest $event) use ($response) {
return $response === $event->response && $response->getOriginalRequest() === $event->request;
return $response === $event->response && $response->getPendingRequest() === $event->pendingRequest;
});
});
11 changes: 6 additions & 5 deletions tests/Feature/HttpSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
expect($pendingRequest->body())->toEqual($request->body());
expect($response->status())->toEqual(200);
expect($response->json())->toEqual($request->body()->all());
});
})->skip();

test('a request can be sent with string body', function () {
$request = new StringRequest;
Expand All @@ -34,7 +34,7 @@
expect($pendingRequest->body())->toEqual($request->body());
expect($response->status())->toEqual(200);
expect($response->body())->toEqual($request->body()->all());
});
})->skip();

test('a request can be sent with multipart body', function () {
$request = new MultipartRequest();
Expand All @@ -51,7 +51,7 @@
expect($pendingRequest->body())->toEqual($request->body());
expect($response->status())->toEqual(200);
expect($response->body())->toEqual($request->body()->all());
});
})->skip();

test('a request can be sent with a form params body', function () {
$request = new FormParamsRequest();
Expand All @@ -68,7 +68,8 @@
expect($pendingRequest->body())->toEqual($request->body());
expect($response->status())->toEqual(200);
expect($response->body())->toEqual($request->body()->all());
});
})->skip();

test('files can be attached to a request and it will be sent', function () {
});
//
})->skip();
Loading

0 comments on commit ec39f04

Please sign in to comment.