Add php-cs-fixer and run it

This commit is contained in:
ErickSkrauch 2018-08-20 15:39:57 +03:00
parent e9efb3aa61
commit bbae5d73b5
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
9 changed files with 73 additions and 96 deletions

2
.gitignore vendored
View File

@ -3,4 +3,6 @@
/vendor /vendor
composer.phar composer.phar
composer.lock composer.lock
.php_cs.cache
.php_cs
.DS_Store .DS_Store

8
.php_cs.dist Normal file
View File

@ -0,0 +1,8 @@
<?php
$finder = \PhpCsFixer\Finder::create()
->in(__DIR__);
return \Ely\CS\Config::create([
'visibility_required' => ['property', 'method'],
'ternary_to_null_coalescing' => false,
])->setFinder($finder);

View File

@ -5,11 +5,15 @@ php:
- 5.6 - 5.6
- 7.0 - 7.0
- 7.1 - 7.1
- 7.2
env: env:
matrix: matrix:
- PREFER_LOWEST="--prefer-lowest" - PREFER_LOWEST="--prefer-lowest"
- PREFER_LOWEST="" - PREFER_LOWEST=""
fast_finish: true
allow_failures:
- php: nightly
before_script: before_script:
- travis_retry composer self-update - travis_retry composer self-update

View File

@ -20,7 +20,8 @@
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^4.8 || ^5.0", "phpunit/phpunit": "^4.8 || ^5.0",
"mockery/mockery": "~0.9" "mockery/mockery": "~0.9",
"ely/php-code-style": "^0.2.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
@ -29,7 +30,7 @@
}, },
"autoload-dev": { "autoload-dev": {
"psr-4": { "psr-4": {
"Ely\\OAuth2\\Client\\Test\\": "tests/src/" "Ely\\OAuth2\\Client\\Test\\": "tests/"
} }
} }
} }

View File

@ -3,7 +3,6 @@ namespace Ely\OAuth2\Client\Exception;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException as LeagueIdentityProviderException; use League\OAuth2\Client\Provider\Exception\IdentityProviderException as LeagueIdentityProviderException;
class IdentityProviderException extends LeagueIdentityProviderException class IdentityProviderException extends LeagueIdentityProviderException {
{
} }

View File

