diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f0ce1af --- /dev/null +++ b/.travis.yml @@ -0,0 +1,14 @@ +language: php + +php: +- 5.4 +- 5.5 +- 5.6 +- 7 + +before_script: +- travis_retry composer self-update +- travis_retry composer install --no-interaction --prefer-source + +script: +- php vendor/phpunit/phpunit/phpunit diff --git a/CHANGELOG.md b/CHANGELOG.md index b857b3b..0ecdb78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,12 @@ All notable changes to this project will be documented in this file. ## [Unreleased][unreleased] + +## 0.2 - 2015-06-21 ### Added +- [Youthweb API 0.2](https://github.com/youthweb/youthweb-api/releases/tag/0.2) Support +- phpunit tests +- Travis-CI Support - this CHANGELOG.md ## 0.1 - 2015-04-20 @@ -10,4 +15,5 @@ All notable changes to this project will be documented in this file. - First workable client for Youthweb API [Version 0.1](https://github.com/youthweb/youthweb-api/releases/tag/0.1) - Http client based on [guzzlehttp/guzzle ~5.0](https://github.com/guzzle/guzzle) -[unreleased]: https://github.com/youthweb/php-youthweb-api/compare/0.1...HEAD +[unreleased]: https://github.com/youthweb/php-youthweb-api/compare/0.2...HEAD +[0.2]: https://github.com/youthweb/php-youthweb-api/compare/0.1...0.2 diff --git a/README.md b/README.md index 9be8eb5..f9f4ad0 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,13 @@ # PHP Youthweb API -PHP Youthweb API ist ein objektorientierter Wrapper in PHP für die [Youthweb API](https://github.com/youthweb/youthweb-api). +[![Latest Version](https://img.shields.io/github/release/youthweb/php-youthweb-api.svg?style=flat-square)](https://github.com/youthweb/php-youthweb-api/releases) +[![Software License GLPv2](http://img.shields.io/badge/License-GPLv2-brightgreen.svg?style=flat-square)](LICENSE) +[![Build Status](http://img.shields.io/travis/youthweb/php-youthweb-api.svg?style=flat-square)](https://travis-ci.org/youthweb/php-youthweb-api) +[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/youthweb/youthweb-api?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -API Version: [0.1](https://github.com/youthweb/youthweb-api/releases/tag/0.1) +PHP Youthweb API ist ein objektorientierter Wrapper in PHP 5.4+ für die [Youthweb API](https://github.com/youthweb/youthweb-api). + +Unterstütze API Version: [0.2](https://github.com/youthweb/youthweb-api/releases/tag/0.2) ## Installation @@ -54,5 +59,4 @@ Der Changelog ist [hier](https://github.com/youthweb/php-youthweb-api/blob/maste ## Todo -- Tests - Request Error Handling diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..aa4c057 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,27 @@ + + + + + + tests/unit/ + + + + + src/ + + + diff --git a/src/Client.php b/src/Client.php index 78a9e5b..35b4233 100644 --- a/src/Client.php +++ b/src/Client.php @@ -26,6 +26,7 @@ public function getResource($name) { $classes = array( 'account' => 'Account', + 'stats' => 'Stats', ); if ( ! isset($classes[$name]) ) @@ -33,14 +34,12 @@ public function getResource($name) throw new \InvalidArgumentException('The ressource "' . $name . '" does not exists.'); } - if ( isset($this->resources[$name]) ) + if ( ! isset($this->resources[$name]) ) { - return $this->resources[$name]; + $resource = 'Youthweb\\Api\\Resource\\'.$classes[$name]; + $this->resources[$name] = new $resource($this); } - $resource = 'Youthweb\\Api\\Resource\\'.$classes[$name]; - $this->resources[$name] = new $resource($this); - return $this->resources[$name]; } diff --git a/src/Resource/Account.php b/src/Resource/Account.php index cacdea5..cda3de8 100644 --- a/src/Resource/Account.php +++ b/src/Resource/Account.php @@ -23,6 +23,11 @@ public function stats() 'cat' => 'stats', ); - return $this->get('', $params); + $stats = $this->get('', $params); + + return array( + 'user_total' => $stats->user_total, + 'user_online' => $stats->user_online, + ); } } diff --git a/src/Resource/Stats.php b/src/Resource/Stats.php new file mode 100644 index 0000000..cd0686e --- /dev/null +++ b/src/Resource/Stats.php @@ -0,0 +1,40 @@ + 'account', + 'forum' => 'forum', + 'groups' => 'groups', + ); + + if ( ! isset($ids[$id]) ) + { + throw new \InvalidArgumentException('The ressource id "' . $id . '" does not exists.'); + } + + $params = array( + 'action' => 'stats', + 'cat' => $id, + ); + + return $this->get('', $params); + } +} diff --git a/src/autoload.php b/src/autoload.php index a616d8c..9a3ab82 100644 --- a/src/autoload.php +++ b/src/autoload.php @@ -15,4 +15,12 @@ { require_once $path; } + + // Text classes + $path = dirname(__FILE__).'/../tests/'.$class.'.php'; + + if ( file_exists($path) ) + { + require_once $path; + } }); diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php new file mode 100644 index 0000000..37e8c9e --- /dev/null +++ b/tests/Bootstrap.php @@ -0,0 +1,11 @@ + wget http://getcomposer.org/composer.phar +> php composer.phar install +"); +} diff --git a/tests/Fixtures/MockClient.php b/tests/Fixtures/MockClient.php new file mode 100644 index 0000000..0aad103 --- /dev/null +++ b/tests/Fixtures/MockClient.php @@ -0,0 +1,66 @@ +useOriginalGetMethod ) + { + return parent::get($path, $data); + } + + return $this->runRequest($path, 'GET', $data); + } + + /** + * @param string $path + * @param string $method + * @param array $data + * + * @return string + * + * @throws \Exception If anything goes wrong on curl request + */ + protected function runRequest($path, $method = 'GET', array $data = array()) + { + if ( $this->runRequestReturnValue !== null ) + { + return $this->runRequestReturnValue; + } + + return array( + 'path' => $path, + 'method' => $method, + 'data' => $data, + ); + } +} diff --git a/tests/unit/ClientTest.php b/tests/unit/ClientTest.php new file mode 100644 index 0000000..41332e8 --- /dev/null +++ b/tests/unit/ClientTest.php @@ -0,0 +1,60 @@ +assertInstanceOf('Youthweb\Api\Client', $client->setUrl('http://test.local')); + } + + /** + * @test + */ + public function testGetUrlReturnsValueFromSetUrl() + { + $client = (new Client())->setUrl('http://test.local'); + + $this->assertSame('http://test.local', $client->getUrl()); + } + + /** + * @test + */ + public function testSetHttpClientReturnsClient() + { + $client = new Client(); + + $stub = $this->getMock('Youthweb\Api\HttpClientInterface'); + + $this->assertInstanceOf('Youthweb\Api\Client', $client->setHttpClient($stub)); + } + + /** + * @test + * @dataProvider getResoursesClassesProvider + */ + public function testGetApiInstance($resource_name, $class_name) + { + $client = new Client(); + + $this->assertInstanceOf($class_name, $client->getResource($resource_name)); + } + + public function getResoursesClassesProvider() + { + return array( + array('account', 'Youthweb\Api\Resource\Account'), + array('stats', 'Youthweb\Api\Resource\Stats'), + ); + } +} diff --git a/tests/unit/Resource/AccountTest.php b/tests/unit/Resource/AccountTest.php new file mode 100644 index 0000000..c832061 --- /dev/null +++ b/tests/unit/Resource/AccountTest.php @@ -0,0 +1,24 @@ +useOriginalGetMethod = false; + $client->runRequestReturnValue = json_decode('{"user_total":5503,"user_online":74}', false); + + $response = $client->getResource('account')->stats(); + + $this->assertTrue(is_array($response)); + $this->assertSame(array('user_total' => 5503, 'user_online' => 74), $response); + } +} diff --git a/tests/unit/Resource/StatsTest.php b/tests/unit/Resource/StatsTest.php new file mode 100644 index 0000000..6781384 --- /dev/null +++ b/tests/unit/Resource/StatsTest.php @@ -0,0 +1,66 @@ +useOriginalGetMethod = false; + $client->runRequestReturnValue = json_decode('{"data":{"type":"stats","id":"account","attributes":{"user_total":5503,"user_total_female":2831, "user_total_male":2672,"user_online":74,"user_online_24h":629,"user_online_7d":1035,"user_online_30d":1600,"userpics":3441}}}', false); + + $response = $client->getResource('stats')->show('account'); + + $this->assertTrue(is_object($response)); + $this->assertSame($client->runRequestReturnValue, $response); + } + + /** + * @test + */ + public function testShowForumReturnsObject() + { + $client = new MockClient(); + $client->useOriginalGetMethod = false; + $client->runRequestReturnValue = json_decode('{"data":{"type":"stats","id":"forum","attributes":{"authors_total":1480,"threads_total":2094,"posts_total":121387}}}', false); + + $response = $client->getResource('stats')->show('forum'); + + $this->assertTrue(is_object($response)); + $this->assertSame($client->runRequestReturnValue, $response); + } + + /** + * @test + */ + public function testShowGroupsReturnsObject() + { + $client = new MockClient(); + $client->useOriginalGetMethod = false; + $client->runRequestReturnValue = json_decode('{"data":{"type":"stats","id":"groups","attributes":{"groups_total":614,"users_total":2073}}}', false); + + $response = $client->getResource('stats')->show('groups'); + + $this->assertTrue(is_object($response)); + $this->assertSame($client->runRequestReturnValue, $response); + } + + /** + * @test + * @expectedException InvalidArgumentException + * @expectedExceptionMessage The ressource id "foobar" does not exists. + */ + public function testShowFoobarThrowsException() + { + $client = new MockClient(); + + $response = $client->getResource('stats')->show('foobar'); + } +}