From 24d5c9efc4e0f0f76a0a891bbf5ba56cd9d508ce Mon Sep 17 00:00:00 2001 From: Fredrik Liljegren Date: Tue, 28 Oct 2014 11:14:23 +0100 Subject: [PATCH 1/3] Easier install. --- .gitignore | 1 + .travis.yml | 7 +------ Makefile | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index 9940a70..73a6194 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /build/ /vendor/ +composer.phar diff --git a/.travis.yml b/.travis.yml index 656d918..915b10a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,13 +5,8 @@ php: - 5.4 - 5.3 -before_script: - - curl -s http://getcomposer.org/installer | php - - php composer.phar install --dev --no-interaction - script: - - mkdir -p build/logs - - ./vendor/bin/phpunit + - make test after_script: - php vendor/bin/coveralls -v diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..93a8c32 --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +install: composer.phar + ./composer.phar install + +update: + ./composer.phar update + +test: vendor/bin/phpunit build + ./vendor/bin/phpunit --strict + +composer.phar: + curl -s http://getcomposer.org/installer | php + +vendor/bin/phpunit: install + +build: + mkdir build + +clean: + rm -f composer.phar + rm -fr vendor + rm -fr build From 53fef751036ddf89012d27ea967930eeac95bb71 Mon Sep 17 00:00:00 2001 From: Fredrik Liljegren Date: Tue, 28 Oct 2014 11:15:29 +0100 Subject: [PATCH 2/3] More verbose message from Exception. --- lib/Exception.php | 47 +++++++++++++++++++++++------------------ tests/ExceptionTest.php | 4 ++-- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/lib/Exception.php b/lib/Exception.php index 17657ba..5ddab66 100644 --- a/lib/Exception.php +++ b/lib/Exception.php @@ -40,9 +40,32 @@ public function getRequestUri() { return $this->connection->getUri(); } + /// Get the data part of the rpc error. public function getData() { return $this->request->errorData; } - public function __toString() { + /// Get the message part of the rpc error. + public function getRpcMessage() { return $this->request->errorMessage; } + + // + // Protected + // + + protected $request; ///< Tivoka\Client\Request The request that failed + protected $connection; ///< Tivoka\Client\Connection\WebSocket The connection that was used + + /** + * Construct a new Exception. + * + * This can't be protected since it extends Exception, but it shouldn't be used. Use factory. + */ + public function __construct(\Tivoka\Client\Connection\WebSocket $connection, Request $request) { + $this->request = $request; + $this->connection = $connection; + + parent::__construct($this->formatMessage(), $request->error); + } + + protected function formatMessage() { $method = $this->request->method; $message = $this->getMessage(); $data = $this->request->errorData; @@ -58,28 +81,10 @@ public function __toString() { $json_encode_flags = JSON_PRETTY_PRINT; } - $output .= "\n" . json_encode($data, $json_encode_flags); + $output .= "\nData: " . json_encode($data, $json_encode_flags); } + $output .= "\nRequest: " . $this->request->request; return $output; } - - // - // Protected - // - - protected $request; ///< Tivoka\Client\Request The request that failed - protected $connection; ///< Tivoka\Client\Connection\WebSocket The connection that was used - - /** - * Construct a new Exception. - * - * This can't be protected since it extends Exception, but it shouldn't be used. Use factory. - */ - public function __construct(\Tivoka\Client\Connection\WebSocket $connection, Request $request) { - $this->request = $request; - $this->connection = $connection; - - parent::__construct($request->errorMessage, $request->error); - } } diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php index 9c35891..34de0ca 100644 --- a/tests/ExceptionTest.php +++ b/tests/ExceptionTest.php @@ -22,14 +22,14 @@ public function testUnspecifiedExceptionCode() { $this->assertNotNull($e, 'A Textalk\WebshopClient\Exception should be thrown.'); } - public function testToStringWithData() { + public function testGetMessageWithData() { try { $connection = new Connection(array('webshop' => 22222)); $new_order = new ApiInstance('Order', null, $connection); $new_order->set(array('language' => 'foo')); // Will not validate } catch (Textalk\WebshopClient\Exception $e) { - $string = "$e"; + $string = $e->getMessage(); $data = $e->getData(); $this->assertRegExp('/^Order.set: /', $string); From 1ad4d462137467daef4136a30d3fb97259048e6b Mon Sep 17 00:00:00 2001 From: Fredrik Liljegren Date: Tue, 28 Oct 2014 13:54:14 +0100 Subject: [PATCH 3/3] Fixing message in exception getMessage. --- lib/Exception.php | 10 +++++----- tests/ExceptionTest.php | 10 ++++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/Exception.php b/lib/Exception.php index 5ddab66..07414f8 100644 --- a/lib/Exception.php +++ b/lib/Exception.php @@ -66,9 +66,9 @@ public function __construct(\Tivoka\Client\Connection\WebSocket $connection, Req } protected function formatMessage() { - $method = $this->request->method; - $message = $this->getMessage(); - $data = $this->request->errorData; + $method = $this->request->method; + $message = $this->request->errorMessage; + $data = $this->request->errorData; $phpversion = explode('.', phpversion()); @@ -81,9 +81,9 @@ protected function formatMessage() { $json_encode_flags = JSON_PRETTY_PRINT; } - $output .= "\nData: " . json_encode($data, $json_encode_flags); + $output .= "\nError data: " . json_encode($data, $json_encode_flags); } - $output .= "\nRequest: " . $this->request->request; + $output .= "\nOn request: " . $this->request->request; return $output; } diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php index 34de0ca..e291102 100644 --- a/tests/ExceptionTest.php +++ b/tests/ExceptionTest.php @@ -25,15 +25,17 @@ public function testUnspecifiedExceptionCode() { public function testGetMessageWithData() { try { $connection = new Connection(array('webshop' => 22222)); - $new_order = new ApiInstance('Order', null, $connection); - $new_order->set(array('language' => 'foo')); // Will not validate + + // Make an invalid set call. + $connection->Context->set(array('webshop' => 'Bad shop'), true); } catch (Textalk\WebshopClient\Exception $e) { $string = $e->getMessage(); $data = $e->getData(); - $this->assertRegExp('/^Order.set: /', $string); - $this->assertInternalType('array', $data); + $this->assertRegExp('/^Context.set: Invalid params./', $string); + $this->assertRegExp('/Error data:/', $string); + $this->assertRegExp('/On request:/', $string); } $this->assertNotNull($e, 'A Textalk\WebshopClient\Exception should be thrown.');