HummingBirdAnimeClient/tests/AnimeClient/mocks.php

194 lines
3.6 KiB
PHP
Raw Permalink Normal View History

2022-03-04 12:19:47 -05:00
<?php declare(strict_types=1);
2015-10-19 12:50:46 -04:00
/**
* All the mock classes that extend the classes they are used to test
*/
2017-02-15 16:40:18 -05:00
use Aviat\AnimeClient\Model\{
API as BaseApiModel,
2022-03-04 12:19:47 -05:00
Anime as AnimeModel,
2017-02-15 16:40:18 -05:00
Manga as MangaModel
};
2015-10-19 12:50:46 -04:00
use Aviat\Ion\Transformer\AbstractTransformer;
2017-02-15 16:40:18 -05:00
use Aviat\Ion\View\{HtmlView, HttpView, JsonView};
2022-03-04 12:19:47 -05:00
use Aviat\Ion\{Enum, Friend, Json};
2015-10-19 12:50:46 -04:00
// -----------------------------------------------------------------------------
// Mock the default error handler
// -----------------------------------------------------------------------------
2022-03-04 12:19:47 -05:00
class MockErrorHandler
{
public function addDataTable(string $name, array $values=[]): void
{
}
2015-10-19 12:50:46 -04:00
}
// -----------------------------------------------------------------------------
// Ion Mocks
// -----------------------------------------------------------------------------
2022-03-04 12:19:47 -05:00
class TestEnum extends Enum
{
2023-05-19 10:56:23 -04:00
final public const FOO = 'bar';
final public const BAR = 'foo';
final public const FOOBAR = 'baz';
2015-10-19 12:50:46 -04:00
}
2022-03-04 12:19:47 -05:00
class FriendGrandParentTestClass
{
2020-04-13 09:20:05 -04:00
protected int $grandParentProtected = 84;
2015-10-19 12:50:46 -04:00
}
2022-03-04 12:19:47 -05:00
class FriendParentTestClass extends FriendGrandParentTestClass
{
2020-04-13 09:20:05 -04:00
protected int $parentProtected = 47;
private int $parentPrivate = 654;
2015-10-19 12:50:46 -04:00
}
2022-03-04 12:19:47 -05:00
class FriendTestClass extends FriendParentTestClass
{
2020-04-13 09:20:05 -04:00
protected int $protected = 356;
private int $private = 486;
2015-10-19 12:50:46 -04:00
2020-04-13 09:20:05 -04:00
protected function getProtected(): int
2015-10-19 12:50:46 -04:00
{
return 4;
}
2020-04-13 09:20:05 -04:00
private function getPrivate(): int
2015-10-19 12:50:46 -04:00
{
return 23;
}
}
2022-03-04 12:19:47 -05:00
class TestTransformer extends AbstractTransformer
{
2020-04-13 09:20:05 -04:00
public function transform($item): array
2015-10-19 12:50:46 -04:00
{
$out = [];
$genre_list = (array) $item;
2015-10-21 15:43:51 -04:00
2022-03-04 12:19:47 -05:00
foreach ($genre_list as $genre)
2015-10-19 12:50:46 -04:00
{
$out[] = $genre['name'];
}
2015-10-21 15:43:51 -04:00
2015-10-19 12:50:46 -04:00
return $out;
}
}
2022-03-04 12:19:47 -05:00
trait MockViewOutputTrait
{
protected function output(): void
{
2015-10-19 12:50:46 -04:00
$reflect = new ReflectionClass($this);
$properties = $reflect->getProperties();
$props = [];
2022-03-04 12:19:47 -05:00
foreach ($properties as $reflectProp)
2015-10-19 12:50:46 -04:00
{
$reflectProp->setAccessible(TRUE);
$props[$reflectProp->getName()] = $reflectProp->getValue($this);
}
2020-07-31 16:22:32 -04:00
$view = new TestView();
2015-10-19 12:50:46 -04:00
$friend = new Friend($view);
2022-03-04 12:19:47 -05:00
foreach ($props as $name => $val)
2015-10-19 12:50:46 -04:00
{
$friend->__set($name, $val);
}
$friend->output();
}
}
2022-03-04 12:19:47 -05:00
class MockUtil
{
public function get_cached_image($api_path, $series_slug, $type = 'anime'): string
{
return "/public/images/{$type}/{$series_slug}.jpg";
}
}
2022-03-04 12:19:47 -05:00
class TestView extends HttpView
{
public function send(): void
{
}
protected function output(): void
2016-01-08 11:40:24 -05:00
{
/*$content =& $this->response->content;
2016-01-08 11:40:24 -05:00
$content->set($this->output);
$content->setType($this->contentType);
$content->setCharset('utf-8');*/
2016-01-08 11:40:24 -05:00
}
}
2022-03-04 12:19:47 -05:00
class TestHtmlView extends HtmlView
{
2015-10-19 12:50:46 -04:00
use MockViewOutputTrait;
}
2022-03-04 12:19:47 -05:00
class TestHttpView extends HttpView
{
2015-10-19 12:50:46 -04:00
use MockViewOutputTrait;
}
2022-03-04 12:19:47 -05:00
class TestJsonView extends JsonView
{
public function __destruct()
{
}
2015-10-19 12:50:46 -04:00
}
// -----------------------------------------------------------------------------
// AnimeClient Mocks
// -----------------------------------------------------------------------------
2022-03-04 12:19:47 -05:00
trait MockInjectionTrait
{
public function __get(string $key): mixed
2015-10-19 12:50:46 -04:00
{
2022-03-04 12:19:47 -05:00
return $this->{$key};
2015-10-19 12:50:46 -04:00
}
2015-10-21 15:43:51 -04:00
public function __set(string $key, mixed $value)
2015-10-19 12:50:46 -04:00
{
2022-03-04 12:19:47 -05:00
$this->{$key} = $value;
2015-10-19 12:50:46 -04:00
return $this;
}
}
2022-03-04 12:19:47 -05:00
class MockBaseApiModel extends BaseApiModel
{
2015-10-21 15:43:51 -04:00
use MockInjectionTrait;
2022-03-04 12:19:47 -05:00
2020-04-13 09:20:05 -04:00
protected string $base_url = 'https://httpbin.org/';
2015-10-19 12:50:46 -04:00
2016-10-20 22:09:36 -04:00
protected function _get_list_from_api(string $status): array
{
return [];
}
2015-10-21 15:43:51 -04:00
}
2015-10-19 12:50:46 -04:00
2022-03-04 12:19:47 -05:00
class TestAnimeModel extends AnimeModel
{
2015-11-17 16:45:41 -05:00
use MockInjectionTrait;
}
2015-10-21 15:43:51 -04:00
2022-03-04 12:19:47 -05:00
class TestMangaModel extends MangaModel
{
2015-10-21 15:43:51 -04:00
use MockInjectionTrait;
2015-11-17 16:45:41 -05:00
2015-11-18 10:31:42 -05:00
protected function _check_cache($response)
2015-11-17 16:45:41 -05:00
{
2015-11-18 10:31:42 -05:00
$file = __DIR__ . '/test_data/manga_list/manga-transformed.json';
2022-03-04 12:19:47 -05:00
2016-01-07 13:45:43 -05:00
return Json::decodeFile($file);
2015-11-17 16:45:41 -05:00
}
2015-10-19 12:50:46 -04:00
}
2022-03-04 12:19:47 -05:00
// End of mocks.php