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 diff --git a/lib/Exception.php b/lib/Exception.php index 17657ba..07414f8 100644 --- a/lib/Exception.php +++ b/lib/Exception.php @@ -40,29 +40,11 @@ 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() { - $method = $this->request->method; - $message = $this->getMessage(); - $data = $this->request->errorData; - - $phpversion = explode('.', phpversion()); - - $output = "$method: $message"; - - if (!empty($data)) { - $json_encode_flags = 0; - - if (($phpversion[0] === '5' && $phpversion[1] >= 4) || $phpversion[0] > 5) { - $json_encode_flags = JSON_PRETTY_PRINT; - } - - $output .= "\n" . json_encode($data, $json_encode_flags); - } - - return $output; - } + /// Get the message part of the rpc error. + public function getRpcMessage() { return $this->request->errorMessage; } // // Protected @@ -80,6 +62,29 @@ public function __construct(\Tivoka\Client\Connection\WebSocket $connection, Req $this->request = $request; $this->connection = $connection; - parent::__construct($request->errorMessage, $request->error); + parent::__construct($this->formatMessage(), $request->error); + } + + protected function formatMessage() { + $method = $this->request->method; + $message = $this->request->errorMessage; + $data = $this->request->errorData; + + $phpversion = explode('.', phpversion()); + + $output = "$method: $message"; + + if (!empty($data)) { + $json_encode_flags = 0; + + if (($phpversion[0] === '5' && $phpversion[1] >= 4) || $phpversion[0] > 5) { + $json_encode_flags = JSON_PRETTY_PRINT; + } + + $output .= "\nError data: " . json_encode($data, $json_encode_flags); + } + $output .= "\nOn request: " . $this->request->request; + + return $output; } } diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php index 9c35891..e291102 100644 --- a/tests/ExceptionTest.php +++ b/tests/ExceptionTest.php @@ -22,18 +22,20 @@ 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 + + // Make an invalid set call. + $connection->Context->set(array('webshop' => 'Bad shop'), true); } catch (Textalk\WebshopClient\Exception $e) { - $string = "$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.');