diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..c14fdd1 --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1,3 @@ +service_name: travis-ci +coverage_clover: build/coverage-clover.xml +json_path: build/coveralls-upload.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..153cf3e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +; top-most EditorConfig file +root = true + +; Unix-style newlines +[*] +end_of_line = LF + +[*.php] +indent_style = space +indent_size = 4 diff --git a/.gitignore b/.gitignore index 4fbb073..53d8a87 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /vendor/ +/build/ /composer.lock +/phpunit.xml diff --git a/.travis.yml b/.travis.yml index 5926668..a70f444 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,21 +1,43 @@ language: php -php: - - 5.4 - - 5.5 - - 5.6 - - 7.0 +sudo: false notifications: - email: dev@anime-db.org + email: deploy@anime-db.org + +branches: + except: + - /^analysis-.*$/ + +matrix: + fast_finish: true + include: + - php: 7.1 + - php: 7.0 + - php: 5.6 + - php: 5.5 + - php: 5.5 + env: SYMFONY_VERSION=2.7.* + - php: 5.5 + env: SYMFONY_VERSION=2.8.* + - php: 5.5 + env: SYMFONY_VERSION=3.0.* + - php: hhvm + sudo: required + dist: trusty + group: edge + +before_install: + - if [ "$TRAVIS_PHP_VERSION" = "hhvm" ]; then echo 'xdebug.enable = on' >> /etc/hhvm/php.ini; fi + - if [ -n "$GH_TOKEN" ]; then composer config github-oauth.github.com ${GH_TOKEN}; fi; before_script: - - composer self-update - - composer install --dev + - if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --dev --no-update; fi; + - composer install --prefer-dist --no-interaction --no-scripts --no-progress script: - - phpunit --verbose --coverage-clover=coverage.clover + - vendor/bin/phpunit --coverage-clover build/coverage-clover.xml after_script: - - wget https://scrutinizer-ci.com/ocular.phar - - php ocular.phar code-coverage:upload --format=php-clover coverage.clover + - vendor/bin/ocular code-coverage:upload --format=php-clover build/coverage-clover.xml + - vendor/bin/coveralls -v -c .coveralls.yml diff --git a/README.md b/README.md index 79ffd08..9eeee58 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,90 @@ -
- -[![Latest Stable Version](https://poser.pugx.org/anime-db/shikimori-browser-bundle/v/stable.png)](https://packagist.org/packages/anime-db/shikimori-browser-bundle) -[![Latest Unstable Version](https://poser.pugx.org/anime-db/shikimori-browser-bundle/v/unstable.png)](https://packagist.org/packages/anime-db/shikimori-browser-bundle) -[![Total Downloads](https://poser.pugx.org/anime-db/shikimori-browser-bundle/downloads)](https://packagist.org/packages/anime-db/shikimori-browser-bundle) -[![Build Status](https://travis-ci.org/anime-db/shikimori-browser-bundle.png)](https://travis-ci.org/anime-db/shikimori-browser-bundle) -[![Code Coverage](https://scrutinizer-ci.com/g/anime-db/shikimori-browser-bundle/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/anime-db/shikimori-browser-bundle/?branch=master) -[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/anime-db/shikimori-browser-bundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/anime-db/shikimori-browser-bundle/?branch=master) -[![SensioLabsInsight](https://insight.sensiolabs.com/projects/fde73716-6558-46ff-b3a9-f2f989a59d0c/mini.png)](https://insight.sensiolabs.com/projects/fde73716-6558-46ff-b3a9-f2f989a59d0c) -[![StyleCI](https://styleci.io/repos/18437335/shield)](https://styleci.io/repos/18437335) -[![Dependency Status](https://www.versioneye.com/user/projects/5746f648ce8d0e004130cad7/badge.svg?style=flat)](https://www.versioneye.com/user/projects/5746f648ce8d0e004130cad7) -[![License](https://poser.pugx.org/anime-db/shikimori-browser-bundle/license.png)](https://packagist.org/packages/anime-db/shikimori-browser-bundle) - -shikimori-browser-bundle -======================== +[![Shikimori](http://anime-db.org/bundles/animedboffsite/images/shikimori.org.png)](https://shikimori.org) + +[![Latest Stable Version](https://img.shields.io/packagist/v/anime-db/shikimori-browser-bundle.svg?maxAge=3600&label=stable)](https://packagist.org/packages/anime-db/shikimori-browser-bundle) +[![Total Downloads](https://img.shields.io/packagist/dt/anime-db/shikimori-browser-bundle.svg?maxAge=3600)](https://packagist.org/packages/anime-db/shikimori-browser-bundle) +[![Build Status](https://img.shields.io/travis/anime-db/shikimori-browser-bundle.svg?maxAge=3600)](https://travis-ci.org/anime-db/shikimori-browser-bundle) +[![Coverage Status](https://img.shields.io/coveralls/anime-db/shikimori-browser-bundle.svg?maxAge=3600)](https://coveralls.io/github/anime-db/shikimori-browser-bundle?branch=master) +[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/anime-db/shikimori-browser-bundle.svg?maxAge=3600)](https://scrutinizer-ci.com/g/anime-db/shikimori-browser-bundle/?branch=master) +[![SensioLabs Insight](https://img.shields.io/sensiolabs/i/fde73716-6558-46ff-b3a9-f2f989a59d0c.svg?maxAge=3600&label=SLInsight)](https://insight.sensiolabs.com/projects/fde73716-6558-46ff-b3a9-f2f989a59d0c) +[![StyleCI](https://styleci.io/repos/18437335/shield?branch=master)](https://styleci.io/repos/18437335) +[![License](https://img.shields.io/packagist/l/anime-db/shikimori-browser-bundle.svg?maxAge=3600)](https://github.com/anime-db/shikimori-browser-bundle) Shikimori.org API browser +========================= + +Read API documentation here: http://shikimori.org/api/doc + +Installation +------------ + +Pretty simple with [Composer](http://packagist.org), run: + +```sh +composer anime-db/shikimori-browser-bundle +``` + +Configuration +------------- + +```yml +anime_db_shikimori_browser: + # API host + # As a default used 'https://shikimori.org' + host: 'https://shikimori.org' + + # Prefix for API resurces + # As a default used '/api/' + prefix: '/api/' +``` + +Usage +----- + +First get browser + +```php +$browser = $this->get('anime_db.shikimori.browser'); +``` + +List animes ([docs](https://shikimori.org/api/doc/1.0/animes/index)) + +```php +$animes = $browser->get('animes', ['limit' => 10]); +``` + +Mark all messages as read ([docs](https://shikimori.org/api/doc/1.0/messages/read_all)) + +```php +$response = $browser->post('messages/read_all'); +``` + +Update a message ([docs](https://shikimori.org/api/doc/1.0/messages/update)) + +```php +$response = $browser->patch('messages/12', [ + "message" => [ + "body": "blablabla", + ], +]); +``` + +Update a comment ([docs](https://shikimori.org/api/doc/1.0/comments/update)) + +```php +$response = $browser->put('comments/8', [ + "message" => [ + "body": "blablabla", + ], +]); +``` + +Destroy a message ([docs](https://shikimori.org/api/doc/1.0/messages/destroy)) + +```php +$browser->delete('messages/12'); +``` + +License +------- + +This bundle is under the [MIT license](http://opensource.org/licenses/MIT). See the complete license in the file: LICENSE diff --git a/composer.json b/composer.json index c05adaa..10de861 100644 --- a/composer.json +++ b/composer.json @@ -1,27 +1,35 @@ { - "name" : "anime-db/shikimori-browser-bundle", - "license" : "GPL-3.0", - "type" : "library", - "description" : "Plugin Shikimori for the catalog Anime DB", - "homepage" : "http://github.com/anime-db/shikimori-browser-bundle", - "autoload" : { - "psr-4" : { - "AnimeDb\\Bundle\\ShikimoriBrowserBundle\\" : "src/" - } - }, - "autoload-dev" : { - "psr-4" : { - "AnimeDb\\Bundle\\ShikimoriBrowserBundle\\Tests\\" : "tests/" - } - }, - "require" : { - "php" : ">=5.3.3", - "guzzle/guzzle" : "3.9.*" - }, - "require-dev" : { - "symfony/dependency-injection" : ">=2.3.17", - "symfony/expression-language" : ">=2.3.17", - "symfony/config" : ">=2.3.17", - "symfony/http-kernel" : ">=2.3.17" - } + "name": "anime-db/shikimori-browser-bundle", + "license": "GPL-3.0", + "type": "library", + "description": "Plugin Shikimori for the catalog Anime DB", + "homepage": "http://github.com/anime-db/shikimori-browser-bundle", + "autoload": { + "psr-4": { + "AnimeDb\\Bundle\\ShikimoriBrowserBundle\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "AnimeDb\\Bundle\\ShikimoriBrowserBundle\\Tests\\": "tests/" + } + }, + "require": { + "php": ">=5.5", + "guzzlehttp/guzzle" : "~6.3" + }, + "require-dev": { + "symfony/dependency-injection": "~2.7|~3.0", + "symfony/expression-language": "~2.7|~3.0", + "symfony/config": "~2.7|~3.0", + "symfony/http-kernel": "~2.7|~3.0", + "phpunit/phpunit": "~4.8", + "scrutinizer/ocular": "~1.3", + "satooshi/php-coveralls": "^1.0" + }, + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + } } diff --git a/phpunit.xml b/phpunit.xml.dist similarity index 100% rename from phpunit.xml rename to phpunit.xml.dist diff --git a/src/AnimeDbShikimoriBrowserBundle.php b/src/AnimeDbShikimoriBrowserBundle.php index 4cabf0e..7e613c8 100644 --- a/src/AnimeDbShikimoriBrowserBundle.php +++ b/src/AnimeDbShikimoriBrowserBundle.php @@ -1,4 +1,5 @@ load('parameters.yml'); $loader->load('services.yml'); + + $config = $this->processConfiguration(new Configuration(), $configs); + + $container + ->getDefinition('anime_db.shikimori.browser') + ->replaceArgument(1, $config['host']) + ->replaceArgument(2, $config['prefix']) + ; } } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php new file mode 100644 index 0000000..d917d69 --- /dev/null +++ b/src/DependencyInjection/Configuration.php @@ -0,0 +1,46 @@ + + * @copyright Copyright (c) 2011, Peter Gribanov + * @license http://opensource.org/licenses/GPL-3.0 GPL v3 + */ + +namespace AnimeDb\Bundle\ShikimoriBrowserBundle\DependencyInjection; + +use Symfony\Component\Config\Definition\Builder\TreeBuilder; +use Symfony\Component\Config\Definition\ConfigurationInterface; + +class Configuration implements ConfigurationInterface +{ + /** + * Config tree builder. + * + * Example config: + * + * anime_db_shikimori_browser: + * host: 'https://shikimori.org' + * prefix: '/api/' + * + * @return TreeBuilder + */ + public function getConfigTreeBuilder() + { + return (new TreeBuilder()) + ->root('anime_db_shikimori_browser') + ->children() + ->scalarNode('host') + ->cannotBeEmpty() + ->defaultValue('https://shikimori.org') + ->end() + ->scalarNode('prefix') + ->cannotBeEmpty() + ->defaultValue('/api/') + ->end() + ->end() + ->end() + ; + } +} diff --git a/src/Resources/config/parameters.yml b/src/Resources/config/parameters.yml deleted file mode 100644 index 5d11076..0000000 --- a/src/Resources/config/parameters.yml +++ /dev/null @@ -1,4 +0,0 @@ -parameters: - anime_db.shikimori.host: 'http://shikimori.org' - anime_db.shikimori.api.host: 'http://shikimori.org' - anime_db.shikimori.api.prefix: '/api' diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 7e31a15..2d19e8b 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -1,12 +1,8 @@ services: anime_db.shikimori.browser: class: AnimeDb\Bundle\ShikimoriBrowserBundle\Service\Browser - arguments: - - '@anime_db.shikimori.browser.client' - - '%anime_db.shikimori.host%' - - '%anime_db.shikimori.api.prefix%' + arguments: [ '@anime_db.shikimori.browser.client', ~, ~ ] anime_db.shikimori.browser.client: - class: Guzzle\Http\Client - arguments: [ '%anime_db.shikimori.api.host%' ] + class: GuzzleHttp\Client public: false diff --git a/src/Service/Browser.php b/src/Service/Browser.php index 569015d..1676e2a 100644 --- a/src/Service/Browser.php +++ b/src/Service/Browser.php @@ -1,4 +1,5 @@ client = $client; $this->host = $host; - $this->api_prefix = $api_prefix; + $this->prefix = $prefix; } /** - * @return string + * @param string $resource + * @param array $options + * + * @return array */ - public function getHost() + public function get($resource, array $options = []) { - return $this->host; + return $this->request('GET', $resource, $options); } /** - * @return string + * @param string $resource + * @param array $options + * + * @return array */ - public function getApiHost() + public function post($resource, array $options = []) { - return $this->client->getBaseUrl(); + return $this->request('POST', $resource, $options); } /** - * @param int $timeout + * @param string $resource + * @param array $options * - * @return Browser + * @return array */ - public function setTimeout($timeout) + public function put($resource, array $options = []) { - $this->client->setDefaultOption('timeout', $timeout); - - return $this; + return $this->request('PUT', $resource, $options); } /** - * @param int $proxy + * @param string $resource + * @param array $options * - * @return Browser + * @return array */ - public function setProxy($proxy) + public function patch($resource, array $options = []) { - $this->client->setDefaultOption('proxy', $proxy); - - return $this; + return $this->request('PATCH', $resource, $options); } /** - * Get data from path. + * @param string $resource + * @param array $options * + * @return array + */ + public function delete($resource, array $options = []) + { + return $this->request('DELETE', $resource, $options); + } + + /** + * @param string $method * @param string $path + * @param array $options * * @return array */ - public function get($path) + private function request($method, $path = '', array $options = []) { - $response = $this->client->get($this->api_prefix.$path)->send(); - if ($response->isError()) { - throw new \RuntimeException('Failed to query the server '.$this->client->getBaseUrl()); + try { + $response = $this->client->request($method, $this->host.$this->prefix.$path, $options); + } catch (\Exception $e) { + throw ResponseException::failed($this->host, $e); } - $body = json_decode($response->getBody(true), true); + $body = json_decode($response->getBody()->getContents(), true); if (json_last_error() !== JSON_ERROR_NONE || !is_array($body)) { - throw new \RuntimeException('Invalid response from the server '.$this->client->getBaseUrl()); + throw ResponseException::invalidResponse($this->host); } return $body; diff --git a/src/Service/Exception/ResponseException.php b/src/Service/Exception/ResponseException.php new file mode 100644 index 0000000..83272a4 --- /dev/null +++ b/src/Service/Exception/ResponseException.php @@ -0,0 +1,35 @@ + + * @copyright Copyright (c) 2011, Peter Gribanov + * @license http://opensource.org/licenses/GPL-3.0 GPL v3 + */ + +namespace AnimeDb\Bundle\ShikimoriBrowserBundle\Service\Exception; + +class ResponseException extends \RuntimeException +{ + /** + * @param string $host + * @param \Exception $previous + * + * @return ResponseException + */ + public static function failed($host, \Exception $previous) + { + return new self(sprintf('Failed to query the server "%s"', $host), $previous->getCode(), $previous); + } + + /** + * @param string $host + * + * @return ResponseException + */ + public static function invalidResponse($host) + { + return new self(sprintf('Invalid response from the server "%s"', $host)); + } +} diff --git a/tests/DependencyInjection/AnimeDbShikimoriBrowserExtensionTest.php b/tests/DependencyInjection/AnimeDbShikimoriBrowserExtensionTest.php index fc9fbd9..01cee8a 100644 --- a/tests/DependencyInjection/AnimeDbShikimoriBrowserExtensionTest.php +++ b/tests/DependencyInjection/AnimeDbShikimoriBrowserExtensionTest.php @@ -1,4 +1,5 @@ container = $this->getMock(ContainerBuilder::class); + $this->extension = new AnimeDbShikimoriBrowserExtension(); + } + + public function config() { - /* @var $builder \PHPUnit_Framework_MockObject_MockObject|ContainerBuilder */ - $builder = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder'); + return [ + [ + [], + 'https://shikimori.org', + '/api/', + ], + [ + [ + 'anime_db_shikimori_browser' => [ + 'host' => 'http://shikimori.org', + 'prefix' => '/api/v2/', + ], + ], + 'http://shikimori.org', + '/api/v2/', + ], + ]; + } + + /** + * @dataProvider config + * + * @param array $config + * @param string $host + * @param string $prefix + */ + public function testLoad(array $config, $host, $prefix) + { + $browser = $this->getMock(Definition::class); + $browser + ->expects($this->at(0)) + ->method('replaceArgument') + ->with(1, $host) + ->will($this->returnSelf()) + ; + $browser + ->expects($this->at(1)) + ->method('replaceArgument') + ->with(2, $prefix) + ->will($this->returnSelf()) + ; + + $this->container + ->expects($this->once()) + ->method('getDefinition') + ->with('anime_db.shikimori.browser') + ->will($this->returnValue($browser)) + ; - $di = new AnimeDbShikimoriBrowserExtension(); - $di->load([], $builder); + $this->extension->load($config, $this->container); } } diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php new file mode 100644 index 0000000..82a1c14 --- /dev/null +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -0,0 +1,56 @@ + + * @copyright Copyright (c) 2011, Peter Gribanov + * @license http://opensource.org/licenses/GPL-3.0 GPL v3 + */ + +namespace AnimeDb\Bundle\ShikimoriBrowserBundle\Tests\DependencyInjection; + +use AnimeDb\Bundle\ShikimoriBrowserBundle\DependencyInjection\Configuration; +use Symfony\Component\Config\Definition\ArrayNode; +use Symfony\Component\Config\Definition\Builder\TreeBuilder; +use Symfony\Component\Config\Definition\ScalarNode; + +class ConfigurationTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Configuration + */ + private $configuration; + + protected function setUp() + { + $this->configuration = new Configuration(); + } + + public function testConfigTree() + { + $tree_builder = $this->configuration->getConfigTreeBuilder(); + + $this->assertInstanceOf(TreeBuilder::class, $tree_builder); + + /* @var $tree ArrayNode */ + $tree = $tree_builder->buildTree(); + + $this->assertInstanceOf(ArrayNode::class, $tree); + $this->assertEquals('anime_db_shikimori_browser', $tree->getName()); + + /* @var $children ScalarNode[] */ + $children = $tree->getChildren(); + + $this->assertInternalType('array', $children); + $this->assertEquals(['host', 'prefix'], array_keys($children)); + + $this->assertInstanceOf(ScalarNode::class, $children['host']); + $this->assertEquals('https://shikimori.org', $children['host']->getDefaultValue()); + $this->assertFalse($children['host']->isRequired()); + + $this->assertInstanceOf(ScalarNode::class, $children['prefix']); + $this->assertEquals('/api/', $children['prefix']->getDefaultValue()); + $this->assertFalse($children['prefix']->isRequired()); + } +} diff --git a/tests/Service/BrowserTest.php b/tests/Service/BrowserTest.php index 32a7a9c..3b85889 100644 --- a/tests/Service/BrowserTest.php +++ b/tests/Service/BrowserTest.php @@ -1,4 +1,5 @@ client = $this - ->getMockBuilder('\Guzzle\Http\Client') + ->getMockBuilder(Client::class) ->disableOriginalConstructor() - ->getMock(); - - $this->browser = new Browser($this->client, $this->host, $this->api_prefix); - } - - public function testGetHost() - { - $this->assertEquals($this->host, $this->browser->getHost()); - } - - public function testGetApiHost() - { - $this->client - ->expects($this->once()) - ->method('getBaseUrl') - ->will($this->returnValue('baz')); - - $this->assertEquals('baz', $this->browser->getApiHost()); - } - - public function testSetTimeout() - { - $timeout = 123; - $this->client - ->expects($this->once()) - ->method('setDefaultOption') - ->with('timeout', $timeout); + ->getMock() + ; + $this->response = $this + ->getMockBuilder(Response::class) + ->disableOriginalConstructor() + ->getMock() + ; - $this->assertEquals($this->browser, $this->browser->setTimeout($timeout)); + $this->browser = new Browser($this->client, $this->host, $this->prefix); } - public function testSetProxy() + public function requests() { - $proxy = '127.0.0.1'; - $this->client - ->expects($this->once()) - ->method('setDefaultOption') - ->with('proxy', $proxy); - - $this->assertEquals($this->browser, $this->browser->setProxy($proxy)); + return [ + [ + 'GET', + ['user' => 123], + ['ignored' => true], + ], + [ + 'POST', + ['user' => 123], + ['ignored' => true], + ], + [ + 'PUT', + ['user' => 123], + ['ignored' => true], + ], + [ + 'PATCH', + ['user' => 123], + ['ignored' => true], + ], + [ + 'DELETE', + ['user' => 123], + ['ignored' => true], + ], + ]; } /** - * @expectedException \RuntimeException + * @expectedException \AnimeDb\Bundle\ShikimoriBrowserBundle\Service\Exception\ResponseException + * @dataProvider requests + * + * @param string $method + * @param array $options + * @param array $data */ - public function testGetFailedTransport() + public function testGetFailedTransport($method, array $options, array $data) { - $this->buildDialogue('baz', true); - $this->browser->get('baz'); + $this->buildDialogue($method, 'baz', true, $data, $options); + + switch ($method) { + case 'GET': + $this->browser->get('baz', $options); + break; + case 'POST': + $this->browser->post('baz', $options); + break; + case 'PUT': + $this->browser->put('baz', $options); + break; + case 'PATCH': + $this->browser->patch('baz', $options); + break; + case 'DELETE': + $this->browser->delete('baz', $options); + break; + } } /** - * @expectedException \RuntimeException + * @expectedException \AnimeDb\Bundle\ShikimoriBrowserBundle\Service\Exception\ResponseException + * @dataProvider requests + * + * @param string $method + * @param array $options */ - public function testGetFailedResponseBody() + public function testGetFailedResponseBody($method, array $options) { - $this->buildDialogue('baz', false); - $this->browser->get('baz'); + $this->buildDialogue($method, 'baz', false, [], $options); + + switch ($method) { + case 'GET': + $this->browser->get('baz', $options); + break; + case 'POST': + $this->browser->post('baz', $options); + break; + case 'PUT': + $this->browser->put('baz', $options); + break; + case 'PATCH': + $this->browser->patch('baz', $options); + break; + case 'DELETE': + $this->browser->delete('baz', $options); + break; + } } - public function testGet() + /** + * @dataProvider requests + * + * @param string $method + * @param array $options + * @param array $data + */ + public function testGet($method, array $options, array $data) { - $data = ['test' => 123]; - $this->buildDialogue('baz', false, $data); - $this->assertEquals($data, $this->browser->get('baz')); + $this->buildDialogue($method, 'baz', false, $data, $options); + + switch ($method) { + case 'GET': + $this->assertEquals($data, $this->browser->get('baz', $options)); + break; + case 'POST': + $this->assertEquals($data, $this->browser->post('baz', $options)); + break; + case 'PUT': + $this->assertEquals($data, $this->browser->put('baz', $options)); + break; + case 'PATCH': + $this->assertEquals($data, $this->browser->patch('baz', $options)); + break; + case 'DELETE': + $this->assertEquals($data, $this->browser->delete('baz', $options)); + break; + } } /** + * @param string $method * @param string $path - * @param bool $is_error - * @param mixed $data + * @param bool $is_error + * @param array $data + * @param array $options */ - protected function buildDialogue($path, $is_error, $data = null) + protected function buildDialogue($method, $path, $is_error, array $data = [], array $options = []) { - /* @var $request \PHPUnit_Framework_MockObject_MockObject|RequestInterface */ - $request = $this->getMock('\Guzzle\Http\Message\RequestInterface'); - /* @var $response \PHPUnit_Framework_MockObject_MockObject|Response */ - $response = $this - ->getMockBuilder('\Guzzle\Http\Message\Response') - ->disableOriginalConstructor() - ->getMock(); + $body = $this->getMock(StreamInterface::class); + $body + ->expects($is_error ? $this->never() : $this->once()) + ->method('getContents') + ->will($this->returnValue($data ? json_encode($data) : null)) + ; $this->client ->expects($this->once()) - ->method('get') - ->with($this->api_prefix.$path) - ->will($this->returnValue($request)); - $request - ->expects($this->once()) - ->method('send') - ->will($this->returnValue($response)); - $response - ->expects($this->once()) - ->method('isError') - ->will($this->returnValue($is_error)); + ->method('request') + ->with($method, $this->host.$this->prefix.$path, $options) + ->will($is_error ? $this->throwException(new \Exception()) : $this->returnValue($this->response)) + ; if (!$is_error) { - $response + $this->response ->expects($this->once()) ->method('getBody') - ->with(true) - ->will($this->returnValue($data ? json_encode($data) : $data)); + ->will($this->returnValue($body)) + ; } } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 9ea21bf..5dea307 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,6 +1,7 @@