Skip to content

Commit

Permalink
Replace mockery/mockery with native PHPUnit mock
Browse files Browse the repository at this point in the history
  • Loading branch information
greew committed Oct 15, 2024
1 parent acc8344 commit b39616d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 45 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
},
"require-dev": {
"roave/security-advisories": "dev-latest",
"mockery/mockery": "^1.3.5",
"phpunit/phpunit": "^9.5",
"friendsofphp/php-cs-fixer": "^3.64"
},
Expand Down
110 changes: 66 additions & 44 deletions tests/src/Provider/AzureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace Greew\OAuth2\Test\Client\Provider;

use Greew\OAuth2\Client\Provider\Azure;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Tool\QueryBuilderTrait;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

class AzureTest extends TestCase
{
Expand All @@ -27,7 +29,6 @@ protected function setUp(): void

public function tearDown(): void
{
m::close();
parent::tearDown();
}

Expand Down Expand Up @@ -79,7 +80,7 @@ public function testSettingAuthEndpoints(): void
$customAuthUrl = uniqid('', true);
$customResourceOwnerUrl = uniqid('', true);
$customTenantId = uniqid('', true);
$token = m::mock('League\OAuth2\Client\Token\AccessToken');
$token = $this->createMock(AccessToken::class);

$this->provider = new Azure([
'clientId' => 'mock_client_id',
Expand All @@ -100,15 +101,20 @@ public function testSettingAuthEndpoints(): void

public function testGetAccessToken(): void
{
$response = m::mock('Psr\Http\Message\ResponseInterface');
$response->shouldReceive('getBody')->andReturn(
'{"access_token":"mock_access_token","authentication_token":"","code":"","expires_in":3600,' .
'"refresh_token":"mock_refresh_token","scope":"","state":"","token_type":""}',
);
$response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);

$client = m::mock('GuzzleHttp\ClientInterface');
$client->shouldReceive('send')->times(1)->andReturn($response);
$streamInterface = $this->createMock(StreamInterface::class);
$streamInterface
->method('__toString')
->willReturn('{"access_token":"mock_access_token","authentication_token":"","code":"","expires_in":3600,"refresh_token":"mock_refresh_token","scope":"","state":"","token_type":""}');

$response = $this->createMock(ResponseInterface::class);
$response->method('getBody')->willReturn($streamInterface);
$response->method('getHeader')->willReturn(['content-type' => 'json']);

$client = $this->createMock('GuzzleHttp\ClientInterface');
$client
->expects($this->once())
->method('send')
->willReturn($response);
$this->provider->setHttpClient($client);

$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
Expand All @@ -122,28 +128,32 @@ public function testGetAccessToken(): void

public function testUserData(): void
{
$email = uniqid('', true);
$firstname = uniqid('', true);
$lastname = uniqid('', true);
$name = uniqid('', true);
$userId = rand(1000, 9999);
$urls = uniqid('', true);

$postResponse = m::mock('Psr\Http\Message\ResponseInterface');
$postResponse->shouldReceive('getBody')->andReturn(
'{"access_token":"mock_access_token","authentication_token":"","code":"","expires_in":3600,' .
'"refresh_token":"mock_refresh_token","scope":"","state":"","token_type":""}',
);
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);

$userResponse = m::mock('Psr\Http\Message\ResponseInterface');
$userResponse->shouldReceive('getBody')->andReturn('{"id": ' . $userId . '}');
$userResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);

$client = m::mock('GuzzleHttp\ClientInterface');
$client->shouldReceive('send')
->times(2)
->andReturn($postResponse, $userResponse);

$postResponseStreamInterface = $this->createMock(StreamInterface::class);
$postResponseStreamInterface
->method('__toString')
->willReturn('{"access_token":"mock_access_token","authentication_token":"","code":"","expires_in":3600,"refresh_token":"mock_refresh_token","scope":"","state":"","token_type":""}');

$postResponse = $this->createMock(ResponseInterface::class);
$postResponse->method('getBody')->willReturn($postResponseStreamInterface);
$postResponse->method('getHeader')->willReturn(['content-type' => 'json']);

$userResponseStreamInterface = $this->createMock(StreamInterface::class);
$userResponseStreamInterface
->method('__toString')
->willReturn('{"id": ' . $userId . '}');

$userResponse = $this->createMock(ResponseInterface::class);
$userResponse->method('getBody')->willReturn($userResponseStreamInterface);
$userResponse->method('getHeader')->willReturn(['content-type' => 'json']);

$client = $this->createMock('GuzzleHttp\ClientInterface');
$client
->expects($this->exactly(2))
->method('send')
->willReturnOnConsecutiveCalls($postResponse, $userResponse);

$this->provider->setHttpClient($client);

$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
Expand All @@ -158,17 +168,29 @@ public function testExceptionThrownWhenErrorObjectReceived(): void
$this->expectException(\League\OAuth2\Client\Provider\Exception\IdentityProviderException::class);
$message = uniqid('', true);

$postResponse = m::mock('Psr\Http\Message\ResponseInterface');
$postResponse->shouldReceive('getBody')->andReturn(
'{"error": "request_token_expired", "error_description": "' . $message . '"}',
);
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$postResponse->shouldReceive('getStatusCode')->andReturn(500);

$client = m::mock('GuzzleHttp\ClientInterface');
$client->shouldReceive('send')
->times(1)
->andReturn($postResponse);
$postResponseStreamInterface = $this->createMock(StreamInterface::class);
$postResponseStreamInterface
->method('__toString')
->willReturn('{"error": "invalid_grant", "error_description": "' . $message . '"}');

$postResponse = $this->createMock(ResponseInterface::class);
$postResponse
->method('getBody')
->willReturn($postResponseStreamInterface);

$postResponse
->method('getHeader')
->willReturn(['content-type' => 'json']);

$postResponse
->method('getStatusCode')
->willReturn(400);

$client = $this->createMock('GuzzleHttp\ClientInterface');
$client
->expects($this->once())
->method('send')
->willReturn($postResponse);
$this->provider->setHttpClient($client);

$this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
Expand Down

0 comments on commit b39616d

Please sign in to comment.