@ -10,8 +10,7 @@ use Psr\Http\Message\ResponseInterface;
/** /**
* @method ResourceOwner getResourceOwner(AccessToken $token) * @method ResourceOwner getResourceOwner(AccessToken $token)
*/ */
class Provider extends AbstractProvider class Provider extends AbstractProvider {
{
use BearerAuthorizationTrait; use BearerAuthorizationTrait;
const REDIRECT_URI_STATIC_PAGE = 'static_page'; const REDIRECT_URI_STATIC_PAGE = 'static_page';
@ -20,32 +19,28 @@ class Provider extends AbstractProvider
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function getBaseAuthorizationUrl() public function getBaseAuthorizationUrl() {
{
return 'https://account.ely.by/oauth2/v1/' . $this->clientId; return 'https://account.ely.by/oauth2/v1/' . $this->clientId;
} }
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function getBaseAccessTokenUrl(array $params) public function getBaseAccessTokenUrl(array $params) {
{
return 'https://account.ely.by/api/oauth2/v1/token'; return 'https://account.ely.by/api/oauth2/v1/token';
} }
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function getResourceOwnerDetailsUrl(AccessToken $token) public function getResourceOwnerDetailsUrl(AccessToken $token) {
{
return 'https://account.ely.by/api/account/v1/info'; return 'https://account.ely.by/api/account/v1/info';
} }
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function getAuthorizationParameters(array $options) protected function getAuthorizationParameters(array $options) {
{
$params = parent::getAuthorizationParameters($options); $params = parent::getAuthorizationParameters($options);
// client_id applied to base url // client_id applied to base url
// approval_prompt not supported // approval_prompt not supported
@ -57,8 +52,7 @@ class Provider extends AbstractProvider
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function getDefaultScopes() protected function getDefaultScopes() {
{
return [ return [
'account_info', 'account_info',
]; ];
@ -67,8 +61,7 @@ class Provider extends AbstractProvider
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function checkResponse(ResponseInterface $response, $data) protected function checkResponse(ResponseInterface $response, $data) {
{
$statusCode = $response->getStatusCode(); $statusCode = $response->getStatusCode();
if ($statusCode !== 200) { if ($statusCode !== 200) {
$message = isset($data['message']) ? $data['message'] : $response->getReasonPhrase(); $message = isset($data['message']) ? $data['message'] : $response->getReasonPhrase();
@ -83,8 +76,8 @@ class Provider extends AbstractProvider
* @param AccessToken $token * @param AccessToken $token
* @return ResourceOwner * @return ResourceOwner
*/ */
protected function createResourceOwner(array $response, AccessToken $token) protected function createResourceOwner(array $response, AccessToken $token) {
{
return new ResourceOwner($response); return new ResourceOwner($response);
} }
} }

View File

@ -4,8 +4,8 @@ namespace Ely\OAuth2\Client;
use DateTime; use DateTime;
use League\OAuth2\Client\Provider\ResourceOwnerInterface; use League\OAuth2\Client\Provider\ResourceOwnerInterface;
class ResourceOwner implements ResourceOwnerInterface class ResourceOwner implements ResourceOwnerInterface {
{
/** /**
* Raw response * Raw response
* *
@ -18,8 +18,7 @@ class ResourceOwner implements ResourceOwnerInterface
* *
* @param array $response * @param array $response
*/ */
public function __construct(array $response = []) public function __construct(array $response = []) {
{
$this->response = $response; $this->response = $response;
} }
@ -28,8 +27,7 @@ class ResourceOwner implements ResourceOwnerInterface
* *
* @return string * @return string
*/ */
public function getId() public function getId() {
{
return $this->response['id']; return $this->response['id'];
} }
@ -38,8 +36,7 @@ class ResourceOwner implements ResourceOwnerInterface
* *
* @return string * @return string
*/ */
public function getUuid() public function getUuid() {
{
return $this->response['uuid']; return $this->response['uuid'];
} }
@ -48,8 +45,7 @@ class ResourceOwner implements ResourceOwnerInterface
* *
* @return string * @return string
*/ */
public function getUsername() public function getUsername() {
{
return $this->response['username']; return $this->response['username'];
} }
@ -59,8 +55,7 @@ class ResourceOwner implements ResourceOwnerInterface
* *
* @return string|null * @return string|null
*/ */
public function getEmail() public function getEmail() {
{
return isset($this->response['email']) ? $this->response['email'] : null; return isset($this->response['email']) ? $this->response['email'] : null;
} }
@ -69,8 +64,7 @@ class ResourceOwner implements ResourceOwnerInterface
* *
* @return DateTime * @return DateTime
*/ */
public function getRegisteredAt() public function getRegisteredAt() {
{
return new DateTime('@' . $this->response['registeredAt']); return new DateTime('@' . $this->response['registeredAt']);
} }
@ -79,8 +73,7 @@ class ResourceOwner implements ResourceOwnerInterface
* *
* @return string * @return string
*/ */
public function getProfileLink() public function getProfileLink() {
{
return $this->response['profileLink']; return $this->response['profileLink'];
} }
@ -90,8 +83,7 @@ class ResourceOwner implements ResourceOwnerInterface
* *
* @return string * @return string
*/ */
public function getPreferredLanguage() public function getPreferredLanguage() {
{
return $this->response['preferredLanguage']; return $this->response['preferredLanguage'];
} }
@ -101,8 +93,7 @@ class ResourceOwner implements ResourceOwnerInterface
* *
* @return string * @return string
*/ */
public function getSkinUrl() public function getSkinUrl() {
{
return "http://skinsystem.ely.by/skins/{$this->getUsername()}.png"; return "http://skinsystem.ely.by/skins/{$this->getUsername()}.png";
} }
@ -111,10 +102,10 @@ class ResourceOwner implements ResourceOwnerInterface
* *
* @return array * @return array
*/ */
public function toArray() public function toArray() {
{
return array_merge($this->response, [ return array_merge($this->response, [
'skinUrl' => $this->getSkinUrl(), 'skinUrl' => $this->getSkinUrl(),
]); ]);
} }
} }

View File

@ -8,15 +8,14 @@ use League\OAuth2\Client\Token\AccessToken;
use Mockery as m; use Mockery as m;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
class ProviderTest extends \PHPUnit_Framework_TestCase class ProviderTest extends \PHPUnit_Framework_TestCase {
{
/** /**
* @var Provider * @var Provider
*/ */
protected $provider; protected $provider;
protected function setUp() protected function setUp() {
{
$this->provider = new Provider([ $this->provider = new Provider([
'clientId' => 'mock_client_id', 'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret', 'clientSecret' => 'mock_secret',
@ -24,21 +23,18 @@ class ProviderTest extends \PHPUnit_Framework_TestCase
]); ]);
} }
public function tearDown() public function tearDown() {
{
m::close(); m::close();
parent::tearDown(); parent::tearDown();
} }
public function testGetResourceOwnerDetailsUrl() public function testGetResourceOwnerDetailsUrl() {
{
$url = $this->provider->getResourceOwnerDetailsUrl(new AccessToken(['access_token' => 'mock_token'])); $url = $this->provider->getResourceOwnerDetailsUrl(new AccessToken(['access_token' => 'mock_token']));
$uri = parse_url($url); $uri = parse_url($url);
$this->assertEquals('/api/account/v1/info', $uri['path']); $this->assertEquals('/api/account/v1/info', $uri['path']);
} }
public function testGetAuthorizationUrl() public function testGetAuthorizationUrl() {
{
$url = $this->provider->getAuthorizationUrl(); $url = $this->provider->getAuthorizationUrl();
$uri = parse_url($url); $uri = parse_url($url);
parse_str($uri['query'], $query); parse_str($uri['query'], $query);
@ -53,8 +49,7 @@ class ProviderTest extends \PHPUnit_Framework_TestCase
$this->assertNotNull($this->provider->getState()); $this->assertNotNull($this->provider->getState());
} }
public function testScopes() public function testScopes() {
{
$options = ['scope' => ['minecraft_server_session', 'account_info']]; $options = ['scope' => ['minecraft_server_session', 'account_info']];
$url = $this->provider->getAuthorizationUrl($options); $url = $this->provider->getAuthorizationUrl($options);
@ -62,8 +57,7 @@ class ProviderTest extends \PHPUnit_Framework_TestCase
$this->assertContains(urlencode(implode(',', $options['scope'])), $url); $this->assertContains(urlencode(implode(',', $options['scope'])), $url);
} }
public function testGetBaseAccessTokenUrl() public function testGetBaseAccessTokenUrl() {
{
$params = []; $params = [];
$url = $this->provider->getBaseAccessTokenUrl($params); $url = $this->provider->getBaseAccessTokenUrl($params);
@ -72,8 +66,7 @@ class ProviderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('/api/oauth2/v1/token', $uri['path']); $this->assertEquals('/api/oauth2/v1/token', $uri['path']);
} }
public function testGetAccessToken() public function testGetAccessToken() {
{
/** @var m\Mock|ResponseInterface $response */ /** @var m\Mock|ResponseInterface $response */
$response = m::mock(ResponseInterface::class); $response = m::mock(ResponseInterface::class);
$response->shouldReceive('getBody')->andReturn($this->getAccessTokenResponse()); $response->shouldReceive('getBody')->andReturn($this->getAccessTokenResponse());
@ -96,11 +89,10 @@ class ProviderTest extends \PHPUnit_Framework_TestCase
* @expectedException \Ely\OAuth2\Client\Exception\IdentityProviderException * @expectedException \Ely\OAuth2\Client\Exception\IdentityProviderException
* @expectedExceptionMessageRegExp /Exception message .+/ * @expectedExceptionMessageRegExp /Exception message .+/
*/ */
public function testExceptionThrownWhenErrorObjectReceived() public function testExceptionThrownWhenErrorObjectReceived() {
{
$name = 'Error ' . uniqid(); $name = 'Error ' . uniqid();
$message = 'Exception message ' . uniqid(); $message = 'Exception message ' . uniqid();
$status = rand(400, 600); $status = mt_rand(400, 600);
/** @var m\Mock|ResponseInterface $postResponse */ /** @var m\Mock|ResponseInterface $postResponse */
$postResponse = m::mock(ResponseInterface::class); $postResponse = m::mock(ResponseInterface::class);
$postResponse->shouldReceive('getBody')->andReturn(json_encode([ $postResponse->shouldReceive('getBody')->andReturn(json_encode([
@ -125,8 +117,7 @@ class ProviderTest extends \PHPUnit_Framework_TestCase
* @expectedException \Ely\OAuth2\Client\Exception\IdentityProviderException * @expectedException \Ely\OAuth2\Client\Exception\IdentityProviderException
* @expectedExceptionMessage Bad Gateway * @expectedExceptionMessage Bad Gateway
*/ */
public function testExceptionThrownOnIncorrectContentType() public function testExceptionThrownOnIncorrectContentType() {
{
/** @var m\Mock|ResponseInterface $postResponse */ /** @var m\Mock|ResponseInterface $postResponse */
$postResponse = m::mock(ResponseInterface::class); $postResponse = m::mock(ResponseInterface::class);
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'text/html; charset=UTF-8']); $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'text/html; charset=UTF-8']);
@ -143,8 +134,7 @@ class ProviderTest extends \PHPUnit_Framework_TestCase
$this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
} }
public function testGetResourceOwner() public function testGetResourceOwner() {
{
/** @var m\Mock|ResponseInterface $postResponse */ /** @var m\Mock|ResponseInterface $postResponse */
$postResponse = m::mock(ResponseInterface::class); $postResponse = m::mock(ResponseInterface::class);
$postResponse->shouldReceive('getBody')->andReturn($this->getAccessTokenResponse()); $postResponse->shouldReceive('getBody')->andReturn($this->getAccessTokenResponse());
@ -162,8 +152,8 @@ class ProviderTest extends \PHPUnit_Framework_TestCase
/** @var m\Mock|ClientInterface $client */ /** @var m\Mock|ClientInterface $client */
$client = m::mock(ClientInterface::class); $client = m::mock(ClientInterface::class);
$client->shouldReceive('send') $client->shouldReceive('send')
->times(2) ->times(2)
->andReturn($postResponse, $userResponse); ->andReturn($postResponse, $userResponse);
$this->provider->setHttpClient($client); $this->provider->setHttpClient($client);
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
@ -172,12 +162,12 @@ class ProviderTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf(ResourceOwner::class, $user); $this->assertInstanceOf(ResourceOwner::class, $user);
} }
private function getAccessTokenResponse() private function getAccessTokenResponse() {
{
return json_encode([ return json_encode([
'access_token' => 'mock_access_token', 'access_token' => 'mock_access_token',
'token_type' => 'bearer', 'token_type' => 'bearer',
'expires_in' => 3600, 'expires_in' => 3600,
]); ]);
} }
} }

View File

@ -3,53 +3,44 @@ namespace Ely\OAuth2\Client\Test;
use Ely\OAuth2\Client\ResourceOwner; use Ely\OAuth2\Client\ResourceOwner;
class ResourceOwnerTest extends \PHPUnit_Framework_TestCase class ResourceOwnerTest extends \PHPUnit_Framework_TestCase {
{
public function testGetId() public function testGetId() {
{
$this->assertEquals(1, $this->createModel()->getId()); $this->assertEquals(1, $this->createModel()->getId());
} }
public function testGetUuid() public function testGetUuid() {
{
$this->assertEquals('ffc8fdc9-5824-509e-8a57-c99b940fb996', $this->createModel()->getUuid()); $this->assertEquals('ffc8fdc9-5824-509e-8a57-c99b940fb996', $this->createModel()->getUuid());
} }
public function testGetUsername() public function testGetUsername() {
{
$this->assertEquals('ErickSkrauch', $this->createModel()->getUsername()); $this->assertEquals('ErickSkrauch', $this->createModel()->getUsername());
} }
public function testGetEmail() public function testGetEmail() {
{
$this->assertEquals('erickskrauch@ely.by', $this->createModel()->getEmail()); $this->assertEquals('erickskrauch@ely.by', $this->createModel()->getEmail());
$this->assertNull($this->createModelWithoutEmail()->getEmail()); $this->assertNull($this->createModelWithoutEmail()->getEmail());
} }
public function testGetRegisteredAt() public function testGetRegisteredAt() {
{
$registeredAt = $this->createModel()->getRegisteredAt(); $registeredAt = $this->createModel()->getRegisteredAt();
$this->assertInstanceOf(\DateTime::class, $registeredAt); $this->assertInstanceOf(\DateTime::class, $registeredAt);
$this->assertEquals(1470566470, $registeredAt->getTimestamp()); $this->assertEquals(1470566470, $registeredAt->getTimestamp());
} }
public function testGetProfileLink() public function testGetProfileLink() {
{
$this->assertEquals('http://ely.by/u1', $this->createModel()->getProfileLink()); $this->assertEquals('http://ely.by/u1', $this->createModel()->getProfileLink());
} }
public function testGetPreferredLanguage() public function testGetPreferredLanguage() {
{
$this->assertEquals('be', $this->createModel()->getPreferredLanguage()); $this->assertEquals('be', $this->createModel()->getPreferredLanguage());
} }
public function testGetSkinUrl() public function testGetSkinUrl() {
{
$this->assertEquals('http://skinsystem.ely.by/skins/ErickSkrauch.png', $this->createModel()->getSkinUrl()); $this->assertEquals('http://skinsystem.ely.by/skins/ErickSkrauch.png', $this->createModel()->getSkinUrl());
} }
public function testToArray() public function testToArray() {
{
$array = $this->createModel()->toArray(); $array = $this->createModel()->toArray();
$this->assertTrue(is_array($array)); $this->assertTrue(is_array($array));
$this->assertEquals(1, $array['id']); $this->assertEquals(1, $array['id']);
@ -64,21 +55,19 @@ class ResourceOwnerTest extends \PHPUnit_Framework_TestCase
$this->assertArrayNotHasKey('email', $array); $this->assertArrayNotHasKey('email', $array);
} }
private function createModelWithoutEmail() private function createModelWithoutEmail() {
{
$params = $this->getAllResponseParams(); $params = $this->getAllResponseParams();
unset($params['email']); unset($params['email']);
return new ResourceOwner($params); return new ResourceOwner($params);
} }
private function createModel() private function createModel() {
{
return new ResourceOwner($this->getAllResponseParams()); return new ResourceOwner($this->getAllResponseParams());
} }
private function getAllResponseParams() private function getAllResponseParams() {
{
return json_decode(file_get_contents(__DIR__ . '/data/identity-info-response.json'), true); return json_decode(file_get_contents(__DIR__ . '/data/identity-info-response.json'), true);
} }
} }