1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86:
<?php
namespace League\OAuth2\Client\Test\Grant;
use Eloquent\Phony\Phpunit\Phony;
use GuzzleHttp\ClientInterface;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Test\Provider\Fake as MockProvider;
abstract class GrantTestCase extends TestCase
{
protected $provider;
protected function setUp()
{
$this->provider = new MockProvider(array(
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',
));
}
abstract public function testToString();
abstract public function providerGetAccessToken();
abstract protected function getParamExpectation();
public function testGetAccessToken($grant, array $params = [])
{
$stream = Phony::mock(StreamInterface::class);
$stream->__toString->returns(
'{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}'
);
$response = Phony::mock(ResponseInterface::class);
$response->getBody->returns($stream->get());
$response->getHeader->with('content-type')->returns('application/json');
$client = Phony::mock(ClientInterface::class);
$client->send->returns($response->get());
$this->provider->setHttpClient($client->get());
$token = $this->provider->getAccessToken($grant, $params);
$this->assertInstanceOf(AccessToken::class, $token);
Phony::inOrder(
$client->send->times(1)->calledWith(
$this->callback(function ($request) {
parse_str((string) $request->getBody(), $body);
return call_user_func($this->getParamExpectation(), $body);
})
),
$response->getBody->times(1)->called(),
$stream->__toString->times(1)->called(),
$response->getHeader->times(1)->called()
);
}
}