From 94a6e03ac4c286e5060de7bce610955683e0404a Mon Sep 17 00:00:00 2001 From: Salavat Sitdikov Date: Tue, 19 Jun 2018 12:13:10 +0000 Subject: [PATCH] Apply fixes from StyleCI --- examples/example.php | 21 +-- src/Client/AbstractClient.php | 4 +- src/Client/ClientInterface.php | 4 +- src/Client/TelegraphClient.php | 5 +- src/Exception/ContentTextRequired.php | 6 +- src/Exception/ShortNameRequiredException.php | 6 +- src/Request/AbstractAccountRequest.php | 4 +- src/Request/AbstractPageRequest.php | 4 +- src/Request/CreateAccountRequest.php | 12 +- src/Request/CreatePageRequest.php | 16 +- src/Request/EditAccountInfoRequest.php | 9 +- src/Request/EditPageRequest.php | 10 +- src/Request/GetAccountInfoRequest.php | 11 +- src/Request/GetPageListRequest.php | 16 +- src/Request/GetPageRequest.php | 6 +- src/Request/GetViewsRequest.php | 5 +- src/Request/RequestInterface.php | 4 +- .../RequestObject/ViewsRequestObject.php | 4 +- src/Request/RevokeAccessTokenRequest.php | 5 +- src/Type/Account.php | 4 +- .../ContentType/AbstractNodeElementType.php | 5 +- src/Type/ContentType/ImageType.php | 20 +- src/Type/ContentType/LinkType.php | 4 +- .../ContentType/NodeElementTypeInterface.php | 4 +- src/Type/ContentType/ParagraphType.php | 4 +- src/Type/ContentType/YoutubeType.php | 8 +- src/Type/Page.php | 4 +- src/Type/PageList.php | 4 +- src/Type/PageViews.php | 4 +- tests/Account/CreateAccountRequestTest.php | 8 +- tests/Account/GetPageRequestTest.php | 6 +- tests/Client/TelegraphClientTest.php | 174 +++++++++--------- tests/Page/ContentTypesTest.php | 24 +-- tests/Page/CreatePageRequestTest.php | 20 +- 34 files changed, 267 insertions(+), 178 deletions(-) diff --git a/examples/example.php b/examples/example.php index c0dc6af..38811b8 100644 --- a/examples/example.php +++ b/examples/example.php @@ -1,20 +1,18 @@ 'https://api.telegra.ph/']); @@ -22,6 +20,7 @@ $account = new Account(); $account->setShortName('Test.Account'); + try { $account = $telegraph->createAccount( new CreateAccountRequest($account) @@ -56,4 +55,4 @@ ); } catch (\Exception $e) { // logger -} \ No newline at end of file +} diff --git a/src/Client/AbstractClient.php b/src/Client/AbstractClient.php index 9c13a3a..9fbd5ce 100644 --- a/src/Client/AbstractClient.php +++ b/src/Client/AbstractClient.php @@ -1,4 +1,6 @@ -getUrlRequest(), $request->getParams() ); + return $request->handleResponse($response); } diff --git a/src/Exception/ContentTextRequired.php b/src/Exception/ContentTextRequired.php index bad1fe1..2f95562 100644 --- a/src/Exception/ContentTextRequired.php +++ b/src/Exception/ContentTextRequired.php @@ -1,4 +1,6 @@ -getBody()->getContents()); if ($json->ok === false && isset($json->error)) { switch ($json->error) { - case ('SHORT_NAME_REQUIRED'): + case 'SHORT_NAME_REQUIRED': throw new ShortNameRequiredException(); } + throw new \Exception($json->error); } $this->account->setShortName($json->result->short_name); @@ -49,6 +54,7 @@ public function handleResponse(ResponseInterface $response): Account $this->account->setAuthorUrl($json->result->author_url); $this->account->setAuthUrl($json->result->auth_url); $this->account->setAccessToken($json->result->access_token); + return $this->account; } } diff --git a/src/Request/CreatePageRequest.php b/src/Request/CreatePageRequest.php index bbbf634..b59cfde 100644 --- a/src/Request/CreatePageRequest.php +++ b/src/Request/CreatePageRequest.php @@ -1,4 +1,6 @@ - $this->account->getAccessToken(), - 'title' => $this->page->getTitle(), - 'content' => $this->page->getContent(), + 'title' => $this->page->getTitle(), + 'content' => $this->page->getContent(), ]; if ($this->page->getAuthorName()) { $params['author_name'] = $this->page->getAuthorName(); @@ -29,13 +31,16 @@ public function getParams(): array if ($this->returnContent) { $params['return_content'] = true; } + return ['json' => $params]; } /** - * @param ResponseInterface $response + * @param ResponseInterface $response + * * @throws ContentTextRequired * @throws \Exception + * * @return \SSitdikov\TelegraphAPI\Type\Page */ public function handleResponse(ResponseInterface $response): Page @@ -43,7 +48,7 @@ public function handleResponse(ResponseInterface $response): Page $json = json_decode($response->getBody()->getContents()); if ($json->ok === false && isset($json->error)) { switch ($json->error) { - case ('CONTENT_TEXT_REQUIRED'): + case 'CONTENT_TEXT_REQUIRED': throw new ContentTextRequired(); default: throw new \Exception($json->error); @@ -60,6 +65,7 @@ public function handleResponse(ResponseInterface $response): Page if ($this->getReturnContent()) { $this->page->setContent($json->result->content); } + return $this->page; } } diff --git a/src/Request/EditAccountInfoRequest.php b/src/Request/EditAccountInfoRequest.php index 28bdb5a..7597a19 100644 --- a/src/Request/EditAccountInfoRequest.php +++ b/src/Request/EditAccountInfoRequest.php @@ -1,4 +1,6 @@ -account->setAuthorName($json->result->author_name); $this->account->setAuthorUrl($json->result->author_url); $this->account->setShortName($json->result->short_name); + return $this->account; } } diff --git a/src/Request/EditPageRequest.php b/src/Request/EditPageRequest.php index b79a0d5..d0ec77f 100644 --- a/src/Request/EditPageRequest.php +++ b/src/Request/EditPageRequest.php @@ -1,4 +1,6 @@ - $this->account->getAccessToken(), - 'title' => $this->page->getTitle(), - 'content' => $this->page->getContent(), + 'title' => $this->page->getTitle(), + 'content' => $this->page->getContent(), ]; if ($this->page->getAuthorName()) { $params['author_name'] = $this->page->getAuthorName(); @@ -28,6 +30,7 @@ public function getParams(): array if ($this->returnContent) { $params['return_content'] = true; } + return ['json' => $params]; } @@ -51,6 +54,7 @@ public function handleResponse(ResponseInterface $response): Page if ($this->getReturnContent()) { $this->page->setContent($json->result->content); } + return $this->page; } } diff --git a/src/Request/GetAccountInfoRequest.php b/src/Request/GetAccountInfoRequest.php index d342f49..dfb2e51 100644 --- a/src/Request/GetAccountInfoRequest.php +++ b/src/Request/GetAccountInfoRequest.php @@ -1,4 +1,6 @@ - $this->account->getAccessToken(), - 'fields' => $this->fields, + 'fields' => $this->fields, ]; return ['json' => $params]; } /** - * @param ResponseInterface $response + * @param ResponseInterface $response + * * @throws \Exception + * * @return Account */ public function handleResponse(ResponseInterface $response): Account @@ -57,6 +61,7 @@ public function handleResponse(ResponseInterface $response): Account $account->setPageCount($json->result->page_count); } $account->setAccessToken($this->account->getAccessToken()); + return $account; } } diff --git a/src/Request/GetPageListRequest.php b/src/Request/GetPageListRequest.php index f223ebf..a3dd243 100644 --- a/src/Request/GetPageListRequest.php +++ b/src/Request/GetPageListRequest.php @@ -1,4 +1,6 @@ - $this->account->getAccessToken(), - 'offset' => $this->offset, - 'limit' => $this->limit, + 'offset' => $this->offset, + 'limit' => $this->limit, ]; + return ['json' => $params]; } /** - * @param ResponseInterface $response + * @param ResponseInterface $response + * * @throws \Exception + * * @return PageList */ public function handleResponse(ResponseInterface $response): PageList @@ -64,6 +71,7 @@ public function handleResponse(ResponseInterface $response): PageList $pageList = new PageList(); $pageList->setTotalCount($json->result->total_count); $pageList->setPages($json->result->pages); + return $pageList; } } diff --git a/src/Request/GetPageRequest.php b/src/Request/GetPageRequest.php index e8c258d..27ad877 100644 --- a/src/Request/GetPageRequest.php +++ b/src/Request/GetPageRequest.php @@ -1,4 +1,6 @@ - $this->returnContent, ]; + return ['json' => $params]; } @@ -40,6 +43,7 @@ public function handleResponse(ResponseInterface $response): Page if ($this->getReturnContent()) { $this->page->setContent($json->result->content); } + return $this->page; } } diff --git a/src/Request/GetViewsRequest.php b/src/Request/GetViewsRequest.php index f6e1603..311c000 100644 --- a/src/Request/GetViewsRequest.php +++ b/src/Request/GetViewsRequest.php @@ -1,4 +1,6 @@ -viewsRequest->getMonth(); $params['year'] = $this->viewsRequest->getYear(); } + return ['json' => $params]; } diff --git a/src/Request/RequestInterface.php b/src/Request/RequestInterface.php index e4cf5aa..014ada1 100644 --- a/src/Request/RequestInterface.php +++ b/src/Request/RequestInterface.php @@ -1,4 +1,6 @@ - $this->account->getAccessToken()]; + return ['json' => $response]; } diff --git a/src/Type/Account.php b/src/Type/Account.php index e0674be..ce82fbb 100644 --- a/src/Type/Account.php +++ b/src/Type/Account.php @@ -1,4 +1,6 @@ -children)) { $result['children'] = $this->children; } + return $result; } } diff --git a/src/Type/ContentType/ImageType.php b/src/Type/ContentType/ImageType.php index d1d8923..db3ef4e 100644 --- a/src/Type/ContentType/ImageType.php +++ b/src/Type/ContentType/ImageType.php @@ -1,4 +1,6 @@ -tag = 'figure'; $image = [ - 'tag' => 'img', + 'tag' => 'img', 'attrs' => [ 'src' => $this->attrs['src'], ], ]; $imageDiv = [ - 'tag' => 'div', + 'tag' => 'div', 'attrs' => [ 'class' => 'figure_wrapper', ], @@ -57,12 +59,12 @@ public function jsonSerialize() $this->attrs = []; $children = [ - $imageDiv + $imageDiv, ]; if ($this->caption) { $captionElement = [ - 'tag' => 'figcaption', + 'tag' => 'figcaption', 'children' => [ $this->caption, ], @@ -74,6 +76,4 @@ public function jsonSerialize() return parent::jsonSerialize(); } - - } diff --git a/src/Type/ContentType/LinkType.php b/src/Type/ContentType/LinkType.php index c58fcb4..fa2b773 100644 --- a/src/Type/ContentType/LinkType.php +++ b/src/Type/ContentType/LinkType.php @@ -1,4 +1,6 @@ - 'iframe', + 'tag' => 'iframe', 'attrs' => [ 'src' => '/embed/youtube?url='.rawurlencode($src), ], @@ -20,7 +22,7 @@ public function setSrc($src) public function setCaption($caption) { $this->children[] = [ - 'tag' => 'figcaption', + 'tag' => 'figcaption', 'children' => [ $caption, ], diff --git a/src/Type/Page.php b/src/Type/Page.php index a5f7b72..a4eb63f 100644 --- a/src/Type/Page.php +++ b/src/Type/Page.php @@ -1,4 +1,6 @@ -assertEquals('createAccount', $request->getUrlRequest()); $expectedParams = [ 'json' => [ - 'short_name' => $shortName, + 'short_name' => $shortName, 'author_name' => $authorName, - 'author_url' => $authorUrl, + 'author_url' => $authorUrl, ], ]; $this->assertEquals($expectedParams, $request->getParams()); diff --git a/tests/Account/GetPageRequestTest.php b/tests/Account/GetPageRequestTest.php index 30b55e8..243b0ed 100644 --- a/tests/Account/GetPageRequestTest.php +++ b/tests/Account/GetPageRequestTest.php @@ -1,4 +1,6 @@ - [ 'access_token' => $accessToken, - 'fields' => [ + 'fields' => [ 'short_name', 'author_name', 'author_url', diff --git a/tests/Client/TelegraphClientTest.php b/tests/Client/TelegraphClientTest.php index 0b56bd2..82a6aeb 100644 --- a/tests/Client/TelegraphClientTest.php +++ b/tests/Client/TelegraphClientTest.php @@ -1,4 +1,6 @@ - true, + 'ok' => true, 'result' => [ - 'short_name' => $this->account->getShortName(), - 'author_name' => '', - 'author_url' => '', - 'auth_url' => $authUrl, + 'short_name' => $this->account->getShortName(), + 'author_name' => '', + 'author_url' => '', + 'auth_url' => $authUrl, 'access_token' => $accessToken, ], ]; @@ -97,7 +99,7 @@ private function getTelegraph($responseContent): TelegraphClient public function createAccountError() { $responseContent = [ - 'ok' => false, + 'ok' => false, 'error' => 'SomeStringError', ]; $telegraph = $this->getTelegraph($responseContent); @@ -115,7 +117,7 @@ public function createAccountError() public function createAccountErrorNotRequiredFields() { $responseContent = [ - 'ok' => false, + 'ok' => false, 'error' => 'SHORT_NAME_REQUIRED', ]; @@ -136,14 +138,14 @@ public function getAccountInfo() $accessToken = md5($authUrl.random_bytes(16)); $pageCount = random_int(100, 9999); $responseContent = [ - 'ok' => true, + 'ok' => true, 'result' => [ - 'short_name' => $this->account->getShortName(), - 'author_name' => '', - 'author_url' => '', - 'auth_url' => $authUrl, + 'short_name' => $this->account->getShortName(), + 'author_name' => '', + 'author_url' => '', + 'auth_url' => $authUrl, 'access_token' => $accessToken, - 'page_count' => $pageCount, + 'page_count' => $pageCount, ], ]; @@ -164,7 +166,7 @@ public function getAccountInfoError() { $errorString = md5(random_bytes(16)); $responseContent = [ - 'ok' => false, + 'ok' => false, 'error' => $errorString, ]; @@ -191,14 +193,14 @@ public function editAccountInfo() $this->account->setAuthorName($authorName); $this->account->setAuthorUrl($authorUrl); $responseContent = [ - 'ok' => true, + 'ok' => true, 'result' => [ - 'short_name' => $this->account->getShortName(), - 'author_name' => $authorName, - 'author_url' => $authorUrl, - 'auth_url' => $authUrl, + 'short_name' => $this->account->getShortName(), + 'author_name' => $authorName, + 'author_url' => $authorUrl, + 'auth_url' => $authUrl, 'access_token' => $accessToken, - 'page_count' => $pageCount, + 'page_count' => $pageCount, ], ]; @@ -221,7 +223,7 @@ public function editAccountInfoError() { $errorString = md5(random_bytes(16)); $responseContent = [ - 'ok' => false, + 'ok' => false, 'error' => $errorString, ]; @@ -243,10 +245,10 @@ public function revokeAccessToken() $authUrl = md5(random_bytes(16)); $this->account->setAccessToken($accessToken); $responseContent = [ - 'ok' => true, + 'ok' => true, 'result' => [ 'access_token' => $newAccessToken, - 'auth_url' => $authUrl, + 'auth_url' => $authUrl, ], ]; @@ -268,7 +270,7 @@ public function revokeAccessTokenError() { $errorString = md5(random_bytes(16)); $responseContent = [ - 'ok' => false, + 'ok' => false, 'error' => $errorString, ]; @@ -292,14 +294,14 @@ public function createPage() $views = random_int(1, 999); $canEdit = rand(0, 1) === 1 ? true : false; $responseContent = [ - 'ok' => true, + 'ok' => true, 'result' => [ - 'title' => $title, - 'path' => $path, - 'url' => $url, + 'title' => $title, + 'path' => $path, + 'url' => $url, 'description' => $description, - 'views' => $views, - 'can_edit' => $canEdit, + 'views' => $views, + 'can_edit' => $canEdit, ], ]; @@ -349,16 +351,16 @@ public function createPageWithContent() ['tag' => 'a', 'attrs' => ['href' => 'link'], 'children' => ['text']], ]; $responseContent = [ - 'ok' => true, + 'ok' => true, 'result' => [ - 'title' => $title, - 'path' => $path, - 'url' => $url, + 'title' => $title, + 'path' => $path, + 'url' => $url, 'description' => $description, - 'views' => $views, - 'can_edit' => $canEdit, - 'content' => $content, - 'image_url' => $imageUrl, + 'views' => $views, + 'can_edit' => $canEdit, + 'content' => $content, + 'image_url' => $imageUrl, ], ]; @@ -382,7 +384,7 @@ public function createPageErrorContentRequired() { $errorString = 'CONTENT_TEXT_REQUIRED'; $responseContent = [ - 'ok' => false, + 'ok' => false, 'error' => $errorString, ]; @@ -402,7 +404,7 @@ public function createPageError() { $errorString = md5(random_bytes(16)); $responseContent = [ - 'ok' => false, + 'ok' => false, 'error' => $errorString, ]; @@ -427,15 +429,15 @@ public function getPage() $canEdit = rand(0, 1) === 1 ? true : false; $imageUrl = md5(random_bytes(16)); $responseContent = [ - 'ok' => true, + 'ok' => true, 'result' => [ - 'title' => $title, - 'path' => $path, - 'url' => $url, + 'title' => $title, + 'path' => $path, + 'url' => $url, 'description' => $description, - 'views' => $views, - 'can_edit' => $canEdit, - 'image_url' => $imageUrl, + 'views' => $views, + 'can_edit' => $canEdit, + 'image_url' => $imageUrl, ], ]; @@ -486,16 +488,16 @@ public function getPageWithContent() ['tag' => 'a', 'attrs' => ['href' => 'link'], 'children' => ['text']], ]; $responseContent = [ - 'ok' => true, + 'ok' => true, 'result' => [ - 'title' => $title, - 'path' => $path, - 'url' => $url, + 'title' => $title, + 'path' => $path, + 'url' => $url, 'description' => $description, - 'views' => $views, - 'can_edit' => $canEdit, - 'content' => $content, - 'image_url' => $imageUrl, + 'views' => $views, + 'can_edit' => $canEdit, + 'content' => $content, + 'image_url' => $imageUrl, ], ]; @@ -519,7 +521,7 @@ public function getPageError() { $errorString = md5(random_bytes(16)); $responseContent = [ - 'ok' => false, + 'ok' => false, 'error' => $errorString, ]; @@ -544,15 +546,15 @@ public function editPage() $canEdit = rand(0, 1) === 1 ? true : false; $imageUrl = md5(random_bytes(16)); $responseContent = [ - 'ok' => true, + 'ok' => true, 'result' => [ - 'title' => $title, - 'path' => $path, - 'url' => $url, + 'title' => $title, + 'path' => $path, + 'url' => $url, 'description' => $description, - 'views' => $views, - 'can_edit' => $canEdit, - 'image_url' => $imageUrl, + 'views' => $views, + 'can_edit' => $canEdit, + 'image_url' => $imageUrl, ], ]; @@ -603,16 +605,16 @@ public function editPageWithContent() ['tag' => 'a', 'attrs' => ['href' => 'link'], 'children' => ['text']], ]; $responseContent = [ - 'ok' => true, + 'ok' => true, 'result' => [ - 'title' => $title, - 'path' => $path, - 'url' => $url, + 'title' => $title, + 'path' => $path, + 'url' => $url, 'description' => $description, - 'views' => $views, - 'can_edit' => $canEdit, - 'content' => $content, - 'image_url' => $imageUrl, + 'views' => $views, + 'can_edit' => $canEdit, + 'content' => $content, + 'image_url' => $imageUrl, ], ]; @@ -639,7 +641,7 @@ public function editPageError() { $errorString = md5(random_bytes(16)); $responseContent = [ - 'ok' => false, + 'ok' => false, 'error' => $errorString, ]; @@ -658,9 +660,9 @@ public function getPageList() { $totalCount = random_int(1, 999); $responseContent = [ - 'ok' => true, + 'ok' => true, 'result' => [ - 'pages' => [], + 'pages' => [], 'total_count' => $totalCount, ], ]; @@ -676,13 +678,13 @@ public function getPageList() $this->assertEquals([ 'json' => [ 'access_token' => $this->account->getAccessToken(), - 'limit' => $limit, - 'offset' => $offset, + 'limit' => $limit, + 'offset' => $offset, ], ], $request->getParams()); $this->assertEquals($totalCount, $pageList->getTotalCount()); - /** + /* * @todo Check pages result */ $this->assertEquals([], $pageList->getPages()); @@ -691,8 +693,8 @@ public function getPageList() $this->assertEquals([ 'json' => [ 'access_token' => $this->account->getAccessToken(), - 'limit' => 50, - 'offset' => 0, + 'limit' => 50, + 'offset' => 0, ], ], $request->getParams()); } @@ -704,7 +706,7 @@ public function getPageListError() { $errorString = md5(random_bytes(16)); $responseContent = [ - 'ok' => false, + 'ok' => false, 'error' => $errorString, ]; @@ -723,7 +725,7 @@ public function getPageViews() { $views = random_int(1, 99); $responseContent = [ - 'ok' => true, + 'ok' => true, 'result' => [ 'views' => $views, ], @@ -746,10 +748,10 @@ public function getPageViews() $request = new GetViewsRequest($requestObject); $this->assertEquals([ 'json' => [ - 'year' => $year, + 'year' => $year, 'month' => $month, - 'day' => $day, - 'hour' => $hour, + 'day' => $day, + 'hour' => $hour, ], ], $request->getParams()); @@ -764,7 +766,7 @@ public function getViewsError() { $errorString = md5(random_bytes(16)); $responseContent = [ - 'ok' => false, + 'ok' => false, 'error' => $errorString, ]; diff --git a/tests/Page/ContentTypesTest.php b/tests/Page/ContentTypesTest.php index 8f18e9a..b9cb68d 100644 --- a/tests/Page/ContentTypesTest.php +++ b/tests/Page/ContentTypesTest.php @@ -1,4 +1,6 @@ -setHref('https://github.com'); $this->assertJson(json_encode([ - 'tag' => 'a', - 'attrs' => ['href' => 'https://github.com'], + 'tag' => 'a', + 'attrs' => ['href' => 'https://github.com'], 'children' => ['Test link'], ]), json_encode($link)); } @@ -36,13 +38,11 @@ public function imageType() $image->setSrc('https://github.com/logo.png'); $this->assertJson(json_encode([ - 'tag' => 'img', + 'tag' => 'img', 'attrs' => [ 'src' => 'https://github.com/logo.png', ], ]), json_encode($image)); - - } /** @@ -55,7 +55,7 @@ public function paragraphType() $this->assertJson( json_encode([ - 'tag' => 'p', + 'tag' => 'p', 'children' => [ 'Test', ], @@ -69,11 +69,11 @@ public function paragraphType() $this->assertJson( json_encode([ - 'tag' => 'p', + 'tag' => 'p', 'children' => [ 'Test', [ - 'tag' => 'p', + 'tag' => 'p', 'children' => [ 'Test', ], @@ -95,16 +95,16 @@ public function youtubeType() $this->assertJson( json_encode([ - 'tag' => 'figure', + 'tag' => 'figure', 'children' => [ [ - 'tag' => 'iframe', + 'tag' => 'iframe', 'attrs' => [ 'src' => 'test', ], ], [ - 'tag' => 'figcaption', + 'tag' => 'figcaption', 'children' => [ 'Caption', ], diff --git a/tests/Page/CreatePageRequestTest.php b/tests/Page/CreatePageRequestTest.php index 8d59cc0..5d99e0c 100644 --- a/tests/Page/CreatePageRequestTest.php +++ b/tests/Page/CreatePageRequestTest.php @@ -1,6 +1,8 @@ - [ 'access_token' => $accessToken, - 'title' => $title, - 'content' => $content, + 'title' => $title, + 'content' => $content, ], ]; $this->assertEquals($expectedParams, $request->getParams()); @@ -53,11 +55,11 @@ public function simpleCreatePageRequest() $request->isReturnContent(); $expectedParams = [ 'json' => [ - 'access_token' => $accessToken, - 'title' => $title, - 'content' => $content, - 'author_name' => $authorName, - 'author_url' => $authorUrl, + 'access_token' => $accessToken, + 'title' => $title, + 'content' => $content, + 'author_name' => $authorName, + 'author_url' => $authorUrl, 'return_content' => true, ], ];