Skip to content

Commit

Permalink
bug: mockery not setup correctly (#1785)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JimTools authored Sep 22, 2024
1 parent d16746c commit add2c05
Show file tree
Hide file tree
Showing 24 changed files with 64 additions and 95 deletions.
6 changes: 2 additions & 4 deletions tests/Unit/AccessTokenControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Laravel\Passport\Http\Controllers\AccessTokenController;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\Exception\OAuthServerException as LeagueException;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery as m;
use Nyholm\Psr7\Response;
use PHPUnit\Framework\TestCase;
Expand All @@ -14,10 +15,7 @@

class AccessTokenControllerTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
use MockeryPHPUnitIntegration;

public function test_a_token_can_be_issued()
{
Expand Down
2 changes: 0 additions & 2 deletions tests/Unit/AccessTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class AccessTokenTest extends TestCase
{
protected function tearDown(): void
{
parent::tearDown();

Passport::$withInheritedScopes = false;
}

Expand Down
6 changes: 2 additions & 4 deletions tests/Unit/ApiTokenCookieFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
use Illuminate\Encryption\Encrypter;
use Laravel\Passport\ApiTokenCookieFactory;
use Laravel\Passport\Passport;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Cookie;

class ApiTokenCookieFactoryTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
use MockeryPHPUnitIntegration;

public function test_cookie_can_be_successfully_created()
{
Expand Down
6 changes: 2 additions & 4 deletions tests/Unit/ApproveAuthorizationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
use Laravel\Passport\Http\Controllers\ApproveAuthorizationController;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery as m;
use Nyholm\Psr7\Response;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;

class ApproveAuthorizationControllerTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
use MockeryPHPUnitIntegration;

public function test_complete_authorization_request()
{
Expand Down
6 changes: 2 additions & 4 deletions tests/Unit/AuthorizationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use League\OAuth2\Server\Exception\OAuthServerException as LeagueException;
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery as m;
use Nyholm\Psr7\Response;
use PHPUnit\Framework\TestCase;
Expand All @@ -25,10 +26,7 @@

class AuthorizationControllerTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
use MockeryPHPUnitIntegration;

public function test_authorization_view_is_presented()
{
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/AuthorizedAccessTokenControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
use Laravel\Passport\RefreshTokenRepository;
use Laravel\Passport\Token;
use Laravel\Passport\TokenRepository;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;

class AuthorizedAccessTokenControllerTest extends TestCase
{
use MockeryPHPUnitIntegration;
/**
* @var \Mockery\Mock|\Laravel\Passport\TokenRepository
*/
Expand Down
10 changes: 2 additions & 8 deletions tests/Unit/BridgeAccessTokenRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
use Laravel\Passport\Bridge\Client;
use Laravel\Passport\Bridge\Scope;
use Laravel\Passport\TokenRepository;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class BridgeAccessTokenRepositoryTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
use MockeryPHPUnitIntegration;

public function test_access_tokens_can_be_persisted()
{
Expand Down Expand Up @@ -59,8 +57,6 @@ public function test_access_tokens_can_be_revoked()

$repository = new AccessTokenRepository($tokenRepository, $events);
$repository->revokeAccessToken('token-id');

$this->expectNotToPerformAssertions();
}

public function test_access_token_revoke_event_is_not_dispatched_when_nothing_happened()
Expand All @@ -73,8 +69,6 @@ public function test_access_token_revoke_event_is_not_dispatched_when_nothing_ha

$repository = new AccessTokenRepository($tokenRepository, $events);
$repository->revokeAccessToken('token-id');

$this->expectNotToPerformAssertions();
}

public function test_can_get_new_access_token()
Expand Down
3 changes: 3 additions & 0 deletions tests/Unit/BridgeClientRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
use Laravel\Passport\Bridge\Client;
use Laravel\Passport\Bridge\ClientRepository as BridgeClientRepository;
use Laravel\Passport\ClientRepository;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class BridgeClientRepositoryTest extends TestCase
{
use MockeryPHPUnitIntegration;

/**
* @var \Laravel\Passport\ClientRepository
*/
Expand Down
6 changes: 2 additions & 4 deletions tests/Unit/BridgeRefreshTokenRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
use Laravel\Passport\Bridge\RefreshToken;
use Laravel\Passport\Bridge\RefreshTokenRepository as BridgeRefreshTokenRepository;
use Laravel\Passport\RefreshTokenRepository;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class BridgeRefreshTokenRepositoryTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
use MockeryPHPUnitIntegration;

public function test_access_tokens_can_be_persisted()
{
Expand Down
33 changes: 18 additions & 15 deletions tests/Unit/BridgeScopeRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
use Laravel\Passport\Client as ClientModel;
use Laravel\Passport\ClientRepository;
use Laravel\Passport\Passport;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class BridgeScopeRepositoryTest extends TestCase
{
use MockeryPHPUnitIntegration;

protected function tearDown(): void
{
Passport::$withInheritedScopes = false;
Expand All @@ -24,9 +27,9 @@ public function test_invalid_scopes_are_removed()
'scope-1' => 'description',
]);

$client = Mockery::mock(ClientModel::class)->makePartial();
$client = m::mock(ClientModel::class)->makePartial();

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

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

$clients = Mockery::mock(ClientRepository::class);
$clients = m::mock(ClientRepository::class);
$clients->shouldReceive('findActive')
->with('id')
->andReturn(Mockery::mock(ClientModel::class)->makePartial());
->andReturn(m::mock(ClientModel::class)->makePartial());

$repository = new ScopeRepository($clients);

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

$client = Mockery::mock(ClientModel::class)->makePartial();
$client = m::mock(ClientModel::class)->makePartial();
$client->scopes = null;

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

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

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

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

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

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

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

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

$client = Mockery::mock(ClientModel::class)->makePartial();
$client = m::mock(ClientModel::class)->makePartial();

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

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

$clients = Mockery::mock(ClientRepository::class);
$clients = m::mock(ClientRepository::class);
$clients->shouldReceive('findActive')
->with('id')
->andReturn(Mockery::mock(ClientModel::class)->makePartial());
->andReturn(m::mock(ClientModel::class)->makePartial());

$repository = new ScopeRepository($clients);

Expand Down
6 changes: 2 additions & 4 deletions tests/Unit/CheckClientCredentialsForAnyScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
use Laravel\Passport\Http\Middleware\CheckClientCredentialsForAnyScope;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\ResourceServer;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;

class CheckClientCredentialsForAnyScopeTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
use MockeryPHPUnitIntegration;

public function test_request_is_passed_along_if_token_is_valid()
{
Expand Down
6 changes: 2 additions & 4 deletions tests/Unit/CheckClientCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
use Laravel\Passport\Http\Middleware\CheckClientCredentials;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\ResourceServer;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;

class CheckClientCredentialsTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
use MockeryPHPUnitIntegration;

public function test_request_is_passed_along_if_token_is_valid()
{
Expand Down
6 changes: 2 additions & 4 deletions tests/Unit/CheckForAnyScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

use Laravel\Passport\Exceptions\AuthenticationException;
use Laravel\Passport\Http\Middleware\CheckForAnyScope as CheckScopes;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class CheckForAnyScopeTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
use MockeryPHPUnitIntegration;

public function test_request_is_passed_along_if_scopes_are_present_on_token()
{
Expand Down
6 changes: 2 additions & 4 deletions tests/Unit/CheckScopesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

use Laravel\Passport\Exceptions\AuthenticationException;
use Laravel\Passport\Http\Middleware\CheckScopes;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class CheckScopesTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
use MockeryPHPUnitIntegration;

public function test_request_is_passed_along_if_scopes_are_present_on_token()
{
Expand Down
6 changes: 2 additions & 4 deletions tests/Unit/ClientControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@
use Laravel\Passport\ClientRepository;
use Laravel\Passport\Http\Controllers\ClientController;
use Laravel\Passport\Http\Rules\RedirectRule;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;

class ClientControllerTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
use MockeryPHPUnitIntegration;

public function test_all_the_clients_for_the_current_user_can_be_retrieved()
{
Expand Down
6 changes: 2 additions & 4 deletions tests/Unit/CreateFreshApiTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
use Laravel\Passport\ApiTokenCookieFactory;
use Laravel\Passport\Http\Middleware\CreateFreshApiToken;
use Laravel\Passport\Passport;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Cookie;

class CreateFreshApiTokenTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
use MockeryPHPUnitIntegration;

public function testShouldReceiveAFreshToken()
{
Expand Down
Loading

0 comments on commit add2c05

Please sign in to comment.