From 60b8fd5a56a72b4e4aa73a75857b3aa6d7bd5cfb Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 6 Jun 2015 13:41:38 +0200 Subject: [PATCH 01/11] Unittests --- phpunit.xml.dist | 27 ++++++++++++++++++ tests/Bootstrap.php | 4 +++ tests/unit/ClientTest.php | 59 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 phpunit.xml.dist create mode 100644 tests/Bootstrap.php create mode 100644 tests/unit/ClientTest.php 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/tests/Bootstrap.php b/tests/Bootstrap.php new file mode 100644 index 0000000..ea0f562 --- /dev/null +++ b/tests/Bootstrap.php @@ -0,0 +1,4 @@ + wget http://getcomposer.org/composer.phar\n> php composer.phar install\n"); +} diff --git a/tests/unit/ClientTest.php b/tests/unit/ClientTest.php new file mode 100644 index 0000000..54301e5 --- /dev/null +++ b/tests/unit/ClientTest.php @@ -0,0 +1,59 @@ +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'), + ); + } +} From 56bd587d2b63371c7d6623af0348ed98469bc73d Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 6 Jun 2015 13:42:50 +0200 Subject: [PATCH 02/11] Refactoring --- src/Client.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Client.php b/src/Client.php index 78a9e5b..76e2da8 100644 --- a/src/Client.php +++ b/src/Client.php @@ -33,14 +33,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]; } From 435052c153825d23ea7f185ac334514f4d32a5c9 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 6 Jun 2015 13:59:08 +0200 Subject: [PATCH 03/11] .travis.yml erstellt --- .travis.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .travis.yml 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 From 251ec8c38b7f7b9171306dbc399482413ca747e0 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 6 Jun 2015 14:02:05 +0200 Subject: [PATCH 04/11] Changelog und Readme angepasst --- CHANGELOG.md | 2 ++ README.md | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b857b3b..2339d3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file. ## [Unreleased][unreleased] ### Added +- phpunit tests +- Travis-CI Support - this CHANGELOG.md ## 0.1 - 2015-04-20 diff --git a/README.md b/README.md index 9be8eb5..d3220c9 100644 --- a/README.md +++ b/README.md @@ -54,5 +54,4 @@ Der Changelog ist [hier](https://github.com/youthweb/php-youthweb-api/blob/maste ## Todo -- Tests - Request Error Handling From ff6404a22d6763dc175cb04cf29df3d9d3701f13 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 6 Jun 2015 14:24:33 +0200 Subject: [PATCH 05/11] =?UTF-8?q?stats=20Resource=20erg=C3=A4nzt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Client.php | 1 + src/Resource/Stats.php | 40 +++++++++++++++++++++++++++++++++++++++ tests/unit/ClientTest.php | 1 + 3 files changed, 42 insertions(+) create mode 100644 src/Resource/Stats.php diff --git a/src/Client.php b/src/Client.php index 76e2da8..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]) ) 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/tests/unit/ClientTest.php b/tests/unit/ClientTest.php index 54301e5..41332e8 100644 --- a/tests/unit/ClientTest.php +++ b/tests/unit/ClientTest.php @@ -54,6 +54,7 @@ public function getResoursesClassesProvider() { return array( array('account', 'Youthweb\Api\Resource\Account'), + array('stats', 'Youthweb\Api\Resource\Stats'), ); } } From 25c4ae7c8a15ffd9c57f31b0e1e5fe2668a741b0 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 6 Jun 2015 14:29:32 +0200 Subject: [PATCH 06/11] =?UTF-8?q?Badges=20erg=C3=A4nzt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d3220c9..817035b 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.1](https://github.com/youthweb/youthweb-api/releases/tag/0.1) ## Installation From c9bcae970db66185e39c3d20de78cf8b4331caf0 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 6 Jun 2015 14:31:21 +0200 Subject: [PATCH 07/11] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2339d3f..1c43c01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. ## [Unreleased][unreleased] ### Added +- Youthweb API 0.2 Support - phpunit tests - Travis-CI Support - this CHANGELOG.md From ae7ae8d489723347fd556642690e240c3529ff9c Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 6 Jun 2015 18:08:03 +0200 Subject: [PATCH 08/11] BC --- src/Resource/Account.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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, + ); } } From 9146e6525e374d0ad153bacd54dae787e844ad53 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 11 Jun 2015 22:28:56 +0200 Subject: [PATCH 09/11] =?UTF-8?q?Tests=20f=C3=BCr=20Ressources=20geschrieb?= =?UTF-8?q?en,=20MockClient=20erstellt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/autoload.php | 8 ++++ tests/Bootstrap.php | 11 ++++- tests/Fixtures/MockClient.php | 66 +++++++++++++++++++++++++++++ tests/unit/Resource/AccountTest.php | 24 +++++++++++ tests/unit/Resource/StatsTest.php | 66 +++++++++++++++++++++++++++++ 5 files changed, 173 insertions(+), 2 deletions(-) create mode 100644 tests/Fixtures/MockClient.php create mode 100644 tests/unit/Resource/AccountTest.php create mode 100644 tests/unit/Resource/StatsTest.php 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 index ea0f562..37e8c9e 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -1,4 +1,11 @@ wget http://getcomposer.org/composer.phar\n> php composer.phar install\n"); + +include_once(__DIR__.'/../src/autoload.php'); + +if ( ! @include_once __DIR__.'/../vendor/autoload.php') +{ + exit("You must set up the project dependencies, run the following commands: +> 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..4c114e9 --- /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', $data = '') + { + if ( $this->runRequestReturnValue !== null ) + { + return $this->runRequestReturnValue; + } + + return array( + 'path' => $path, + 'method' => $method, + 'data' => $data, + ); + } +} 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'); + } +} From 2dfd83cb72c9caadfdbac8bf3a5d20d20b6442d9 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 11 Jun 2015 22:45:54 +0200 Subject: [PATCH 10/11] Fix Tests --- tests/Fixtures/MockClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Fixtures/MockClient.php b/tests/Fixtures/MockClient.php index 4c114e9..0aad103 100644 --- a/tests/Fixtures/MockClient.php +++ b/tests/Fixtures/MockClient.php @@ -50,7 +50,7 @@ public function get($path, array $data = array()) * * @throws \Exception If anything goes wrong on curl request */ - protected function runRequest($path, $method = 'GET', $data = '') + protected function runRequest($path, $method = 'GET', array $data = array()) { if ( $this->runRequestReturnValue !== null ) { From 0f4ffe959096e5718a52f655ff31edb47343af2f Mon Sep 17 00:00:00 2001 From: Art4 Date: Sun, 21 Jun 2015 09:21:43 +0200 Subject: [PATCH 11/11] =?UTF-8?q?Vorbereitungen=20f=C3=BCr=20Version=200.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 7 +++++-- README.md | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c43c01..0ecdb78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,10 @@ All notable changes to this project will be documented in this file. ## [Unreleased][unreleased] + +## 0.2 - 2015-06-21 ### Added -- Youthweb API 0.2 Support +- [Youthweb API 0.2](https://github.com/youthweb/youthweb-api/releases/tag/0.2) Support - phpunit tests - Travis-CI Support - this CHANGELOG.md @@ -13,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 817035b..f9f4ad0 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ 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.1](https://github.com/youthweb/youthweb-api/releases/tag/0.1) +Unterstütze API Version: [0.2](https://github.com/youthweb/youthweb-api/releases/tag/0.2) ## Installation