Skip to content

Commit add2c05

Browse files
authored
bug: mockery not setup correctly (#1785)
* bug: mockery not setup correctly Fixed a subtle bug in the way Mockery is setup in the project, versions of Mockery before version 1.0.0 would automatically register a listener with PHPUnit meaning that assertions in mockery would automatically count towards PHPUnit's assertion count. This results in devs adding hacks to make the unit tests pass i.e adding the `DoesNotPerformAssertions` attribute. https://docs.mockery.io/en/latest/reference/phpunit_integration.html * chore: apply cs fixes
1 parent d16746c commit add2c05

24 files changed

+64
-95
lines changed

tests/Unit/AccessTokenControllerTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Laravel\Passport\Http\Controllers\AccessTokenController;
77
use League\OAuth2\Server\AuthorizationServer;
88
use League\OAuth2\Server\Exception\OAuthServerException as LeagueException;
9+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
910
use Mockery as m;
1011
use Nyholm\Psr7\Response;
1112
use PHPUnit\Framework\TestCase;
@@ -14,10 +15,7 @@
1415

1516
class AccessTokenControllerTest extends TestCase
1617
{
17-
protected function tearDown(): void
18-
{
19-
m::close();
20-
}
18+
use MockeryPHPUnitIntegration;
2119

2220
public function test_a_token_can_be_issued()
2321
{

tests/Unit/AccessTokenTest.php

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ class AccessTokenTest extends TestCase
1111
{
1212
protected function tearDown(): void
1313
{
14-
parent::tearDown();
15-
1614
Passport::$withInheritedScopes = false;
1715
}
1816

tests/Unit/ApiTokenCookieFactoryTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
use Illuminate\Encryption\Encrypter;
88
use Laravel\Passport\ApiTokenCookieFactory;
99
use Laravel\Passport\Passport;
10+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
1011
use Mockery as m;
1112
use PHPUnit\Framework\TestCase;
1213
use Symfony\Component\HttpFoundation\Cookie;
1314

1415
class ApiTokenCookieFactoryTest extends TestCase
1516
{
16-
protected function tearDown(): void
17-
{
18-
m::close();
19-
}
17+
use MockeryPHPUnitIntegration;
2018

2119
public function test_cookie_can_be_successfully_created()
2220
{

tests/Unit/ApproveAuthorizationControllerTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66
use Laravel\Passport\Http\Controllers\ApproveAuthorizationController;
77
use League\OAuth2\Server\AuthorizationServer;
88
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
9+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
910
use Mockery as m;
1011
use Nyholm\Psr7\Response;
1112
use PHPUnit\Framework\TestCase;
1213
use Psr\Http\Message\ResponseInterface;
1314

1415
class ApproveAuthorizationControllerTest extends TestCase
1516
{
16-
protected function tearDown(): void
17-
{
18-
m::close();
19-
}
17+
use MockeryPHPUnitIntegration;
2018

2119
public function test_complete_authorization_request()
2220
{

tests/Unit/AuthorizationControllerTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use League\OAuth2\Server\Exception\OAuthServerException as LeagueException;
1818
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
1919
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
20+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
2021
use Mockery as m;
2122
use Nyholm\Psr7\Response;
2223
use PHPUnit\Framework\TestCase;
@@ -25,10 +26,7 @@
2526

2627
class AuthorizationControllerTest extends TestCase
2728
{
28-
protected function tearDown(): void
29-
{
30-
m::close();
31-
}
29+
use MockeryPHPUnitIntegration;
3230

3331
public function test_authorization_view_is_presented()
3432
{

tests/Unit/AuthorizedAccessTokenControllerTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
use Laravel\Passport\RefreshTokenRepository;
99
use Laravel\Passport\Token;
1010
use Laravel\Passport\TokenRepository;
11+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
1112
use Mockery as m;
1213
use PHPUnit\Framework\TestCase;
1314
use Symfony\Component\HttpFoundation\Response;
1415

1516
class AuthorizedAccessTokenControllerTest extends TestCase
1617
{
18+
use MockeryPHPUnitIntegration;
1719
/**
1820
* @var \Mockery\Mock|\Laravel\Passport\TokenRepository
1921
*/

tests/Unit/BridgeAccessTokenRepositoryTest.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010
use Laravel\Passport\Bridge\Client;
1111
use Laravel\Passport\Bridge\Scope;
1212
use Laravel\Passport\TokenRepository;
13+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
1314
use Mockery as m;
1415
use PHPUnit\Framework\TestCase;
1516

1617
class BridgeAccessTokenRepositoryTest extends TestCase
1718
{
18-
protected function tearDown(): void
19-
{
20-
m::close();
21-
}
19+
use MockeryPHPUnitIntegration;
2220

2321
public function test_access_tokens_can_be_persisted()
2422
{
@@ -59,8 +57,6 @@ public function test_access_tokens_can_be_revoked()
5957

6058
$repository = new AccessTokenRepository($tokenRepository, $events);
6159
$repository->revokeAccessToken('token-id');
62-
63-
$this->expectNotToPerformAssertions();
6460
}
6561

6662
public function test_access_token_revoke_event_is_not_dispatched_when_nothing_happened()
@@ -73,8 +69,6 @@ public function test_access_token_revoke_event_is_not_dispatched_when_nothing_ha
7369

7470
$repository = new AccessTokenRepository($tokenRepository, $events);
7571
$repository->revokeAccessToken('token-id');
76-
77-
$this->expectNotToPerformAssertions();
7872
}
7973

8074
public function test_can_get_new_access_token()

tests/Unit/BridgeClientRepositoryTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
use Laravel\Passport\Bridge\Client;
77
use Laravel\Passport\Bridge\ClientRepository as BridgeClientRepository;
88
use Laravel\Passport\ClientRepository;
9+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
910
use Mockery as m;
1011
use PHPUnit\Framework\TestCase;
1112

1213
class BridgeClientRepositoryTest extends TestCase
1314
{
15+
use MockeryPHPUnitIntegration;
16+
1417
/**
1518
* @var \Laravel\Passport\ClientRepository
1619
*/

tests/Unit/BridgeRefreshTokenRepositoryTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@
99
use Laravel\Passport\Bridge\RefreshToken;
1010
use Laravel\Passport\Bridge\RefreshTokenRepository as BridgeRefreshTokenRepository;
1111
use Laravel\Passport\RefreshTokenRepository;
12+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
1213
use Mockery as m;
1314
use PHPUnit\Framework\TestCase;
1415

1516
class BridgeRefreshTokenRepositoryTest extends TestCase
1617
{
17-
protected function tearDown(): void
18-
{
19-
m::close();
20-
}
18+
use MockeryPHPUnitIntegration;
2119

2220
public function test_access_tokens_can_be_persisted()
2321
{

tests/Unit/BridgeScopeRepositoryTest.php

+18-15
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
use Laravel\Passport\Client as ClientModel;
99
use Laravel\Passport\ClientRepository;
1010
use Laravel\Passport\Passport;
11-
use Mockery;
11+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
12+
use Mockery as m;
1213
use PHPUnit\Framework\TestCase;
1314

1415
class BridgeScopeRepositoryTest extends TestCase
1516
{
17+
use MockeryPHPUnitIntegration;
18+
1619
protected function tearDown(): void
1720
{
1821
Passport::$withInheritedScopes = false;
@@ -24,9 +27,9 @@ public function test_invalid_scopes_are_removed()
2427
'scope-1' => 'description',
2528
]);
2629

27-
$client = Mockery::mock(ClientModel::class)->makePartial();
30+
$client = m::mock(ClientModel::class)->makePartial();
2831

29-
$clients = Mockery::mock(ClientRepository::class);
32+
$clients = m::mock(ClientRepository::class);
3033
$clients->shouldReceive('findActive')->withAnyArgs()->andReturn($client);
3134

3235
$repository = new ScopeRepository($clients);
@@ -44,10 +47,10 @@ public function test_invalid_scopes_are_removed_without_a_client_repository()
4447
'scope-1' => 'description',
4548
]);
4649

47-
$clients = Mockery::mock(ClientRepository::class);
50+
$clients = m::mock(ClientRepository::class);
4851
$clients->shouldReceive('findActive')
4952
->with('id')
50-
->andReturn(Mockery::mock(ClientModel::class)->makePartial());
53+
->andReturn(m::mock(ClientModel::class)->makePartial());
5154

5255
$repository = new ScopeRepository($clients);
5356

@@ -65,10 +68,10 @@ public function test_clients_do_not_restrict_scopes_by_default()
6568
'scope-2' => 'description',
6669
]);
6770

68-
$client = Mockery::mock(ClientModel::class)->makePartial();
71+
$client = m::mock(ClientModel::class)->makePartial();
6972
$client->scopes = null;
7073

71-
$clients = Mockery::mock(ClientRepository::class);
74+
$clients = m::mock(ClientRepository::class);
7275
$clients->shouldReceive('findActive')->withAnyArgs()->andReturn($client);
7376

7477
$repository = new ScopeRepository($clients);
@@ -87,10 +90,10 @@ public function test_scopes_disallowed_for_client_are_removed()
8790
'scope-2' => 'description',
8891
]);
8992

90-
$client = Mockery::mock(ClientModel::class)->makePartial();
93+
$client = m::mock(ClientModel::class)->makePartial();
9194
$client->scopes = ['scope-1'];
9295

93-
$clients = Mockery::mock(ClientRepository::class);
96+
$clients = m::mock(ClientRepository::class);
9497
$clients->shouldReceive('findActive')->withAnyArgs()->andReturn($client);
9598

9699
$repository = new ScopeRepository($clients);
@@ -112,10 +115,10 @@ public function test_scopes_disallowed_for_client_are_removed_but_inherited_scop
112115
'scope-2' => 'description',
113116
]);
114117

115-
$client = Mockery::mock(ClientModel::class)->makePartial();
118+
$client = m::mock(ClientModel::class)->makePartial();
116119
$client->scopes = ['scope-1'];
117120

118-
$clients = Mockery::mock(ClientRepository::class);
121+
$clients = m::mock(ClientRepository::class);
119122
$clients->shouldReceive('findActive')->withAnyArgs()->andReturn($client);
120123

121124
$repository = new ScopeRepository($clients);
@@ -133,9 +136,9 @@ public function test_superuser_scope_cant_be_applied_if_wrong_grant()
133136
'scope-1' => 'description',
134137
]);
135138

136-
$client = Mockery::mock(ClientModel::class)->makePartial();
139+
$client = m::mock(ClientModel::class)->makePartial();
137140

138-
$clients = Mockery::mock(ClientRepository::class);
141+
$clients = m::mock(ClientRepository::class);
139142
$clients->shouldReceive('findActive')->withAnyArgs()->andReturn($client);
140143

141144
$repository = new ScopeRepository($clients);
@@ -153,10 +156,10 @@ public function test_superuser_scope_cant_be_applied_if_wrong_grant_without_a_cl
153156
'scope-1' => 'description',
154157
]);
155158

156-
$clients = Mockery::mock(ClientRepository::class);
159+
$clients = m::mock(ClientRepository::class);
157160
$clients->shouldReceive('findActive')
158161
->with('id')
159-
->andReturn(Mockery::mock(ClientModel::class)->makePartial());
162+
->andReturn(m::mock(ClientModel::class)->makePartial());
160163

161164
$repository = new ScopeRepository($clients);
162165

tests/Unit/CheckClientCredentialsForAnyScopeTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
use Laravel\Passport\Http\Middleware\CheckClientCredentialsForAnyScope;
88
use League\OAuth2\Server\Exception\OAuthServerException;
99
use League\OAuth2\Server\ResourceServer;
10+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
1011
use Mockery as m;
1112
use PHPUnit\Framework\TestCase;
1213
use Psr\Http\Message\ServerRequestInterface;
1314

1415
class CheckClientCredentialsForAnyScopeTest extends TestCase
1516
{
16-
protected function tearDown(): void
17-
{
18-
m::close();
19-
}
17+
use MockeryPHPUnitIntegration;
2018

2119
public function test_request_is_passed_along_if_token_is_valid()
2220
{

tests/Unit/CheckClientCredentialsTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
use Laravel\Passport\Http\Middleware\CheckClientCredentials;
88
use League\OAuth2\Server\Exception\OAuthServerException;
99
use League\OAuth2\Server\ResourceServer;
10+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
1011
use Mockery as m;
1112
use PHPUnit\Framework\TestCase;
1213
use Psr\Http\Message\ServerRequestInterface;
1314

1415
class CheckClientCredentialsTest extends TestCase
1516
{
16-
protected function tearDown(): void
17-
{
18-
m::close();
19-
}
17+
use MockeryPHPUnitIntegration;
2018

2119
public function test_request_is_passed_along_if_token_is_valid()
2220
{

tests/Unit/CheckForAnyScopeTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44

55
use Laravel\Passport\Exceptions\AuthenticationException;
66
use Laravel\Passport\Http\Middleware\CheckForAnyScope as CheckScopes;
7+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
78
use Mockery as m;
89
use PHPUnit\Framework\TestCase;
910

1011
class CheckForAnyScopeTest extends TestCase
1112
{
12-
protected function tearDown(): void
13-
{
14-
m::close();
15-
}
13+
use MockeryPHPUnitIntegration;
1614

1715
public function test_request_is_passed_along_if_scopes_are_present_on_token()
1816
{

tests/Unit/CheckScopesTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44

55
use Laravel\Passport\Exceptions\AuthenticationException;
66
use Laravel\Passport\Http\Middleware\CheckScopes;
7+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
78
use Mockery as m;
89
use PHPUnit\Framework\TestCase;
910

1011
class CheckScopesTest extends TestCase
1112
{
12-
protected function tearDown(): void
13-
{
14-
m::close();
15-
}
13+
use MockeryPHPUnitIntegration;
1614

1715
public function test_request_is_passed_along_if_scopes_are_present_on_token()
1816
{

tests/Unit/ClientControllerTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88
use Laravel\Passport\ClientRepository;
99
use Laravel\Passport\Http\Controllers\ClientController;
1010
use Laravel\Passport\Http\Rules\RedirectRule;
11+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
1112
use Mockery as m;
1213
use PHPUnit\Framework\TestCase;
1314
use Symfony\Component\HttpFoundation\Response;
1415

1516
class ClientControllerTest extends TestCase
1617
{
17-
protected function tearDown(): void
18-
{
19-
m::close();
20-
}
18+
use MockeryPHPUnitIntegration;
2119

2220
public function test_all_the_clients_for_the_current_user_can_be_retrieved()
2321
{

tests/Unit/CreateFreshApiTokenTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
use Laravel\Passport\ApiTokenCookieFactory;
88
use Laravel\Passport\Http\Middleware\CreateFreshApiToken;
99
use Laravel\Passport\Passport;
10+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
1011
use Mockery as m;
1112
use PHPUnit\Framework\TestCase;
1213
use Symfony\Component\HttpFoundation\Cookie;
1314

1415
class CreateFreshApiTokenTest extends TestCase
1516
{
16-
protected function tearDown(): void
17-
{
18-
m::close();
19-
}
17+
use MockeryPHPUnitIntegration;
2018

2119
public function testShouldReceiveAFreshToken()
2220
{

0 commit comments

Comments
 (0)