diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml index 64e4a12..261e377 100644 --- a/.github/workflows/coding-standards.yml +++ b/.github/workflows/coding-standards.yml @@ -37,3 +37,9 @@ jobs: - name: "Run PHP CS Fixer" run: "vendor/bin/php-cs-fixer fix --verbose --dry-run" + + - name: "Install PHPUnit for PHPStan" + run: "php vendor/bin/simple-phpunit install" + + - name: "Run PHPStan" + run: "php vendor/bin/phpstan analyse --no-progress" diff --git a/composer.json b/composer.json index 135988e..eb29e2b 100644 --- a/composer.json +++ b/composer.json @@ -40,6 +40,10 @@ }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.0", + "phpstan/extension-installer": "^1.1", + "phpstan/phpstan": "^1.2", + "phpstan/phpstan-deprecation-rules": "^1.0", + "phpstan/phpstan-phpunit": "^1.0", "symfony/phpunit-bridge": "^6.0" }, "config": { diff --git a/lib/Imgur/Api/AbstractApi.php b/lib/Imgur/Api/AbstractApi.php index 57a6b3b..fbf7120 100644 --- a/lib/Imgur/Api/AbstractApi.php +++ b/lib/Imgur/Api/AbstractApi.php @@ -14,12 +14,12 @@ abstract class AbstractApi { /** - * @var \Imgur\Client + * @var Client */ protected $client; /** - * @var \Imgur\Pager\PagerInterface + * @var PagerInterface */ protected $pager; @@ -31,16 +31,12 @@ public function __construct(Client $client, PagerInterface $pager = null) /** * Perform a GET request and return the parsed response. - * - * @param string $url - * - * @return array */ - public function get($url, $parameters = []) + public function get(string $url, array $parameters = []): array { $httpClient = $this->client->getHttpClient(); - if (!empty($this->pager)) { + if (null !== $this->pager) { $parameters['page'] = $this->pager->getPage(); $parameters['perPage'] = $this->pager->getResultsPerPage(); } @@ -52,12 +48,8 @@ public function get($url, $parameters = []) /** * Perform a POST request and return the parsed response. - * - * @param string $url - * - * @return array */ - public function post($url, $parameters = []) + public function post(string $url, array $parameters = []): array { $httpClient = $this->client->getHttpClient(); @@ -68,12 +60,8 @@ public function post($url, $parameters = []) /** * Perform a PUT request and return the parsed response. - * - * @param string $url - * - * @return array */ - public function put($url, $parameters = []) + public function put(string $url, array $parameters = []): array { $httpClient = $this->client->getHttpClient(); @@ -84,12 +72,8 @@ public function put($url, $parameters = []) /** * Perform a DELETE request and return the parsed response. - * - * @param string $url - * - * @return array */ - public function delete($url, $parameters = []) + public function delete(string $url, array $parameters = []): array { $httpClient = $this->client->getHttpClient(); @@ -101,10 +85,9 @@ public function delete($url, $parameters = []) /** * Validate "sort" parameter and throw an exception if it's a bad value. * - * @param string $sort Input value - * @param array $possibleValues + * @param string $sort Input value */ - protected function validateSortArgument($sort, $possibleValues) + protected function validateSortArgument(string $sort, array $possibleValues): void { $this->validateArgument('Sort', $sort, $possibleValues); } @@ -112,10 +95,9 @@ protected function validateSortArgument($sort, $possibleValues) /** * Validate "window" parameter and throw an exception if it's a bad value. * - * @param string $window Input value - * @param array $possibleValues + * @param string $window Input value */ - protected function validateWindowArgument($window, $possibleValues) + protected function validateWindowArgument(string $window, array $possibleValues): void { $this->validateArgument('Window', $window, $possibleValues); } @@ -123,10 +105,9 @@ protected function validateWindowArgument($window, $possibleValues) /** * Validate "vote" parameter and throw an exception if it's a bad value. * - * @param string $vote Input value - * @param array $possibleValues + * @param string $vote Input value */ - protected function validateVoteArgument($vote, $possibleValues) + protected function validateVoteArgument(string $vote, array $possibleValues): void { $this->validateArgument('Vote', $vote, $possibleValues); } @@ -138,7 +119,7 @@ protected function validateVoteArgument($vote, $possibleValues) * @param string $input Input value * @param array $possibleValues Possible values for this argument */ - private function validateArgument($type, $input, $possibleValues) + private function validateArgument(string $type, string $input, array $possibleValues): void { if (!\in_array($input, $possibleValues, true)) { throw new InvalidArgumentException($type . ' parameter "' . $input . '" is wrong. Possible values are: ' . implode(', ', $possibleValues)); diff --git a/lib/Imgur/Api/Account.php b/lib/Imgur/Api/Account.php index 33656a4..27ff5a6 100644 --- a/lib/Imgur/Api/Account.php +++ b/lib/Imgur/Api/Account.php @@ -31,7 +31,7 @@ public function base($username = 'me') * * @param string $username * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function deleteAccount($username) { @@ -106,7 +106,7 @@ public function settings($username = 'me') * * @see https://api.imgur.com/endpoints/account#update-settings * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function changeAccountSettings($parameters) { @@ -147,7 +147,7 @@ public function accountGalleryProfile($username = 'me') * * @see https://api.imgur.com/endpoints/account#verify-email * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function verifyUsersEmail($username = 'me') { @@ -161,7 +161,7 @@ public function verifyUsersEmail($username = 'me') * * @see https://api.imgur.com/endpoints/account#send-verify-email * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function sendVerificationEmail($username = 'me') { @@ -207,7 +207,7 @@ public function album($albumId, $username = 'me') * * @see https://api.imgur.com/endpoints/account#album-ids * - * @return array + * @return array (@see https://api.imgur.com/models/basic) */ public function albumIds($username = 'me', $page = 0) { @@ -221,7 +221,7 @@ public function albumIds($username = 'me', $page = 0) * * @see https://api.imgur.com/endpoints/account#album-count * - * @return int + * @return array (@see https://api.imgur.com/models/basic) */ public function albumCount($username = 'me') { @@ -236,7 +236,7 @@ public function albumCount($username = 'me') * * @see https://api.imgur.com/endpoints/account#album-delete * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function albumDelete($albumId, $username = 'me') { @@ -286,7 +286,7 @@ public function comment($commentId, $username = 'me') * * @see https://api.imgur.com/endpoints/account#comment-ids * - * @return array + * @return array (@see https://api.imgur.com/models/basic) */ public function commentIds($username = 'me', $page = 0, $sort = 'newest') { @@ -302,7 +302,7 @@ public function commentIds($username = 'me', $page = 0, $sort = 'newest') * * @see https://api.imgur.com/endpoints/account#comment-count * - * @return int + * @return array (@see https://api.imgur.com/models/basic) */ public function commentCount($username = 'me') { @@ -317,7 +317,7 @@ public function commentCount($username = 'me') * * @see https://api.imgur.com/endpoints/account#comment-delete * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function commentDelete($commentId, $username = 'me') { @@ -364,7 +364,7 @@ public function image($imageId, $username = 'me') * * @see https://api.imgur.com/endpoints/account#image-ids * - * @return array + * @return array (@see https://api.imgur.com/models/basic) */ public function imageIds($username = 'me', $page = 0) { @@ -378,7 +378,7 @@ public function imageIds($username = 'me', $page = 0) * * @see https://api.imgur.com/endpoints/account#image-count * - * @return int + * @return array (@see https://api.imgur.com/models/basic) */ public function imageCount($username = 'me') { @@ -393,7 +393,7 @@ public function imageCount($username = 'me') * * @see https://api.imgur.com/endpoints/account#image-delete * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function imageDelete($deleteHash, $username = 'me') { diff --git a/lib/Imgur/Api/Album.php b/lib/Imgur/Api/Album.php index f96d8ff..cfbf75a 100644 --- a/lib/Imgur/Api/Album.php +++ b/lib/Imgur/Api/Album.php @@ -64,7 +64,7 @@ public function albumImage($albumId, $imageId) * * @see https://api.imgur.com/endpoints/album#album-upload * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function create($data) { @@ -80,7 +80,7 @@ public function create($data) * * @see https://api.imgur.com/endpoints/album#album-update * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function update($deletehashOrAlbumId, $data) { @@ -96,7 +96,7 @@ public function update($deletehashOrAlbumId, $data) * * @see https://api.imgur.com/endpoints/album#album-delete * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function deleteAlbum($deletehashOrAlbumId) { @@ -110,7 +110,7 @@ public function deleteAlbum($deletehashOrAlbumId) * * @see https://api.imgur.com/endpoints/album#album-favorite * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function favoriteAlbum($albumId) { @@ -125,7 +125,7 @@ public function favoriteAlbum($albumId) * * @see https://api.imgur.com/endpoints/album#album-set-to * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function setAlbumImages($albumId, array $imageIds) { @@ -140,7 +140,7 @@ public function setAlbumImages($albumId, array $imageIds) * * @see https://api.imgur.com/endpoints/album#album-add-to * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function addImages($albumId, array $imageIds) { @@ -155,7 +155,7 @@ public function addImages($albumId, array $imageIds) * * @see https://api.imgur.com/endpoints/album#album-remove-from * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function removeImages($deletehashOrAlbumId, array $imageIds) { diff --git a/lib/Imgur/Api/Comment.php b/lib/Imgur/Api/Comment.php index 20a5bcb..827e635 100644 --- a/lib/Imgur/Api/Comment.php +++ b/lib/Imgur/Api/Comment.php @@ -34,7 +34,7 @@ public function comment($commentId) * * @see https://api.imgur.com/endpoints/comment#comment-create * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function create($data) { @@ -52,7 +52,7 @@ public function create($data) * * @see https://api.imgur.com/endpoints/comment#comment-delete * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function deleteComment($commentId) { @@ -81,7 +81,7 @@ public function replies($commentId) * * @see https://api.imgur.com/endpoints/comment#comment-reply-create * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function createReply($commentId, $data) { @@ -100,7 +100,7 @@ public function createReply($commentId, $data) * * @see https://api.imgur.com/endpoints/comment#comment-vote * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function vote($commentId, $vote) { @@ -116,7 +116,7 @@ public function vote($commentId, $vote) * * @see https://api.imgur.com/endpoints/comment#comment-report * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function report($commentId) { diff --git a/lib/Imgur/Api/Conversation.php b/lib/Imgur/Api/Conversation.php index 90b3327..432ed9b 100644 --- a/lib/Imgur/Api/Conversation.php +++ b/lib/Imgur/Api/Conversation.php @@ -47,7 +47,7 @@ public function conversation($conversationId) * * @see https://api.imgur.com/endpoints/conversation#message-create * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function messageCreate($data) { @@ -65,7 +65,7 @@ public function messageCreate($data) * * @see https://api.imgur.com/endpoints/conversation#message-delete * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function conversationDelete($conversationId) { @@ -79,7 +79,7 @@ public function conversationDelete($conversationId) * * @see https://api.imgur.com/endpoints/conversation#message-report * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function reportSender($username) { @@ -93,7 +93,7 @@ public function reportSender($username) * * @see https://api.imgur.com/endpoints/conversation#message-block * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function blockSender($username) { diff --git a/lib/Imgur/Api/CustomGallery.php b/lib/Imgur/Api/CustomGallery.php index 77a6967..2adf684 100644 --- a/lib/Imgur/Api/CustomGallery.php +++ b/lib/Imgur/Api/CustomGallery.php @@ -66,7 +66,7 @@ public function image($imageId) * * @see https://api.imgur.com/endpoints/custom_gallery#custom-gallery-add * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function addTags(array $tags) { @@ -78,7 +78,7 @@ public function addTags(array $tags) * * @see https://api.imgur.com/endpoints/custom_gallery#custom-gallery-remove * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function removeTags(array $tags) { @@ -92,7 +92,7 @@ public function removeTags(array $tags) * * @see https://api.imgur.com/endpoints/custom_gallery#filtered-out-block * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function blockTag($tag) { @@ -106,7 +106,7 @@ public function blockTag($tag) * * @see https://api.imgur.com/endpoints/custom_gallery#filtered-out-unblock * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function unBlockTag($tag) { diff --git a/lib/Imgur/Api/Gallery.php b/lib/Imgur/Api/Gallery.php index dd0a999..88e24e7 100644 --- a/lib/Imgur/Api/Gallery.php +++ b/lib/Imgur/Api/Gallery.php @@ -169,7 +169,7 @@ public function galleryItemTags($imageOrAlbumId) * * @see https://api.imgur.com/endpoints/gallery#gallery-tag-vote * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function galleryVoteTag($id, $name, $vote) { @@ -220,7 +220,7 @@ public function randomGalleryImages($page = 0) * * @see https://api.imgur.com/endpoints/gallery#to-gallery * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function submitToGallery($imageOrAlbumId, $data) { @@ -238,7 +238,7 @@ public function submitToGallery($imageOrAlbumId, $data) * * @see https://api.imgur.com/endpoints/gallery#from-gallery * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function removeFromGallery($imageOrAlbumId) { @@ -280,7 +280,7 @@ public function image($imageId) * * @see https://api.imgur.com/endpoints/gallery#gallery-reporting * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function report($imageOrAlbumId) { @@ -309,7 +309,7 @@ public function votes($imageOrAlbumId) * * @see https://api.imgur.com/endpoints/gallery#gallery-voting * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function vote($imageOrAlbumId, $vote) { @@ -358,7 +358,7 @@ public function comment($imageOrAlbumId, $commentId) * * @see https://api.imgur.com/endpoints/gallery#gallery-comment-creation * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function createComment($imageOrAlbumId, $data) { @@ -378,7 +378,7 @@ public function createComment($imageOrAlbumId, $data) * * @see https://api.imgur.com/endpoints/gallery#gallery-comment-reply * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function createReply($imageOrAlbumId, $commentId, $data) { @@ -396,7 +396,7 @@ public function createReply($imageOrAlbumId, $commentId, $data) * * @see https://api.imgur.com/endpoints/gallery#gallery-comment-ids * - * @return array + * @return array (@see https://api.imgur.com/models/basic) */ public function commentIds($imageOrAlbumId) { @@ -410,7 +410,7 @@ public function commentIds($imageOrAlbumId) * * @see https://api.imgur.com/endpoints/gallery#gallery-comment-count * - * @return int + * @return array (@see https://api.imgur.com/models/basic) */ public function commentCount($imageOrAlbumId) { diff --git a/lib/Imgur/Api/Image.php b/lib/Imgur/Api/Image.php index 50e8ae2..a085ad0 100644 --- a/lib/Imgur/Api/Image.php +++ b/lib/Imgur/Api/Image.php @@ -35,7 +35,7 @@ public function image($imageId) * * @see https://api.imgur.com/endpoints/image#image-upload * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function upload($data) { @@ -63,7 +63,7 @@ public function upload($data) * * @see https://api.imgur.com/endpoints/image#image-delete * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function deleteImage($imageIdOrDeleteHash) { @@ -80,7 +80,7 @@ public function deleteImage($imageIdOrDeleteHash) * * @see https://api.imgur.com/endpoints/image#image-update * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function update($imageIdOrDeleteHash, $data) { @@ -94,7 +94,7 @@ public function update($imageIdOrDeleteHash, $data) * * @see https://api.imgur.com/endpoints/image#image-favorite * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function favorite($imageIdOrDeleteHash) { diff --git a/lib/Imgur/Api/Notification.php b/lib/Imgur/Api/Notification.php index c412954..2d9f136 100644 --- a/lib/Imgur/Api/Notification.php +++ b/lib/Imgur/Api/Notification.php @@ -20,10 +20,8 @@ class Notification extends AbstractApi * * @return array With keys "replies" & "messages" */ - public function notifications($new = true) + public function notifications(string $new = 'true') { - $new = $new ? 'true' : 'false'; - return $this->get('notification', ['new' => $new]); } @@ -48,7 +46,7 @@ public function notification($notificationId) * * @see https://api.imgur.com/endpoints/notification#notification-viewed * - * @return bool + * @return array (@see https://api.imgur.com/models/basic) */ public function notificationViewed($notificationId) { diff --git a/lib/Imgur/Api/Topic.php b/lib/Imgur/Api/Topic.php index df484bc..bd078c7 100644 --- a/lib/Imgur/Api/Topic.php +++ b/lib/Imgur/Api/Topic.php @@ -45,7 +45,7 @@ public function galleryTopic($topicId, $sort = 'viral', $page = 0, $window = 'we * View a single item in a gallery topic. * * @param string $topicId The ID or URL-formatted name of the topic. If using a topic's name, replace its spaces with underscores (Mother's_Day) - * @param int $itemId The ID for the gallery item + * @param string $itemId The ID for the gallery item * * @see https://api.imgur.com/endpoints/topic#gallery-topic-item * diff --git a/lib/Imgur/Auth/AuthInterface.php b/lib/Imgur/Auth/AuthInterface.php index d11d71a..79fda82 100644 --- a/lib/Imgur/Auth/AuthInterface.php +++ b/lib/Imgur/Auth/AuthInterface.php @@ -14,20 +14,18 @@ interface AuthInterface * * @param string $responseType (code, token, pin) Determines if Imgur should return an authorization_code, a PIN code, or an opaque access_token * @param string $state Any value which you want Imgur to pass back - * - * @return string */ - public function getAuthenticationUrl($responseType = 'code', $state = null); + public function getAuthenticationUrl(string $responseType = 'code', string $state = null): string; - public function getAccessToken(); + public function getAccessToken(): ?array; - public function requestAccessToken($code, $responseType); + public function requestAccessToken(string $code, string $requestType = null): array; - public function setAccessToken($accessToken); + public function setAccessToken(array $accessToken): void; - public function sign(); + public function sign(): void; - public function refreshToken(); + public function refreshToken(): array; - public function checkAccessTokenExpired(); + public function checkAccessTokenExpired(): bool; } diff --git a/lib/Imgur/Auth/OAuth2.php b/lib/Imgur/Auth/OAuth2.php index 38752e5..d06a21b 100644 --- a/lib/Imgur/Auth/OAuth2.php +++ b/lib/Imgur/Auth/OAuth2.php @@ -32,7 +32,7 @@ class OAuth2 implements AuthInterface /** * The class handling communication with Imgur servers. * - * @var \Imgur\HttpClient\HttpClientInterface + * @var HttpClientInterface */ private $httpClient; @@ -61,11 +61,8 @@ class OAuth2 implements AuthInterface /** * Instantiates the OAuth2 class, but does not trigger the authentication process. - * - * @param string $clientId - * @param string $clientSecret */ - public function __construct(HttpClientInterface $httpClient, $clientId, $clientSecret) + public function __construct(HttpClientInterface $httpClient, string $clientId, string $clientSecret) { $this->clientId = $clientId; $this->clientSecret = $clientSecret; @@ -74,13 +71,8 @@ public function __construct(HttpClientInterface $httpClient, $clientId, $clientS /** * Generates the authentication URL to which a user should be pointed at in order to start the OAuth2 process. - * - * @param string $responseType - * @param string|null $state - * - * @return string */ - public function getAuthenticationURL($responseType = 'code', $state = null) + public function getAuthenticationURL(string $responseType = 'code', string $state = null): string { $httpQueryParameters = [ 'client_id' => $this->clientId, @@ -96,12 +88,9 @@ public function getAuthenticationURL($responseType = 'code', $state = null) /** * Exchanges a code/pin for an access token. * - * @param string $code * @param string $requestType - * - * @return array */ - public function requestAccessToken($code, $requestType) + public function requestAccessToken(string $code, string $requestType = null): array { switch ($requestType) { case 'pin': @@ -146,10 +135,8 @@ public function requestAccessToken($code, $requestType) * you will have to prompt the user for their login information again. * * @throws AuthException - * - * @return array */ - public function refreshToken() + public function refreshToken(): array { $token = $this->getAccessToken(); @@ -182,10 +169,8 @@ public function refreshToken() * @param array $token * * @throws AuthException - * - * @return array */ - public function setAccessToken($token) + public function setAccessToken(array $token = null): void { if (!\is_array($token)) { throw new AuthException('Token is not a valid json string.'); @@ -206,33 +191,29 @@ public function setAccessToken($token) /** * Getter for the current access token. - * - * @return array|null */ - public function getAccessToken() + public function getAccessToken(): ?array { return $this->token; } /** * Check if the current access token (if present), is still usable. - * - * @return bool */ - public function checkAccessTokenExpired() + public function checkAccessTokenExpired(): bool { // don't have the data? Let's assume the token has expired if (!isset($this->token['created_at']) || !isset($this->token['expires_in'])) { return true; } - return ($this->token['created_at'] + $this->token['expires_in']) < time(); + return ((int) ($this->token['created_at'] + $this->token['expires_in'])) < time(); } /** * Add middleware for attaching header signature to each request. */ - public function sign() + public function sign(): void { $this->httpClient->addAuthMiddleware( $this->getAccessToken(), diff --git a/lib/Imgur/Client.php b/lib/Imgur/Client.php index 02acb04..fdc24dc 100644 --- a/lib/Imgur/Client.php +++ b/lib/Imgur/Client.php @@ -2,6 +2,7 @@ namespace Imgur; +use Imgur\Api\AbstractApi; use Imgur\Auth\AuthInterface; use Imgur\Exception\InvalidArgumentException; use Imgur\HttpClient\HttpClient; @@ -25,14 +26,14 @@ class Client /** * The class handling communication with Imgur servers. * - * @var \Imgur\HttpClient\HttpClientInterface + * @var HttpClientInterface */ private $httpClient; /** * The class handling authentication. * - * @var \Imgur\Auth\AuthInterface + * @var AuthInterface */ private $authenticationClient; @@ -48,14 +49,11 @@ public function __construct(AuthInterface $authenticationClient = null, HttpClie } /** - * @param string $name - * @param Imgur\Pager\PagerInterface $pager + * @param PagerInterface $pager * * @throws InvalidArgumentException - * - * @return ApiInterface */ - public function api($name, PagerInterface $pager = null) + public function api(string $name, PagerInterface $pager = null): AbstractApi { if (!$this->getAccessToken()) { $this->sign(); @@ -63,16 +61,16 @@ public function api($name, PagerInterface $pager = null) $apiClass = 'Imgur\\Api\\' . ucfirst($name); if (class_exists($apiClass)) { - return new $apiClass($this, $pager); + /** @var AbstractApi */ + $api = new $apiClass($this, $pager); + + return $api; } throw new InvalidArgumentException('API Method not supported: "' . $name . '" (apiClass: "' . $apiClass . '")'); } - /** - * @return HttpClientInterface - */ - public function getHttpClient() + public function getHttpClient(): HttpClientInterface { if (null === $this->httpClient) { $this->setHttpClient(new HttpClient($this->options)); @@ -81,19 +79,17 @@ public function getHttpClient() return $this->httpClient; } - public function setHttpClient(HttpClientInterface $httpClient) + public function setHttpClient(HttpClientInterface $httpClient): void { $this->httpClient = $httpClient; } /** - * @param string $name - * * @throws InvalidArgumentException * * @return string */ - public function getOption($name) + public function getOption(string $name): ?string { if (!\array_key_exists($name, $this->options)) { throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name)); @@ -103,12 +99,11 @@ public function getOption($name) } /** - * @param string $name - * @param mixed $value + * @param mixed $value * * @throws InvalidArgumentException */ - public function setOption($name, $value) + public function setOption(string $name, $value): void { if (!\array_key_exists($name, $this->options)) { throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name)); @@ -119,10 +114,8 @@ public function setOption($name, $value) /** * Retrieves the Auth object and also instantiates it if not already present. - * - * @return Auth\AuthInterface */ - public function getAuthenticationClient() + public function getAuthenticationClient(): AuthInterface { if (null === $this->authenticationClient) { $this->authenticationClient = new Auth\OAuth2( @@ -138,25 +131,17 @@ public function getAuthenticationClient() /** * Proxy method for the authentication objects URL building method. * - * @param string $responseType * @param string $state - * - * @return string */ - public function getAuthenticationUrl($responseType = 'code', $state = null) + public function getAuthenticationUrl(string $responseType = 'code', string $state = null): string { return $this->getAuthenticationClient()->getAuthenticationUrl($responseType, $state); } /** * Proxy method for exchanging a code for an access token/a pin for an access token. - * - * @param string $code - * @param string $responseType - * - * @return string */ - public function requestAccessToken($code, $responseType = 'code') + public function requestAccessToken(string $code, string $responseType = 'code'): array { return $this->getAuthenticationClient()->requestAccessToken($code, $responseType); } @@ -166,37 +151,31 @@ public function requestAccessToken($code, $responseType = 'code') * * @return array */ - public function getAccessToken() + public function getAccessToken(): ?array { return $this->getAuthenticationClient()->getAccessToken(); } /** * Proxy method for checking if the access token expired. - * - * @return array */ - public function checkAccessTokenExpired() + public function checkAccessTokenExpired(): bool { return $this->getAuthenticationClient()->checkAccessTokenExpired(); } /** * Proxy method for refreshing an access token. - * - * @return array */ - public function refreshToken() + public function refreshToken(): array { return $this->getAuthenticationClient()->refreshToken(); } /** * Proxy method for setting an access token. - * - * @param array $token */ - public function setAccessToken($token) + public function setAccessToken(array $token): void { $this->getAuthenticationClient()->setAccessToken($token); } @@ -204,7 +183,7 @@ public function setAccessToken($token) /** * Proxy method for signing a request. */ - public function sign() + public function sign(): void { $this->getAuthenticationClient()->sign(); } diff --git a/lib/Imgur/Exception/MissingArgumentException.php b/lib/Imgur/Exception/MissingArgumentException.php index 50a00f3..d95d797 100644 --- a/lib/Imgur/Exception/MissingArgumentException.php +++ b/lib/Imgur/Exception/MissingArgumentException.php @@ -4,12 +4,15 @@ class MissingArgumentException extends ErrorException { - public function __construct($required, $code = 0, $previous = null) + /** + * @param array|string $required + */ + public function __construct($required, int $code = 0) { if (\is_string($required)) { $required = [$required]; } - parent::__construct(sprintf('One or more of required ("%s") parameters is missing!', implode('", "', $required)), $code, $previous); + parent::__construct(sprintf('One or more of required ("%s") parameters is missing!', implode('", "', $required)), $code); } } diff --git a/lib/Imgur/HttpClient/HttpClient.php b/lib/Imgur/HttpClient/HttpClient.php index 09d7c20..4c8c71b 100644 --- a/lib/Imgur/HttpClient/HttpClient.php +++ b/lib/Imgur/HttpClient/HttpClient.php @@ -9,6 +9,7 @@ use Imgur\Middleware\AuthMiddleware; use Imgur\Middleware\ErrorMiddleware; use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; /** * Basic client for performing HTTP requests. @@ -20,7 +21,7 @@ class HttpClient implements HttpClientInterface /** * The Guzzle instance. * - * @var \GuzzleHttp\ClientInterface + * @var ClientInterface */ protected $client; @@ -33,12 +34,10 @@ class HttpClient implements HttpClientInterface 'base_url' => 'https://api.imgur.com/3/', ]; + /** @var HandlerStack */ protected $stack; - /** - * @param ClientInterface $client - */ - public function __construct(array $options = [], ClientInterface $client = null) + public function __construct(array $options = [], ClientInterface $client = null, HandlerStack $stack = null) { $this->options = array_merge($options, $this->options); @@ -47,10 +46,7 @@ public function __construct(array $options = [], ClientInterface $client = null) // during test (at least) handler can be injected into the client // so we need to retrieve it to be able to inject our own middleware - $this->stack = HandlerStack::create(); - if (null !== $client) { - $this->stack = $client->getConfig('handler'); - } + $this->stack = (null !== $client && null !== $stack) ? $stack : HandlerStack::create(); $this->stack->push(ErrorMiddleware::error()); @@ -63,7 +59,7 @@ public function __construct(array $options = [], ClientInterface $client = null) /** * {@inheritdoc} */ - public function get($url, array $parameters = []) + public function get($url, array $parameters = []): ResponseInterface { return $this->performRequest($url, $parameters, 'GET'); } @@ -71,7 +67,7 @@ public function get($url, array $parameters = []) /** * {@inheritdoc} */ - public function delete($url, array $parameters = []) + public function delete($url, array $parameters = []): ResponseInterface { return $this->performRequest($url, $parameters, 'DELETE'); } @@ -79,7 +75,7 @@ public function delete($url, array $parameters = []) /** * {@inheritdoc} */ - public function post($url, array $parameters = []) + public function post($url, array $parameters = []): ResponseInterface { return $this->performRequest($url, $parameters, 'POST'); } @@ -87,7 +83,7 @@ public function post($url, array $parameters = []) /** * {@inheritdoc} */ - public function put($url, array $parameters = []) + public function put($url, array $parameters = []): ResponseInterface { return $this->performRequest($url, $parameters, 'PUT'); } @@ -95,7 +91,7 @@ public function put($url, array $parameters = []) /** * {@inheritdoc} */ - public function performRequest($url, $parameters, $httpMethod = 'GET') + public function performRequest($url, $parameters, $httpMethod = 'GET'): ResponseInterface { $options = [ 'headers' => isset($this->options['headers']) ? $this->options['headers'] : [], @@ -124,11 +120,14 @@ public function performRequest($url, $parameters, $httpMethod = 'GET') /** * {@inheritdoc} */ - public function parseResponse($response) + public function parseResponse(ResponseInterface $response): array { - $responseBody = ['data' => [], 'success' => false]; + $responseBody = [ + 'data' => [], + 'success' => false, + ]; - if ($response) { + if ((string) $response->getBody()) { $responseBody = json_decode($response->getBody(), true); } @@ -136,12 +135,9 @@ public function parseResponse($response) } /** - * Push authorization middleware. - * - * @param array $token - * @param string $clientId + * {@inheritdoc} */ - public function addAuthMiddleware($token, $clientId) + public function addAuthMiddleware($token, string $clientId): void { $this->stack->push(Middleware::mapRequest(function (RequestInterface $request) use ($token, $clientId) { return (new AuthMiddleware($token, $clientId))->addAuthHeader($request); diff --git a/lib/Imgur/HttpClient/HttpClientInterface.php b/lib/Imgur/HttpClient/HttpClientInterface.php index 560b04f..0bf8ce0 100644 --- a/lib/Imgur/HttpClient/HttpClientInterface.php +++ b/lib/Imgur/HttpClient/HttpClientInterface.php @@ -2,6 +2,8 @@ namespace Imgur\HttpClient; +use Psr\Http\Message\ResponseInterface; + /** * Basic client for performing HTTP requests. * @@ -14,30 +16,32 @@ interface HttpClientInterface * * @param string $url URL to which the request should point * @param array $parameters Request parameters - * - * @return \Psr\Http\Message\ResponseInterface */ - public function get($url, array $parameters = []); + public function get($url, array $parameters = []): ResponseInterface; /** * Perform a POST request. * * @param string $url URL to which the request should point * @param array $parameters Request parameters - * - * @return \Psr\Http\Message\ResponseInterface */ - public function post($url, array $parameters = []); + public function post($url, array $parameters = []): ResponseInterface; /** * Perform a DELETE request. * * @param string $url URL to which the request should point * @param array $parameters Request parameters + */ + public function delete($url, array $parameters = []): ResponseInterface; + + /** + * Perform a PUT request. * - * @return \Psr\Http\Message\ResponseInterface + * @param string $url URL to which the request should point + * @param array $parameters Request parameters */ - public function delete($url, array $parameters = []); + public function put($url, array $parameters = []): ResponseInterface; /** * Perform the actual request. @@ -45,17 +49,18 @@ public function delete($url, array $parameters = []); * @param string $url URL to which the request should point * @param array $parameters Request parameters * @param string $httpMethod HTTP method to use - * - * @return \Psr\Http\Message\ResponseInterface */ - public function performRequest($url, $parameters, $httpMethod = 'GET'); + public function performRequest($url, $parameters, $httpMethod = 'GET'): ResponseInterface; /** * Parses the Imgur server response. + */ + public function parseResponse(ResponseInterface $response): array; + + /** + * Push authorization middleware. * - * @param object $response - * - * @return array + * @param array|null $token */ - public function parseResponse($response); + public function addAuthMiddleware($token, string $clientId): void; } diff --git a/lib/Imgur/Middleware/AuthMiddleware.php b/lib/Imgur/Middleware/AuthMiddleware.php index 0ee9326..17b7ef7 100644 --- a/lib/Imgur/Middleware/AuthMiddleware.php +++ b/lib/Imgur/Middleware/AuthMiddleware.php @@ -6,16 +6,16 @@ class AuthMiddleware { + /** @var array|null */ private $token; - private $clientId; + private string $clientId; /** * Middleware that add Authorization header. * - * @param array $token - * @param string $clientId + * @param array|null $token */ - public function __construct($token, $clientId) + public function __construct($token, string $clientId) { $this->token = $token; $this->clientId = $clientId; @@ -24,7 +24,7 @@ public function __construct($token, $clientId) /** * Add Authorization header to the request. */ - public function addAuthHeader(RequestInterface $request) + public function addAuthHeader(RequestInterface $request): RequestInterface { if (!empty($this->token['access_token'])) { return $request->withHeader( diff --git a/lib/Imgur/Middleware/ErrorMiddleware.php b/lib/Imgur/Middleware/ErrorMiddleware.php index 0ec63f0..48716ab 100644 --- a/lib/Imgur/Middleware/ErrorMiddleware.php +++ b/lib/Imgur/Middleware/ErrorMiddleware.php @@ -2,6 +2,7 @@ namespace Imgur\Middleware; +use GuzzleHttp\Promise\PromiseInterface; use Imgur\Exception\ErrorException; use Imgur\Exception\RateLimitException; use Imgur\Exception\RuntimeException; @@ -10,6 +11,7 @@ class ErrorMiddleware { + /** @var callable */ private $nextHandler; /** @@ -20,10 +22,7 @@ public function __construct(callable $nextHandler) $this->nextHandler = $nextHandler; } - /** - * @return PromiseInterface - */ - public function __invoke(RequestInterface $request, array $options) + public function __invoke(RequestInterface $request, array $options): PromiseInterface { $fn = $this->nextHandler; @@ -38,7 +37,7 @@ public function __invoke(RequestInterface $request, array $options) * * @return \Closure Returns a function that accepts the next handler */ - public static function error() + public static function error(): \Closure { return function (callable $handler) { return new self($handler); @@ -48,7 +47,7 @@ public static function error() /** * Check for an error. */ - public function checkError(ResponseInterface $response) + public function checkError(ResponseInterface $response): ?ResponseInterface { if ($response->getStatusCode() < 400) { return $response; @@ -92,7 +91,7 @@ public function checkError(ResponseInterface $response) /** * Check if user hit limit. */ - private function checkUserRateLimit(ResponseInterface $response) + private function checkUserRateLimit(ResponseInterface $response): void { $userRemaining = $response->getHeaderLine('X-RateLimit-UserRemaining'); $userLimit = $response->getHeaderLine('X-RateLimit-UserLimit'); @@ -105,14 +104,14 @@ private function checkUserRateLimit(ResponseInterface $response) /** * Check if client hit limit. */ - private function checkClientRateLimit(ResponseInterface $response) + private function checkClientRateLimit(ResponseInterface $response): void { $clientRemaining = $response->getHeaderLine('X-RateLimit-ClientRemaining'); $clientLimit = $response->getHeaderLine('X-RateLimit-ClientLimit'); if ('' !== $clientRemaining && $clientRemaining < 1) { // X-RateLimit-UserReset: Timestamp (unix epoch) for when the credits will be reset. - $resetTime = date('Y-m-d H:i:s', $response->getHeaderLine('X-RateLimit-UserReset')); + $resetTime = date('Y-m-d H:i:s', (int) $response->getHeaderLine('X-RateLimit-UserReset')); throw new RateLimitException('No application credits available. The limit is ' . $clientLimit . ' and will be reset at ' . $resetTime); } @@ -121,14 +120,14 @@ private function checkClientRateLimit(ResponseInterface $response) /** * Check if client hit post limit. */ - private function checkPostRateLimit(ResponseInterface $response) + private function checkPostRateLimit(ResponseInterface $response): void { $postRemaining = $response->getHeaderLine('X-Post-Rate-Limit-Remaining'); $postLimit = $response->getHeaderLine('X-Post-Rate-Limit-Limit'); if ('' !== $postRemaining && $postRemaining < 1) { // X-Post-Rate-Limit-Reset: Time in seconds until your POST ratelimit is reset - $resetTime = date('Y-m-d H:i:s', $response->getHeaderLine('X-Post-Rate-Limit-Reset')); + $resetTime = date('Y-m-d H:i:s', (int) $response->getHeaderLine('X-Post-Rate-Limit-Reset')); throw new RateLimitException('No post credits available. The limit is ' . $postLimit . ' and will be reset at ' . $resetTime); } diff --git a/lib/Imgur/Pager/BasicPager.php b/lib/Imgur/Pager/BasicPager.php index 1da6606..c81d405 100644 --- a/lib/Imgur/Pager/BasicPager.php +++ b/lib/Imgur/Pager/BasicPager.php @@ -11,33 +11,27 @@ */ class BasicPager implements PagerInterface { - private $page; - private $resultsPerPage; + private int $page; + private int $resultsPerPage; - public function __construct($page = 1, $resultsPerPage = 10) + public function __construct(int $page = 1, int $resultsPerPage = 10) { $this->setPage($page ?: 1); $this->setResultsPerPage($resultsPerPage ?: 10); - - return $this; } /** * Get the page number to be retrieved. - * - * @return int */ - public function getPage() + public function getPage(): int { return $this->page; } /** * Get the number of results per page. - * - * @return int */ - public function getResultsPerPage() + public function getResultsPerPage(): int { return $this->resultsPerPage; } @@ -45,11 +39,9 @@ public function getResultsPerPage() /** * Set the page number to be retrieved. * - * @param int $page - * * @return BasicPager */ - public function setPage($page) + public function setPage(int $page): self { $this->page = $page; @@ -59,11 +51,9 @@ public function setPage($page) /** * Set the number of results per page. * - * @param int $resultsPerPage - * * @return BasicPager */ - public function setResultsPerPage($resultsPerPage) + public function setResultsPerPage(int $resultsPerPage): self { $this->resultsPerPage = $resultsPerPage; diff --git a/lib/Imgur/Pager/PagerInterface.php b/lib/Imgur/Pager/PagerInterface.php index 0b544fb..2e6f525 100644 --- a/lib/Imgur/Pager/PagerInterface.php +++ b/lib/Imgur/Pager/PagerInterface.php @@ -12,20 +12,20 @@ interface PagerInterface /** * Get the page number to be retrieved. */ - public function getPage(); + public function getPage(): int; /** * Set the page number to be retrieved. */ - public function setPage($page); + public function setPage(int $page): self; /** * Get the number of results per page. */ - public function getResultsPerPage(); + public function getResultsPerPage(): int; /** * Set the number of results per page. */ - public function setResultsPerPage($resultsPerPage); + public function setResultsPerPage(int $resultsPerPage): self; } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..9744cfb --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,16 @@ +parameters: + level: 7 + paths: + - lib + - tests + + bootstrapFiles: + - vendor/bin/.phpunit/phpunit-8.5-0/vendor/autoload.php + + checkMissingIterableValueType: false + + ignoreErrors: + # because the test is skipped + - + message: '#Unreachable statement#' + path: %currentWorkingDirectory%/tests/Api/TopicTest.php diff --git a/tests/Api/AbstractApiTest.php b/tests/Api/AbstractApiTest.php index ba9e86c..c9ceda4 100644 --- a/tests/Api/AbstractApiTest.php +++ b/tests/Api/AbstractApiTest.php @@ -9,31 +9,31 @@ class AbstractApiTest extends TestCase { - public function testGet() + public function testGet(): void { $image = $this->getApiMock('get'); $image->get('gallery/search/time/0', ['q' => '20minutes']); } - public function testDelete() + public function testDelete(): void { $image = $this->getApiMock('delete'); $image->delete('image/ZOY11VC'); } - public function testPost() + public function testPost(): void { $image = $this->getApiMock('post'); $image->post('album/VOMXz', ['title' => 'yo']); } - public function testPut() + public function testPut(): void { $image = $this->getApiMock('put'); $image->put('album/VOMXz', ['title' => 'yo']); } - protected function getApiMock($method) + protected function getApiMock(string $method): TestAbstractApi { $httpClient = $this->getMockBuilder('Imgur\HttpClient\HttpClient') ->disableOriginalConstructor() diff --git a/tests/Api/AccountTest.php b/tests/Api/AccountTest.php index 740ea56..06248d3 100644 --- a/tests/Api/AccountTest.php +++ b/tests/Api/AccountTest.php @@ -13,23 +13,22 @@ class AccountTest extends ApiTestCase { - public function testBaseReal() + public function testBaseReal(): void { $this->expectException(\Imgur\Exception\ErrorException::class); $this->expectExceptionMessage('Authentication required'); - $guzzleClient = new GuzzleClient(['base_uri' => 'https://api.imgur.com/3/']); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient(); $client = new Client(null, $httpClient); $account = new Account($client); $account->base(); } - public function testBaseWithResponse() + public function testBaseWithResponse(): void { $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode([ + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode([ 'data' => [ 'id' => 703058, 'url' => 'j0k3rx', @@ -45,7 +44,7 @@ public function testBaseWithResponse() $handler = HandlerStack::create($mock); $guzzleClient = new GuzzleClient(['handler' => $handler]); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient([], $guzzleClient, $handler); $client = new Client(null, $httpClient); $account = new Account($client); @@ -59,7 +58,7 @@ public function testBaseWithResponse() $this->assertArrayHasKey('pro_expiration', $result); } - public function testBase() + public function testBase(): void { $expectedValue = [ 'data' => [ @@ -69,7 +68,7 @@ public function testBase() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me') @@ -78,7 +77,7 @@ public function testBase() $this->assertSame($expectedValue, $api->base()); } - public function testDeleteAccount() + public function testDeleteAccount(): void { $expectedValue = [ 'data' => true, @@ -86,7 +85,7 @@ public function testDeleteAccount() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('delete') ->with('account/imgur') @@ -95,7 +94,7 @@ public function testDeleteAccount() $this->assertSame($expectedValue, $api->deleteAccount('imgur')); } - public function testGalleryFavorites() + public function testGalleryFavorites(): void { $expectedValue = [ 'data' => [ @@ -107,7 +106,7 @@ public function testGalleryFavorites() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/gallery_favorites/0/newest') @@ -116,15 +115,15 @@ public function testGalleryFavorites() $this->assertSame($expectedValue, $api->galleryFavorites()); } - public function testGalleryFavoritesWrongValues() + public function testGalleryFavoritesWrongValues(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->galleryFavorites('me', 0, 'bad sort'); + $this->getApiAccountMock()->galleryFavorites('me', 0, 'bad sort'); } - public function testFavorites() + public function testFavorites(): void { $expectedValue = [ 'data' => [ @@ -136,7 +135,7 @@ public function testFavorites() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/favorites') @@ -145,7 +144,7 @@ public function testFavorites() $this->assertSame($expectedValue, $api->favorites()); } - public function testSubmissions() + public function testSubmissions(): void { $expectedValue = [ 'data' => [ @@ -157,7 +156,7 @@ public function testSubmissions() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/submissions/0') @@ -166,7 +165,7 @@ public function testSubmissions() $this->assertSame($expectedValue, $api->submissions()); } - public function testSettings() + public function testSettings(): void { $expectedValue = [ 'data' => [ @@ -178,7 +177,7 @@ public function testSettings() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/settings') @@ -187,7 +186,7 @@ public function testSettings() $this->assertSame($expectedValue, $api->settings()); } - public function testChangeAccountSettings() + public function testChangeAccountSettings(): void { $expectedValue = [ 'data' => true, @@ -195,7 +194,7 @@ public function testChangeAccountSettings() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('post') ->with('account/me/settings') @@ -206,7 +205,7 @@ public function testChangeAccountSettings() ])); } - public function testAccountStats() + public function testAccountStats(): void { $expectedValue = [ 'data' => [ @@ -218,7 +217,7 @@ public function testAccountStats() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/stats') @@ -227,7 +226,7 @@ public function testAccountStats() $this->assertSame($expectedValue, $api->accountStats()); } - public function testAccountGalleryProfile() + public function testAccountGalleryProfile(): void { $expectedValue = [ 'data' => [ @@ -239,7 +238,7 @@ public function testAccountGalleryProfile() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/gallery_profile') @@ -248,7 +247,7 @@ public function testAccountGalleryProfile() $this->assertSame($expectedValue, $api->accountGalleryProfile()); } - public function testVerifyUsersEmail() + public function testVerifyUsersEmail(): void { $expectedValue = [ 'data' => [ @@ -260,7 +259,7 @@ public function testVerifyUsersEmail() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/verifyemail') @@ -269,7 +268,7 @@ public function testVerifyUsersEmail() $this->assertSame($expectedValue, $api->verifyUsersEmail()); } - public function testSendVerificationEmail() + public function testSendVerificationEmail(): void { $expectedValue = [ 'data' => true, @@ -277,7 +276,7 @@ public function testSendVerificationEmail() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('post') ->with('account/me/verifyemail') @@ -286,7 +285,7 @@ public function testSendVerificationEmail() $this->assertSame($expectedValue, $api->sendVerificationEmail()); } - public function testAlbums() + public function testAlbums(): void { $expectedValue = [ 'data' => [ @@ -298,7 +297,7 @@ public function testAlbums() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/albums/0') @@ -307,7 +306,7 @@ public function testAlbums() $this->assertSame($expectedValue, $api->albums()); } - public function testAlbum() + public function testAlbum(): void { $expectedValue = [ 'data' => [ @@ -319,7 +318,7 @@ public function testAlbum() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/album/Arn5NUt') @@ -328,7 +327,7 @@ public function testAlbum() $this->assertSame($expectedValue, $api->album('Arn5NUt')); } - public function testAlbumIds() + public function testAlbumIds(): void { $expectedValue = [ 'data' => [ @@ -340,7 +339,7 @@ public function testAlbumIds() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/albums/ids/0') @@ -349,7 +348,7 @@ public function testAlbumIds() $this->assertSame($expectedValue, $api->albumIds()); } - public function testAlbumCount() + public function testAlbumCount(): void { $expectedValue = [ 'data' => [ @@ -361,7 +360,7 @@ public function testAlbumCount() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/albums/count') @@ -370,7 +369,7 @@ public function testAlbumCount() $this->assertSame($expectedValue, $api->albumCount()); } - public function testAlbumDelete() + public function testAlbumDelete(): void { $expectedValue = [ 'data' => true, @@ -378,7 +377,7 @@ public function testAlbumDelete() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('delete') ->with('account/me/album/Arn5NUt') @@ -387,7 +386,7 @@ public function testAlbumDelete() $this->assertSame($expectedValue, $api->albumDelete('Arn5NUt')); } - public function testComments() + public function testComments(): void { $expectedValue = [ 'data' => [ @@ -399,7 +398,7 @@ public function testComments() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/comments/newest/0') @@ -408,15 +407,15 @@ public function testComments() $this->assertSame($expectedValue, $api->comments()); } - public function testCommentsWrongValues() + public function testCommentsWrongValues(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->comments('me', 0, 'bad sort'); + $this->getApiAccountMock()->comments('me', 0, 'bad sort'); } - public function testComment() + public function testComment(): void { $expectedValue = [ 'data' => [ @@ -428,7 +427,7 @@ public function testComment() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/comment/726305564') @@ -437,7 +436,7 @@ public function testComment() $this->assertSame($expectedValue, $api->comment('726305564')); } - public function testCommentIds() + public function testCommentIds(): void { $expectedValue = [ 'data' => [ @@ -449,7 +448,7 @@ public function testCommentIds() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/comments/ids/newest/0') @@ -458,15 +457,15 @@ public function testCommentIds() $this->assertSame($expectedValue, $api->commentIds()); } - public function testCommentIdsWrongValues() + public function testCommentIdsWrongValues(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->commentIds('me', 0, 'bad sort'); + $this->getApiAccountMock()->commentIds('me', 0, 'bad sort'); } - public function testCommentCount() + public function testCommentCount(): void { $expectedValue = [ 'data' => [ @@ -478,7 +477,7 @@ public function testCommentCount() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/comments/count') @@ -487,7 +486,7 @@ public function testCommentCount() $this->assertSame($expectedValue, $api->commentCount()); } - public function testCommentDelete() + public function testCommentDelete(): void { $expectedValue = [ 'data' => true, @@ -495,7 +494,7 @@ public function testCommentDelete() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('delete') ->with('account/me/comment/726305564') @@ -504,7 +503,7 @@ public function testCommentDelete() $this->assertSame($expectedValue, $api->commentDelete('726305564')); } - public function testImages() + public function testImages(): void { $expectedValue = [ 'data' => [ @@ -516,7 +515,7 @@ public function testImages() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/images/0') @@ -525,7 +524,7 @@ public function testImages() $this->assertSame($expectedValue, $api->images()); } - public function testImage() + public function testImage(): void { $expectedValue = [ 'data' => [ @@ -537,7 +536,7 @@ public function testImage() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/image/iCMrM1P') @@ -546,7 +545,7 @@ public function testImage() $this->assertSame($expectedValue, $api->image('iCMrM1P')); } - public function testImageIds() + public function testImageIds(): void { $expectedValue = [ 'data' => [ @@ -558,7 +557,7 @@ public function testImageIds() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/images/ids/0') @@ -567,7 +566,7 @@ public function testImageIds() $this->assertSame($expectedValue, $api->imageIds()); } - public function testImageCount() + public function testImageCount(): void { $expectedValue = [ 'data' => [ @@ -579,7 +578,7 @@ public function testImageCount() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/images/count') @@ -588,7 +587,7 @@ public function testImageCount() $this->assertSame($expectedValue, $api->imageCount()); } - public function testImageDelete() + public function testImageDelete(): void { $expectedValue = [ 'data' => true, @@ -596,7 +595,7 @@ public function testImageDelete() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('delete') ->with('account/me/image/iCMrM1P') @@ -605,7 +604,7 @@ public function testImageDelete() $this->assertSame($expectedValue, $api->imageDelete('iCMrM1P')); } - public function testReplies() + public function testReplies(): void { $expectedValue = [ 'data' => [ @@ -617,7 +616,7 @@ public function testReplies() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAccountMock(); $api->expects($this->once()) ->method('get') ->with('account/me/notifications/replies', ['new' => 'false']) @@ -625,9 +624,4 @@ public function testReplies() $this->assertSame($expectedValue, $api->replies()); } - - protected function getApiClass() - { - return 'Imgur\Api\Account'; - } } diff --git a/tests/Api/AlbumOrImageTest.php b/tests/Api/AlbumOrImageTest.php index 9e08384..8a1165c 100644 --- a/tests/Api/AlbumOrImageTest.php +++ b/tests/Api/AlbumOrImageTest.php @@ -14,26 +14,26 @@ class AlbumOrImageTest extends TestCase { - public function testWithImageId() + public function testWithImageId(): void { $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode(['data' => 'ok !'])), + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode(['data' => ['ok !']])), ]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); - $httpClient = new HttpClient([], $client); + $httpClient = new HttpClient([], $client, $handler); $client = new Client(null, $httpClient); $api = new AlbumOrImage($client); - $this->assertSame('ok !', $api->find('ZOY11VC')); + $this->assertSame(['ok !'], $api->find('ZOY11VC')); } - public function testWithAlbumId() + public function testWithAlbumId(): void { $mock = new MockHandler([ - new Response(404, ['Content-Type' => 'application/json'], json_encode([ + new Response(404, ['Content-Type' => 'application/json'], (string) json_encode([ 'data' => [ 'error' => 'Unable to find an image with the id, 8pCqe', 'request' => '/3/image/8pCqe', @@ -42,26 +42,26 @@ public function testWithAlbumId() 'success' => false, 'status' => 404, ])), - new Response(200, ['Content-Type' => 'application/json'], json_encode(['data' => 'ok !'])), + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode(['data' => ['ok !']])), ]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); - $httpClient = new HttpClient([], $client); + $httpClient = new HttpClient([], $client, $handler); $client = new Client(null, $httpClient); $api = new AlbumOrImage($client); - $this->assertSame('ok !', $api->find('8pCqe')); + $this->assertSame(['ok !'], $api->find('8pCqe')); } - public function testWithBadId() + public function testWithBadId(): void { $this->expectException(ErrorException::class); $this->expectExceptionMessage('id'); $mock = new MockHandler([ - new Response(404, ['Content-Type' => 'application/json'], json_encode([ + new Response(404, ['Content-Type' => 'application/json'], (string) json_encode([ 'data' => [ 'error' => 'Unable to find an image with the id, xxxxxxx', 'request' => '/3/image/xxxxxxx', @@ -70,7 +70,7 @@ public function testWithBadId() 'success' => false, 'status' => 404, ])), - new Response(404, ['Content-Type' => 'application/json'], json_encode([ + new Response(404, ['Content-Type' => 'application/json'], (string) json_encode([ 'data' => [ 'error' => 'Unable to find an album with the id, xxxxxxx', 'request' => '/3/album/xxxxxxx', @@ -83,7 +83,7 @@ public function testWithBadId() $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); - $httpClient = new HttpClient([], $client); + $httpClient = new HttpClient([], $client, $handler); $client = new Client(null, $httpClient); $api = new AlbumOrImage($client); @@ -91,13 +91,13 @@ public function testWithBadId() $api->find('xxxxxxx'); } - public function testWithImageIdButBadResponse() + public function testWithImageIdButBadResponse(): void { $this->expectException(ErrorException::class); $this->expectExceptionMessage('oops'); $mock = new MockHandler([ - new Response(500, ['Content-Type' => 'application/json'], json_encode([ + new Response(500, ['Content-Type' => 'application/json'], (string) json_encode([ 'data' => [ 'error' => 'oops !', 'request' => '/3/image/xxxxxxx', @@ -110,7 +110,7 @@ public function testWithImageIdButBadResponse() $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); - $httpClient = new HttpClient([], $client); + $httpClient = new HttpClient([], $client, $handler); $client = new Client(null, $httpClient); $api = new AlbumOrImage($client); @@ -118,13 +118,13 @@ public function testWithImageIdButBadResponse() $api->find('ZOY11VC'); } - public function testWithAlbumIdButBadResponse() + public function testWithAlbumIdButBadResponse(): void { $this->expectException(ErrorException::class); $this->expectExceptionMessage('oops'); $mock = new MockHandler([ - new Response(404, ['Content-Type' => 'application/json'], json_encode([ + new Response(404, ['Content-Type' => 'application/json'], (string) json_encode([ 'data' => [ 'error' => 'Unable to find an image with the id, xxxxxxx', 'request' => '/3/image/xxxxxxx', @@ -133,7 +133,7 @@ public function testWithAlbumIdButBadResponse() 'success' => false, 'status' => 404, ])), - new Response(500, ['Content-Type' => 'application/json'], json_encode([ + new Response(500, ['Content-Type' => 'application/json'], (string) json_encode([ 'data' => [ 'error' => 'oops !', 'request' => '/3/image/xxxxxxx', @@ -146,7 +146,7 @@ public function testWithAlbumIdButBadResponse() $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); - $httpClient = new HttpClient([], $client); + $httpClient = new HttpClient([], $client, $handler); $client = new Client(null, $httpClient); $api = new AlbumOrImage($client); diff --git a/tests/Api/AlbumTest.php b/tests/Api/AlbumTest.php index 40c55f1..eb51da6 100644 --- a/tests/Api/AlbumTest.php +++ b/tests/Api/AlbumTest.php @@ -12,23 +12,22 @@ class AlbumTest extends ApiTestCase { - public function testBaseReal() + public function testBaseReal(): void { $this->expectException(\Imgur\Exception\ErrorException::class); $this->expectExceptionMessage('Authentication required'); - $guzzleClient = new GuzzleClient(['base_uri' => 'https://api.imgur.com/3/']); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient(); $client = new Client(null, $httpClient); $album = new Album($client); $album->album('VOMXz'); } - public function testBaseWithResponse() + public function testBaseWithResponse(): void { $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode([ + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode([ 'data' => [ 'id' => 'VOMXz', 'title' => 'sample', @@ -84,7 +83,7 @@ public function testBaseWithResponse() $handler = HandlerStack::create($mock); $guzzleClient = new GuzzleClient(['handler' => $handler]); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient([], $guzzleClient, $handler); $client = new Client(null, $httpClient); $album = new Album($client); @@ -113,7 +112,7 @@ public function testBaseWithResponse() $this->assertArrayHasKey('images', $result); } - public function testAlbum() + public function testAlbum(): void { $expectedValue = [ 'data' => [ @@ -123,7 +122,7 @@ public function testAlbum() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAlbumMock(); $api->expects($this->once()) ->method('get') ->with('album/VOMXz') @@ -132,7 +131,7 @@ public function testAlbum() $this->assertSame($expectedValue, $api->album('VOMXz')); } - public function testAlbumImages() + public function testAlbumImages(): void { $expectedValue = [ 'data' => [ @@ -142,7 +141,7 @@ public function testAlbumImages() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAlbumMock(); $api->expects($this->once()) ->method('get') ->with('album/VOMXz/images') @@ -151,7 +150,7 @@ public function testAlbumImages() $this->assertSame($expectedValue, $api->albumImages('VOMXz')); } - public function testAlbumImage() + public function testAlbumImage(): void { $expectedValue = [ 'data' => [ @@ -161,7 +160,7 @@ public function testAlbumImage() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAlbumMock(); $api->expects($this->once()) ->method('get') ->with('album/VOMXz/image/POvvB') @@ -170,7 +169,7 @@ public function testAlbumImage() $this->assertSame($expectedValue, $api->albumImage('VOMXz', 'POvvB')); } - public function testCreate() + public function testCreate(): void { $expectedValue = [ 'data' => true, @@ -178,16 +177,16 @@ public function testCreate() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAlbumMock(); $api->expects($this->once()) ->method('post') ->with('album') ->willReturn($expectedValue); - $this->assertSame($expectedValue, $api->create('VOMXz')); + $this->assertSame($expectedValue, $api->create(['VOMXz'])); } - public function testUpdate() + public function testUpdate(): void { $expectedValue = [ 'data' => true, @@ -195,7 +194,7 @@ public function testUpdate() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAlbumMock(); $api->expects($this->once()) ->method('post') ->with('album/VOMXz') @@ -206,7 +205,7 @@ public function testUpdate() ])); } - public function testDeleteAlbum() + public function testDeleteAlbum(): void { $expectedValue = [ 'data' => [ @@ -216,7 +215,7 @@ public function testDeleteAlbum() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAlbumMock(); $api->expects($this->once()) ->method('delete') ->with('album/VOMXz') @@ -225,7 +224,7 @@ public function testDeleteAlbum() $this->assertSame($expectedValue, $api->deleteAlbum('VOMXz')); } - public function testFavoriteAlbum() + public function testFavoriteAlbum(): void { $expectedValue = [ 'data' => true, @@ -233,7 +232,7 @@ public function testFavoriteAlbum() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAlbumMock(); $api->expects($this->once()) ->method('post') ->with('album/VOMXz/favorite') @@ -242,7 +241,7 @@ public function testFavoriteAlbum() $this->assertSame($expectedValue, $api->favoriteAlbum('VOMXz')); } - public function testSetAlbumImages() + public function testSetAlbumImages(): void { $expectedValue = [ 'data' => true, @@ -250,7 +249,7 @@ public function testSetAlbumImages() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAlbumMock(); $api->expects($this->once()) ->method('post') ->with('album/VOMXz') @@ -259,7 +258,7 @@ public function testSetAlbumImages() $this->assertSame($expectedValue, $api->setAlbumImages('VOMXz', ['POvvB', 'P1vvB'])); } - public function testAddImages() + public function testAddImages(): void { $expectedValue = [ 'data' => true, @@ -267,7 +266,7 @@ public function testAddImages() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAlbumMock(); $api->expects($this->once()) ->method('post') ->with('album/VOMXz/add') @@ -276,7 +275,7 @@ public function testAddImages() $this->assertSame($expectedValue, $api->addImages('VOMXz', ['POvvB', 'P1vvB'])); } - public function testRemoveImages() + public function testRemoveImages(): void { $expectedValue = [ 'data' => true, @@ -284,7 +283,7 @@ public function testRemoveImages() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiAlbumMock(); $api->expects($this->once()) ->method('delete') ->with('album/VOMXz/remove_images') @@ -292,9 +291,4 @@ public function testRemoveImages() $this->assertSame($expectedValue, $api->removeImages('VOMXz', ['POvvB', 'P1vvB'])); } - - protected function getApiClass() - { - return 'Imgur\Api\Album'; - } } diff --git a/tests/Api/ApiTestCase.php b/tests/Api/ApiTestCase.php index 2442a6c..894cc29 100644 --- a/tests/Api/ApiTestCase.php +++ b/tests/Api/ApiTestCase.php @@ -3,13 +3,97 @@ namespace Imgur\tests\Api; use Imgur\Client; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; abstract class ApiTestCase extends TestCase { - abstract protected function getApiClass(); + /** + * @return \Imgur\Api\Account&MockObject + */ + protected function getApiAccountMock() + { + return $this->getApiMock('Imgur\Api\Account'); + } + + /** + * @return \Imgur\Api\Album&MockObject + */ + protected function getApiAlbumMock() + { + return $this->getApiMock('Imgur\Api\Album'); + } + + /** + * @return \Imgur\Api\Comment&MockObject + */ + protected function getApiCommentMock() + { + return $this->getApiMock('Imgur\Api\Comment'); + } + + /** + * @return \Imgur\Api\Conversation&MockObject + */ + protected function getApiConversationMock() + { + return $this->getApiMock('Imgur\Api\Conversation'); + } + + /** + * @return \Imgur\Api\CustomGallery&MockObject + */ + protected function getApiCustomGalleryMock() + { + return $this->getApiMock('Imgur\Api\CustomGallery'); + } + + /** + * @return \Imgur\Api\Gallery&MockObject + */ + protected function getApiGalleryMock() + { + return $this->getApiMock('Imgur\Api\Gallery'); + } + + /** + * @return \Imgur\Api\Image&MockObject + */ + protected function getApiImageMock() + { + return $this->getApiMock('Imgur\Api\Image'); + } + + /** + * @return \Imgur\Api\Memegen&MockObject + */ + protected function getApiMemegenMock() + { + return $this->getApiMock('Imgur\Api\Memegen'); + } + + /** + * @return \Imgur\Api\Notification&MockObject + */ + protected function getApiNotificationMock() + { + return $this->getApiMock('Imgur\Api\Notification'); + } + + /** + * @return \Imgur\Api\Topic&MockObject + */ + protected function getApiTopicMock() + { + return $this->getApiMock('Imgur\Api\Topic'); + } - protected function getApiMock() + /** + * @param class-string $class + * + * @return mixed + */ + private function getApiMock($class) { $httpClient = $this->getMockBuilder('Imgur\HttpClient\HttpClient') ->disableOriginalConstructor() @@ -29,8 +113,8 @@ protected function getApiMock() $client = new Client(null, $httpClient); - return $this->getMockBuilder($this->getApiClass()) - ->setMethods(['get', 'post', 'put', 'delete']) + return $this->getMockBuilder($class) + ->onlyMethods(['get', 'post', 'put', 'delete']) ->setConstructorArgs([$client]) ->getMock(); } diff --git a/tests/Api/CommentTest.php b/tests/Api/CommentTest.php index f0dff54..6b701a9 100644 --- a/tests/Api/CommentTest.php +++ b/tests/Api/CommentTest.php @@ -12,23 +12,22 @@ class CommentTest extends ApiTestCase { - public function testBaseReal() + public function testBaseReal(): void { $this->expectException(\Imgur\Exception\ErrorException::class); $this->expectExceptionMessage('Authentication required'); - $guzzleClient = new GuzzleClient(['base_uri' => 'https://api.imgur.com/3/']); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient(); $client = new Client(null, $httpClient); $comment = new Comment($client); $comment->comment('726305564'); } - public function testBaseWithResponse() + public function testBaseWithResponse(): void { $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode([ + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode([ 'data' => [ 'id' => 726305564, 'image_id' => 'XVO7F', @@ -54,7 +53,7 @@ public function testBaseWithResponse() $handler = HandlerStack::create($mock); $guzzleClient = new GuzzleClient(['handler' => $handler]); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient([], $guzzleClient, $handler); $client = new Client(null, $httpClient); $comment = new Comment($client); @@ -78,7 +77,7 @@ public function testBaseWithResponse() $this->assertArrayHasKey('children', $result); } - public function testComment() + public function testComment(): void { $expectedValue = [ 'data' => [ @@ -88,7 +87,7 @@ public function testComment() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiCommentMock(); $api->expects($this->once()) ->method('get') ->with('comment/726305564') @@ -97,7 +96,7 @@ public function testComment() $this->assertSame($expectedValue, $api->comment('726305564')); } - public function testCreate() + public function testCreate(): void { $expectedValue = [ 'data' => true, @@ -105,7 +104,7 @@ public function testCreate() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiCommentMock(); $api->expects($this->once()) ->method('post') ->with('comment') @@ -114,15 +113,15 @@ public function testCreate() $this->assertSame($expectedValue, $api->create(['image_id' => 'ZOY11VC', 'comment' => 'I agree'])); } - public function testCreateParamMissing() + public function testCreateParamMissing(): void { $this->expectException(\Imgur\Exception\MissingArgumentException::class); $this->expectExceptionMessage('parameters is missing'); - $this->getApiMock()->create('726305564'); + $this->getApiCommentMock()->create(['726305564']); } - public function testDeleteComment() + public function testDeleteComment(): void { $expectedValue = [ 'data' => true, @@ -130,7 +129,7 @@ public function testDeleteComment() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiCommentMock(); $api->expects($this->once()) ->method('delete') ->with('comment/726305564') @@ -139,7 +138,7 @@ public function testDeleteComment() $this->assertSame($expectedValue, $api->deleteComment('726305564')); } - public function testReplies() + public function testReplies(): void { $expectedValue = [ 'data' => [ @@ -149,7 +148,7 @@ public function testReplies() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiCommentMock(); $api->expects($this->once()) ->method('get') ->with('comment/726305564/replied') @@ -158,7 +157,7 @@ public function testReplies() $this->assertSame($expectedValue, $api->replies('726305564')); } - public function testCreateReply() + public function testCreateReply(): void { $expectedValue = [ 'data' => true, @@ -166,7 +165,7 @@ public function testCreateReply() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiCommentMock(); $api->expects($this->once()) ->method('post') ->with('comment/726305565') @@ -175,15 +174,15 @@ public function testCreateReply() $this->assertSame($expectedValue, $api->createReply('726305565', ['image_id' => 'ZOY11VC', 'comment' => 'I agree'])); } - public function testCreateReplyParamMissing() + public function testCreateReplyParamMissing(): void { $this->expectException(\Imgur\Exception\MissingArgumentException::class); $this->expectExceptionMessage('parameters is missing'); - $this->getApiMock()->createReply('726305564', []); + $this->getApiCommentMock()->createReply('726305564', []); } - public function testVote() + public function testVote(): void { $expectedValue = [ 'data' => true, @@ -191,7 +190,7 @@ public function testVote() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiCommentMock(); $api->expects($this->once()) ->method('post') ->with('comment/726305564/vote/up') @@ -200,15 +199,15 @@ public function testVote() $this->assertSame($expectedValue, $api->vote('726305564', 'up')); } - public function testVoteWrongVoteValue() + public function testVoteWrongVoteValue(): void { $this->expectException(\Imgur\Exception\InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->vote('726305564', 'bad vote'); + $this->getApiCommentMock()->vote('726305564', 'bad vote'); } - public function testReport() + public function testReport(): void { $expectedValue = [ 'data' => true, @@ -216,7 +215,7 @@ public function testReport() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiCommentMock(); $api->expects($this->once()) ->method('post') ->with('comment/726305564/report') @@ -224,9 +223,4 @@ public function testReport() $this->assertSame($expectedValue, $api->report('726305564')); } - - protected function getApiClass() - { - return 'Imgur\Api\Comment'; - } } diff --git a/tests/Api/ConversationTest.php b/tests/Api/ConversationTest.php index 0856d20..63c29e1 100644 --- a/tests/Api/ConversationTest.php +++ b/tests/Api/ConversationTest.php @@ -12,23 +12,22 @@ class ConversationTest extends ApiTestCase { - public function testBaseReal() + public function testBaseReal(): void { $this->expectException(\Imgur\Exception\ErrorException::class); $this->expectExceptionMessage('Authentication required'); - $guzzleClient = new GuzzleClient(['base_uri' => 'https://api.imgur.com/3/']); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient(); $client = new Client(null, $httpClient); $conversation = new Conversation($client); $conversation->conversations(); } - public function testBaseWithResponse() + public function testBaseWithResponse(): void { $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode([ + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode([ 'data' => [ [ 'id' => 11247233, @@ -46,7 +45,7 @@ public function testBaseWithResponse() $handler = HandlerStack::create($mock); $guzzleClient = new GuzzleClient(['handler' => $handler]); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient([], $guzzleClient, $handler); $client = new Client(null, $httpClient); $conversation = new Conversation($client); @@ -60,7 +59,7 @@ public function testBaseWithResponse() $this->assertArrayhasKey('datetime', $result[0]); } - public function testConversations() + public function testConversations(): void { $expectedValue = [ 'data' => [ @@ -72,7 +71,7 @@ public function testConversations() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiConversationMock(); $api->expects($this->once()) ->method('get') ->with('conversations') @@ -81,7 +80,7 @@ public function testConversations() $this->assertSame($expectedValue, $api->conversations()); } - public function testConversation() + public function testConversation(): void { $expectedValue = [ 'data' => [ @@ -91,7 +90,7 @@ public function testConversation() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiConversationMock(); $api->expects($this->once()) ->method('get') ->with('conversations/11247233') @@ -100,7 +99,7 @@ public function testConversation() $this->assertSame($expectedValue, $api->conversation('11247233')); } - public function testMessageCreate() + public function testMessageCreate(): void { $expectedValue = [ 'data' => true, @@ -108,7 +107,7 @@ public function testMessageCreate() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiConversationMock(); $api->expects($this->once()) ->method('post') ->with('conversations/imgur') @@ -117,15 +116,15 @@ public function testMessageCreate() $this->assertSame($expectedValue, $api->messageCreate(['recipient' => 'imgur', 'body' => 'YO !'])); } - public function testMessageCreateParamMissing() + public function testMessageCreateParamMissing(): void { $this->expectException(\Imgur\Exception\MissingArgumentException::class); $this->expectExceptionMessage('parameters is missing'); - $this->getApiMock()->messageCreate([]); + $this->getApiConversationMock()->messageCreate([]); } - public function testConversationDelete() + public function testConversationDelete(): void { $expectedValue = [ 'data' => true, @@ -133,7 +132,7 @@ public function testConversationDelete() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiConversationMock(); $api->expects($this->once()) ->method('delete') ->with('conversations/11247233') @@ -142,7 +141,7 @@ public function testConversationDelete() $this->assertSame($expectedValue, $api->conversationDelete('11247233')); } - public function testReportSender() + public function testReportSender(): void { $expectedValue = [ 'data' => true, @@ -150,7 +149,7 @@ public function testReportSender() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiConversationMock(); $api->expects($this->once()) ->method('post') ->with('conversations/report/imgur') @@ -159,7 +158,7 @@ public function testReportSender() $this->assertSame($expectedValue, $api->reportSender('imgur')); } - public function testBlockSender() + public function testBlockSender(): void { $expectedValue = [ 'data' => true, @@ -167,7 +166,7 @@ public function testBlockSender() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiConversationMock(); $api->expects($this->once()) ->method('post') ->with('conversations/block/imgur') @@ -175,9 +174,4 @@ public function testBlockSender() $this->assertSame($expectedValue, $api->blockSender('imgur')); } - - protected function getApiClass() - { - return 'Imgur\Api\Conversation'; - } } diff --git a/tests/Api/CustomGalleryTest.php b/tests/Api/CustomGalleryTest.php index 1e8ce5c..2b2b088 100644 --- a/tests/Api/CustomGalleryTest.php +++ b/tests/Api/CustomGalleryTest.php @@ -13,23 +13,22 @@ class CustomGalleryTest extends ApiTestCase { - public function testBaseReal() + public function testBaseReal(): void { $this->expectException(\Imgur\Exception\ErrorException::class); $this->expectExceptionMessage('Authentication required'); - $guzzleClient = new GuzzleClient(['base_uri' => 'https://api.imgur.com/3/']); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient(); $client = new Client(null, $httpClient); $customGallery = new CustomGallery($client); $customGallery->customGallery(); } - public function testBaseWithResponse() + public function testBaseWithResponse(): void { $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode([ + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode([ 'data' => [ 'link' => 'http://imgur.com/custom', 'tags' => [ @@ -77,7 +76,7 @@ public function testBaseWithResponse() $handler = HandlerStack::create($mock); $guzzleClient = new GuzzleClient(['handler' => $handler]); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient([], $guzzleClient, $handler); $client = new Client(null, $httpClient); $customGallery = new CustomGallery($client); @@ -90,7 +89,7 @@ public function testBaseWithResponse() $this->assertArrayHasKey('items', $result); } - public function testCustomGallery() + public function testCustomGallery(): void { $expectedValue = [ 'data' => [ @@ -100,7 +99,7 @@ public function testCustomGallery() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiCustomGalleryMock(); $api->expects($this->once()) ->method('get') ->with('g/custom/viral/week/0') @@ -109,23 +108,23 @@ public function testCustomGallery() $this->assertSame($expectedValue, $api->customGallery()); } - public function testCustomGalleryWrongSortValue() + public function testCustomGalleryWrongSortValue(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->customGallery('bad sort'); + $this->getApiCustomGalleryMock()->customGallery('bad sort'); } - public function testCustomGalleryWrongWindowValue() + public function testCustomGalleryWrongWindowValue(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->customGallery('viral', 0, 'bad window'); + $this->getApiCustomGalleryMock()->customGallery('viral', 0, 'bad window'); } - public function testFiltered() + public function testFiltered(): void { $expectedValue = [ 'data' => [ @@ -135,7 +134,7 @@ public function testFiltered() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiCustomGalleryMock(); $api->expects($this->once()) ->method('get') ->with('g/filtered/viral/week/0') @@ -144,23 +143,23 @@ public function testFiltered() $this->assertSame($expectedValue, $api->filtered()); } - public function testFilteredWrongSortValue() + public function testFilteredWrongSortValue(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->filtered('bad sort'); + $this->getApiCustomGalleryMock()->filtered('bad sort'); } - public function testFilteredWrongWindowValue() + public function testFilteredWrongWindowValue(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->filtered('viral', 0, 'bad window'); + $this->getApiCustomGalleryMock()->filtered('viral', 0, 'bad window'); } - public function testImage() + public function testImage(): void { $expectedValue = [ 'data' => [ @@ -170,7 +169,7 @@ public function testImage() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiCustomGalleryMock(); $api->expects($this->once()) ->method('get') ->with('g/custom/ccWiaRJ') @@ -179,7 +178,7 @@ public function testImage() $this->assertSame($expectedValue, $api->image('ccWiaRJ')); } - public function testAddTags() + public function testAddTags(): void { $expectedValue = [ 'data' => true, @@ -187,7 +186,7 @@ public function testAddTags() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiCustomGalleryMock(); $api->expects($this->once()) ->method('put') ->with('g/add_tags', ['tags' => 'cats,funny']) @@ -196,7 +195,7 @@ public function testAddTags() $this->assertSame($expectedValue, $api->addTags(['cats', 'funny'])); } - public function testRemoveTags() + public function testRemoveTags(): void { $expectedValue = [ 'data' => true, @@ -204,7 +203,7 @@ public function testRemoveTags() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiCustomGalleryMock(); $api->expects($this->once()) ->method('delete') ->with('g/remove_tags', ['tags' => 'cats,funny']) @@ -213,7 +212,7 @@ public function testRemoveTags() $this->assertSame($expectedValue, $api->removeTags(['cats', 'funny'])); } - public function testBlockTag() + public function testBlockTag(): void { $expectedValue = [ 'data' => true, @@ -221,7 +220,7 @@ public function testBlockTag() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiCustomGalleryMock(); $api->expects($this->once()) ->method('post') ->with('g/block_tag', ['tag' => 'funny']) @@ -230,7 +229,7 @@ public function testBlockTag() $this->assertSame($expectedValue, $api->blockTag('funny')); } - public function testUnBlockTag() + public function testUnBlockTag(): void { $expectedValue = [ 'data' => true, @@ -238,7 +237,7 @@ public function testUnBlockTag() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiCustomGalleryMock(); $api->expects($this->once()) ->method('post') ->with('g/unblock_tag', ['tag' => 'funny']) @@ -246,9 +245,4 @@ public function testUnBlockTag() $this->assertSame($expectedValue, $api->unBlockTag('funny')); } - - protected function getApiClass() - { - return 'Imgur\Api\CustomGallery'; - } } diff --git a/tests/Api/GalleryTest.php b/tests/Api/GalleryTest.php index 14d5196..4220c09 100644 --- a/tests/Api/GalleryTest.php +++ b/tests/Api/GalleryTest.php @@ -13,23 +13,22 @@ class GalleryTest extends ApiTestCase { - public function testBaseReal() + public function testBaseReal(): void { $this->expectException(\Imgur\Exception\ErrorException::class); $this->expectExceptionMessage('Authentication required'); - $guzzleClient = new GuzzleClient(['base_uri' => 'https://api.imgur.com/3/']); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient(); $client = new Client(null, $httpClient); $gallery = new Gallery($client); $gallery->gallery(); } - public function testBaseWithResponse() + public function testBaseWithResponse(): void { $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode([ + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode([ 'data' => [ [ 'id' => 'y1Od4', @@ -69,7 +68,7 @@ public function testBaseWithResponse() $handler = HandlerStack::create($mock); $guzzleClient = new GuzzleClient(['handler' => $handler]); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient([], $guzzleClient, $handler); $client = new Client(null, $httpClient); $gallery = new Gallery($client); @@ -105,7 +104,7 @@ public function testBaseWithResponse() $this->assertArrayHasKey('is_ad', $result[0]); } - public function testGallery() + public function testGallery(): void { $expectedValue = [ 'data' => [ @@ -117,7 +116,7 @@ public function testGallery() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('gallery/hot/viral/day/0', ['showViral' => 'true']) @@ -126,31 +125,31 @@ public function testGallery() $this->assertSame($expectedValue, $api->gallery()); } - public function testGalleryWrongSortValue() + public function testGalleryWrongSortValue(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->gallery('hot', 'bad sort'); + $this->getApiGalleryMock()->gallery('hot', 'bad sort'); } - public function testGalleryWrongSectionValue() + public function testGalleryWrongSectionValue(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->gallery('bad section'); + $this->getApiGalleryMock()->gallery('bad section'); } - public function testGalleryWrongWindowValue() + public function testGalleryWrongWindowValue(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->gallery('hot', 'viral', 0, 'bad window'); + $this->getApiGalleryMock()->gallery('hot', 'viral', 0, 'bad window'); } - public function testMemesSubgallery() + public function testMemesSubgallery(): void { $expectedValue = [ 'data' => [ @@ -162,7 +161,7 @@ public function testMemesSubgallery() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('g/memes/viral/day/0') @@ -171,23 +170,23 @@ public function testMemesSubgallery() $this->assertSame($expectedValue, $api->memesSubgallery()); } - public function testMemesSubgalleryWrongSortValue() + public function testMemesSubgalleryWrongSortValue(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->memesSubgallery('bad sort'); + $this->getApiGalleryMock()->memesSubgallery('bad sort'); } - public function testMemesSubgalleryWrongWindowValue() + public function testMemesSubgalleryWrongWindowValue(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->memesSubgallery('viral', 0, 'bad window'); + $this->getApiGalleryMock()->memesSubgallery('viral', 0, 'bad window'); } - public function testMemeSubgalleryImage() + public function testMemeSubgalleryImage(): void { $expectedValue = [ 'data' => [ @@ -197,7 +196,7 @@ public function testMemeSubgalleryImage() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('g/memes/MPx6ZXr') @@ -206,7 +205,7 @@ public function testMemeSubgalleryImage() $this->assertSame($expectedValue, $api->memeSubgalleryImage('MPx6ZXr')); } - public function testSubredditGalleries() + public function testSubredditGalleries(): void { $expectedValue = [ 'data' => [ @@ -218,7 +217,7 @@ public function testSubredditGalleries() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('gallery/r/pics/time/day/0') @@ -227,23 +226,23 @@ public function testSubredditGalleries() $this->assertSame($expectedValue, $api->subredditGalleries('pics')); } - public function testSubredditGalleriesWrongSortValue() + public function testSubredditGalleriesWrongSortValue(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->subredditGalleries('pics', 'bad sort'); + $this->getApiGalleryMock()->subredditGalleries('pics', 'bad sort'); } - public function testSubredditGalleriesWrongWindowValue() + public function testSubredditGalleriesWrongWindowValue(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->subredditGalleries('pics', 'time', 0, 'bad window'); + $this->getApiGalleryMock()->subredditGalleries('pics', 'time', 0, 'bad window'); } - public function testSubredditImage() + public function testSubredditImage(): void { $expectedValue = [ 'data' => [ @@ -255,7 +254,7 @@ public function testSubredditImage() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('gallery/r/pics/yB1PpjL') @@ -264,7 +263,7 @@ public function testSubredditImage() $this->assertSame($expectedValue, $api->subredditImage('pics', 'yB1PpjL')); } - public function testGalleryTag() + public function testGalleryTag(): void { $expectedValue = [ 'data' => [ @@ -274,7 +273,7 @@ public function testGalleryTag() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('gallery/t/funny/viral/week/0') @@ -283,23 +282,23 @@ public function testGalleryTag() $this->assertSame($expectedValue, $api->galleryTag('funny')); } - public function testGalleryTagWrongSortValue() + public function testGalleryTagWrongSortValue(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->galleryTag('funny', 'bad sort'); + $this->getApiGalleryMock()->galleryTag('funny', 'bad sort'); } - public function testGalleryTagWrongWindowValue() + public function testGalleryTagWrongWindowValue(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->galleryTag('funny', 'time', 0, 'bad window'); + $this->getApiGalleryMock()->galleryTag('funny', 'time', 0, 'bad window'); } - public function testGalleryTagImage() + public function testGalleryTagImage(): void { $expectedValue = [ 'data' => [ @@ -309,7 +308,7 @@ public function testGalleryTagImage() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('gallery/t/funny/yB1PpjL') @@ -318,7 +317,7 @@ public function testGalleryTagImage() $this->assertSame($expectedValue, $api->galleryTagImage('funny', 'yB1PpjL')); } - public function testGalleryItemTags() + public function testGalleryItemTags(): void { $expectedValue = [ 'data' => [ @@ -328,7 +327,7 @@ public function testGalleryItemTags() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('gallery/y1Od4/tags') @@ -337,7 +336,7 @@ public function testGalleryItemTags() $this->assertSame($expectedValue, $api->galleryItemTags('y1Od4')); } - public function testGalleryVoteTag() + public function testGalleryVoteTag(): void { $expectedValue = [ 'data' => true, @@ -345,7 +344,7 @@ public function testGalleryVoteTag() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('post') ->with('gallery/y1Od4/vote/tag/funny/up') @@ -354,15 +353,15 @@ public function testGalleryVoteTag() $this->assertSame($expectedValue, $api->galleryVoteTag('y1Od4', 'funny', 'up')); } - public function testGalleryVoteTagWrongVoteValue() + public function testGalleryVoteTagWrongVoteValue(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->galleryVoteTag('y1Od4', 'funny', 'bad vote'); + $this->getApiGalleryMock()->galleryVoteTag('y1Od4', 'funny', 'bad vote'); } - public function testSearch() + public function testSearch(): void { $expectedValue = [ 'data' => [ @@ -374,7 +373,7 @@ public function testSearch() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('gallery/search/time/0', ['q' => '20minutes']) @@ -383,15 +382,15 @@ public function testSearch() $this->assertSame($expectedValue, $api->search('20minutes')); } - public function testSearchWrongValues() + public function testSearchWrongValues(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->search('pics', 'bad sort'); + $this->getApiGalleryMock()->search('pics', 'bad sort'); } - public function testRandomGalleryImages() + public function testRandomGalleryImages(): void { $expectedValue = [ 'data' => [ @@ -403,7 +402,7 @@ public function testRandomGalleryImages() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('gallery/random/random/0') @@ -412,7 +411,7 @@ public function testRandomGalleryImages() $this->assertSame($expectedValue, $api->randomGalleryImages()); } - public function testSubmitToGallery() + public function testSubmitToGallery(): void { $expectedValue = [ 'data' => true, @@ -420,7 +419,7 @@ public function testSubmitToGallery() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('post') ->with('gallery/y1Od4') @@ -429,15 +428,15 @@ public function testSubmitToGallery() $this->assertSame($expectedValue, $api->submitToGallery('y1Od4', ['title' => 'yo'])); } - public function testSubmitToGalleryParamMissing() + public function testSubmitToGalleryParamMissing(): void { $this->expectException(\Imgur\Exception\MissingArgumentException::class); $this->expectExceptionMessage('parameters is missing'); - $this->getApiMock()->submitToGallery('y1Od4', []); + $this->getApiGalleryMock()->submitToGallery('y1Od4', []); } - public function testRemoveFromGallery() + public function testRemoveFromGallery(): void { $expectedValue = [ 'data' => true, @@ -445,7 +444,7 @@ public function testRemoveFromGallery() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('delete') ->with('gallery/ccWiaRJ') @@ -454,7 +453,7 @@ public function testRemoveFromGallery() $this->assertSame($expectedValue, $api->removeFromGallery('ccWiaRJ')); } - public function testAlbum() + public function testAlbum(): void { $expectedValue = [ 'data' => [ @@ -464,7 +463,7 @@ public function testAlbum() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('gallery/album/VOMXz') @@ -473,7 +472,7 @@ public function testAlbum() $this->assertSame($expectedValue, $api->album('VOMXz')); } - public function testImage() + public function testImage(): void { $expectedValue = [ 'data' => [ @@ -483,7 +482,7 @@ public function testImage() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('gallery/image/ccWiaRJ') @@ -492,7 +491,7 @@ public function testImage() $this->assertSame($expectedValue, $api->image('ccWiaRJ')); } - public function testReport() + public function testReport(): void { $expectedValue = [ 'data' => true, @@ -500,7 +499,7 @@ public function testReport() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('post') ->with('gallery/VOMXz/report') @@ -509,7 +508,7 @@ public function testReport() $this->assertSame($expectedValue, $api->report('VOMXz')); } - public function testVotes() + public function testVotes(): void { $expectedValue = [ 'data' => [ @@ -519,7 +518,7 @@ public function testVotes() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('gallery/VOMXz/votes') @@ -528,7 +527,7 @@ public function testVotes() $this->assertSame($expectedValue, $api->votes('VOMXz')); } - public function testVote() + public function testVote(): void { $expectedValue = [ 'data' => true, @@ -536,7 +535,7 @@ public function testVote() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('post') ->with('gallery/VOMXz/vote/up') @@ -545,15 +544,15 @@ public function testVote() $this->assertSame($expectedValue, $api->vote('VOMXz', 'up')); } - public function testVoteWrongVoteValue() + public function testVoteWrongVoteValue(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->vote('VOMXz', 'bad vote'); + $this->getApiGalleryMock()->vote('VOMXz', 'bad vote'); } - public function testComments() + public function testComments(): void { $expectedValue = [ 'data' => [ @@ -565,7 +564,7 @@ public function testComments() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('gallery/VOMXz/comments/best') @@ -574,15 +573,15 @@ public function testComments() $this->assertSame($expectedValue, $api->comments('VOMXz')); } - public function testCommentsWrongValues() + public function testCommentsWrongValues(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->comments('VOMXz', 'bad sort'); + $this->getApiGalleryMock()->comments('VOMXz', 'bad sort'); } - public function testComment() + public function testComment(): void { $expectedValue = [ 'data' => [ @@ -592,7 +591,7 @@ public function testComment() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('gallery/VOMXz/comment/1234') @@ -601,7 +600,7 @@ public function testComment() $this->assertSame($expectedValue, $api->comment('VOMXz', '1234')); } - public function testCreateComment() + public function testCreateComment(): void { $expectedValue = [ 'data' => true, @@ -609,7 +608,7 @@ public function testCreateComment() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('post') ->with('gallery/VOMXz/comment') @@ -618,15 +617,15 @@ public function testCreateComment() $this->assertSame($expectedValue, $api->createComment('VOMXz', ['comment' => 'yo'])); } - public function testCreateCommentParamMissing() + public function testCreateCommentParamMissing(): void { $this->expectException(\Imgur\Exception\MissingArgumentException::class); $this->expectExceptionMessage('parameters is missing'); - $this->getApiMock()->createComment('y1Od4', []); + $this->getApiGalleryMock()->createComment('y1Od4', []); } - public function testCreateReply() + public function testCreateReply(): void { $expectedValue = [ 'data' => true, @@ -634,7 +633,7 @@ public function testCreateReply() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('post') ->with('gallery/VOMXz/comment/123') @@ -643,15 +642,15 @@ public function testCreateReply() $this->assertSame($expectedValue, $api->createReply('VOMXz', '123', ['comment' => 'yo'])); } - public function testCreateReplyParamMissing() + public function testCreateReplyParamMissing(): void { $this->expectException(\Imgur\Exception\MissingArgumentException::class); $this->expectExceptionMessage('parameters is missing'); - $this->getApiMock()->createReply('y1Od4', '123', []); + $this->getApiGalleryMock()->createReply('y1Od4', '123', []); } - public function testCommentIds() + public function testCommentIds(): void { $expectedValue = [ 'data' => [ @@ -663,7 +662,7 @@ public function testCommentIds() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('gallery/VOMXz/comments/ids') @@ -672,7 +671,7 @@ public function testCommentIds() $this->assertSame($expectedValue, $api->commentIds('VOMXz')); } - public function testCommentCount() + public function testCommentCount(): void { $expectedValue = [ 'data' => 21, @@ -680,7 +679,7 @@ public function testCommentCount() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiGalleryMock(); $api->expects($this->once()) ->method('get') ->with('gallery/VOMXz/comments/count') @@ -688,9 +687,4 @@ public function testCommentCount() $this->assertSame($expectedValue, $api->commentCount('VOMXz')); } - - protected function getApiClass() - { - return 'Imgur\Api\Gallery'; - } } diff --git a/tests/Api/ImageTest.php b/tests/Api/ImageTest.php index 6f6cb16..844c624 100644 --- a/tests/Api/ImageTest.php +++ b/tests/Api/ImageTest.php @@ -12,23 +12,22 @@ class ImageTest extends ApiTestCase { - public function testBaseReal() + public function testBaseReal(): void { $this->expectException(\Imgur\Exception\ErrorException::class); $this->expectExceptionMessage('Authentication required'); - $guzzleClient = new GuzzleClient(['base_uri' => 'https://api.imgur.com/3/']); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient(); $client = new Client(null, $httpClient); $image = new Image($client); $image->image('ZOY11VC'); } - public function testBaseWithResponse() + public function testBaseWithResponse(): void { $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode([ + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode([ 'data' => [ 'id' => 'ZOY11VC', 'title' => null, @@ -60,7 +59,7 @@ public function testBaseWithResponse() $handler = HandlerStack::create($mock); $guzzleClient = new GuzzleClient(['handler' => $handler]); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient([], $guzzleClient, $handler); $client = new Client(null, $httpClient); $image = new Image($client); @@ -90,7 +89,7 @@ public function testBaseWithResponse() $this->assertArrayHasKey('link', $result); } - public function testImage() + public function testImage(): void { $expectedValue = [ 'data' => [ @@ -100,7 +99,7 @@ public function testImage() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiImageMock(); $api->expects($this->once()) ->method('get') ->with('image/ZOY11VC') @@ -109,7 +108,7 @@ public function testImage() $this->assertSame($expectedValue, $api->image('ZOY11VC')); } - public function testUploadWithUrl() + public function testUploadWithUrl(): void { $expectedValue = [ 'data' => true, @@ -117,7 +116,7 @@ public function testUploadWithUrl() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiImageMock(); $api->expects($this->once()) ->method('post') ->with('image') @@ -126,7 +125,7 @@ public function testUploadWithUrl() $this->assertSame($expectedValue, $api->upload(['type' => 'url', 'image' => 'http://i.imgur.com/ZOY11VC.png'])); } - public function testUploadWithFile() + public function testUploadWithFile(): void { $expectedValue = [ 'data' => true, @@ -134,7 +133,7 @@ public function testUploadWithFile() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiImageMock(); $api->expects($this->once()) ->method('post') ->with('image') @@ -143,23 +142,23 @@ public function testUploadWithFile() $this->assertSame($expectedValue, $api->upload(['type' => 'file', 'image' => __DIR__ . '/ZOY11VC.png'])); } - public function testUploadWithBadType() + public function testUploadWithBadType(): void { $this->expectException(\Imgur\Exception\InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->upload(['type' => 'other', 'image' => 'http://i.imgur.com/ZOY11VC.png']); + $this->getApiImageMock()->upload(['type' => 'other', 'image' => 'http://i.imgur.com/ZOY11VC.png']); } - public function testUploadWithUrlParamMissing() + public function testUploadWithUrlParamMissing(): void { $this->expectException(\Imgur\Exception\MissingArgumentException::class); $this->expectExceptionMessage('parameters is missing'); - $this->getApiMock()->upload([]); + $this->getApiImageMock()->upload([]); } - public function testDeleteImage() + public function testDeleteImage(): void { $expectedValue = [ 'data' => true, @@ -167,7 +166,7 @@ public function testDeleteImage() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiImageMock(); $api->expects($this->once()) ->method('delete') ->with('image/ZOY11VC') @@ -176,7 +175,7 @@ public function testDeleteImage() $this->assertSame($expectedValue, $api->deleteImage('ZOY11VC')); } - public function testUpdate() + public function testUpdate(): void { $expectedValue = [ 'data' => true, @@ -184,7 +183,7 @@ public function testUpdate() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiImageMock(); $api->expects($this->once()) ->method('post') ->with('image/ZOY11VC') @@ -193,7 +192,7 @@ public function testUpdate() $this->assertSame($expectedValue, $api->update('ZOY11VC', ['title' => 'hihihi'])); } - public function testFavorite() + public function testFavorite(): void { $expectedValue = [ 'data' => true, @@ -201,7 +200,7 @@ public function testFavorite() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiImageMock(); $api->expects($this->once()) ->method('post') ->with('image/ZOY11VC/favorite') @@ -209,9 +208,4 @@ public function testFavorite() $this->assertSame($expectedValue, $api->favorite('ZOY11VC')); } - - protected function getApiClass() - { - return 'Imgur\Api\Image'; - } } diff --git a/tests/Api/MemegenTest.php b/tests/Api/MemegenTest.php index 7dcf2f6..53f708b 100644 --- a/tests/Api/MemegenTest.php +++ b/tests/Api/MemegenTest.php @@ -12,23 +12,22 @@ class MemegenTest extends ApiTestCase { - public function testBaseReal() + public function testBaseReal(): void { $this->expectException(\Imgur\Exception\ErrorException::class); $this->expectExceptionMessage('Authentication required'); - $guzzleClient = new GuzzleClient(['base_uri' => 'https://api.imgur.com/3/']); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient(); $client = new Client(null, $httpClient); $memegen = new Memegen($client); $memegen->defaultMemes(); } - public function testBaseWithResponse() + public function testBaseWithResponse(): void { $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode([ + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode([ 'data' => [ [ 'id' => '2tNR7P7', @@ -60,7 +59,7 @@ public function testBaseWithResponse() $handler = HandlerStack::create($mock); $guzzleClient = new GuzzleClient(['handler' => $handler]); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient([], $guzzleClient, $handler); $client = new Client(null, $httpClient); $memegen = new Memegen($client); @@ -88,7 +87,7 @@ public function testBaseWithResponse() $this->assertArrayHasKey('link', $result[0]); } - public function testMemegen() + public function testMemegen(): void { $expectedValue = [ 'data' => [ @@ -98,7 +97,7 @@ public function testMemegen() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiMemegenMock(); $api->expects($this->once()) ->method('get') ->with('memegen/defaults') @@ -106,9 +105,4 @@ public function testMemegen() $this->assertSame($expectedValue, $api->defaultMemes()); } - - protected function getApiClass() - { - return 'Imgur\Api\Memegen'; - } } diff --git a/tests/Api/NotificationTest.php b/tests/Api/NotificationTest.php index a8d9663..9ea3518 100644 --- a/tests/Api/NotificationTest.php +++ b/tests/Api/NotificationTest.php @@ -12,23 +12,22 @@ class NotificationTest extends ApiTestCase { - public function testBaseReal() + public function testBaseReal(): void { $this->expectException(\Imgur\Exception\ErrorException::class); $this->expectExceptionMessage('Authentication required'); - $guzzleClient = new GuzzleClient(['base_uri' => 'https://api.imgur.com/3/']); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient(); $client = new Client(null, $httpClient); $notification = new Notification($client); $notification->notifications(); } - public function testBaseWithResponse() + public function testBaseWithResponse(): void { $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode([ + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode([ 'data' => [ 'replies' => [], 'messages' => [ @@ -60,7 +59,7 @@ public function testBaseWithResponse() $handler = HandlerStack::create($mock); $guzzleClient = new GuzzleClient(['handler' => $handler]); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient([], $guzzleClient, $handler); $client = new Client(null, $httpClient); $notification = new Notification($client); @@ -82,7 +81,7 @@ public function testBaseWithResponse() $this->assertArrayHasKey('datetime', $result['messages'][0]['content']); } - public function testNotifications() + public function testNotifications(): void { $expectedValue = [ 'data' => [ @@ -93,7 +92,7 @@ public function testNotifications() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiNotificationMock(); $api->expects($this->once()) ->method('get') ->with('notification', ['new' => 'true']) @@ -102,7 +101,27 @@ public function testNotifications() $this->assertSame($expectedValue, $api->notifications()); } - public function testNotification() + public function testNotificationsFalse(): void + { + $expectedValue = [ + 'data' => [ + 'replies' => [], + 'messages' => [], + ], + 'success' => true, + 'status' => 200, + ]; + + $api = $this->getApiNotificationMock(); + $api->expects($this->once()) + ->method('get') + ->with('notification', ['new' => 'false']) + ->willReturn($expectedValue); + + $this->assertSame($expectedValue, $api->notifications('false')); + } + + public function testNotification(): void { $expectedValue = [ 'data' => [ @@ -112,7 +131,7 @@ public function testNotification() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiNotificationMock(); $api->expects($this->once()) ->method('get') ->with('notification/76878181') @@ -121,7 +140,7 @@ public function testNotification() $this->assertSame($expectedValue, $api->notification('76878181')); } - public function testNotificationViewed() + public function testNotificationViewed(): void { $expectedValue = [ 'data' => true, @@ -129,7 +148,7 @@ public function testNotificationViewed() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiNotificationMock(); $api->expects($this->once()) ->method('post') ->with('notification/76878181') @@ -137,9 +156,4 @@ public function testNotificationViewed() $this->assertSame($expectedValue, $api->notificationViewed('76878181')); } - - protected function getApiClass() - { - return 'Imgur\Api\Notification'; - } } diff --git a/tests/Api/TopicTest.php b/tests/Api/TopicTest.php index c93369e..9b874cf 100644 --- a/tests/Api/TopicTest.php +++ b/tests/Api/TopicTest.php @@ -12,25 +12,24 @@ class TopicTest extends ApiTestCase { - public function testBaseReal() + public function testBaseReal(): void { $this->expectException(\Imgur\Exception\ErrorException::class); $this->expectExceptionMessage('Authentication required'); $this->markTestSkipped('Topic endpoint does not always return 401 with no authentication ...'); - $guzzleClient = new GuzzleClient(['base_uri' => 'https://api.imgur.com/3/']); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient(); $client = new Client(null, $httpClient); $topic = new Topic($client); $topic->defaultTopics(); } - public function testBaseWithResponse() + public function testBaseWithResponse(): void { $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode([ + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode([ 'data' => [ [ 'id' => 155, @@ -107,7 +106,7 @@ public function testBaseWithResponse() $handler = HandlerStack::create($mock); $guzzleClient = new GuzzleClient(['handler' => $handler]); - $httpClient = new HttpClient([], $guzzleClient); + $httpClient = new HttpClient([], $guzzleClient, $handler); $client = new Client(null, $httpClient); $topic = new Topic($client); @@ -124,7 +123,7 @@ public function testBaseWithResponse() $this->assertArrayHasKey('isHero', $result[0]); } - public function testTopic() + public function testTopic(): void { $expectedValue = [ 'data' => [ @@ -134,7 +133,7 @@ public function testTopic() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiTopicMock(); $api->expects($this->once()) ->method('get') ->with('topics/defaults') @@ -143,7 +142,7 @@ public function testTopic() $this->assertSame($expectedValue, $api->defaultTopics()); } - public function testGalleryTopic() + public function testGalleryTopic(): void { $expectedValue = [ 'data' => [ @@ -153,32 +152,32 @@ public function testGalleryTopic() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiTopicMock(); $api->expects($this->once()) ->method('get') ->with('topics/155/viral/week/0') ->willReturn($expectedValue); - $this->assertSame($expectedValue, $api->galleryTopic(155)); + $this->assertSame($expectedValue, $api->galleryTopic('155')); } - public function testGalleryTopicWrongSortValue() + public function testGalleryTopicWrongSortValue(): void { $this->expectException(\Imgur\Exception\InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->galleryTopic(155, 'bad sort'); + $this->getApiTopicMock()->galleryTopic('155', 'bad sort'); } - public function testGalleryTopicWrongWindowValue() + public function testGalleryTopicWrongWindowValue(): void { $this->expectException(\Imgur\Exception\InvalidArgumentException::class); $this->expectExceptionMessage('is wrong. Possible values are'); - $this->getApiMock()->galleryTopic(155, 'viral', 0, 'bad window'); + $this->getApiTopicMock()->galleryTopic('155', 'viral', 0, 'bad window'); } - public function testGalleryTopicItem() + public function testGalleryTopicItem(): void { $expectedValue = [ 'data' => [ @@ -188,17 +187,12 @@ public function testGalleryTopicItem() 'status' => 200, ]; - $api = $this->getApiMock(); + $api = $this->getApiTopicMock(); $api->expects($this->once()) ->method('get') ->with('topics/155/Fbae9SG') ->willReturn($expectedValue); - $this->assertSame($expectedValue, $api->galleryTopicItem(155, 'Fbae9SG')); - } - - protected function getApiClass() - { - return 'Imgur\Api\Topic'; + $this->assertSame($expectedValue, $api->galleryTopicItem('155', 'Fbae9SG')); } } diff --git a/tests/Auth/OAuth2Test.php b/tests/Auth/OAuth2Test.php index 84062ab..19aa13c 100644 --- a/tests/Auth/OAuth2Test.php +++ b/tests/Auth/OAuth2Test.php @@ -13,22 +13,22 @@ class OAuth2Test extends TestCase { - public function testGetAuthenticationUrl() + public function testGetAuthenticationUrl(): void { $client = new GuzzleClient(); - $auth = new OAuth2(new HttpClient([], $client), 123, 456); + $auth = new OAuth2(new HttpClient([], $client), '123', '456'); $this->assertSame('https://api.imgur.com/oauth2/authorize?client_id=123&response_type=code', $auth->getAuthenticationUrl()); $this->assertSame('https://api.imgur.com/oauth2/authorize?client_id=123&response_type=pin', $auth->getAuthenticationUrl('pin')); $this->assertSame('https://api.imgur.com/oauth2/authorize?client_id=123&response_type=code&state=draft', $auth->getAuthenticationUrl('code', 'draft')); - $auth = new OAuth2(new HttpClient([], $client), 456, 789); + $auth = new OAuth2(new HttpClient([], $client), '456', '789'); $this->assertSame('https://api.imgur.com/oauth2/authorize?client_id=456&response_type=pin', $auth->getAuthenticationUrl('pin')); $this->assertSame('https://api.imgur.com/oauth2/authorize?client_id=456&response_type=code', $auth->getAuthenticationUrl()); $this->assertSame('https://api.imgur.com/oauth2/authorize?client_id=456&response_type=code&state=draft', $auth->getAuthenticationUrl('code', 'draft')); } - public function testRequestAccessTokenBadStatusCode() + public function testRequestAccessTokenBadStatusCode(): void { $this->expectException(AuthException::class); $this->expectExceptionMessage('Request for access token failed'); @@ -39,19 +39,19 @@ public function testRequestAccessTokenBadStatusCode() $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); - $auth = new OAuth2(new HttpClient([], $client), 123, 456); + $auth = new OAuth2(new HttpClient([], $client), '123', '456'); $auth->requestAccessToken('code', null); } - public function testRequestAccessTokenWithCode() + public function testRequestAccessTokenWithCode(): void { $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode(['access_token' => 'T0K3N'])), + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode(['access_token' => 'T0K3N'])), ]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); - $auth = new OAuth2(new HttpClient([], $client), 123, 456); + $auth = new OAuth2(new HttpClient([], $client), '123', '456'); $result = $auth->requestAccessToken('code', null); $this->assertArrayHasKey('access_token', $result); @@ -60,15 +60,15 @@ public function testRequestAccessTokenWithCode() $this->assertGreaterThanOrEqual(time(), $result['created_at']); } - public function testRequestAccessTokenWithPin() + public function testRequestAccessTokenWithPin(): void { $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode(['access_token' => 'T0K3N'])), + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode(['access_token' => 'T0K3N'])), ]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); - $auth = new OAuth2(new HttpClient([], $client), 123, 456); + $auth = new OAuth2(new HttpClient([], $client), '123', '456'); $result = $auth->requestAccessToken('code', 'pin'); $this->assertArrayHasKey('access_token', $result); @@ -77,81 +77,81 @@ public function testRequestAccessTokenWithPin() $this->assertLessThanOrEqual(time(), $result['created_at']); } - public function testRefreshTokenBadStatusCode() + public function testRefreshTokenBadStatusCode(): void { $this->expectException(AuthException::class); $this->expectExceptionMessage('Request for refresh access token failed'); $mock = new MockHandler([ - new Response(400, ['Content-Type' => 'application/json'], json_encode(['data' => ['request' => '/3/account', 'error' => 'oops2', 'method' => 'GET'], 'success' => false, 'status' => 400])), + new Response(400, ['Content-Type' => 'application/json'], (string) json_encode(['data' => ['request' => '/3/account', 'error' => 'oops2', 'method' => 'GET'], 'success' => false, 'status' => 400])), ]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); - $auth = new OAuth2(new HttpClient([], $client), 123, 456); + $auth = new OAuth2(new HttpClient([], $client), '123', '456'); $auth->refreshToken(); } - public function testRefreshToken() + public function testRefreshToken(): void { $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode(['access_token' => 'T0K3N', 'refresh_token' => 'FR35H'])), + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode(['access_token' => 'T0K3N', 'refresh_token' => 'FR35H'])), ]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); - $auth = new OAuth2(new HttpClient([], $client), 123, 456); + $auth = new OAuth2(new HttpClient([], $client), '123', '456'); $result = $auth->refreshToken(); $this->assertArrayHasKey('access_token', $result); $this->assertSame('T0K3N', $result['access_token']); } - public function testSetAccessTokenNull() + public function testSetAccessTokenNull(): void { $this->expectException(AuthException::class); $this->expectExceptionMessage('Token is not a valid json string.'); $client = new GuzzleClient(); - $auth = new OAuth2(new HttpClient([], $client), 123, 456); - $result = $auth->setAccessToken(null); + $auth = new OAuth2(new HttpClient([], $client), '123', '456'); + $auth->setAccessToken(null); } - public function testSetAccessTokenEmpty() + public function testSetAccessTokenEmpty(): void { $this->expectException(AuthException::class); $this->expectExceptionMessage('Access token could not be retrieved from the decoded json response.'); $client = new GuzzleClient(); - $auth = new OAuth2(new HttpClient([], $client), 123, 456); + $auth = new OAuth2(new HttpClient([], $client), '123', '456'); $auth->setAccessToken(['data']); } - public function testCheckAccessTokenExpiredFromScratch() + public function testCheckAccessTokenExpiredFromScratch(): void { $client = new GuzzleClient(); - $auth = new OAuth2(new HttpClient([], $client), 123, 456); + $auth = new OAuth2(new HttpClient([], $client), '123', '456'); $this->assertTrue($auth->checkAccessTokenExpired()); } - public function testCheckAccessTokenExpired() + public function testCheckAccessTokenExpired(): void { $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode(['access_token' => 'T0K3N', 'expires_in' => 3600])), + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode(['access_token' => 'T0K3N', 'expires_in' => 3600])), ]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); - $auth = new OAuth2(new HttpClient([], $client), 123, 456); + $auth = new OAuth2(new HttpClient([], $client), '123', '456'); $auth->requestAccessToken('code', null); $this->assertFalse($auth->checkAccessTokenExpired()); } - public function testAuthenticatedRequest() + public function testAuthenticatedRequest(): void { $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode(['data' => ['access_token' => 'T0K3N', 'expires_in' => 3600]])), + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode(['data' => ['access_token' => 'T0K3N', 'expires_in' => 3600]])), new Response(200), ]); $handler = HandlerStack::create($mock); @@ -159,7 +159,7 @@ public function testAuthenticatedRequest() $httpClient = new HttpClient([], $client); - $auth = new OAuth2($httpClient, 123, 456); + $auth = new OAuth2($httpClient, '123', '456'); $auth->requestAccessToken('code', null); $httpClient->get('http://google.com'); diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 6fed831..572d99b 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -2,74 +2,100 @@ namespace Imgur\tests; +use Imgur\Auth\OAuth2; use Imgur\Client; use Imgur\Exception\InvalidArgumentException; +use Imgur\HttpClient\HttpClient; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; class ClientTest extends TestCase { - public function testNoParameters() + public function testNoParameters(): void { $client = new Client(); - $this->assertInstanceOf('Imgur\HttpClient\HttpClient', $client->getHttpClient()); - $this->assertInstanceOf('Imgur\Auth\OAuth2', $client->getAuthenticationClient()); + $client->setOption('client_id', 'xx'); + $client->setOption('client_secret', 'xx'); + + $this->assertInstanceOf(HttpClient::class, $client->getHttpClient()); + $this->assertInstanceOf(OAuth2::class, $client->getAuthenticationClient()); } - public function testAuthenticationParameter() + public function testAuthenticationParameter(): void { - $client = new Client($this->getAuthenticationClientMock()); - $this->assertInstanceOf('Imgur\HttpClient\HttpClient', $client->getHttpClient()); - $this->assertInstanceOf('Imgur\Auth\AuthInterface', $client->getAuthenticationClient()); + $authenticationClient = $this->getAuthenticationClientMock(); + $client = new Client($authenticationClient); + $client->setOption('client_id', 'xx'); + $client->setOption('client_secret', 'xx'); + + $this->assertInstanceOf(HttpClient::class, $client->getHttpClient()); + $this->assertInstanceOf(OAuth2::class, $client->getAuthenticationClient()); } - public function testHttpParameter() + public function testHttpParameter(): void { - $client = new Client(null, $this->getHttpClientMock()); - $this->assertInstanceOf('Imgur\HttpClient\HttpClientInterface', $client->getHttpClient()); - $this->assertInstanceOf('Imgur\Auth\OAuth2', $client->getAuthenticationClient()); + $httpClient = $this->getHttpClientMock(); + $client = new Client(null, $httpClient); + $client->setOption('client_id', 'xx'); + $client->setOption('client_secret', 'xx'); + + $this->assertInstanceOf(HttpClient::class, $client->getHttpClient()); + $this->assertInstanceOf(OAuth2::class, $client->getAuthenticationClient()); } - public function testBothParameter() + public function testBothParameter(): void { - $client = new Client($this->getAuthenticationClientMock(), $this->getHttpClientMock()); - $this->assertInstanceOf('Imgur\HttpClient\HttpClientInterface', $client->getHttpClient()); - $this->assertInstanceOf('Imgur\Auth\AuthInterface', $client->getAuthenticationClient()); + $httpClient = $this->getHttpClientMock(); + $authenticationClient = $this->getAuthenticationClientMock(); + $client = new Client($authenticationClient, $httpClient); + $client->setOption('client_id', 'xx'); + $client->setOption('client_secret', 'xx'); + + $this->assertInstanceOf(HttpClient::class, $client->getHttpClient()); + $this->assertInstanceOf(OAuth2::class, $client->getAuthenticationClient()); } /** * @dataProvider getApiClassesProvider + * + * @param class-string $class */ - public function testGetApiInstance($apiName, $class) + public function testGetApiInstance(string $apiName, $class): void { $client = new Client(); + $client->setOption('client_id', 'xx'); + $client->setOption('client_secret', 'xx'); + $this->assertInstanceOf($class, $client->api($apiName)); } - public function getApiClassesProvider() + public function getApiClassesProvider(): array { return [ - ['account', 'Imgur\Api\Account'], - ['album', 'Imgur\Api\Album'], - ['comment', 'Imgur\Api\Comment'], - ['gallery', 'Imgur\Api\Gallery'], - ['image', 'Imgur\Api\Image'], - ['conversation', 'Imgur\Api\Conversation'], - ['notification', 'Imgur\Api\Notification'], - ['memegen', 'Imgur\Api\Memegen'], - ['customGallery', 'Imgur\Api\CustomGallery'], - ['topic', 'Imgur\Api\Topic'], + ['account', \Imgur\Api\Account::class], + ['album', \Imgur\Api\Album::class], + ['comment', \Imgur\Api\Comment::class], + ['gallery', \Imgur\Api\Gallery::class], + ['image', \Imgur\Api\Image::class], + ['conversation', \Imgur\Api\Conversation::class], + ['notification', \Imgur\Api\Notification::class], + ['memegen', \Imgur\Api\Memegen::class], + ['customGallery', \Imgur\Api\CustomGallery::class], + ['topic', \Imgur\Api\Topic::class], ]; } - public function testNotGetApiInstance() + public function testNotGetApiInstance(): void { $this->expectException(InvalidArgumentException::class); $client = new Client(); + $client->setOption('client_id', 'xx'); + $client->setOption('client_secret', 'xx'); $client->api('do_not_exist'); } - public function testGetOptionNotDefined() + public function testGetOptionNotDefined(): void { $this->expectException(InvalidArgumentException::class); @@ -77,7 +103,7 @@ public function testGetOptionNotDefined() $client->getOption('do_not_exist'); } - public function testSetOptionNotDefined() + public function testSetOptionNotDefined(): void { $this->expectException(InvalidArgumentException::class); @@ -88,7 +114,7 @@ public function testSetOptionNotDefined() /** * @dataProvider getOptions */ - public function testGetOption($option, $value) + public function testGetOption(string $option, string $value): void { $client = new Client(); $client->setOption($option, $value); @@ -96,7 +122,7 @@ public function testGetOption($option, $value) $this->assertSame($value, $client->getOption($option)); } - public function getOptions() + public function getOptions(): array { return [ ['base_url', 'url'], @@ -105,23 +131,24 @@ public function getOptions() ]; } - public function testGetAuthenticationUrl() + public function testGetAuthenticationUrl(): void { - $client = new Client(); - $this->assertSame('https://api.imgur.com/oauth2/authorize?response_type=code', $client->getAuthenticationUrl()); - $this->assertSame('https://api.imgur.com/oauth2/authorize?response_type=pin', $client->getAuthenticationUrl('pin')); - $this->assertSame('https://api.imgur.com/oauth2/authorize?response_type=code&state=draft', $client->getAuthenticationUrl('code', 'draft')); + // $client = new Client(); + // $this->assertSame('https://api.imgur.com/oauth2/authorize?response_type=code', $client->getAuthenticationUrl()); + // $this->assertSame('https://api.imgur.com/oauth2/authorize?response_type=pin', $client->getAuthenticationUrl('pin')); + // $this->assertSame('https://api.imgur.com/oauth2/authorize?response_type=code&state=draft', $client->getAuthenticationUrl('code', 'draft')); $client = new Client(); $client->setOption('client_id', 123); + $client->setOption('client_secret', 'xx'); $this->assertSame('https://api.imgur.com/oauth2/authorize?client_id=123&response_type=pin', $client->getAuthenticationUrl('pin')); $this->assertSame('https://api.imgur.com/oauth2/authorize?client_id=123&response_type=code', $client->getAuthenticationUrl()); $this->assertSame('https://api.imgur.com/oauth2/authorize?client_id=123&response_type=code&state=draft', $client->getAuthenticationUrl('code', 'draft')); } - public function testCheckAccessTokenExpired() + public function testCheckAccessTokenExpired(): void { - $authenticationClient = $this->getAuthenticationClientMock(['checkAccessTokenExpired']); + $authenticationClient = $this->getAuthenticationClientMock(); $authenticationClient->expects($this->once()) ->method('checkAccessTokenExpired') ->with(); @@ -130,7 +157,7 @@ public function testCheckAccessTokenExpired() $client->checkAccessTokenExpired(); } - public function testRequestAccessToken() + public function testRequestAccessToken(): void { $httpClient = $this->getHttpClientMock(); $authenticationClient = $this->getAuthenticationClientMock(); @@ -142,7 +169,7 @@ public function testRequestAccessToken() $client->requestAccessToken('code'); } - public function testRefreshToken() + public function testRefreshToken(): void { $httpClient = $this->getHttpClientMock(); $authenticationClient = $this->getAuthenticationClientMock(); @@ -153,41 +180,37 @@ public function testRefreshToken() $client->refreshToken(); } - public function testSetAccessToken() + public function testSetAccessToken(): void { $httpClient = $this->getHttpClientMock(); $authenticationClient = $this->getAuthenticationClientMock(); $authenticationClient->expects($this->once()) ->method('setAccessToken') - ->with('token'); + ->with(['token']); $client = new Client($authenticationClient, $httpClient); - $client->setAccessToken('token'); + $client->setAccessToken(['token']); } - private function getHttpClientMock(array $methods = []) + /** + * @return HttpClient&MockObject + */ + private function getHttpClientMock(array $methods = []): object { - $methods = array_merge( - ['get', 'post', 'delete', 'request', 'performRequest', 'createRequest', 'parseResponse'], - $methods - ); - - return $this->getMockBuilder('Imgur\HttpClient\HttpClient') + return $this->getMockBuilder(HttpClient::class) ->disableOriginalConstructor() - ->setMethods($methods) + ->onlyMethods(['get', 'post', 'delete', 'performRequest', 'parseResponse']) + ->addMethods(array_merge_recursive(['request', 'createRequest'], $methods)) ->getMock(); } - private function getAuthenticationClientMock(array $methods = []) + /** + * @return OAuth2&MockObject + */ + private function getAuthenticationClientMock() { - $methods = array_merge( - ['getAuthenticationUrl', 'getAccessToken', 'requestAccessToken', 'setAccessToken', 'sign', 'refreshToken'], - $methods - ); - return $this->getMockBuilder('Imgur\Auth\OAuth2') ->disableOriginalConstructor() - ->setMethods($methods) ->getMock(); } } diff --git a/tests/HttpClient/HttpClientTest.php b/tests/HttpClient/HttpClientTest.php index 792ba01..acfd1f6 100644 --- a/tests/HttpClient/HttpClientTest.php +++ b/tests/HttpClient/HttpClientTest.php @@ -6,12 +6,13 @@ use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; +use Imgur\Exception\RateLimitException; use Imgur\HttpClient\HttpClient; use PHPUnit\Framework\TestCase; class HttpClientTest extends TestCase { - public function testOptionsToConstructor() + public function testOptionsToConstructor(): void { $httpClient = new TestHttpClient([ 'headers' => ['Cache-Control' => 'no-cache'], @@ -21,13 +22,13 @@ public function testOptionsToConstructor() $this->assertNull($httpClient->getOption('base_uri')); } - public function testDoGETRequest() + public function testDoGETRequest(): void { $path = '/some/path'; $parameters = ['a' => 'b']; $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode(['data' => 'ok !'])), + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode(['data' => ['ok !']])), ]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); @@ -37,16 +38,16 @@ public function testDoGETRequest() $result = $httpClient->parseResponse($response); - $this->assertSame('ok !', $result); + $this->assertSame(['ok !'], $result); } - public function testDoPOSTRequest() + public function testDoPOSTRequest(): void { $path = '/some/path'; $parameters = ['a' => 'b']; $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode(['data' => 'ok !'])), + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode(['data' => ['ok !']])), ]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); @@ -56,10 +57,10 @@ public function testDoPOSTRequest() $result = $httpClient->parseResponse($response); - $this->assertSame('ok !', $result); + $this->assertSame(['ok !'], $result); } - public function testDoPOSTRequestWithMultipart() + public function testDoPOSTRequestWithMultipart(): void { $path = '/some/path'; $parameters = [ @@ -68,7 +69,7 @@ public function testDoPOSTRequestWithMultipart() ]; $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode(['data' => 'ok !'])), + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode(['data' => ['ok !']])), ]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); @@ -78,16 +79,16 @@ public function testDoPOSTRequestWithMultipart() $result = $httpClient->parseResponse($response); - $this->assertSame('ok !', $result); + $this->assertSame(['ok !'], $result); } - public function testDoPUTRequest() + public function testDoPUTRequest(): void { $path = '/some/path'; $parameters = ['a' => 'b']; $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode(['data' => 'ok !'])), + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode(['data' => ['ok !']])), ]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); @@ -97,16 +98,16 @@ public function testDoPUTRequest() $result = $httpClient->parseResponse($response); - $this->assertSame('ok !', $result); + $this->assertSame(['ok !'], $result); } - public function testDoDELETERequest() + public function testDoDELETERequest(): void { $path = '/some/path'; $parameters = ['a' => 'b']; $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode(['data' => 'ok !'])), + new Response(200, ['Content-Type' => 'application/json'], (string) json_encode(['data' => ['ok !']])), ]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); @@ -116,16 +117,16 @@ public function testDoDELETERequest() $result = $httpClient->parseResponse($response); - $this->assertSame('ok !', $result); + $this->assertSame(['ok !'], $result); } - public function testDoCustomRequest() + public function testDoCustomRequest(): void { $path = '/some/path'; $options = ['c' => 'd']; $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], json_encode(['data' => true])), + new Response(200, ['Content-Type' => 'application/json']), ]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); @@ -135,12 +136,12 @@ public function testDoCustomRequest() $result = $httpClient->parseResponse($response); - $this->assertTrue($result); + $this->assertEmpty($result); } - public function testThrowExceptionWhenApiIsExceeded() + public function testThrowExceptionWhenApiIsExceeded(): void { - $this->expectException(\Imgur\Exception\RateLimitException::class); + $this->expectException(RateLimitException::class); $this->expectExceptionMessage('No user credits available. The limit is 10'); $path = '/some/path'; @@ -148,42 +149,42 @@ public function testThrowExceptionWhenApiIsExceeded() $mock = new MockHandler([ new Response(429, [ - 'X-RateLimit-UserLimit' => 10, - 'X-RateLimit-UserRemaining' => 0, + 'X-RateLimit-UserLimit' => '10', + 'X-RateLimit-UserRemaining' => '0', ]), ]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); - $httpClient = new TestHttpClient([], $client); + $httpClient = new TestHttpClient([], $client, $handler); $httpClient->get($path, $parameters); } - public function testThrowExceptionWhenClientApiIsExceeded() + public function testThrowExceptionWhenClientApiIsExceeded(): void { - $this->expectException(\Imgur\Exception\RateLimitException::class); + $this->expectException(RateLimitException::class); $this->expectExceptionMessage('No application credits available. The limit is 10 and will be reset at'); $path = '/some/path'; $parameters = ['a' => 'b']; $response = new Response(429, [ - 'X-RateLimit-UserLimit' => 10, - 'X-RateLimit-UserRemaining' => 10, - 'X-RateLimit-ClientLimit' => 10, - 'X-RateLimit-ClientRemaining' => 0, - 'X-RateLimit-UserReset' => 1474318026, + 'X-RateLimit-UserLimit' => '10', + 'X-RateLimit-UserRemaining' => '10', + 'X-RateLimit-ClientLimit' => '10', + 'X-RateLimit-ClientRemaining' => '0', + 'X-RateLimit-UserReset' => '1474318026', ]); $mock = new MockHandler([$response]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); - $httpClient = new TestHttpClient([], $client); + $httpClient = new TestHttpClient([], $client, $handler); $httpClient->get($path, $parameters); } - public function testThrowExceptionWhenBadRequestPlainError() + public function testThrowExceptionWhenBadRequestPlainError(): void { $this->expectException(\Imgur\Exception\RuntimeException::class); $this->expectExceptionMessage('oops'); @@ -192,21 +193,21 @@ public function testThrowExceptionWhenBadRequestPlainError() $parameters = ['a' => 'b']; $response = new Response(429, [ - 'X-RateLimit-UserLimit' => 10, - 'X-RateLimit-UserRemaining' => 10, - 'X-RateLimit-ClientLimit' => 10, - 'X-RateLimit-ClientRemaining' => 10, + 'X-RateLimit-UserLimit' => '10', + 'X-RateLimit-UserRemaining' => '10', + 'X-RateLimit-ClientLimit' => '10', + 'X-RateLimit-ClientRemaining' => '10', ], 'oops'); $mock = new MockHandler([$response]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); - $httpClient = new TestHttpClient([], $client); + $httpClient = new TestHttpClient([], $client, $handler); $httpClient->get($path, $parameters); } - public function testThrowExceptionWhenBadRequestJsonError() + public function testThrowExceptionWhenBadRequestJsonError(): void { $this->expectException(\Imgur\Exception\ErrorException::class); $this->expectExceptionMessage('Request to: /3/account failed with: "oops2"'); @@ -215,21 +216,21 @@ public function testThrowExceptionWhenBadRequestJsonError() $parameters = ['a' => 'b']; $response = new Response(429, [ - 'X-RateLimit-UserLimit' => 10, - 'X-RateLimit-UserRemaining' => 10, - 'X-RateLimit-ClientLimit' => 10, - 'X-RateLimit-ClientRemaining' => 10, - ], json_encode(['data' => ['request' => '/3/account', 'error' => 'oops2', 'method' => 'GET'], 'success' => false, 'status' => 403])); + 'X-RateLimit-UserLimit' => '10', + 'X-RateLimit-UserRemaining' => '10', + 'X-RateLimit-ClientLimit' => '10', + 'X-RateLimit-ClientRemaining' => '10', + ], (string) json_encode(['data' => ['request' => '/3/account', 'error' => 'oops2', 'method' => 'GET'], 'success' => false, 'status' => 403])); $mock = new MockHandler([$response]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); - $httpClient = new TestHttpClient([], $client); + $httpClient = new TestHttpClient([], $client, $handler); $httpClient->get($path, $parameters); } - public function testThrowExceptionWhenBadRequestNoClientMock() + public function testThrowExceptionWhenBadRequestNoClientMock(): void { $this->expectException(\Imgur\Exception\RuntimeException::class); $this->expectExceptionMessage('oops'); @@ -238,21 +239,21 @@ public function testThrowExceptionWhenBadRequestNoClientMock() $parameters = ['a' => 'b']; $response = new Response(429, [ - 'X-RateLimit-UserLimit' => 10, - 'X-RateLimit-UserRemaining' => 10, - 'X-RateLimit-ClientLimit' => 10, - 'X-RateLimit-ClientRemaining' => 10, + 'X-RateLimit-UserLimit' => '10', + 'X-RateLimit-UserRemaining' => '10', + 'X-RateLimit-ClientLimit' => '10', + 'X-RateLimit-ClientRemaining' => '10', ], 'oops'); $mock = new MockHandler([$response]); $handler = HandlerStack::create($mock); $client = new GuzzleClient(['handler' => $handler]); - $httpClient = new HttpClient([], $client); + $httpClient = new HttpClient([], $client, $handler); $httpClient->get($path, $parameters); } - // public function testThrowLogicException() + // public function testThrowLogicException(): void // { // $this->expectException(\Imgur\Exception\ErrorException::class); // @@ -288,7 +289,10 @@ public function testThrowExceptionWhenBadRequestNoClientMock() class TestHttpClient extends HttpClient { - public function getOption($name, $default = null) + /** + * @return array|string|null + */ + public function getOption(string $name, string $default = null) { return isset($this->options[$name]) ? $this->options[$name] : $default; } diff --git a/tests/Middleware/AuthMiddlewareTest.php b/tests/Middleware/AuthMiddlewareTest.php index 040c777..a274db4 100644 --- a/tests/Middleware/AuthMiddlewareTest.php +++ b/tests/Middleware/AuthMiddlewareTest.php @@ -14,9 +14,9 @@ class AuthMiddlewareTest extends TestCase { - public function testDefineClientIdOnBadToken() + public function testDefineClientIdOnBadToken(): void { - $token = 'token'; + $token = ['token']; $clientId = 'clientid'; $mock = new MockHandler([ @@ -39,7 +39,7 @@ function (RequestInterface $request, array $options) { $this->assertInstanceOf(PromiseInterface::class, $promise); } - public function testDefineBeareOnGoodToken() + public function testDefineBeareOnGoodToken(): void { $token = ['access_token' => 'T0K3N']; $clientId = 'clientid'; diff --git a/tests/Middleware/ErrorMiddlewareTest.php b/tests/Middleware/ErrorMiddlewareTest.php index c49f36c..458d7f5 100644 --- a/tests/Middleware/ErrorMiddlewareTest.php +++ b/tests/Middleware/ErrorMiddlewareTest.php @@ -6,13 +6,14 @@ use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; +use Imgur\Exception\ErrorException; use Imgur\Exception\RateLimitException; use Imgur\Middleware\ErrorMiddleware; use PHPUnit\Framework\TestCase; class ErrorMiddlewareTest extends TestCase { - public function testNothinHappenOnOKResponse() + public function testNothinHappenOnOKResponse(): void { $mock = new MockHandler([ new Response(200), @@ -28,15 +29,15 @@ public function testNothinHappenOnOKResponse() $this->assertSame(200, $response->getStatusCode()); } - public function testRateLimitUser() + public function testRateLimitUser(): void { $this->expectException(RateLimitException::class); $this->expectExceptionMessage('No user credits available. The limit is 10'); $mock = new MockHandler([ new Response(429, [ - 'X-RateLimit-UserRemaining' => 0, - 'X-RateLimit-UserLimit' => 10, + 'X-RateLimit-UserRemaining' => '0', + 'X-RateLimit-UserLimit' => '10', ]), ]); $stack = new HandlerStack($mock); @@ -47,18 +48,18 @@ public function testRateLimitUser() $handler($request, [])->wait(); } - public function testRateLimitClient() + public function testRateLimitClient(): void { $this->expectException(RateLimitException::class); $this->expectExceptionMessage('No application credits available. The limit is 10 and will be reset at 2015-09-04'); $mock = new MockHandler([ new Response(429, [ - 'X-RateLimit-UserRemaining' => 9, - 'X-RateLimit-UserLimit' => 10, - 'X-RateLimit-ClientRemaining' => 0, - 'X-RateLimit-ClientLimit' => 10, - 'X-RateLimit-UserReset' => 1441401387, // 4/9/2015 23:16:27 + 'X-RateLimit-UserRemaining' => '9', + 'X-RateLimit-UserLimit' => '10', + 'X-RateLimit-ClientRemaining' => '0', + 'X-RateLimit-ClientLimit' => '10', + 'X-RateLimit-UserReset' => '1441401387', // 4/9/2015 23:16:27 ]), ]); $stack = new HandlerStack($mock); @@ -69,18 +70,18 @@ public function testRateLimitClient() $handler($request, [])->wait(); } - public function testErrorOverCapacity() + public function testErrorOverCapacity(): void { - $this->expectException(\Imgur\Exception\ErrorException::class); + $this->expectException(ErrorException::class); $this->expectExceptionMessage('Request failed with: "Imgur is temporarily over capacity. Please try again later."'); $mock = new MockHandler([ new Response(429, [ - 'X-RateLimit-UserRemaining' => 9, - 'X-RateLimit-UserLimit' => 10, - 'X-RateLimit-ClientRemaining' => 9, - 'X-RateLimit-ClientLimit' => 10, - ], json_encode(['status' => 500, 'success' => false, 'data' => ['error' => 'Imgur is temporarily over capacity. Please try again later.']])), + 'X-RateLimit-UserRemaining' => '9', + 'X-RateLimit-UserLimit' => '10', + 'X-RateLimit-ClientRemaining' => '9', + 'X-RateLimit-ClientLimit' => '10', + ], (string) json_encode(['status' => 500, 'success' => false, 'data' => ['error' => 'Imgur is temporarily over capacity. Please try again later.']])), ]); $stack = new HandlerStack($mock); $stack->push(ErrorMiddleware::error()); @@ -90,18 +91,18 @@ public function testErrorOverCapacity() $handler($request, [])->wait(); } - public function testErrorUploadingTooFast() + public function testErrorUploadingTooFast(): void { - $this->expectException(\Imgur\Exception\ErrorException::class); + $this->expectException(ErrorException::class); $this->expectExceptionMessage('Request to: /3/image.json failed with: "You are uploading too fast. Please wait 59 more minutes."'); $mock = new MockHandler([ new Response(429, [ - 'X-RateLimit-UserRemaining' => 9, - 'X-RateLimit-UserLimit' => 10, - 'X-RateLimit-ClientRemaining' => 9, - 'X-RateLimit-ClientLimit' => 10, - ], json_encode(['status' => 400, 'success' => false, 'data' => ['error' => ['code' => 429, 'message' => 'You are uploading too fast. Please wait 59 more minutes.', 'type' => 'ImgurException', 'exception' => []], 'request' => '/3/image.json', 'method' => 'POST']])), + 'X-RateLimit-UserRemaining' => '9', + 'X-RateLimit-UserLimit' => '10', + 'X-RateLimit-ClientRemaining' => '9', + 'X-RateLimit-ClientLimit' => '10', + ], (string) json_encode(['status' => 400, 'success' => false, 'data' => ['error' => ['code' => 429, 'message' => 'You are uploading too fast. Please wait 59 more minutes.', 'type' => 'ImgurException', 'exception' => []], 'request' => '/3/image.json', 'method' => 'POST']])), ]); $stack = new HandlerStack($mock); $stack->push(ErrorMiddleware::error()); @@ -111,18 +112,18 @@ public function testErrorUploadingTooFast() $handler($request, [])->wait(); } - public function testErrorNoMessageInError() + public function testErrorNoMessageInError(): void { - $this->expectException(\Imgur\Exception\ErrorException::class); + $this->expectException(ErrorException::class); $this->expectExceptionMessage('Request to: /3/image.json failed with: "Error code: 666"'); $mock = new MockHandler([ new Response(429, [ - 'X-RateLimit-UserRemaining' => 9, - 'X-RateLimit-UserLimit' => 10, - 'X-RateLimit-ClientRemaining' => 9, - 'X-RateLimit-ClientLimit' => 10, - ], json_encode(['status' => 400, 'success' => false, 'data' => ['error' => ['code' => 666, 'type' => 'ImgurException', 'exception' => []], 'request' => '/3/image.json', 'method' => 'POST']])), + 'X-RateLimit-UserRemaining' => '9', + 'X-RateLimit-UserLimit' => '10', + 'X-RateLimit-ClientRemaining' => '9', + 'X-RateLimit-ClientLimit' => '10', + ], (string) json_encode(['status' => 400, 'success' => false, 'data' => ['error' => ['code' => 666, 'type' => 'ImgurException', 'exception' => []], 'request' => '/3/image.json', 'method' => 'POST']])), ]); $stack = new HandlerStack($mock); $stack->push(ErrorMiddleware::error()); @@ -132,18 +133,18 @@ public function testErrorNoMessageInError() $handler($request, [])->wait(); } - public function testErrorWithJson() + public function testErrorWithJson(): void { - $this->expectException(\Imgur\Exception\ErrorException::class); + $this->expectException(ErrorException::class); $this->expectExceptionMessage('Request to: /here failed with: "oops"'); $mock = new MockHandler([ new Response(429, [ - 'X-RateLimit-UserRemaining' => 9, - 'X-RateLimit-UserLimit' => 10, - 'X-RateLimit-ClientRemaining' => 9, - 'X-RateLimit-ClientLimit' => 10, - ], json_encode(['data' => ['request' => '/here', 'error' => 'oops']])), + 'X-RateLimit-UserRemaining' => '9', + 'X-RateLimit-UserLimit' => '10', + 'X-RateLimit-ClientRemaining' => '9', + 'X-RateLimit-ClientLimit' => '10', + ], (string) json_encode(['data' => ['request' => '/here', 'error' => 'oops']])), ]); $stack = new HandlerStack($mock); $stack->push(ErrorMiddleware::error()); @@ -153,17 +154,17 @@ public function testErrorWithJson() $handler($request, [])->wait(); } - public function testErrorWithoutJson() + public function testErrorWithoutJson(): void { $this->expectException(\Imgur\Exception\RuntimeException::class); $this->expectExceptionMessage('hihi'); $mock = new MockHandler([ new Response(429, [ - 'X-RateLimit-UserRemaining' => 9, - 'X-RateLimit-UserLimit' => 10, - 'X-RateLimit-ClientRemaining' => 9, - 'X-RateLimit-ClientLimit' => 10, + 'X-RateLimit-UserRemaining' => '9', + 'X-RateLimit-UserLimit' => '10', + 'X-RateLimit-ClientRemaining' => '9', + 'X-RateLimit-ClientLimit' => '10', ], 'hihi'), ]); $stack = new HandlerStack($mock); @@ -174,16 +175,16 @@ public function testErrorWithoutJson() $handler($request, [])->wait(); } - public function testRateLimitPost() + public function testRateLimitPost(): void { $this->expectException(RateLimitException::class); $this->expectExceptionMessage('No post credits available. The limit is 10 and will be reset at 2015-09-04'); $mock = new MockHandler([ new Response(429, [ - 'X-Post-Rate-Limit-Remaining' => 0, - 'X-Post-Rate-Limit-Limit' => 10, - 'X-Post-Rate-Limit-Reset' => 1441401387, // 4/9/2015 23:16:27 + 'X-Post-Rate-Limit-Remaining' => '0', + 'X-Post-Rate-Limit-Limit' => '10', + 'X-Post-Rate-Limit-Reset' => '1441401387', // 4/9/2015 23:16:27 ]), ]); $stack = new HandlerStack($mock);