More test coverage

This commit is contained in:
Timothy Warren 2015-10-21 15:43:51 -04:00
parent 32d20a9234
commit 4ad6178bf0
9 changed files with 123 additions and 54 deletions

View File

@ -125,7 +125,7 @@ class Anime extends API {
$response = $this->client->get("anime/{$anime_id}", $config); $response = $this->client->get("anime/{$anime_id}", $config);
return $response->json(); return json_decode($response->getBody(), TRUE);
} }
/** /**

View File

@ -53,4 +53,24 @@ class AnimeCollectionModelTest extends AnimeClient_TestCase {
$collectionModel = new Friend(new AnimeCollectionModel($this->container)); $collectionModel = new Friend(new AnimeCollectionModel($this->container));
$this->assertFalse($collectionModel->valid_database); $this->assertFalse($collectionModel->valid_database);
} }
public function testNonExistentDatabase()
{
$this->container->set('config', new Config([
'database' => [
'collection' => [
'type' => 'sqlite',
'host' => '',
'user' => '',
'pass' => '',
'port' => '',
'name' => 'default',
'database' => '',
'file' => '/foo/bar/baz/foobazbar.db',
]
]
]));
$collectionModel = new Friend(new AnimeCollectionModel($this->container));
$this->assertFalse($collectionModel->valid_database);
}
} }

View File

@ -1,30 +0,0 @@
<?php
use Aviat\Ion\Friend;
use Aviat\Ion\Di\ContainerInterface;
use Aviat\AnimeClient\Model\Anime as AnimeModel;
class AnimeModelTest extends AnimeClient_TestCase {
public function setUp()
{
parent::setUp();
$this->animeModel = new Friend(new TestAnimeModel($this->container));
}
protected function _pluck_anime_titles($array)
{
$out = [];
foreach($array as $index => $item)
{
$out[] = $item['anime']['title'];
}
return $out;
}
/*public function testSortByName()
{
$data = $this->animeModel->_get_list_from_api("completed");
}*/
}

View File

@ -0,0 +1,62 @@
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Aviat\Ion\Friend;
use Aviat\Ion\Di\ContainerInterface;
use Aviat\AnimeClient\Model\Anime as AnimeModel;
class AnimeModelTest extends AnimeClient_TestCase {
public function setUp()
{
parent::setUp();
$this->animeModel = new Friend(new TestAnimeModel($this->container));
$this->mockDir = __DIR__ . '/../../test_data/anime_list/search_mocks';
}
public function dataSearch()
{
return [
'nonsense search' => [
'search' => 'foo',
],
'search for common series' => [
'search' => 'Fate',
],
'search for weird series' => [
'search' => 'Twintails',
]
];
}
/**
* @dataProvider dataSearch
*/
public function testSearch($search)
{
// Mock requests
$json = file_get_contents(_dir($this->mockDir, "{$search}.json"));
$client = $this->getMockClient(200, [
'Content-Type' => 'application/json'
], $json);
$this->animeModel->__set('client', $client);
$actual = $this->animeModel->search($search);
$this->assertEquals(json_decode($json, TRUE), $actual);
}
public function testSearchBadResponse()
{
$client = $this->getMockClient(400, [
'Content-Type' => 'application/json'
], "[]");
$this->animeModel->__set('client', $client);
$this->setExpectedException('\RuntimeException');
$this->animeModel->search('');
}
}

View File

@ -1,6 +1,10 @@
<?php <?php
use Aura\Web\WebFactory; use Aura\Web\WebFactory;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Aviat\AnimeClient\Config; use Aviat\AnimeClient\Config;
@ -69,5 +73,25 @@ class AnimeClient_TestCase extends PHPUnit_Framework_TestCase {
$this->container->set('request', $web_factory->newRequest()); $this->container->set('request', $web_factory->newRequest());
$this->container->set('response', $web_factory->newResponse()); $this->container->set('response', $web_factory->newResponse());
} }
/**
* Create a mock guzzle client for testing
* api call methods
*
* @param int $code The status code
* @param array $headers
* @param string $body
* @return Client
*/
public function getMockClient($code, $headers, $body)
{
$mock = new MockHandler([
new Response($code, $headers, $body)
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
return $client;
}
} }
// End of AnimeClient_TestCase.php // End of AnimeClient_TestCase.php

View File

@ -59,17 +59,17 @@ class FriendTestClass extends FriendParentTestClass {
} }
class TestTransformer extends AbstractTransformer { class TestTransformer extends AbstractTransformer {
public function transform($item) public function transform($item)
{ {
$out = []; $out = [];
$genre_list = (array) $item; $genre_list = (array) $item;
foreach($genre_list as $genre) foreach($genre_list as $genre)
{ {
$out[] = $genre['name']; $out[] = $genre['name'];
} }
return $out; return $out;
} }
} }
@ -114,16 +114,13 @@ class TestJsonView extends JsonView {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// AnimeClient Mocks // AnimeClient Mocks
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
class MockBaseApiModel extends BaseApiModel {
protected $base_url = 'https://httpbin.org/';
trait MockInjectionTrait {
public function __get($key) public function __get($key)
{ {
return $this->$key; return $this->$key;
} }
public function __set($key, $value) public function __set($key, $value)
{ {
$this->$key = $value; $this->$key = $value;
@ -131,22 +128,15 @@ class MockBaseApiModel extends BaseApiModel {
} }
} }
class MockBaseApiModel extends BaseApiModel {
use MockInjectionTrait;
protected $base_url = 'https://httpbin.org/';
}
class TestAnimeModel extends AnimeModel { class TestAnimeModel extends AnimeModel {
protected $transformed_data_file; use MockInjectionTrait;
public function __construct(ContainerInterface $container)
{
parent::__construct($container);
$this->transformed_data_file = _dir(
TEST_DATA_DIR, 'anime_list','anime-completed-transformed.json'
);
}
protected function _get_list_from_api($status="all")
{
$data = json_decode(file_get_contents($this->transformed_data_file), TRUE);
return $data;
}
} }
// End of mocks.php // End of mocks.php

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1 @@
[{"id":8710,"mal_id":24705,"slug":"ore-twintails-ni-narimasu","status":"Finished Airing","url":"https://hummingbird.me/anime/ore-twintails-ni-narimasu","title":"Ore, Twintails ni Narimasu.","alternate_title":"Gonna be the Twin-Tails!!","episode_count":12,"episode_length":24,"cover_image":"https://static.hummingbird.me/anime/poster_images/000/008/710/large/ore-twintails-ni-narimasu.jpg?1416244663","synopsis":"Mitsuka Souji is a first year high school student who greatly loves the \"twintails\" hairstyle. One day a beautiful girl, Twoearle, who comes from another world suddenly appeared in front of him and gave him the power to transform into the twintails warrior TailRed. Now Souji, with the help of his childhood friend Tsube Aika who can becomes the twintails warrior TailBlue, must fight in order to protect the peace on earth.\n\n(Source: Wikipedia)","show_type":"TV","started_airing":"2014-10-10","finished_airing":"2014-12-26","community_rating":3.32172789396078,"age_rating":"PG13","genres":[{"name":"Action"},{"name":"Comedy"},{"name":"Fantasy"},{"name":"Romance"},{"name":"School"},{"name":"Gender Bender"}]}]

File diff suppressed because one or more lines are too long