From bcbaa4b89b94f409d33cf4a99391efb520421e21 Mon Sep 17 00:00:00 2001 From: Percy Mamedy Date: Mon, 6 Jun 2016 10:31:35 +0400 Subject: [PATCH] Refactor some styles and codes --- .gitignore | 2 +- README.md | 3 +-- src/Bridge.php | 5 +++-- tests/TestBridge.php | 6 ------ tests/TestToken.php | 51 ++++++++++++++++++++++++++++++++++---------- 5 files changed, 45 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 337a38d..0fdcea6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /vendor/ -/.idea composer.lock +composer.phar diff --git a/README.md b/README.md index fc6525f..796faa5 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,7 @@ [![SensioLabsInsight](https://insight.sensiolabs.com/projects/4e21093a-cc60-4a75-b7fe-cb29053faf6c/big.png)](https://insight.sensiolabs.com/projects/4e21093a-cc60-4a75-b7fe-cb29053faf6c) -
- +[![StyleCI](https://styleci.io/repos/59507474/shield?style=flat)](https://styleci.io/repos/59507474) [![Build Status](https://travis-ci.org/findbrok/php-watson-api-bridge.svg?branch=master)](https://travis-ci.org/findbrok/php-watson-api-bridge) [![Latest Stable Version](https://poser.pugx.org/findbrok/php-watson-api-bridge/v/stable)](https://packagist.org/packages/findbrok/php-watson-api-bridge) [![Total Downloads](https://poser.pugx.org/findbrok/php-watson-api-bridge/downloads)](https://packagist.org/packages/findbrok/php-watson-api-bridge) diff --git a/src/Bridge.php b/src/Bridge.php index 742be18..929bf3b 100644 --- a/src/Bridge.php +++ b/src/Bridge.php @@ -5,6 +5,7 @@ use FindBrok\WatsonBridge\Exceptions\WatsonBridgeException; use GuzzleHttp\Client; use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\Psr7\Response; /** * Class Bridge. @@ -233,11 +234,11 @@ public function cleanOptions($options = []) /** * Failed Request to Watson. * - * @param \GuzzleHttp\Psr7\Response $response + * @param Response $response * * @throws WatsonBridgeException */ - public function failedRequest($response) + public function failedRequest(Response $response) { //Decode Response $decodedResponse = json_decode($response->getBody()->getContents(), true); diff --git a/tests/TestBridge.php b/tests/TestBridge.php index 3c92e6b..d44e1a4 100644 --- a/tests/TestBridge.php +++ b/tests/TestBridge.php @@ -266,12 +266,6 @@ public function testGetTokenMethodWhenTokenExpiredAndFetchTokenAgain() $this->deleteTestTokenFile('token-foofoo'); } - /** - * Test a Get request which fails. - * - * @expectedException \FindBrok\WatsonBridge\Exceptions\WatsonBridgeException - }*/ - /** * Test that when the token is expired we refresh the token and try again. * diff --git a/tests/TestToken.php b/tests/TestToken.php index 7babd2b..aea1cc0 100644 --- a/tests/TestToken.php +++ b/tests/TestToken.php @@ -39,6 +39,35 @@ public function tearDown() unlink($this->getTokenStoragePath('token-username.json')); } + /** + * Creates a test token file. + * + * @param string $name + * @param array $data + * + * @return void + */ + public function createTestTokenFile($name = '', $data = []) + { + file_put_contents( + $this->getTokenStoragePath($name.'.json'), + collect($data)->toJson(), + LOCK_EX + ); + } + + /** + * Delete a test token file. + * + * @param string $name + * + * @return void + */ + public function deleteTestTokenFile($name = '') + { + unlink($this->getTokenStoragePath($name.'.json')); + } + /** * Return Token Storage Folder. * @@ -145,7 +174,7 @@ public function testSaveMethod() $this->assertFileExists($this->getTokenStoragePath('token-username2.json')); $this->assertJsonStringEqualsJsonFile($this->getTokenStoragePath('token-username2.json'), collect($payload)->toJson()); - unlink($this->getTokenStoragePath('token-username2.json')); + $this->deleteTestTokenFile('token-username2'); } /** @@ -155,11 +184,11 @@ public function testSaveMethod() */ public function testLoadFromFileMethodAndGetPayLoadMethod() { - file_put_contents($this->getTokenStoragePath('token-username3.json'), collect([ + $this->createTestTokenFile('token-username3', [ 'token' => 'sometoken', 'expires_in' => 3600, 'created' => 1463977413, - ])->toJson(), LOCK_EX); + ]); $token = new Token('username3'); $this->assertEquals([ @@ -168,7 +197,7 @@ public function testLoadFromFileMethodAndGetPayLoadMethod() 'created' => 1463977413, ], $token->getPayload()); - unlink($this->getTokenStoragePath('token-username3.json')); + $this->deleteTestTokenFile('token-username3'); } /** @@ -182,16 +211,16 @@ public function testIsValidMethod() $token2 = new Token('username2'); $this->assertFalse($token2->isValid()); - file_put_contents($this->getTokenStoragePath('token-username3.json'), collect([ + $this->createTestTokenFile('token-username3', [ 'token' => 'sometoken', 'expires_in' => 3600, 'created' => 1463977413, - ])->toJson(), LOCK_EX); + ]); $token3 = new Token('username3'); $this->assertFalse($token3->isValid()); - unlink($this->getTokenStoragePath('token-username3.json')); + $this->deleteTestTokenFile('token-username3'); } /** @@ -201,11 +230,11 @@ public function testIsValidMethod() */ public function testGetTokenMethod() { - file_put_contents($this->getTokenStoragePath('token-username3.json'), collect([ + $this->createTestTokenFile('token-username3', [ 'token' => 'sometoken', 'expires_in' => 3600, 'created' => Carbon::now()->format('U'), - ])->toJson(), LOCK_EX); + ]); $token3 = new Token('username3'); $this->assertEquals('sometoken', $token3->getToken()); @@ -213,7 +242,7 @@ public function testGetTokenMethod() $token2 = new Token('username2'); $this->assertNull($token2->getToken()); - unlink($this->getTokenStoragePath('token-username3.json')); + $this->deleteTestTokenFile('token-username3'); } /** @@ -235,6 +264,6 @@ public function testUpdateTokenMethod() $this->assertEquals('newToken', $token->getToken()); $this->assertFileExists($this->getTokenStoragePath('token-username3.json')); - unlink($this->getTokenStoragePath('token-username3.json')); + $this->deleteTestTokenFile('token-username3'); } }