Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Jun 21, 2015
2 parents 66bc5d5 + 0f4ffe9 commit fdd0e92
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 10 deletions.
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
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
### Added
- 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
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -54,5 +59,4 @@ Der Changelog ist [hier](https://github.com/youthweb/php-youthweb-api/blob/maste

## Todo

- Tests
- Request Error Handling
27 changes: 27 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
stopOnError="false"
stopOnIncomplete="false"
stopOnSkipped="false"
bootstrap="tests/Bootstrap.php"
>
<testsuites>
<testsuite name="all">
<directory suffix="Test.php">tests/unit/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
9 changes: 4 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,20 @@ public function getResource($name)
{
$classes = array(
'account' => 'Account',
'stats' => 'Stats',
);

if ( ! isset($classes[$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];
}

Expand Down
7 changes: 6 additions & 1 deletion src/Resource/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
40 changes: 40 additions & 0 deletions src/Resource/Stats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Youthweb\Api\Resource;

/**
* Get the stats
*
* @link https://github.com/youthweb/youthweb-api#stats
*/
class Stats extends AbstractResource
{
/**
* Get the stats
*
* @link https://github.com/youthweb/youthweb-api#stats
*
* @param string $id
* @return array the stats
*/
public function show($id)
{
$ids = array(
'account' => '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);
}
}
8 changes: 8 additions & 0 deletions src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@
{
require_once $path;
}

// Text classes
$path = dirname(__FILE__).'/../tests/'.$class.'.php';

if ( file_exists($path) )
{
require_once $path;
}
});
11 changes: 11 additions & 0 deletions tests/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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
");
}
66 changes: 66 additions & 0 deletions tests/Fixtures/MockClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Youthweb\Api\Fixtures;

use Youthweb\Api\Client;

/**
* Mock client class
*/
class MockClient extends Client
{
/**
* Return value the mocked runRequest method should return.
*
* @var mixed
*/
public $runRequestReturnValue = null;

/**
* Return value the mocked runRequest method should return.
*
* @var mixed
*/
public $useOriginalGetMethod = false;

/**
* Just return the data from runRequest().
*
* @param string $path
* @param array $data
*
* @return array
*/
public function get($path, array $data = array())
{
if ( $this->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,
);
}
}
60 changes: 60 additions & 0 deletions tests/unit/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Youthweb\Api\Tests;

use Youthweb\Api\Client;
use InvalidArgumentException;

class ClientTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function testSetUrlReturnsClient()
{
$client = new Client();

$this->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'),
);
}
}
24 changes: 24 additions & 0 deletions tests/unit/Resource/AccountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Youthweb\Api\Tests\Resource;

use Youthweb\Api\Fixtures\MockClient;
use InvalidArgumentException;

class AccountTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function testStatsReturnsArray()
{
$client = new MockClient();
$client->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);
}
}
Loading

0 comments on commit fdd0e92

Please sign in to comment.