diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..4be7bbd --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,42 @@ +name: tests + +on: + push: + pull_request: + +jobs: + linux_tests: + runs-on: ubuntu-20.04 + + strategy: + matrix: + php: ['8.1', '8.2'] + stability: ['prefer-lowest', 'prefer-stable'] + + name: PHP ${{ matrix.php }} - ${{ matrix.stability }} + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer:v2 + coverage: none + + - name: Check for syntax errors in PHP files + run: find ./ -type f -name '*.php' -print0 | xargs -0 -L1 -P4 -- php -l + + - name: Check composer files + run: composer validate --strict + + - name: Install dependencies + run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress + + - name: Execute tests + run: vendor/bin/phpunit --verbose + + - name: Check coding standard + run: vendor/bin/php-cs-fixer --no-interaction --dry-run --diff -v fix lib/ \ No newline at end of file diff --git a/.gitignore b/.gitignore index 987e2a2..fe8f41c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ composer.lock vendor +/.phpunit.cache +/.phpunit.result.cache +/.php-cs-fixer.cache diff --git a/composer.json b/composer.json index 63e9f5f..eb654b9 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "A PHP client to use the Ticketpark API", "type": "library", "require": { - "php": ">=5.6", + "php": ">=8.1", "kriswallsmith/buzz": "^0.15.0" }, "license": "MIT", @@ -15,6 +15,14 @@ ], "minimum-stability": "stable", "autoload": { - "psr-4": { "Ticketpark\\ApiClient\\": "lib/ApiClient" } + "psr-4": { + "Ticketpark\\ApiClient\\": "lib/ApiClient", + "Ticketpark\\ApiClient\\Test\\": "test/ApiClient" + } + }, + "require-dev": { + "phpunit/phpunit": "^9.6", + "rector/rector": "^0.18.3", + "friendsofphp/php-cs-fixer": "^3.27" } } diff --git a/lib/ApiClient/Exception/TokenGenerationException.php b/lib/ApiClient/Exception/TokenGenerationException.php index deda847..4996d4f 100644 --- a/lib/ApiClient/Exception/TokenGenerationException.php +++ b/lib/ApiClient/Exception/TokenGenerationException.php @@ -4,5 +4,4 @@ class TokenGenerationException extends \Exception { - -} \ No newline at end of file +} diff --git a/lib/ApiClient/TicketparkApiClient.php b/lib/ApiClient/TicketparkApiClient.php index 3ee1c8d..ab80970 100644 --- a/lib/ApiClient/TicketparkApiClient.php +++ b/lib/ApiClient/TicketparkApiClient.php @@ -3,192 +3,80 @@ namespace Ticketpark\ApiClient; use Buzz\Browser; +use Buzz\Client\Curl; +use Buzz\Message\Response; use Ticketpark\ApiClient\Exception\TokenGenerationException; use Ticketpark\ApiClient\Token\AccessToken; use Ticketpark\ApiClient\Token\RefreshToken; class TicketparkApiClient { - /** - * @const string ROOT_URL - */ - const ROOT_URL = 'https://api.ticketpark.ch'; - - /** - * @const int REFRESH_TOKEN_LIFETIME - */ - const REFRESH_TOKEN_LIFETIME = 30 * 86400; - - /** - * @var string $apiKey - */ - protected $apiKey; - - /** - * @var string $apiSecret - */ - protected $apiSecret; - - /** - * @var string $username - */ - protected $username; - - /** - * @var string $password - */ - protected $password; - - /** - * @var Browser - */ - protected $browser; - - /** - * @var RefreshToken - */ - protected $refreshToken; - - /** - * @var AccessToken - */ - protected $accessToken; - - /** - * Constructor - * - * @param string $apiKey - * @param string $apiSecret - */ - public function __construct($apiKey, $apiSecret) - { - $this->apiKey = $apiKey; - $this->apiSecret = $apiSecret; + private const ROOT_URL = 'https://api.ticketpark.ch'; + private const REFRESH_TOKEN_LIFETIME = 30 * 86400; + + private ?string $username = null; + private ?string $password = null; + private ?Browser $browser = null; + private ?RefreshToken $refreshToken = null; + private ?AccessToken $accessToken = null; + + public function __construct( + private readonly string $apiKey, + private readonly string $apiSecret + ) { } - /** - * Set browser - * - * @param Browser $browser - * @return $this - */ - public function setBrowser(Browser $browser = null) + public function setBrowser(Browser $browser = null): void { $this->browser = $browser; - - return $this; } - /** - * Get browser - * - * @return \Buzz\Browser - */ - public function getBrowser() + public function getBrowser(): Browser { - if (null == $this->browser) { - $this->browser = new \Buzz\Browser(new \Buzz\Client\Curl()); + if (null === $this->browser) { + $this->browser = new Browser(new Curl()); } return $this->browser; } - /** - * Set user credentials - * - * @param string $username - * @param string $password - */ - public function setUserCredentials($username, $password) + public function setUserCredentials(string $username, string $password): void { $this->username = $username; $this->password = $password; - - return $this; } - /** - * Get access token - * - * @return AccessToken - */ - public function getAccessToken() + public function getAccessToken(): ?AccessToken { return $this->accessToken; } - /** - * Set access token object - * - * @param AccessToken $accessToken - * @return $this - */ - public function setAccessTokenInstance(AccessToken $accessToken) + public function setAccessTokenInstance(AccessToken $accessToken): void { $this->accessToken = $accessToken; - - return $this; } - /** - * Set access token - * - * @param string $accessToken - * @return $this - */ - public function setAccessToken($accessToken) + public function setAccessToken(string $accessToken): void { $this->accessToken = new AccessToken($accessToken); - - return $this; } - /** - * Get refresh token - * - * @return RefreshToken - */ - public function getRefreshToken() + public function getRefreshToken(): ?RefreshToken { return $this->refreshToken; } - /** - * Set refresh token object - * - * @param RefreshToken $refreshToken - * @return $this - */ - public function setRefreshTokenInstance(RefreshToken $refreshToken) + public function setRefreshTokenInstance(RefreshToken $refreshToken): void { $this->refreshToken = $refreshToken; - - return $this; } - - /** - * Set refresh token - * - * @param RefreshToken $refreshToken - * @return $this - */ - public function setRefreshToken($refreshToken) + public function setRefreshToken(string $refreshToken): void { $this->refreshToken = new RefreshToken($refreshToken); - - return $this; } - /** - * GET - * - * @param string $path - * @param array $parameters - * @param array $headers - * @return \Buzz\Message\Response - */ - public function get($path, $parameters = array(), $headers = array()) + public function get(string $path, array $parameters = [], array $headers = []): Response { $params = ''; if (count($parameters)) { @@ -198,27 +86,12 @@ public function get($path, $parameters = array(), $headers = array()) return $this->getBrowser()->get(self::ROOT_URL . $path . $params, $this->getDefaultHeaders($headers)); } - /** - * POST - * - * @param string $path - * @param string $content - * @param array $headers - * @return \Buzz\Message\Response - */ - public function post($path, $content = '', $headers = array()) + public function post(string $path, string $content = '', array $headers = []): Response { - return $this->getBrowser()->post(self::ROOT_URL . $path, $this->getDefaultHeaders($headers), json_encode($content)); + return $this->getBrowser()->post(self::ROOT_URL . $path, $this->getDefaultHeaders($headers), json_encode($content, JSON_THROW_ON_ERROR)); } - /** - * HEAD - * - * @param string $path - * @param array $headers - * @return \Buzz\Message\Response - */ - public function head($path, $parameters = array(), $headers = array()) + public function head($path, array $parameters = [], array $headers = []): Response { $params = ''; if (count($parameters)) { @@ -228,58 +101,25 @@ public function head($path, $parameters = array(), $headers = array()) return $this->getBrowser()->head(self::ROOT_URL . $path . $params, $this->getDefaultHeaders($headers)); } - /** - * PATCH - * - * @param string $path - * @param string $content - * @param array $headers - * @return \Buzz\Message\Response - */ - public function patch($path, $content = '', $headers = array()) + public function patch(string $path, string $content = '', array $headers = []): Response { - return $this->getBrowser()->patch(self::ROOT_URL . $path, $this->getDefaultHeaders($headers), json_encode($content)); + return $this->getBrowser()->patch(self::ROOT_URL . $path, $this->getDefaultHeaders($headers), json_encode($content, JSON_THROW_ON_ERROR)); } - /** - * PUT - * - * @param string $path - * @param string $content - * @param array $headers - * @return \Buzz\Message\MessageInterface - */ - public function put($path, $content = '', $headers = array()) - { - return $this->getBrowser()->put(self::ROOT_URL . $path, $this->getDefaultHeaders($headers), json_encode($content)); - } - - /** - * DELETE - * - * @param string $path - * @param array $headers - * @return \Buzz\Message\Response - */ - public function delete($path, $headers = array()) + public function delete(string $path, array $headers = []): Response { return $this->getBrowser()->delete(self::ROOT_URL . $path, $this->getDefaultHeaders($headers)); } - /** - * Generate new tokens - * - * @throws TokenGenerationException - */ - public function generateTokens() + public function generateTokens(): void { // Try with refresh token $refreshToken = $this->getRefreshToken(); if ($refreshToken && !$refreshToken->hasExpired()) { - $data = array( + $data = [ 'refresh_token' => $refreshToken->getToken(), 'grant_type' => 'refresh_token' - ); + ]; if ($this->doGenerateTokens($data)) { return; @@ -288,11 +128,11 @@ public function generateTokens() // Try with user credentials if (!isset($data) && $this->username) { - $data = array( + $data = [ 'username' => $this->username, 'password' => $this->password, 'grant_type' => 'password' - ); + ]; if ($this->doGenerateTokens($data)) { return; @@ -302,28 +142,14 @@ public function generateTokens() throw new TokenGenerationException('Failed to generate a access tokens. Make sure to provide a valid refresh token or user credentials.'); } - /** - * Get headers - * - * @return array - */ - protected function getDefaultHeaders($customHeaders = array()) + private function getDefaultHeaders(array $customHeaders = []): array { - $headers = array( - 'Content-Type' => 'application/json', - 'Accept' => 'application/json', - 'Authorization' => 'Bearer ' . $this->getValidAccessToken() - ); + $headers = ['Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $this->getValidAccessToken()]; return array_merge($customHeaders, $headers); } - /** - * Get a valid access token - * - * @return string - */ - protected function getValidAccessToken() + private function getValidAccessToken(): string { $accessToken = $this->getAccessToken(); @@ -335,26 +161,18 @@ protected function getValidAccessToken() return $accessToken->getToken(); } - /** - * Actually tries to generate tokens. - * - * Returns whether token generation has been successful. - * - * @param array $data - * @return bool - */ - protected function doGenerateTokens(array $data) + protected function doGenerateTokens(array $data): bool { - $headers = array( + $headers = [ 'Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => 'application/json', 'Authorization' => 'Basic '.base64_encode($this->apiKey . ':' . $this->apiSecret) - ); + ]; $response = $this->getBrowser()->post(self::ROOT_URL . '/oauth/v2/token', $headers, $data); if (200 == $response->getStatusCode()) { - $response = json_decode($response->getContent(), true); + $response = json_decode((string) $response->getContent(), true, 512, JSON_THROW_ON_ERROR); $this->accessToken = new AccessToken( $response['access_token'], @@ -371,4 +189,4 @@ protected function doGenerateTokens(array $data) return false; } -} \ No newline at end of file +} diff --git a/lib/ApiClient/Token/AccessToken.php b/lib/ApiClient/Token/AccessToken.php index 84736b7..828921d 100644 --- a/lib/ApiClient/Token/AccessToken.php +++ b/lib/ApiClient/Token/AccessToken.php @@ -4,5 +4,4 @@ class AccessToken extends Token { - -} \ No newline at end of file +} diff --git a/lib/ApiClient/Token/RefreshToken.php b/lib/ApiClient/Token/RefreshToken.php index 746b51c..d201675 100644 --- a/lib/ApiClient/Token/RefreshToken.php +++ b/lib/ApiClient/Token/RefreshToken.php @@ -4,5 +4,4 @@ class RefreshToken extends Token { - -} \ No newline at end of file +} diff --git a/lib/ApiClient/Token/Token.php b/lib/ApiClient/Token/Token.php index 45280ae..2c34258 100644 --- a/lib/ApiClient/Token/Token.php +++ b/lib/ApiClient/Token/Token.php @@ -4,75 +4,25 @@ abstract class Token { - /** - * Seconds to consider token as expired before it actually expires - * - * @const int TIMEOUT_BUFFER - */ - const TIMEOUT_BUFFER = 10; + private const TIMEOUT_BUFFER = 10; - /** - * @var string $token - */ - protected $token; - - /** - * @var \DateTime - */ - protected $expiration; - - /** - * @return string - */ - public function getToken() - { - return $this->token; - } - - /** - * @param string|null $token - * @param \DateTime|null $expiration - */ - public function __construct($token = null, \DateTime $expiration = null) - { - $this->token = $token; - $this->expiration = $expiration; + public function __construct( + private readonly string $token, + private readonly ?\DateTime $expiration = null + ) { } - /** - * @param string $token - * @return Token - */ - public function setToken($token) + public function getToken(): string { - $this->token = $token; - - return $this; + return $this->token; } - /** - * @return \DateTime - */ - public function getExpiration() + public function getExpiration(): ?\DateTime { return $this->expiration; } - /** - * @param \DateTime $expiration - * @return Token - */ - public function setExpiration(\DateTime $expiration) - { - $this->expiration = $expiration; - - return $this; - } - - /** - * @return bool - */ - public function hasExpired() + public function hasExpired(): bool { if (null == $this->getExpiration()) { @@ -81,4 +31,4 @@ public function hasExpired() return $this->getExpiration()->getTimestamp() < (time() + self::TIMEOUT_BUFFER); } -} \ No newline at end of file +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5984109..2e53e5a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,9 +1,9 @@ - - - - - ./test/ApiClient/ - - + + + + + ./test + + diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..199185a --- /dev/null +++ b/rector.php @@ -0,0 +1,17 @@ +paths([ + __DIR__ . '/lib', + __DIR__ . '/test', + ]); + + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_81 + ]); +}; diff --git a/test/ApiClient/TicketparkApiClientTest.php b/test/ApiClient/TicketparkApiClientTest.php index 7983ea7..7cbe205 100644 --- a/test/ApiClient/TicketparkApiClientTest.php +++ b/test/ApiClient/TicketparkApiClientTest.php @@ -2,96 +2,91 @@ namespace Ticketpark\ApiClient\Test; +use Buzz\Browser; +use Buzz\Message\Response; +use PHPUnit\Framework\TestCase; +use Ticketpark\ApiClient\Exception\TokenGenerationException; use Ticketpark\ApiClient\TicketparkApiClient; use Ticketpark\ApiClient\Token\AccessToken; use Ticketpark\ApiClient\Token\RefreshToken; -class TicketparkApiClientTest extends \PHPUnit_Framework_TestCase +class TicketparkApiClientTest extends TestCase { protected $apiClient; - public function setUp() + public function setUp(): void { $this->apiClient = new TicketparkApiClient('apiKey', 'apiSecret'); } public function testDefaultBrowser() { - $this->assertInstanceOf('Buzz\Browser', $this->apiClient->getBrowser()); - } - - public function testSetValidBrowser() - { - $this->assertInstanceOf('Ticketpark\ApiClient\TicketparkApiClient', $this->apiClient->setBrowser($this->getBrowserMock())); - $this->assertEquals('testBrowser', $this->apiClient->getBrowser()->getTestName()); + $this->assertInstanceOf(Browser::class, $this->apiClient->getBrowser()); } public function testSetAccessToken() { $this->apiClient->setAccessToken('foo'); - $this->assertInstanceOf('Ticketpark\ApiClient\Token\AccessToken', $this->apiClient->getAccessToken()); + $this->assertInstanceOf(AccessToken::class, $this->apiClient->getAccessToken()); $this->assertEquals('foo', $this->apiClient->getAccessToken()->getToken()); } public function testSetAccessTokenInstance() { - $accessToken = new AccessToken(); - $accessToken->setToken('bar'); + $accessToken = new AccessToken('bar'); $this->apiClient->setAccessTokenInstance($accessToken); - $this->assertInstanceOf('Ticketpark\ApiClient\Token\AccessToken', $this->apiClient->getAccessToken()); + $this->assertInstanceOf(AccessToken::class, $this->apiClient->getAccessToken()); $this->assertEquals('bar', $this->apiClient->getAccessToken()->getToken()); } public function testSetRefreshToken() { $this->apiClient->setRefreshToken('foo'); - $this->assertInstanceOf('Ticketpark\ApiClient\Token\RefreshToken', $this->apiClient->getRefreshToken()); + $this->assertInstanceOf(RefreshToken::class, $this->apiClient->getRefreshToken()); $this->assertEquals('foo', $this->apiClient->getRefreshToken()->getToken()); } public function testSetRefreshTokenInstance() { - $refreshToken = new RefreshToken(); - $refreshToken->setToken('bar'); + $refreshToken = new RefreshToken('bar'); $this->apiClient->setRefreshTokenInstance($refreshToken); - $this->assertInstanceOf('Ticketpark\ApiClient\Token\RefreshToken', $this->apiClient->getRefreshToken()); + $this->assertInstanceOf(RefreshToken::class, $this->apiClient->getRefreshToken()); $this->assertEquals('bar', $this->apiClient->getRefreshToken()->getToken()); } - /** - * @expectedException Ticketpark\ApiClient\Exception\TokenGenerationException - */ public function testGenerateTokensWithoutData() { + $this->expectException(TokenGenerationException::class); + $this->apiClient->generateTokens(); } public function testGenerateTokensWithUserCredentials() { - $this->apiClient->setBrowser($this->getBrowserMock(array( - 'post' => array( + $this->apiClient->setBrowser($this->getBrowserMock([ + 'post' => [ 'expects' => $this->once(), - 'with' => array( + 'with' => [ $this->equalTo('https://api.ticketpark.ch/oauth/v2/token'), $this->anything(), - $this->equalTo(array( + $this->equalTo([ 'username' => 'username', 'password' => 'password', 'grant_type' => 'password' - )), - ), - 'response' => array( + ]), + ], + 'response' => [ 'status' => 200, - 'content' => array( + 'content' => [ 'access_token' => 'accessToken', 'refresh_token' => 'refreshToken', 'expires_in' => 60 - ) - ) - ) - ))); + ] + ] + ] + ])); $this->apiClient->setUserCredentials('username', 'password'); $this->apiClient->generateTokens(); @@ -100,29 +95,28 @@ public function testGenerateTokensWithUserCredentials() $this->assertEquals('refreshToken', $this->apiClient->getRefreshToken()->getToken()); } - /** - * @expectedException Ticketpark\ApiClient\Exception\TokenGenerationException - */ public function testGenerateTokensWithUserCredentialsFails() { - $this->apiClient->setBrowser($this->getBrowserMock(array( - 'post' => array( + $this->expectException(TokenGenerationException::class); + + $this->apiClient->setBrowser($this->getBrowserMock([ + 'post' => [ 'expects' => $this->once(), - 'with' => array( + 'with' => [ $this->equalTo('https://api.ticketpark.ch/oauth/v2/token'), $this->anything(), - $this->equalTo(array( + $this->equalTo([ 'username' => 'username', 'password' => 'password', 'grant_type' => 'password' - )), - ), - 'response' => array( + ]), + ], + 'response' => [ 'status' => 400, 'content' => '' - ) - ) - ))); + ] + ] + ])); $this->apiClient->setUserCredentials('username', 'password'); $this->apiClient->generateTokens(); @@ -130,27 +124,27 @@ public function testGenerateTokensWithUserCredentialsFails() public function testGenerateTokensWithRefreshToken() { - $this->apiClient->setBrowser($this->getBrowserMock(array( - 'post' => array( + $this->apiClient->setBrowser($this->getBrowserMock([ + 'post' => [ 'expects' => $this->once(), - 'with' => array( + 'with' => [ $this->equalTo('https://api.ticketpark.ch/oauth/v2/token'), $this->anything(), - $this->equalTo(array( + $this->equalTo([ 'refresh_token' => 'mySavedRefreshToken', 'grant_type' => 'refresh_token' - )), - ), - 'response' => array( + ]), + ], + 'response' => [ 'status' => 200, - 'content' => array( + 'content' => [ 'access_token' => 'accessToken', 'refresh_token' => 'refreshToken', 'expires_in' => 60 - ) - ) - ) - ))); + ] + ] + ] + ])); $this->apiClient->setRefreshToken('mySavedRefreshToken'); $this->apiClient->generateTokens(); @@ -159,28 +153,27 @@ public function testGenerateTokensWithRefreshToken() $this->assertEquals('refreshToken', $this->apiClient->getRefreshToken()->getToken()); } - /** - * @expectedException Ticketpark\ApiClient\Exception\TokenGenerationException - */ public function testGenerateTokensWithRefreshTokenFails() { - $this->apiClient->setBrowser($this->getBrowserMock(array( - 'post' => array( + $this->expectException(TokenGenerationException::class); + + $this->apiClient->setBrowser($this->getBrowserMock([ + 'post' => [ 'expects' => $this->once(), - 'with' => array( + 'with' => [ $this->equalTo('https://api.ticketpark.ch/oauth/v2/token'), $this->anything(), - $this->equalTo(array( + $this->equalTo([ 'refresh_token' => 'mySavedRefreshToken', 'grant_type' => 'refresh_token' - )), - ), - 'response' => array( + ]), + ], + 'response' => [ 'status' => 400, 'content' => '' - ) - ) - ))); + ] + ] + ])); $this->apiClient->setRefreshToken('mySavedRefreshToken'); $this->apiClient->generateTokens(); @@ -188,173 +181,142 @@ public function testGenerateTokensWithRefreshTokenFails() public function testGet() { - $this->apiClient->setBrowser($this->getBrowserMock(array( - 'get' => array( + $this->apiClient->setBrowser($this->getBrowserMock([ + 'get' => [ 'expects' => $this->once(), - 'with' => array( + 'with' => [ $this->equalTo('https://api.ticketpark.ch/shows?a=1&b=2&c%5Bd%5D=3'), - $this->equalTo(array( + $this->equalTo([ 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer myAccessToken', 'CustomHeader' => 'foo' - )) - ), - 'response' => array( + ]) + ], + 'response' => [ 'status' => 200, 'content' => '' - ) - ), + ] + ], - ))); + ])); $this->apiClient->setAccessToken('myAccessToken'); - $this->apiClient->get('/shows', array('a' => 1, 'b' => 2, 'c' => array('d' => 3)), array('CustomHeader' => 'foo')); + $this->apiClient->get('/shows', ['a' => 1, 'b' => 2, 'c' => ['d' => 3]], ['CustomHeader' => 'foo']); } public function testHead() { - $this->apiClient->setBrowser($this->getBrowserMock(array( - 'head' => array( + $this->apiClient->setBrowser($this->getBrowserMock([ + 'head' => [ 'expects' => $this->once(), - 'with' => array( + 'with' => [ $this->equalTo('https://api.ticketpark.ch/shows?a=1&b=2&c%5Bd%5D=3'), - $this->equalTo(array( + $this->equalTo([ 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer myAccessToken', 'CustomHeader' => 'foo' - )) - ), - 'response' => array( + ]) + ], + 'response' => [ 'status' => 200, 'content' => '' - ) - ), + ] + ], - ))); + ])); $this->apiClient->setAccessToken('myAccessToken'); - $this->apiClient->head('/shows', array('a' => 1, 'b' => 2, 'c' => array('d' => 3)), array('CustomHeader' => 'foo')); + $this->apiClient->head('/shows', ['a' => 1, 'b' => 2, 'c' => ['d' => 3]], ['CustomHeader' => 'foo']); } public function testPatch() { - $this->apiClient->setBrowser($this->getBrowserMock(array( - 'patch' => array( + $this->apiClient->setBrowser($this->getBrowserMock([ + 'patch' => [ 'expects' => $this->once(), - 'with' => array( + 'with' => [ $this->equalTo('https://api.ticketpark.ch/shows/foo'), - $this->equalTo(array( + $this->equalTo([ 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer myAccessToken', 'CustomHeader' => 'foo' - )), + ]), $this->equalTo('"content"') - ), - 'response' => array( + ], + 'response' => [ 'status' => 200, 'content' => '' - ) - ), + ] + ], - ))); + ])); $this->apiClient->setAccessToken('myAccessToken'); - $this->apiClient->patch('/shows/foo', 'content', array('CustomHeader' => 'foo')); + $this->apiClient->patch('/shows/foo', 'content', ['CustomHeader' => 'foo']); } public function testPost() { - $this->apiClient->setBrowser($this->getBrowserMock(array( - 'post' => array( - 'expects' => $this->once(), - 'with' => array( - $this->equalTo('https://api.ticketpark.ch/shows/foo'), - $this->equalTo(array( - 'Content-Type' => 'application/json', - 'Accept' => 'application/json', - 'Authorization' => 'Bearer myAccessToken', - 'CustomHeader' => 'foo' - )), - $this->equalTo('"content"') - ), - 'response' => array( - 'status' => 200, - 'content' => '' - ) - ), - - ))); - - $this->apiClient->setAccessToken('myAccessToken'); - $this->apiClient->post('/shows/foo', 'content', array('CustomHeader' => 'foo')); - } - - public function testPut() - { - $this->apiClient->setBrowser($this->getBrowserMock(array( - 'put' => array( + $this->apiClient->setBrowser($this->getBrowserMock([ + 'post' => [ 'expects' => $this->once(), - 'with' => array( + 'with' => [ $this->equalTo('https://api.ticketpark.ch/shows/foo'), - $this->equalTo(array( + $this->equalTo([ 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer myAccessToken', 'CustomHeader' => 'foo' - )), + ]), $this->equalTo('"content"') - ), - 'response' => array( + ], + 'response' => [ 'status' => 200, 'content' => '' - ) - ), + ] + ], - ))); + ])); $this->apiClient->setAccessToken('myAccessToken'); - $this->apiClient->put('/shows/foo', 'content', array('CustomHeader' => 'foo')); + $this->apiClient->post('/shows/foo', 'content', ['CustomHeader' => 'foo']); } public function testDelete() { - $this->apiClient->setBrowser($this->getBrowserMock(array( - 'delete' => array( + $this->apiClient->setBrowser($this->getBrowserMock([ + 'delete' => [ 'expects' => $this->once(), - 'with' => array( + 'with' => [ $this->equalTo('https://api.ticketpark.ch/shows/foo'), - $this->equalTo(array( + $this->equalTo([ 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer myAccessToken', 'CustomHeader' => 'foo' - )) - ), - 'response' => array( + ]) + ], + 'response' => [ 'status' => 200, 'content' => '' - ) - ), + ] + ], - ))); + ])); $this->apiClient->setAccessToken('myAccessToken'); - $this->apiClient->delete('/shows/foo', array('CustomHeader' => 'foo')); + $this->apiClient->delete('/shows/foo', ['CustomHeader' => 'foo']); } - protected function getBrowserMock($data = array()) + protected function getBrowserMock($data = []) { - $browser = $this->getMockBuilder('Buzz\Browser') - ->setMethods(array('getTestName', 'head', 'get', 'post', 'patch', 'put', 'delete')) + $browser = $this->getMockBuilder(Browser::class) + ->onlyMethods(['head', 'get', 'post', 'patch', 'delete']) ->getMock(); - $browser - ->method('getTestName') - ->willReturn('testBrowser'); - foreach($data as $method => $params) { $browser ->expects($params['expects']) @@ -368,8 +330,8 @@ protected function getBrowserMock($data = array()) protected function getResponseMock($status, $content) { - $response = $this->getMockBuilder('Buzz\Message\Response') - ->setMethods(array('getStatusCode', 'getContent')) + $response = $this->getMockBuilder(Response::class) + ->onlyMethods(['getStatusCode', 'getContent']) ->getMock(); $response @@ -378,8 +340,8 @@ protected function getResponseMock($status, $content) $response ->method('getContent') - ->willReturn(json_encode($content)); + ->willReturn(json_encode($content, JSON_THROW_ON_ERROR)); return $response; } -} \ No newline at end of file +}