Refactor out some Interdependency between Ion and AnimeClient namespaces

This commit is contained in:
Timothy Warren 2016-07-27 13:18:52 -04:00
parent cc7046f0ec
commit 563adace2f
30 changed files with 473 additions and 338 deletions

View File

@ -41,7 +41,7 @@ return function(array $config_array = []) {
$container->set('config', $config); $container->set('config', $config);
// Create Cache Object // Create Cache Object
$container->set('cache', new CacheManager($container)); $container->set('cache', new CacheManager($config));
// Create Aura Router Object // Create Aura Router Object
$container->set('aura-router', new RouterContainer); $container->set('aura-router', new RouterContainer);
@ -71,9 +71,9 @@ return function(array $config_array = []) {
$container->set('session', $session); $container->set('session', $session);
// Miscellaneous helper methods // Miscellaneous helper methods
$anime_client = new AnimeClient(); $util = new Util($container);
$anime_client->setContainer($container); $container->set('anime-client', $util);
$container->set('anime-client', $anime_client); $container->set('util', $util);
// Models // Models
$container->set('api-model', new Model\API($container)); $container->set('api-model', new Model\API($container));

View File

@ -50,8 +50,8 @@
<?= $helper->menu($menu_name) ?> <?= $helper->menu($menu_name) ?>
<br /> <br />
<ul> <ul>
<li class="<?= AnimeClient::is_not_selected('list', $urlGenerator->last_segment()) ?>"><a href="<?= $urlGenerator->url($route_path) ?>">Cover View</a></li> <li class="<?= Util::is_not_selected('list', $urlGenerator->last_segment()) ?>"><a href="<?= $urlGenerator->url($route_path) ?>">Cover View</a></li>
<li class="<?= AnimeClient::is_selected('list', $urlGenerator->last_segment()) ?>"><a href="<?= $urlGenerator->url("{$route_path}/list") ?>">List View</a></li> <li class="<?= Util::is_selected('list', $urlGenerator->last_segment()) ?>"><a href="<?= $urlGenerator->url("{$route_path}/list") ?>">List View</a></li>
</ul> </ul>
<?php endif ?> <?php endif ?>
</nav> </nav>

View File

@ -18,12 +18,11 @@ use Yosymfony\Toml\Toml;
define('SRC_DIR', realpath(__DIR__ . '/../../')); define('SRC_DIR', realpath(__DIR__ . '/../../'));
/** /**
* Odds and Ends class * Application constants
*/ */
class AnimeClient { class AnimeClient {
use \Aviat\Ion\Di\ContainerAware; const HUMMINGBIRD_AUTH_URL = 'https://hummingbird.me/api/v1/users/authenticate';
const SESSION_SEGMENT = 'Aviat\AnimeClient\Auth'; const SESSION_SEGMENT = 'Aviat\AnimeClient\Auth';
const DEFAULT_CONTROLLER_NAMESPACE = 'Aviat\AnimeClient\Controller'; const DEFAULT_CONTROLLER_NAMESPACE = 'Aviat\AnimeClient\Controller';
const DEFAULT_CONTROLLER = 'Aviat\AnimeClient\Controller\Anime'; const DEFAULT_CONTROLLER = 'Aviat\AnimeClient\Controller\Anime';
@ -32,67 +31,6 @@ class AnimeClient {
const ERROR_MESSAGE_METHOD = 'error_page'; const ERROR_MESSAGE_METHOD = 'error_page';
const SRC_DIR = SRC_DIR; const SRC_DIR = SRC_DIR;
private static $form_pages = [
'edit',
'add',
'update',
'update_form',
'login',
'logout',
'details'
];
/**
* HTML selection helper function
*
* @param string $a - First item to compare
* @param string $b - Second item to compare
* @return string
*/
public static function is_selected($a, $b)
{
return ($a === $b) ? 'selected' : '';
}
/**
* Inverse of selected helper function
*
* @param string $a - First item to compare
* @param string $b - Second item to compare
* @return string
*/
public static function is_not_selected($a, $b)
{
return ($a !== $b) ? 'selected' : '';
}
/**
* Determine whether to show the sub-menu
*
* @return bool
*/
public function is_view_page()
{
$url = $this->container->get('request')
->getUri();
$page_segments = explode("/", $url);
$intersect = array_intersect($page_segments, self::$form_pages);
return empty($intersect);
}
/**
* Determine whether the page is a page with a form, and
* not suitable for redirection
*
* @return boolean
*/
public function is_form_page()
{
return ! $this->is_view_page();
}
/** /**
* Load configuration options from .toml files * Load configuration options from .toml files
* *

View File

@ -15,7 +15,6 @@ namespace Aviat\AnimeClient\Auth;
use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\Di\ContainerInterface;
use Aviat\AnimeClient\AnimeClient; use Aviat\AnimeClient\AnimeClient;
use Aviat\AnimeClient\Model\API;
/** /**
* Hummingbird API Authentication * Hummingbird API Authentication
@ -102,6 +101,5 @@ class HummingbirdAuth {
{ {
return $this->segment->get('auth_token', FALSE); return $this->segment->get('auth_token', FALSE);
} }
} }
// End of HummingbirdAuth.php // End of HummingbirdAuth.php

View File

@ -51,7 +51,7 @@ class BaseCommand extends Command {
*/ */
protected function setupContainer() protected function setupContainer()
{ {
$CONF_DIR = __DIR__ . '/../../../../app/config/'; $CONF_DIR = realpath(__DIR__ . '/../../../../app/config/');
require_once $CONF_DIR . '/base_config.php'; // $base_config require_once $CONF_DIR . '/base_config.php'; // $base_config
$config = AnimeClient::load_toml($CONF_DIR); $config = AnimeClient::load_toml($CONF_DIR);
@ -65,7 +65,7 @@ class BaseCommand extends Command {
$container->set('config', $config); $container->set('config', $config);
// Create Cache Object // Create Cache Object
$container->set('cache', new CacheManager($container)); $container->set('cache', new CacheManager($config));
// Create session Object // Create session Object
$session = (new SessionFactory())->newInstance($_COOKIE); $session = (new SessionFactory())->newInstance($_COOKIE);

View File

@ -13,12 +13,14 @@
namespace Aviat\AnimeClient; namespace Aviat\AnimeClient;
use Aviat\Ion\ConfigInterface;
use InvalidArgumentException; use InvalidArgumentException;
/** /**
* Wrapper for configuration values * Wrapper for configuration values
*/ */
class Config { class Config implements ConfigInterface {
use \Aviat\Ion\ArrayWrapper; use \Aviat\Ion\ArrayWrapper;

View File

@ -17,7 +17,6 @@ use Aura\Web\Response;
use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Friend; use Aviat\Ion\Friend;
use Aviat\AnimeClient\AnimeClient;
/** /**

View File

@ -19,7 +19,8 @@ use GuzzleHttp\Psr7\ResponseInterface;
use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ClientException;
use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\Di\ContainerInterface;
use Aviat\AnimeClient\Model as BaseModel; use Aviat\Ion\Model;
use Aviat\AnimeClient\AnimeClient;
/** /**
* Base model for api interaction * Base model for api interaction
@ -32,7 +33,15 @@ use Aviat\AnimeClient\Model as BaseModel;
* @method ResponseInterface post(string $uri, array $options); * @method ResponseInterface post(string $uri, array $options);
* @method ResponseInterface put(string $uri, array $options); * @method ResponseInterface put(string $uri, array $options);
*/ */
class API extends BaseModel { class API extends Model {
use \Aviat\Ion\Di\ContainerAware;
/**
* Config manager
* @var ConfigInterface
*/
protected $config;
/** /**
* Base url for making api requests * Base url for making api requests
@ -65,7 +74,8 @@ class API extends BaseModel {
*/ */
public function __construct(ContainerInterface $container) public function __construct(ContainerInterface $container)
{ {
parent::__construct($container); $this->container = $container;
$this->config = $container->get('config');
$this->cache = $container->get('cache'); $this->cache = $container->get('cache');
$this->init(); $this->init();
} }
@ -171,7 +181,7 @@ class API extends BaseModel {
*/ */
public function authenticate($username, $password) public function authenticate($username, $password)
{ {
$response = $this->post('https://hummingbird.me/api/v1/users/authenticate', [ $response = $this->post(AnimeClient::HUMMINGBIRD_AUTH_URL, [
'form_params' => [ 'form_params' => [
'username' => $username, 'username' => $username,
'password' => $password 'password' => $password

View File

@ -226,9 +226,10 @@ class Anime extends API {
$response = $this->get("users/{$username}/library", $config); $response = $this->get("users/{$username}/library", $config);
$output = $this->transform($status, $response); $output = $this->transform($status, $response);
$util = $this->container->get('util');
foreach ($output as &$row) foreach ($output as &$row)
{ {
$row['anime']['image'] = $this->get_cached_image($row['anime']['image'], $row['anime']['slug'], 'anime'); $row['anime']['image'] = $util->get_cached_image($row['anime']['image'], $row['anime']['slug'], 'anime');
} }
return $output; return $output;

View File

@ -36,7 +36,7 @@ class Collection extends DB {
* Create a new collection object * Create a new collection object
* *
* @param ContainerInterface $container * @param ContainerInterface $container
* @return boolean * @return void
*/ */
public function __construct(ContainerInterface $container) public function __construct(ContainerInterface $container)
{ {

View File

@ -13,12 +13,14 @@
namespace Aviat\AnimeClient\Model; namespace Aviat\AnimeClient\Model;
use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\Di\ContainerInterface;
use Aviat\AnimeClient\Model as BaseModel; use Aviat\Ion\Model;
/** /**
* Base model for database interaction * Base model for database interaction
*/ */
class DB extends BaseModel { class DB extends Model {
use \Aviat\Ion\Di\ContainerAware;
/** /**
* The query builder object * The query builder object
* @var object $db * @var object $db
@ -38,8 +40,8 @@ class DB extends BaseModel {
*/ */
public function __construct(ContainerInterface $container) public function __construct(ContainerInterface $container)
{ {
parent::__construct($container); $this->db_config = $container->get('config')->get('database');
$this->db_config = (array)$this->config->get('database'); $this->setContainer($container);
} }
} }
// End of BaseDBModel.php // End of DB.php

View File

@ -252,9 +252,11 @@ class Manga extends API {
self::COMPLETED => [], self::COMPLETED => [],
]; ];
$util = $this->container->get('util');
foreach ($data as &$entry) foreach ($data as &$entry)
{ {
$entry['manga']['image'] = $this->get_cached_image( $entry['manga']['image'] = $util->get_cached_image(
$entry['manga']['image'], $entry['manga']['image'],
$entry['manga']['slug'], $entry['manga']['slug'],
'manga' 'manga'

View File

@ -1,140 +1,195 @@
<?php <?php
/** /**
* Hummingbird Anime Client * Hummingbird Anime Client
* *
* An API client for Hummingbird to manage anime and manga watch lists * An API client for Hummingbird to manage anime and manga watch lists
* *
* @package HummingbirdAnimeClient * @package HummingbirdAnimeClient
* @author Timothy J. Warren * @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016 * @copyright Copyright (c) 2015 - 2016
* @link https://github.com/timw4mail/HummingBirdAnimeClient * @link https://github.com/timw4mail/HummingBirdAnimeClient
* @license MIT * @license MIT
*/ */
namespace Aviat\AnimeClient; namespace Aviat\AnimeClient;
use abeautifulsite\SimpleImage; use abeautifulsite\SimpleImage;
use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\ConfigInterface;
use Aviat\Ion\Di\ContainerInterface;
/**
* Common base for all Models /**
*/ * Utility method class
class Model { */
class Util {
use \Aviat\Ion\StringWrapper;
use \Aviat\Ion\Di\ContainerAware;
/**
* The global configuration object private static $form_pages = [
* @var Config 'edit',
*/ 'add',
protected $config; 'update',
'update_form',
/** 'login',
* The container object 'logout',
* @var ContainerInterface 'details'
*/ ];
protected $container;
/**
/** * The config manager
* Constructor * @var ConfigInterface
* */
* @param ContainerInterface $container private $config;
*/
public function __construct(ContainerInterface $container) /**
{ * Set up the Util class
$this->container = $container; *
$this->config = $container->get('config'); * @param ContainerInterface $container
} */
public function __construct(ContainerInterface $container)
/** {
* Get the path of the cached version of the image. Create the cached image $this->setContainer($container);
* if the file does not already exist $this->config = $container->get('config');
* }
* @codeCoverageIgnore
* @param string $api_path - The original image url /**
* @param string $series_slug - The part of the url with the series name, becomes the image name * HTML selection helper function
* @param string $type - Anime or Manga, controls cache path *
* @return string - the frontend path for the cached image * @param string $a - First item to compare
* @throws DomainException * @param string $b - Second item to compare
*/ * @return string
public function get_cached_image($api_path, $series_slug, $type = "anime") */
{ public static function is_selected($a, $b)
$path_parts = explode('?', basename($api_path)); {
$path = current($path_parts); return ($a === $b) ? 'selected' : '';
$ext_parts = explode('.', $path); }
$ext = end($ext_parts);
/**
// Workaround for some broken file extensions * Inverse of selected helper function
if ($ext == "jjpg") *
{ * @param string $a - First item to compare
$ext = "jpg"; * @param string $b - Second item to compare
} * @return string
*/
// Failsafe for weird urls public static function is_not_selected($a, $b)
if (strlen($ext) > 3) {
{ return ($a !== $b) ? 'selected' : '';
return $api_path; }
}
/**
$img_cache_path = $this->config->get('img_cache_path'); * Determine whether to show the sub-menu
$cached_image = "{$series_slug}.{$ext}"; *
$cached_path = "{$img_cache_path}/{$type}/{$cached_image}"; * @return bool
*/
// Cache the file if it doesn't already exist public function is_view_page()
if ( ! file_exists($cached_path)) {
{ $url = $this->container->get('request')
if (function_exists('curl_init')) ->getUri();
{ $page_segments = explode("/", $url);
$ch = curl_init($api_path);
$fp = fopen($cached_path, 'wb'); $intersect = array_intersect($page_segments, self::$form_pages);
curl_setopt_array($ch, [
CURLOPT_FILE => $fp, return empty($intersect);
CURLOPT_HEADER => 0 }
]);
curl_exec($ch); /**
curl_close($ch); * Determine whether the page is a page with a form, and
fclose($fp); * not suitable for redirection
} *
else if (ini_get('allow_url_fopen')) * @return boolean
{ */
copy($api_path, $cached_path); public function is_form_page()
} {
else return ! $this->is_view_page();
{ }
throw new DomainException("Couldn't cache images because they couldn't be downloaded.");
} /**
* Get the path of the cached version of the image. Create the cached image
// Resize the image * if the file does not already exist
if ($type == 'anime') *
{ * @codeCoverageIgnore
$resize_width = 220; * @param string $api_path - The original image url
$resize_height = 319; * @param string $series_slug - The part of the url with the series name, becomes the image name
$this->_resize($cached_path, $resize_width, $resize_height); * @param string $type - Anime or Manga, controls cache path
} * @return string - the frontend path for the cached image
} * @throws DomainException
*/
return "/public/images/{$type}/{$cached_image}"; public function get_cached_image($api_path, $series_slug, $type = "anime")
} {
$path_parts = explode('?', basename($api_path));
/** $path = current($path_parts);
* Resize an image $ext_parts = explode('.', $path);
* $ext = end($ext_parts);
* @codeCoverageIgnore
* @param string $path // Workaround for some broken file extensions
* @param string $width if ($ext == "jjpg")
* @param string $height {
*/ $ext = "jpg";
private function _resize($path, $width, $height) }
{
try // Failsafe for weird urls
{ if (strlen($ext) > 3)
$img = new SimpleImage($path); {
$img->resize($width, $height)->save(); return $api_path;
} }
catch (Exception $e)
{ $img_cache_path = $this->config->get('img_cache_path');
// Catch image errors, since they don't otherwise affect $cached_image = "{$series_slug}.{$ext}";
// functionality $cached_path = "{$img_cache_path}/{$type}/{$cached_image}";
}
} // Cache the file if it doesn't already exist
} if ( ! file_exists($cached_path))
// End of BaseModel.php {
if (function_exists('curl_init'))
{
$ch = curl_init($api_path);
$fp = fopen($cached_path, 'wb');
curl_setopt_array($ch, [
CURLOPT_FILE => $fp,
CURLOPT_HEADER => 0
]);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
else if (ini_get('allow_url_fopen'))
{
copy($api_path, $cached_path);
}
else
{
throw new \DomainException("Couldn't cache images because they couldn't be downloaded.");
}
// Resize the image
if ($type == 'anime')
{
$resize_width = 220;
$resize_height = 319;
$this->_resize($cached_path, $resize_width, $resize_height);
}
}
return "/public/images/{$type}/{$cached_image}";
}
/**
* Resize an image
*
* @codeCoverageIgnore
* @param string $path
* @param string $width
* @param string $height
*/
private function _resize($path, $width, $height)
{
try
{
$img = new SimpleImage($path);
$img->resize($width, $height)->save();
}
catch (Exception $e)
{
// Catch image errors, since they don't otherwise affect
// functionality
}
}
}

View File

@ -12,7 +12,7 @@
namespace Aviat\Ion\Cache; namespace Aviat\Ion\Cache;
use \Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\ConfigInterface;
/** /**
* Class proxying cached and fresh values from the selected cache driver * Class proxying cached and fresh values from the selected cache driver
@ -25,11 +25,12 @@ class CacheManager implements CacheInterface {
protected $driver; protected $driver;
/** /**
* Retreive the appropriate driver from the container * Retrieve the appropriate driver from the container
*
* @param ConfigInterface $config The configuration management class
*/ */
public function __construct(ContainerInterface $container) public function __construct(ConfigInterface $config)
{ {
$config = $container->get('config');
$driverConf = $config->get('cache_driver'); $driverConf = $config->get('cache_driver');
if (empty($driverConf)) if (empty($driverConf))
@ -38,13 +39,13 @@ class CacheManager implements CacheInterface {
} }
$driverClass = __NAMESPACE__ . "\\Driver\\{$driverConf}"; $driverClass = __NAMESPACE__ . "\\Driver\\{$driverConf}";
$driver = new $driverClass($container); $driver = new $driverClass($config);
$this->driver = $driver; $this->driver = $driver;
} }
/** /**
* Retreive a cached value if it exists, otherwise, get the value * Retrieve a cached value if it exists, otherwise, get the value
* from the passed arguments * from the passed arguments
* *
* @param object $object - object to retrieve fresh value from * @param object $object - object to retrieve fresh value from
@ -68,7 +69,7 @@ class CacheManager implements CacheInterface {
} }
/** /**
* Retreive a fresh value from the method, and update the cache * Retrieve a fresh value from the method, and update the cache
* @param object $object - object to retrieve fresh value from * @param object $object - object to retrieve fresh value from
* @param string $method - method name to call * @param string $method - method name to call
* @param [array] $args - the arguments to pass to the retrieval method * @param [array] $args - the arguments to pass to the retrieval method

View File

@ -12,12 +12,13 @@
namespace Aviat\Ion\Cache\Driver; namespace Aviat\Ion\Cache\Driver;
use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\ConfigInterface;
use Aviat\Ion\Cache\CacheDriverInterface;
/** /**
* The Driver for no real cache * The Driver for no real cache
*/ */
class NullDriver implements \Aviat\Ion\Cache\CacheDriverInterface { class NullDriver implements CacheDriverInterface {
/** /**
* 'Cache' for Null data store * 'Cache' for Null data store
@ -25,9 +26,11 @@ class NullDriver implements \Aviat\Ion\Cache\CacheDriverInterface {
protected $data; protected $data;
/** /**
* Create the Redis cache driver * Create the Null cache driver
*
* @param ConfigInterface $config The configuration management class
*/ */
public function __construct(ContainerInterface $container) public function __construct(ConfigInterface $config)
{ {
$this->data = []; $this->data = [];
} }

View File

@ -12,7 +12,7 @@
namespace Aviat\Ion\Cache\Driver; namespace Aviat\Ion\Cache\Driver;
use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\ConfigInterface;
use Aviat\Ion\Cache\CacheDriverInterface; use Aviat\Ion\Cache\CacheDriverInterface;
use Predis\Client; use Predis\Client;
@ -20,17 +20,19 @@ use Predis\Client;
class RedisDriver implements CacheDriverInterface { class RedisDriver implements CacheDriverInterface {
/** /**
* The redis extension class instance * THe Predis library instance
* @var Redis *
* @var Client
*/ */
protected $redis; protected $redis;
/** /**
* Create the Redis cache driver * Create the Redis cache driver
*
* @param ConfigInterface $config The configuration management class
*/ */
public function __construct(ContainerInterface $container) public function __construct(ConfigInterface $config)
{ {
$config = $container->get('config');
$redisConfig = $config->get('redis'); $redisConfig = $config->get('redis');
if (array_key_exists('password', $redisConfig) && $redisConfig['password'] === '') if (array_key_exists('password', $redisConfig) && $redisConfig['password'] === '')
@ -50,7 +52,7 @@ class RedisDriver implements CacheDriverInterface {
} }
/** /**
* Retreive a value from the cache backend * Retrieve a value from the cache backend
* *
* @param string $key * @param string $key
* @return mixed * @return mixed

View File

@ -12,9 +12,9 @@
namespace Aviat\Ion\Cache\Driver; namespace Aviat\Ion\Cache\Driver;
use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\ConfigInterface;
use Aviat\Ion\Cache\CacheDriverInterface; use Aviat\Ion\Cache\CacheDriverInterface;
use Aviat\AnimeClient\Model\DB; use Aviat\Ion\Model\DB;
/** /**
* Driver for caching via a traditional SQL database * Driver for caching via a traditional SQL database
@ -29,15 +29,17 @@ class SQLDriver extends DB implements CacheDriverInterface {
/** /**
* Create the driver object * Create the driver object
*
* @param ConfigInterface $config
*/ */
public function __construct(ContainerInterface $container) public function __construct(ConfigInterface $config)
{ {
parent::__construct($container); parent::__construct($config);
$this->db = \Query($this->db_config['collection']); $this->db = \Query($this->db_config['collection']);
} }
/** /**
* Retreive a value from the cache backend * Retrieve a value from the cache backend
* *
* @param string $key * @param string $key
* @return mixed * @return mixed

View File

@ -0,0 +1,41 @@
<?php
/**
* Ion
*
* Building blocks for web development
*
* @package Ion
* @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016
* @license MIT
*/
namespace Aviat\Ion;
interface ConfigInterface {
/**
* Get a config value
*
* @param array|string $key
* @return mixed
*/
public function get($key);
/**
* Set a config value
*
* @param integer|string|array $key
* @param mixed $value
* @throws \InvalidArgumentException
* @return ConfigInterface
*/
public function set($key, $value);
/**
* Remove a config value
*
* @param string|array $key
* @return void
*/
public function delete($key);
}

21
src/Aviat/Ion/Model.php Normal file
View File

@ -0,0 +1,21 @@
<?php
/**
* Ion
*
* Building blocks for web development
*
* @package Ion
* @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016
* @license MIT
*/
namespace Aviat\Ion;
/**
* Common base for all Models
*/
class Model {
use StringWrapper;
}
// End of Model.php

View File

@ -0,0 +1,51 @@
<?php
/**
* Ion
*
* Building blocks for web development
*
* @package Ion
* @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016
* @license MIT
*/
namespace Aviat\Ion\Model;
use Aviat\Ion\ConfigInterface;
use Aviat\Ion\Model as BaseModel;
/**
* Base model for database interaction
*/
class DB extends BaseModel {
/**
* The query builder object
* @var object $db
*/
protected $db;
/**
* The config manager
* @var ConfigInterface
*/
protected $config;
/**
* The database connection information array
* @var array $db_config
*/
protected $db_config;
/**
* Constructor
*
* @param ConfigInterface $config
*/
public function __construct(ConfigInterface $config)
{
$this->config = $config;
$this->db_config = (array)$config->get('database');
}
}
// End of DB.php

View File

@ -5,13 +5,6 @@ use Aviat\AnimeClient\AnimeClient;
class AnimeClientTest extends AnimeClient_TestCase { class AnimeClientTest extends AnimeClient_TestCase {
public function setUp()
{
parent::setUp();
$this->anime_client = new AnimeClient();
$this->anime_client->setContainer($this->container);
}
/** /**
* Basic sanity test for _dir function * Basic sanity test for _dir function
*/ */
@ -19,70 +12,4 @@ class AnimeClientTest extends AnimeClient_TestCase {
{ {
$this->assertEquals('foo' . DIRECTORY_SEPARATOR . 'bar', \_dir('foo', 'bar')); $this->assertEquals('foo' . DIRECTORY_SEPARATOR . 'bar', \_dir('foo', 'bar'));
} }
public function testIsSelected()
{
// Failure to match
$this->assertEquals('', AnimeClient::is_selected('foo', 'bar'));
// Matches
$this->assertEquals('selected', AnimeClient::is_selected('foo', 'foo'));
}
public function testIsNotSelected()
{
// Failure to match
$this->assertEquals('selected', AnimeClient::is_not_selected('foo', 'bar'));
// Matches
$this->assertEquals('', AnimeClient::is_not_selected('foo', 'foo'));
}
public function dataIsViewPage()
{
return [
[
'uri' => '/anime/update',
'expected' => FALSE
],
[
'uri' => '/anime/watching',
'expected' => TRUE
],
[
'uri' => '/manga/reading',
'expected' => TRUE
],
[
'uri' => '/manga/update',
'expected' => FALSE
]
];
}
/**
* @dataProvider dataIsViewPage
*/
public function testIsViewPage($uri, $expected)
{
$this->setSuperGlobals([
'_SERVER' => [
'REQUEST_URI' => $uri
]
]);
$this->assertEquals($expected, $this->anime_client->is_view_page());
}
/**
* @dataProvider dataIsViewPage
*/
public function testIsFormPage($uri, $expected)
{
$this->setSuperGlobals([
'_SERVER' => [
'REQUEST_URI' => $uri
]
]);
$this->assertEquals(!$expected, $this->anime_client->is_form_page());
}
} }

View File

@ -12,6 +12,7 @@ class MangaModelTest extends AnimeClient_TestCase {
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->container->set('util', new MockUtil($this->container));
$this->model = new Friend(new TestMangaModel($this->container)); $this->model = new Friend(new TestMangaModel($this->container));
$this->mockDir = __DIR__ . '/../../test_data/manga_list'; $this->mockDir = __DIR__ . '/../../test_data/manga_list';
} }

View File

@ -0,0 +1,78 @@
<?php
use Aviat\AnimeClient\Util;
class UtilTest extends AnimeClient_TestCase {
public function setUp()
{
parent::setUp();
$this->util = new Util($this->container);
}
public function testIsSelected()
{
// Failure to match
$this->assertEquals('', Util::is_selected('foo', 'bar'));
// Matches
$this->assertEquals('selected', Util::is_selected('foo', 'foo'));
}
public function testIsNotSelected()
{
// Failure to match
$this->assertEquals('selected', Util::is_not_selected('foo', 'bar'));
// Matches
$this->assertEquals('', Util::is_not_selected('foo', 'foo'));
}
public function dataIsViewPage()
{
return [
[
'uri' => '/anime/update',
'expected' => FALSE
],
[
'uri' => '/anime/watching',
'expected' => TRUE
],
[
'uri' => '/manga/reading',
'expected' => TRUE
],
[
'uri' => '/manga/update',
'expected' => FALSE
]
];
}
/**
* @dataProvider dataIsViewPage
*/
public function testIsViewPage($uri, $expected)
{
$this->setSuperGlobals([
'_SERVER' => [
'REQUEST_URI' => $uri
]
]);
$this->assertEquals($expected, $this->util->is_view_page());
}
/**
* @dataProvider dataIsViewPage
*/
public function testIsFormPage($uri, $expected)
{
$this->setSuperGlobals([
'_SERVER' => [
'REQUEST_URI' => $uri
]
]);
$this->assertEquals(!$expected, $this->util->is_form_page());
}
}

View File

@ -1,12 +1,12 @@
<?php <?php
use Aviat\AnimeClient\Model as BaseModel; use Aviat\Ion\Model as BaseModel;
class BaseModelTest extends AnimeClient_TestCase { class BaseModelTest extends AnimeClient_TestCase {
public function testBaseModelSanity() public function testBaseModelSanity()
{ {
$baseModel = new BaseModel($this->container); $baseModel = new BaseModel();
$this->assertTrue(is_object($baseModel)); $this->assertTrue(is_object($baseModel));
} }
} }

View File

@ -15,7 +15,7 @@ class CacheManagerText extends AnimeClient_TestCase {
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->cache = new CacheManager($this->container); $this->cache = new CacheManager($this->container->get('config'), $this->container);
$this->friend = new Friend($this->cache); $this->friend = new Friend($this->cache);
} }

View File

@ -12,6 +12,6 @@ class CacheNullDriverTest extends AnimeClient_TestCase {
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->driver = new NullDriver($this->container); $this->driver = new NullDriver($this->container->get('config'));
} }
} }

View File

@ -16,16 +16,15 @@ class CacheRedisDriverTestTwo extends AnimeClient_TestCase {
parent::setUp(); parent::setUp();
// Setup config with port and password // Setup config with port and password
$container = new Container(); $config = new Config([
$container->set('config', new Config([
'redis' => [ 'redis' => [
'host' => (array_key_exists('REDIS_HOST', $_ENV)) ? $_ENV['REDIS_HOST'] : 'localhost', 'host' => (array_key_exists('REDIS_HOST', $_ENV)) ? $_ENV['REDIS_HOST'] : 'localhost',
'port' => 6379, 'port' => 6379,
'password' => '', 'password' => '',
'database' => 13, 'database' => 13,
] ]
])); ]);
$this->driver = new RedisDriver($container); $this->driver = new RedisDriver($config);
} }
public function tearDown() public function tearDown()

View File

@ -13,7 +13,7 @@ class CacheRedisDriverTest extends AnimeClient_TestCase {
{ {
parent::setUp(); parent::setUp();
$this->driver = new RedisDriver($this->container); $this->driver = new RedisDriver($this->container->get('config'));
} }
public function tearDown() public function tearDown()

View File

@ -13,7 +13,7 @@ class CacheSQLDriverTest extends AnimeClient_TestCase {
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->driver = new SQLDriver($this->container); $this->driver = new SQLDriver($this->container->get('config'));
$friend = new Friend($this->driver); $friend = new Friend($this->driver);
$friend->db->query('CREATE TABLE IF NOT EXISTS "cache" ("key" TEXT NULL, "value" TEXT NULL, PRIMARY KEY ("key"))'); $friend->db->query('CREATE TABLE IF NOT EXISTS "cache" ("key" TEXT NULL, "value" TEXT NULL, PRIMARY KEY ("key"))');
} }

View File

@ -99,6 +99,13 @@ trait MockViewOutputTrait {
} }
} }
class MockUtil {
public function get_cached_image($api_path, $series_slug, $type = "anime")
{
return "/public/images/{$type}/{$series_slug}.jpg";
}
}
class TestView extends View { class TestView extends View {
public function send() {} public function send() {}
protected function output() protected function output()
@ -157,11 +164,6 @@ class TestAnimeModel extends AnimeModel {
class TestMangaModel extends MangaModel { class TestMangaModel extends MangaModel {
use MockInjectionTrait; use MockInjectionTrait;
public function get_cached_image($api_path, $series_slug, $type = "anime")
{
return "/public/images/{$type}/{$series_slug}.jpg";
}
protected function _check_cache($response) protected function _check_cache($response)
{ {
$file = __DIR__ . '/test_data/manga_list/manga-transformed.json'; $file = __DIR__ . '/test_data/manga_list/manga-transformed.json';