Skip to content

Commit d8cbcdd

Browse files
authored
Merge pull request #8 from Sammyjo20/feature/mocking-assertions
Added mocking assertions
2 parents ca5dd7f + c44fea7 commit d8cbcdd

5 files changed

Lines changed: 210 additions & 4 deletions

File tree

.github/workflows/php-cs-fixer.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ name: Code Style
33
on:
44
push:
55
branches: [ main ]
6-
pull_request:
7-
branches: [ main ]
86

97
jobs:
108
php-cs-fixer:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"php": "^8.0",
2121
"illuminate/support": "^8.0|^9.0",
2222
"illuminate/console": "^8.0|^9.0",
23-
"sammyjo20/saloon": "^0.6.6"
23+
"sammyjo20/saloon": "^0.7.0"
2424
},
2525
"extra": {
2626
"laravel": {

src/Facades/Saloon.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
/**
88
* @see \Sammyjo20\SaloonLaravel\Saloon
9+
* @method fake(array $responses)
10+
* @method mockClient()
11+
* @method assertSent(string|callable $value)
12+
* @method assertNotSent(string|callable $value)
13+
* @method assertSentJson(string $request, array $data)
14+
* @method assertNothingSent()
15+
* @method assertSentCount(int $count)
916
*/
1017
class Saloon extends BaseFacade
1118
{

src/Saloon.php

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class Saloon
1111
{
1212
/**
13-
* The boot method. This is called by Saloon and from within here, can push almost anything
13+
* The boot method. This is called by Saloon and from within here, we can push almost anything
1414
* into Saloon, but most important - we can push interceptors and handlers 🚀
1515
*
1616
* @param LaravelManager $laravelManager
@@ -39,4 +39,72 @@ public static function fake(array $responses): MockClient
3939
{
4040
return MockClient::resolve()->startMocking($responses);
4141
}
42+
43+
/**
44+
* Retrieve the mock client from the container
45+
*
46+
* @return MockClient
47+
*/
48+
public static function mockClient(): MockClient
49+
{
50+
return MockClient::resolve();
51+
}
52+
53+
/**
54+
* Assert that a given request was sent.
55+
*
56+
* @param string|callable $value
57+
* @return void
58+
* @throws \ReflectionException
59+
*/
60+
public static function assertSent(string|callable $value): void
61+
{
62+
static::mockClient()->assertSent($value);
63+
}
64+
65+
/**
66+
* Assert that a given request was not sent.
67+
*
68+
* @param string|callable $value
69+
* @return void
70+
* @throws \ReflectionException
71+
*/
72+
public static function assertNotSent(string|callable $value): void
73+
{
74+
static::mockClient()->assertNotSent($value);
75+
}
76+
77+
/**
78+
* Assert JSON data was sent
79+
*
80+
* @param string $request
81+
* @param array $data
82+
* @return void
83+
* @throws \ReflectionException
84+
*/
85+
public static function assertSentJson(string $request, array $data): void
86+
{
87+
static::mockClient()->assertSentJson($request, $data);
88+
}
89+
90+
/**
91+
* Assert that nothing was sent.
92+
*
93+
* @return void
94+
*/
95+
public static function assertNothingSent(): void
96+
{
97+
static::mockClient()->assertNothingSent();
98+
}
99+
100+
/**
101+
* Assert a request count has been met.
102+
*
103+
* @param int $count
104+
* @return void
105+
*/
106+
public static function assertSentCount(int $count): void
107+
{
108+
static::mockClient()->assertSentCount($count);
109+
}
42110
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
use Sammyjo20\Saloon\Http\MockResponse;
4+
use Sammyjo20\Saloon\Http\SaloonRequest;
5+
use Sammyjo20\Saloon\Http\SaloonResponse;
6+
use Sammyjo20\SaloonLaravel\Facades\Saloon;
7+
use Sammyjo20\SaloonLaravel\Tests\Resources\Requests\UserRequest;
8+
use Sammyjo20\SaloonLaravel\Tests\Resources\Requests\ErrorRequest;
9+
10+
test('that assertSent works with a request', function () {
11+
Saloon::fake([
12+
UserRequest::class => new MockResponse(['name' => 'Sam'], 200),
13+
]);
14+
15+
(new UserRequest())->send();
16+
17+
Saloon::assertSent(UserRequest::class);
18+
});
19+
20+
test('that assertSent works with a closure', function () {
21+
Saloon::fake([
22+
UserRequest::class => new MockResponse(['name' => 'Sam'], 200),
23+
ErrorRequest::class => new MockResponse(['error' => 'Server Error'], 500),
24+
]);
25+
26+
$originalRequest = new UserRequest();
27+
$originalResponse = $originalRequest->send();
28+
29+
Saloon::assertSent(function ($request, $response) use ($originalRequest, $originalResponse) {
30+
expect($request)->toBeInstanceOf(SaloonRequest::class);
31+
expect($response)->toBeInstanceOf(SaloonResponse::class);
32+
33+
expect($request)->toBe($originalRequest);
34+
expect($response)->toBe($originalResponse);
35+
36+
return true;
37+
});
38+
39+
$newRequest = new ErrorRequest();
40+
$newResponse = $newRequest->send();
41+
42+
Saloon::assertSent(function ($request, $response) use ($newRequest, $newResponse) {
43+
expect($request)->toBeInstanceOf(SaloonRequest::class);
44+
expect($response)->toBeInstanceOf(SaloonResponse::class);
45+
46+
expect($request)->toBe($newRequest);
47+
expect($response)->toBe($newResponse);
48+
49+
return true;
50+
});
51+
});
52+
53+
test('that assertSent works with a url', function () {
54+
Saloon::fake([
55+
UserRequest::class => new MockResponse(['name' => 'Sam'], 200),
56+
]);
57+
58+
(new UserRequest())->send();
59+
60+
Saloon::assertSent('samcarre.dev/*');
61+
Saloon::assertSent('/user');
62+
Saloon::assertSent('api/user');
63+
});
64+
65+
test('that assertNotSent works with a request', function () {
66+
Saloon::fake([
67+
UserRequest::class => new MockResponse(['name' => 'Sam'], 200),
68+
ErrorRequest::class => new MockResponse(['error' => 'Server Error'], 500),
69+
]);
70+
71+
(new ErrorRequest())->send();
72+
73+
Saloon::assertNotSent(UserRequest::class);
74+
});
75+
76+
test('that assertNotSent works with a closure', function () {
77+
Saloon::fake([
78+
UserRequest::class => new MockResponse(['name' => 'Sam'], 200),
79+
ErrorRequest::class => new MockResponse(['error' => 'Server Error'], 500),
80+
]);
81+
82+
$originalRequest = new ErrorRequest();
83+
$originalResponse = $originalRequest->send();
84+
85+
Saloon::assertNotSent(function ($request) {
86+
return $request instanceof UserRequest;
87+
});
88+
});
89+
90+
test('that assertNotSent works with a url', function () {
91+
Saloon::fake([
92+
UserRequest::class => new MockResponse(['name' => 'Sam'], 200),
93+
]);
94+
95+
(new UserRequest())->send();
96+
97+
Saloon::assertNotSent('google.com/*');
98+
Saloon::assertNotSent('/error');
99+
});
100+
101+
test('that assertSentJson works properly', function () {
102+
Saloon::fake([
103+
UserRequest::class => new MockResponse(['name' => 'Sam'], 200),
104+
]);
105+
106+
(new UserRequest())->send();
107+
108+
Saloon::assertSentJson(UserRequest::class, [
109+
'name' => 'Sam',
110+
]);
111+
});
112+
113+
test('test assertNothingSent works properly', function () {
114+
Saloon::fake([
115+
UserRequest::class => new MockResponse(['name' => 'Sam'], 200),
116+
]);
117+
118+
Saloon::assertNothingSent();
119+
});
120+
121+
test('test assertSentCount works properly', function () {
122+
Saloon::fake([
123+
new MockResponse(['name' => 'Sam'], 200),
124+
new MockResponse(['name' => 'Taylor'], 200),
125+
new MockResponse(['name' => 'Marcel'], 200),
126+
]);
127+
128+
(new UserRequest())->send();
129+
(new UserRequest())->send();
130+
(new UserRequest())->send();
131+
132+
Saloon::assertSentCount(3);
133+
});

0 commit comments

Comments
 (0)