2015-06-26 12:03:42 -04:00
|
|
|
<?php
|
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
2015-09-15 13:19:29 -04:00
|
|
|
use Aviat\AnimeClient\Container;
|
|
|
|
use Aviat\AnimeClient\Model\API as BaseApiModel;
|
2015-06-26 16:39:10 -04:00
|
|
|
|
2015-06-26 12:03:42 -04:00
|
|
|
class MockBaseApiModel extends BaseApiModel {
|
|
|
|
|
2015-10-15 09:25:30 -04:00
|
|
|
protected $base_url = 'https://httpbin.org/';
|
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
public function __construct(ContainerInterface $container)
|
2015-06-26 12:03:42 -04:00
|
|
|
{
|
2015-09-14 10:54:50 -04:00
|
|
|
parent::__construct($container);
|
2015-06-26 12:03:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __get($key)
|
|
|
|
{
|
|
|
|
return $this->$key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BaseApiModelTest extends AnimeClient_TestCase {
|
|
|
|
|
2015-10-15 09:25:30 -04:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->model = new MockBaseApiModel($this->container);
|
|
|
|
}
|
|
|
|
|
2015-06-26 12:03:42 -04:00
|
|
|
public function testBaseApiModelSanity()
|
|
|
|
{
|
2015-10-15 09:25:30 -04:00
|
|
|
$baseApiModel = $this->model;
|
2015-06-26 12:03:42 -04:00
|
|
|
|
|
|
|
// Some basic type checks for class memebers
|
2015-09-15 13:19:29 -04:00
|
|
|
$this->assertInstanceOf('\Aviat\AnimeClient\Model', $baseApiModel);
|
|
|
|
$this->assertInstanceOf('\Aviat\AnimeClient\Model\API', $baseApiModel);
|
2015-06-26 12:03:42 -04:00
|
|
|
|
|
|
|
$this->assertInstanceOf('\GuzzleHttp\Client', $baseApiModel->client);
|
|
|
|
$this->assertInstanceOf('\GuzzleHttp\Cookie\CookieJar', $baseApiModel->cookieJar);
|
|
|
|
|
|
|
|
$this->assertTrue(is_string($baseApiModel->base_url));
|
|
|
|
$this->assertTrue(empty($baseApiModel->base_url));
|
|
|
|
}
|
|
|
|
|
2015-10-15 09:49:38 -04:00
|
|
|
protected function getIp()
|
|
|
|
{
|
|
|
|
$response = $this->model->get('/ip');
|
|
|
|
$json = json_decode($response->getBody(), TRUE);
|
|
|
|
$ip = $json['origin'];
|
|
|
|
return $ip;
|
|
|
|
}
|
|
|
|
|
2015-10-15 09:25:30 -04:00
|
|
|
public function dataClient()
|
|
|
|
{
|
|
|
|
$user_agent = "Tim's Anime Client/2.0";
|
|
|
|
$headers = [
|
|
|
|
'User-Agent' => $user_agent
|
|
|
|
];
|
|
|
|
|
|
|
|
return [
|
2015-10-15 10:23:00 -04:00
|
|
|
'invalid method' => [
|
2015-10-15 09:25:30 -04:00
|
|
|
'method' => 'foo',
|
|
|
|
'uri' => '',
|
|
|
|
'options' => [],
|
|
|
|
'expected' => NULL,
|
|
|
|
'is_json' => FALSE,
|
|
|
|
],
|
|
|
|
'get' => [
|
|
|
|
'method' => 'get',
|
|
|
|
'uri' => '/get',
|
|
|
|
'options' => [
|
|
|
|
'query' => [
|
|
|
|
'foo' => 'bar'
|
|
|
|
],
|
|
|
|
'headers' => $headers
|
|
|
|
],
|
|
|
|
'expected' => [
|
|
|
|
'args' => [
|
|
|
|
'foo' => 'bar'
|
|
|
|
],
|
|
|
|
'headers' => [
|
|
|
|
'Host' => 'httpbin.org',
|
|
|
|
'User-Agent' => $user_agent
|
|
|
|
],
|
|
|
|
'url' => 'https://httpbin.org/get?foo=bar'
|
|
|
|
],
|
|
|
|
'is_json' => TRUE
|
|
|
|
],
|
|
|
|
'post' => [
|
|
|
|
'method' => 'post',
|
|
|
|
'uri' => '/post',
|
|
|
|
'options' => [
|
|
|
|
'form_params' => [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'baz' => 'foobar'
|
|
|
|
],
|
|
|
|
'headers' => $headers
|
|
|
|
],
|
|
|
|
'expected' => [
|
|
|
|
'args' => [],
|
|
|
|
'data' => '',
|
|
|
|
'files' => [],
|
|
|
|
'form' => [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'baz' => 'foobar'
|
|
|
|
],
|
|
|
|
'headers' => [
|
|
|
|
'Host' => 'httpbin.org',
|
|
|
|
'User-Agent' => $user_agent,
|
|
|
|
'Content-Length' => '18',
|
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded'
|
|
|
|
],
|
|
|
|
'json' => NULL,
|
|
|
|
'url' => 'https://httpbin.org/post'
|
|
|
|
],
|
|
|
|
'is_json' => TRUE
|
|
|
|
],
|
|
|
|
'put' => [
|
|
|
|
'method' => 'put',
|
|
|
|
'uri' => '/put',
|
|
|
|
'options' => [
|
|
|
|
'form_params' => [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'baz' => 'foobar'
|
|
|
|
],
|
|
|
|
'headers' => $headers
|
|
|
|
],
|
|
|
|
'expected' => [
|
|
|
|
'args' => [],
|
|
|
|
'data' => '',
|
|
|
|
'files' => [],
|
|
|
|
'form' => [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'baz' => 'foobar'
|
|
|
|
],
|
|
|
|
'headers' => [
|
|
|
|
'Host' => 'httpbin.org',
|
|
|
|
'User-Agent' => $user_agent,
|
|
|
|
'Content-Length' => '18',
|
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded'
|
|
|
|
],
|
|
|
|
'json' => NULL,
|
|
|
|
'url' => 'https://httpbin.org/put'
|
|
|
|
],
|
|
|
|
'is_json' => TRUE
|
|
|
|
],
|
|
|
|
'patch' => [
|
|
|
|
'method' => 'patch',
|
|
|
|
'uri' => '/patch',
|
|
|
|
'options' => [
|
|
|
|
'form_params' => [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'baz' => 'foobar'
|
|
|
|
],
|
|
|
|
'headers' => $headers
|
|
|
|
],
|
|
|
|
'expected' => [
|
|
|
|
'args' => [],
|
|
|
|
'data' => '',
|
|
|
|
'files' => [],
|
|
|
|
'form' => [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'baz' => 'foobar'
|
|
|
|
],
|
|
|
|
'headers' => [
|
|
|
|
'Host' => 'httpbin.org',
|
|
|
|
'User-Agent' => $user_agent,
|
|
|
|
'Content-Length' => '18',
|
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded'
|
|
|
|
],
|
|
|
|
'json' => NULL,
|
|
|
|
'url' => 'https://httpbin.org/patch'
|
|
|
|
],
|
|
|
|
'is_json' => TRUE
|
|
|
|
],
|
|
|
|
'delete' => [
|
|
|
|
'method' => 'delete',
|
|
|
|
'uri' => '/delete',
|
|
|
|
'options' => [
|
|
|
|
'form_params' => [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'baz' => 'foobar'
|
|
|
|
],
|
|
|
|
'headers' => $headers
|
|
|
|
],
|
|
|
|
'expected' => [
|
|
|
|
'args' => [],
|
|
|
|
'data' => '',
|
|
|
|
'files' => [],
|
|
|
|
'form' => [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'baz' => 'foobar'
|
|
|
|
],
|
|
|
|
'headers' => [
|
|
|
|
'Host' => 'httpbin.org',
|
|
|
|
'User-Agent' => $user_agent,
|
|
|
|
'Content-Length' => '18',
|
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded'
|
|
|
|
],
|
|
|
|
'json' => NULL,
|
|
|
|
'url' => 'https://httpbin.org/delete'
|
|
|
|
],
|
|
|
|
'is_json' => TRUE
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataClient
|
|
|
|
*/
|
|
|
|
public function testClient($method, $uri, $options, $expected, $is_json)
|
|
|
|
{
|
2015-10-15 09:49:38 -04:00
|
|
|
|
2015-10-15 09:25:30 -04:00
|
|
|
$result = $this->model->$method($uri, $options);
|
|
|
|
|
|
|
|
if (is_null($result))
|
|
|
|
{
|
|
|
|
$this->assertNull($expected);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-15 09:49:38 -04:00
|
|
|
// Because you have to make another api call to get the origin ip
|
|
|
|
// address, it needs to be retreived outside of the dataProvider method
|
|
|
|
$expected['origin'] = $this->getIp();
|
2015-10-15 09:25:30 -04:00
|
|
|
$actual = ($is_json)
|
|
|
|
? json_decode($result->getBody(), TRUE)
|
|
|
|
: (string) $result->getBody();
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
2015-06-26 12:03:42 -04:00
|
|
|
}
|