Cleanup tests. Try to fix builds. Include composer.lock.

This commit is contained in:
ErickSkrauch 2019-02-24 02:36:41 +03:00
parent 9b14a1d427
commit 1b4de783ff
7 changed files with 2764 additions and 123 deletions

1
.gitignore vendored
View File

@ -2,7 +2,6 @@
/build
/vendor
composer.phar
composer.lock
.php_cs.cache
.php_cs
.DS_Store

View File

@ -5,6 +5,7 @@ php:
- 7.0
- 7.1
- 7.2
- 7.3
- nightly
cache:
@ -21,6 +22,7 @@ env:
before_script:
- composer global show hirak/prestissimo -q || travis_retry composer global require $DEFAULT_COMPOSER_FLAGS hirak/prestissimo
- '[ "${TRAVIS_PHP_VERSION:0:1}" == "5" ] && composer remove ely/php-code-style'
- travis_retry composer update --no-interaction --prefer-source $PREFER_LOWEST
- travis_retry phpenv rehash

View File

@ -19,9 +19,9 @@
"league/oauth2-client": "^1.0 | ^2.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8 || ^5.0",
"mockery/mockery": "~0.9",
"ely/php-code-style": "^0.2.0"
"ext-json": "*",
"phpunit/phpunit": "^4.8",
"ely/php-code-style": "^0.3.0"
},
"autoload": {
"psr-4": {

2684
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,19 +1,28 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
namespace Ely\OAuth2\Client\Test;
use Ely\OAuth2\Client\Exception\IdentityProviderException;
use Ely\OAuth2\Client\Provider;
use Ely\OAuth2\Client\ResourceOwner;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use League\OAuth2\Client\Token\AccessToken;
use Mockery as m;
use Psr\Http\Message\ResponseInterface;
use PHPUnit\Framework\TestCase;
class ProviderTest extends \PHPUnit_Framework_TestCase {
class ProviderTest extends TestCase {
/**
* @var Provider
*/
protected $provider;
private $provider;
/**
* @var MockHandler
*/
private $mockHandler;
protected function setUp() {
$this->provider = new Provider([
@ -21,17 +30,14 @@ class ProviderTest extends \PHPUnit_Framework_TestCase {
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',
]);
}
public function tearDown() {
m::close();
parent::tearDown();
$this->mockHandler = new MockHandler();
$client = new Client(['handler' => HandlerStack::create($this->mockHandler)]);
$this->provider->setHttpClient($client);
}
public function testGetResourceOwnerDetailsUrl() {
$url = $this->provider->getResourceOwnerDetailsUrl(new AccessToken(['access_token' => 'mock_token']));
$uri = parse_url($url);
$this->assertEquals('/api/account/v1/info', $uri['path']);
$this->assertSame('/api/account/v1/info', parse_url($url, PHP_URL_PATH));
}
public function testGetAuthorizationUrl() {
@ -39,7 +45,7 @@ class ProviderTest extends \PHPUnit_Framework_TestCase {
$uri = parse_url($url);
parse_str($uri['query'], $query);
$this->assertEquals('/oauth2/v1/mock_client_id', $uri['path']);
$this->assertSame('/oauth2/v1/mock_client_id', $uri['path']);
$this->assertArrayNotHasKey('client_id', $query);
$this->assertArrayHasKey('redirect_uri', $query);
$this->assertArrayHasKey('state', $query);
@ -51,115 +57,64 @@ class ProviderTest extends \PHPUnit_Framework_TestCase {
public function testScopes() {
$options = ['scope' => ['minecraft_server_session', 'account_info']];
$url = $this->provider->getAuthorizationUrl($options);
$this->assertContains(urlencode(implode(',', $options['scope'])), $url);
$this->assertContains('scope=minecraft_server_session%2Caccount_info', $url);
}
public function testGetBaseAccessTokenUrl() {
$params = [];
$url = $this->provider->getBaseAccessTokenUrl($params);
$uri = parse_url($url);
$this->assertEquals('/api/oauth2/v1/token', $uri['path']);
$url = $this->provider->getBaseAccessTokenUrl([]);
$this->assertSame('/api/oauth2/v1/token', parse_url($url, PHP_URL_PATH));
}
public function testGetAccessToken() {
/** @var m\Mock|ResponseInterface $response */
$response = m::mock(ResponseInterface::class);
$response->shouldReceive('getBody')->andReturn($this->getAccessTokenResponse());
$response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$response->shouldReceive('getStatusCode')->andReturn(200);
/** @var m\Mock|ClientInterface $client */
$client = m::mock(ClientInterface::class);
$client->shouldReceive('send')->times(1)->andReturn($response);
$this->provider->setHttpClient($client);
$this->mockHandler->append(new Response(200, ['content-type' => 'json'], $this->getAccessTokenResponse()));
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
$this->assertEquals('mock_access_token', $token->getToken());
$this->assertSame('mock_access_token', $token->getToken());
$this->assertNotNull($token->getExpires());
$this->assertNull($token->getRefreshToken());
}
/**
* @expectedException \Ely\OAuth2\Client\Exception\IdentityProviderException
* @expectedExceptionMessageRegExp /Exception message .+/
*/
public function testExceptionThrownWhenErrorObjectReceived() {
$name = 'Error ' . uniqid();
$message = 'Exception message ' . uniqid();
$status = mt_rand(400, 600);
/** @var m\Mock|ResponseInterface $postResponse */
$postResponse = m::mock(ResponseInterface::class);
$postResponse->shouldReceive('getBody')->andReturn(json_encode([
'name' => $name,
'message' => $message,
'status' => $status,
$this->mockHandler->append(new Response(418, ['content-type' => 'json'], json_encode([
'name' => 'Some error happened',
'message' => 'Some exception message',
'status' => 418,
'code' => 0,
]));
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$postResponse->shouldReceive('getStatusCode')->andReturn($status);
])));
$this->setExpectedException(IdentityProviderException::class, 'Some exception message');
/** @var m\Mock|ClientInterface $client */
$client = m::mock(ClientInterface::class);
$client->shouldReceive('send')
->times(1)
->andReturn($postResponse);
$this->provider->setHttpClient($client);
$this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
}
/**
* @expectedException \Ely\OAuth2\Client\Exception\IdentityProviderException
* @expectedExceptionMessage Bad Gateway
*/
public function testExceptionThrownOnIncorrectContentType() {
/** @var m\Mock|ResponseInterface $postResponse */
$postResponse = m::mock(ResponseInterface::class);
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'text/html; charset=UTF-8']);
$postResponse->shouldReceive('getBody')->andReturn('html content');
$postResponse->shouldReceive('getStatusCode')->andReturn(502);
$postResponse->shouldReceive('getReasonPhrase')->andReturn('Bad Gateway');
$this->mockHandler->append(new Response(502, ['content-type' => 'text/html; charset=UTF-8'], 'html content'));
$this->setExpectedException(IdentityProviderException::class, 'Bad Gateway');
/** @var m\Mock|ClientInterface $client */
$client = m::mock(ClientInterface::class);
$client->shouldReceive('send')
->times(1)
->andReturn($postResponse);
$this->provider->setHttpClient($client);
$this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
}
public function testGetResourceOwner() {
/** @var m\Mock|ResponseInterface $postResponse */
$postResponse = m::mock(ResponseInterface::class);
$postResponse->shouldReceive('getBody')->andReturn($this->getAccessTokenResponse());
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$postResponse->shouldReceive('getStatusCode')->andReturn(200);
/** @var m\Mock|ResponseInterface $userResponse */
$userResponse = m::mock(ResponseInterface::class);
$userResponse->shouldReceive('getBody')->andReturn(
file_get_contents(__DIR__ . '/data/identity-info-response.json')
);
$userResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$userResponse->shouldReceive('getStatusCode')->andReturn(200);
/** @var m\Mock|ClientInterface $client */
$client = m::mock(ClientInterface::class);
$client->shouldReceive('send')
->times(2)
->andReturn($postResponse, $userResponse);
$this->provider->setHttpClient($client);
$this->mockHandler->append(new Response(200, ['content-type' => 'json'], $this->getAccessTokenResponse()));
$this->mockHandler->append(new Response(200, ['content-type' => 'json'], json_encode([
'id' => 1,
'uuid' => 'ffc8fdc9-5824-509e-8a57-c99b940fb996',
'username' => 'ErickSkrauch',
'registeredAt' => 1470566470,
'profileLink' => 'http://ely.by/u1',
'preferredLanguage' => 'be',
'email' => 'erickskrauch@ely.by',
])));
/** @var AccessToken $token */
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
$user = $this->provider->getResourceOwner($token);
$this->assertInstanceOf(ResourceOwner::class, $user);
$this->assertSame(0, $this->mockHandler->count());
}
private function getAccessTokenResponse() {

View File

@ -1,55 +1,57 @@
<?php
namespace Ely\OAuth2\Client\Test;
use DateTime;
use Ely\OAuth2\Client\ResourceOwner;
use PHPUnit\Framework\TestCase;
class ResourceOwnerTest extends \PHPUnit_Framework_TestCase {
class ResourceOwnerTest extends TestCase {
public function testGetId() {
$this->assertEquals(1, $this->createModel()->getId());
$this->assertSame(1, $this->createModel()->getId());
}
public function testGetUuid() {
$this->assertEquals('ffc8fdc9-5824-509e-8a57-c99b940fb996', $this->createModel()->getUuid());
$this->assertSame('ffc8fdc9-5824-509e-8a57-c99b940fb996', $this->createModel()->getUuid());
}
public function testGetUsername() {
$this->assertEquals('ErickSkrauch', $this->createModel()->getUsername());
$this->assertSame('ErickSkrauch', $this->createModel()->getUsername());
}
public function testGetEmail() {
$this->assertEquals('erickskrauch@ely.by', $this->createModel()->getEmail());
$this->assertSame('erickskrauch@ely.by', $this->createModel()->getEmail());
$this->assertNull($this->createModelWithoutEmail()->getEmail());
}
public function testGetRegisteredAt() {
$registeredAt = $this->createModel()->getRegisteredAt();
$this->assertInstanceOf(\DateTime::class, $registeredAt);
$this->assertEquals(1470566470, $registeredAt->getTimestamp());
$this->assertInstanceOf(DateTime::class, $registeredAt);
$this->assertSame(1470566470, $registeredAt->getTimestamp());
}
public function testGetProfileLink() {
$this->assertEquals('http://ely.by/u1', $this->createModel()->getProfileLink());
$this->assertSame('http://ely.by/u1', $this->createModel()->getProfileLink());
}
public function testGetPreferredLanguage() {
$this->assertEquals('be', $this->createModel()->getPreferredLanguage());
$this->assertSame('be', $this->createModel()->getPreferredLanguage());
}
public function testGetSkinUrl() {
$this->assertEquals('http://skinsystem.ely.by/skins/ErickSkrauch.png', $this->createModel()->getSkinUrl());
$this->assertSame('http://skinsystem.ely.by/skins/ErickSkrauch.png', $this->createModel()->getSkinUrl());
}
public function testToArray() {
$array = $this->createModel()->toArray();
$this->assertTrue(is_array($array));
$this->assertEquals(1, $array['id']);
$this->assertEquals('ffc8fdc9-5824-509e-8a57-c99b940fb996', $array['uuid']);
$this->assertEquals('ErickSkrauch', $array['username']);
$this->assertEquals('erickskrauch@ely.by', $array['email']);
$this->assertEquals(1470566470, $array['registeredAt']);
$this->assertEquals('http://ely.by/u1', $array['profileLink']);
$this->assertEquals('http://skinsystem.ely.by/skins/ErickSkrauch.png', $array['skinUrl']);
$this->assertInternalType('array', $array);
$this->assertSame(1, $array['id']);
$this->assertSame('ffc8fdc9-5824-509e-8a57-c99b940fb996', $array['uuid']);
$this->assertSame('ErickSkrauch', $array['username']);
$this->assertSame('erickskrauch@ely.by', $array['email']);
$this->assertSame(1470566470, $array['registeredAt']);
$this->assertSame('http://ely.by/u1', $array['profileLink']);
$this->assertSame('http://skinsystem.ely.by/skins/ErickSkrauch.png', $array['skinUrl']);
$array = $this->createModelWithoutEmail()->toArray();
$this->assertArrayNotHasKey('email', $array);
@ -67,7 +69,15 @@ class ResourceOwnerTest extends \PHPUnit_Framework_TestCase {
}
private function getAllResponseParams() {
return json_decode(file_get_contents(__DIR__ . '/data/identity-info-response.json'), true);
return [
'id' => 1,
'uuid' => 'ffc8fdc9-5824-509e-8a57-c99b940fb996',
'username' => 'ErickSkrauch',
'registeredAt' => 1470566470,
'profileLink' => 'http://ely.by/u1',
'preferredLanguage' => 'be',
'email' => 'erickskrauch@ely.by',
];
}
}

View File

@ -1,9 +0,0 @@
{
"id": 1,
"uuid": "ffc8fdc9-5824-509e-8a57-c99b940fb996",
"username": "ErickSkrauch",
"registeredAt": 1470566470,
"profileLink": "http:\/\/ely.by\/u1",
"preferredLanguage": "be",
"email": "erickskrauch@ely.by"
}