From 1b93adefa30db91e7053a2a217ad948f8666b3ee Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Wed, 27 Jul 2016 13:18:52 -0400 Subject: [PATCH] Refactor out some Interdependency between Ion and AnimeClient namespaces --- app/bootstrap.php | 8 +- app/views/header.php | 4 +- src/Aviat/AnimeClient/AnimeClient.php | 66 +--- .../AnimeClient/Auth/HummingbirdAuth.php | 2 - src/Aviat/AnimeClient/Command/BaseCommand.php | 4 +- src/Aviat/AnimeClient/Config.php | 4 +- src/Aviat/AnimeClient/Dispatcher.php | 1 - src/Aviat/AnimeClient/Model/API.php | 18 +- src/Aviat/AnimeClient/Model/Anime.php | 3 +- src/Aviat/AnimeClient/Model/Collection.php | 2 +- src/Aviat/AnimeClient/Model/DB.php | 12 +- src/Aviat/AnimeClient/Model/Manga.php | 4 +- src/Aviat/AnimeClient/{Model.php => Util.php} | 335 ++++++++++-------- src/Aviat/Ion/Cache/CacheManager.php | 15 +- src/Aviat/Ion/Cache/Driver/NullDriver.php | 11 +- src/Aviat/Ion/Cache/Driver/RedisDriver.php | 14 +- src/Aviat/Ion/Cache/Driver/SQLDriver.php | 12 +- src/Aviat/Ion/ConfigInterface.php | 41 +++ src/Aviat/Ion/Model.php | 21 ++ src/Aviat/Ion/Model/DB.php | 51 +++ tests/AnimeClient/AnimeClientTest.php | 73 ---- tests/AnimeClient/Model/MangaModelTest.php | 1 + tests/AnimeClient/UtilTest.php | 78 ++++ tests/{AnimeClient => Ion}/BaseModelTest.php | 4 +- tests/Ion/Cache/CacheManagerTest.php | 2 +- tests/Ion/Cache/Driver/NullDriverTest.php | 2 +- tests/Ion/Cache/Driver/RedisDriver2Test.php | 7 +- tests/Ion/Cache/Driver/RedisDriverTest.php | 2 +- tests/Ion/Cache/Driver/SQLDriverTest.php | 2 +- tests/mocks.php | 12 +- 30 files changed, 473 insertions(+), 338 deletions(-) rename src/Aviat/AnimeClient/{Model.php => Util.php} (62%) create mode 100644 src/Aviat/Ion/ConfigInterface.php create mode 100644 src/Aviat/Ion/Model.php create mode 100644 src/Aviat/Ion/Model/DB.php create mode 100644 tests/AnimeClient/UtilTest.php rename tests/{AnimeClient => Ion}/BaseModelTest.php (62%) diff --git a/app/bootstrap.php b/app/bootstrap.php index c3304640..6b3701ab 100644 --- a/app/bootstrap.php +++ b/app/bootstrap.php @@ -41,7 +41,7 @@ return function(array $config_array = []) { $container->set('config', $config); // Create Cache Object - $container->set('cache', new CacheManager($container)); + $container->set('cache', new CacheManager($config)); // Create Aura Router Object $container->set('aura-router', new RouterContainer); @@ -71,9 +71,9 @@ return function(array $config_array = []) { $container->set('session', $session); // Miscellaneous helper methods - $anime_client = new AnimeClient(); - $anime_client->setContainer($container); - $container->set('anime-client', $anime_client); + $util = new Util($container); + $container->set('anime-client', $util); + $container->set('util', $util); // Models $container->set('api-model', new Model\API($container)); diff --git a/app/views/header.php b/app/views/header.php index 306f5bf9..5ec53689 100644 --- a/app/views/header.php +++ b/app/views/header.php @@ -50,8 +50,8 @@ menu($menu_name) ?>
diff --git a/src/Aviat/AnimeClient/AnimeClient.php b/src/Aviat/AnimeClient/AnimeClient.php index 435afc86..f737124d 100644 --- a/src/Aviat/AnimeClient/AnimeClient.php +++ b/src/Aviat/AnimeClient/AnimeClient.php @@ -18,12 +18,11 @@ use Yosymfony\Toml\Toml; define('SRC_DIR', realpath(__DIR__ . '/../../')); /** - * Odds and Ends class + * Application constants */ 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 DEFAULT_CONTROLLER_NAMESPACE = 'Aviat\AnimeClient\Controller'; const DEFAULT_CONTROLLER = 'Aviat\AnimeClient\Controller\Anime'; @@ -32,67 +31,6 @@ class AnimeClient { const ERROR_MESSAGE_METHOD = 'error_page'; 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 * diff --git a/src/Aviat/AnimeClient/Auth/HummingbirdAuth.php b/src/Aviat/AnimeClient/Auth/HummingbirdAuth.php index c6fa6d02..ac9625cf 100644 --- a/src/Aviat/AnimeClient/Auth/HummingbirdAuth.php +++ b/src/Aviat/AnimeClient/Auth/HummingbirdAuth.php @@ -15,7 +15,6 @@ namespace Aviat\AnimeClient\Auth; use Aviat\Ion\Di\ContainerInterface; use Aviat\AnimeClient\AnimeClient; -use Aviat\AnimeClient\Model\API; /** * Hummingbird API Authentication @@ -102,6 +101,5 @@ class HummingbirdAuth { { return $this->segment->get('auth_token', FALSE); } - } // End of HummingbirdAuth.php \ No newline at end of file diff --git a/src/Aviat/AnimeClient/Command/BaseCommand.php b/src/Aviat/AnimeClient/Command/BaseCommand.php index 980ccc22..2f4b9b48 100644 --- a/src/Aviat/AnimeClient/Command/BaseCommand.php +++ b/src/Aviat/AnimeClient/Command/BaseCommand.php @@ -51,7 +51,7 @@ class BaseCommand extends Command { */ protected function setupContainer() { - $CONF_DIR = __DIR__ . '/../../../../app/config/'; + $CONF_DIR = realpath(__DIR__ . '/../../../../app/config/'); require_once $CONF_DIR . '/base_config.php'; // $base_config $config = AnimeClient::load_toml($CONF_DIR); @@ -65,7 +65,7 @@ class BaseCommand extends Command { $container->set('config', $config); // Create Cache Object - $container->set('cache', new CacheManager($container)); + $container->set('cache', new CacheManager($config)); // Create session Object $session = (new SessionFactory())->newInstance($_COOKIE); diff --git a/src/Aviat/AnimeClient/Config.php b/src/Aviat/AnimeClient/Config.php index e994b42d..3cbf9195 100644 --- a/src/Aviat/AnimeClient/Config.php +++ b/src/Aviat/AnimeClient/Config.php @@ -13,12 +13,14 @@ namespace Aviat\AnimeClient; +use Aviat\Ion\ConfigInterface; + use InvalidArgumentException; /** * Wrapper for configuration values */ -class Config { +class Config implements ConfigInterface { use \Aviat\Ion\ArrayWrapper; diff --git a/src/Aviat/AnimeClient/Dispatcher.php b/src/Aviat/AnimeClient/Dispatcher.php index d1517e38..1d57e3c9 100644 --- a/src/Aviat/AnimeClient/Dispatcher.php +++ b/src/Aviat/AnimeClient/Dispatcher.php @@ -17,7 +17,6 @@ use Aura\Web\Response; use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\Friend; -use Aviat\AnimeClient\AnimeClient; /** diff --git a/src/Aviat/AnimeClient/Model/API.php b/src/Aviat/AnimeClient/Model/API.php index 1fa6511a..feec4985 100644 --- a/src/Aviat/AnimeClient/Model/API.php +++ b/src/Aviat/AnimeClient/Model/API.php @@ -19,7 +19,8 @@ use GuzzleHttp\Psr7\ResponseInterface; use GuzzleHttp\Exception\ClientException; use Aviat\Ion\Di\ContainerInterface; -use Aviat\AnimeClient\Model as BaseModel; +use Aviat\Ion\Model; +use Aviat\AnimeClient\AnimeClient; /** * Base model for api interaction @@ -32,7 +33,15 @@ use Aviat\AnimeClient\Model as BaseModel; * @method ResponseInterface post(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 @@ -65,7 +74,8 @@ class API extends BaseModel { */ public function __construct(ContainerInterface $container) { - parent::__construct($container); + $this->container = $container; + $this->config = $container->get('config'); $this->cache = $container->get('cache'); $this->init(); } @@ -171,7 +181,7 @@ class API extends BaseModel { */ public function authenticate($username, $password) { - $response = $this->post('https://hummingbird.me/api/v1/users/authenticate', [ + $response = $this->post(AnimeClient::HUMMINGBIRD_AUTH_URL, [ 'form_params' => [ 'username' => $username, 'password' => $password diff --git a/src/Aviat/AnimeClient/Model/Anime.php b/src/Aviat/AnimeClient/Model/Anime.php index 8d13c208..449b2648 100644 --- a/src/Aviat/AnimeClient/Model/Anime.php +++ b/src/Aviat/AnimeClient/Model/Anime.php @@ -226,9 +226,10 @@ class Anime extends API { $response = $this->get("users/{$username}/library", $config); $output = $this->transform($status, $response); + $util = $this->container->get('util'); 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; diff --git a/src/Aviat/AnimeClient/Model/Collection.php b/src/Aviat/AnimeClient/Model/Collection.php index 22e19378..0bec057b 100644 --- a/src/Aviat/AnimeClient/Model/Collection.php +++ b/src/Aviat/AnimeClient/Model/Collection.php @@ -36,7 +36,7 @@ class Collection extends DB { * Create a new collection object * * @param ContainerInterface $container - * @return boolean + * @return void */ public function __construct(ContainerInterface $container) { diff --git a/src/Aviat/AnimeClient/Model/DB.php b/src/Aviat/AnimeClient/Model/DB.php index 82167e54..8ad0e9f0 100644 --- a/src/Aviat/AnimeClient/Model/DB.php +++ b/src/Aviat/AnimeClient/Model/DB.php @@ -13,12 +13,14 @@ namespace Aviat\AnimeClient\Model; use Aviat\Ion\Di\ContainerInterface; -use Aviat\AnimeClient\Model as BaseModel; +use Aviat\Ion\Model; /** * Base model for database interaction */ -class DB extends BaseModel { +class DB extends Model { + use \Aviat\Ion\Di\ContainerAware; + /** * The query builder object * @var object $db @@ -38,8 +40,8 @@ class DB extends BaseModel { */ public function __construct(ContainerInterface $container) { - parent::__construct($container); - $this->db_config = (array)$this->config->get('database'); + $this->db_config = $container->get('config')->get('database'); + $this->setContainer($container); } } -// End of BaseDBModel.php \ No newline at end of file +// End of DB.php \ No newline at end of file diff --git a/src/Aviat/AnimeClient/Model/Manga.php b/src/Aviat/AnimeClient/Model/Manga.php index 6a6e3c94..999578af 100644 --- a/src/Aviat/AnimeClient/Model/Manga.php +++ b/src/Aviat/AnimeClient/Model/Manga.php @@ -252,9 +252,11 @@ class Manga extends API { self::COMPLETED => [], ]; + $util = $this->container->get('util'); + foreach ($data as &$entry) { - $entry['manga']['image'] = $this->get_cached_image( + $entry['manga']['image'] = $util->get_cached_image( $entry['manga']['image'], $entry['manga']['slug'], 'manga' diff --git a/src/Aviat/AnimeClient/Model.php b/src/Aviat/AnimeClient/Util.php similarity index 62% rename from src/Aviat/AnimeClient/Model.php rename to src/Aviat/AnimeClient/Util.php index ba4861fa..cfabaca3 100644 --- a/src/Aviat/AnimeClient/Model.php +++ b/src/Aviat/AnimeClient/Util.php @@ -1,140 +1,195 @@ -container = $container; - $this->config = $container->get('config'); - } - - /** - * Get the path of the cached version of the image. Create the cached image - * if the file does not already exist - * - * @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 - * @param string $type - Anime or Manga, controls cache path - * @return string - the frontend path for the cached image - * @throws DomainException - */ - public function get_cached_image($api_path, $series_slug, $type = "anime") - { - $path_parts = explode('?', basename($api_path)); - $path = current($path_parts); - $ext_parts = explode('.', $path); - $ext = end($ext_parts); - - // Workaround for some broken file extensions - if ($ext == "jjpg") - { - $ext = "jpg"; - } - - // Failsafe for weird urls - if (strlen($ext) > 3) - { - return $api_path; - } - - $img_cache_path = $this->config->get('img_cache_path'); - $cached_image = "{$series_slug}.{$ext}"; - $cached_path = "{$img_cache_path}/{$type}/{$cached_image}"; - - // Cache the file if it doesn't already exist - if ( ! file_exists($cached_path)) - { - 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 - } - } -} -// End of BaseModel.php \ No newline at end of file +setContainer($container); + $this->config = $container->get('config'); + } + + /** + * 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(); + } + + /** + * Get the path of the cached version of the image. Create the cached image + * if the file does not already exist + * + * @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 + * @param string $type - Anime or Manga, controls cache path + * @return string - the frontend path for the cached image + * @throws DomainException + */ + public function get_cached_image($api_path, $series_slug, $type = "anime") + { + $path_parts = explode('?', basename($api_path)); + $path = current($path_parts); + $ext_parts = explode('.', $path); + $ext = end($ext_parts); + + // Workaround for some broken file extensions + if ($ext == "jjpg") + { + $ext = "jpg"; + } + + // Failsafe for weird urls + if (strlen($ext) > 3) + { + return $api_path; + } + + $img_cache_path = $this->config->get('img_cache_path'); + $cached_image = "{$series_slug}.{$ext}"; + $cached_path = "{$img_cache_path}/{$type}/{$cached_image}"; + + // Cache the file if it doesn't already exist + if ( ! file_exists($cached_path)) + { + 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 + } + } +} \ No newline at end of file diff --git a/src/Aviat/Ion/Cache/CacheManager.php b/src/Aviat/Ion/Cache/CacheManager.php index b5e32cb4..34fc4887 100644 --- a/src/Aviat/Ion/Cache/CacheManager.php +++ b/src/Aviat/Ion/Cache/CacheManager.php @@ -12,7 +12,7 @@ namespace Aviat\Ion\Cache; -use \Aviat\Ion\Di\ContainerInterface; +use Aviat\Ion\ConfigInterface; /** * Class proxying cached and fresh values from the selected cache driver @@ -25,11 +25,12 @@ class CacheManager implements CacheInterface { 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'); if (empty($driverConf)) @@ -38,13 +39,13 @@ class CacheManager implements CacheInterface { } $driverClass = __NAMESPACE__ . "\\Driver\\{$driverConf}"; - $driver = new $driverClass($container); + $driver = new $driverClass($config); $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 * * @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 string $method - method name to call * @param [array] $args - the arguments to pass to the retrieval method diff --git a/src/Aviat/Ion/Cache/Driver/NullDriver.php b/src/Aviat/Ion/Cache/Driver/NullDriver.php index b7d0bb9c..aca42a2c 100644 --- a/src/Aviat/Ion/Cache/Driver/NullDriver.php +++ b/src/Aviat/Ion/Cache/Driver/NullDriver.php @@ -12,12 +12,13 @@ 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 */ -class NullDriver implements \Aviat\Ion\Cache\CacheDriverInterface { +class NullDriver implements CacheDriverInterface { /** * 'Cache' for Null data store @@ -25,9 +26,11 @@ class NullDriver implements \Aviat\Ion\Cache\CacheDriverInterface { 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 = []; } diff --git a/src/Aviat/Ion/Cache/Driver/RedisDriver.php b/src/Aviat/Ion/Cache/Driver/RedisDriver.php index db265e13..b0b436d6 100644 --- a/src/Aviat/Ion/Cache/Driver/RedisDriver.php +++ b/src/Aviat/Ion/Cache/Driver/RedisDriver.php @@ -12,7 +12,7 @@ namespace Aviat\Ion\Cache\Driver; -use Aviat\Ion\Di\ContainerInterface; +use Aviat\Ion\ConfigInterface; use Aviat\Ion\Cache\CacheDriverInterface; use Predis\Client; @@ -20,17 +20,19 @@ use Predis\Client; class RedisDriver implements CacheDriverInterface { /** - * The redis extension class instance - * @var Redis + * THe Predis library instance + * + * @var Client */ protected $redis; /** * 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'); 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 * @return mixed diff --git a/src/Aviat/Ion/Cache/Driver/SQLDriver.php b/src/Aviat/Ion/Cache/Driver/SQLDriver.php index dc1c47ef..483494e4 100644 --- a/src/Aviat/Ion/Cache/Driver/SQLDriver.php +++ b/src/Aviat/Ion/Cache/Driver/SQLDriver.php @@ -12,9 +12,9 @@ namespace Aviat\Ion\Cache\Driver; -use Aviat\Ion\Di\ContainerInterface; +use Aviat\Ion\ConfigInterface; use Aviat\Ion\Cache\CacheDriverInterface; -use Aviat\AnimeClient\Model\DB; +use Aviat\Ion\Model\DB; /** * Driver for caching via a traditional SQL database @@ -29,15 +29,17 @@ class SQLDriver extends DB implements CacheDriverInterface { /** * 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']); } /** - * Retreive a value from the cache backend + * Retrieve a value from the cache backend * * @param string $key * @return mixed diff --git a/src/Aviat/Ion/ConfigInterface.php b/src/Aviat/Ion/ConfigInterface.php new file mode 100644 index 00000000..9c644bae --- /dev/null +++ b/src/Aviat/Ion/ConfigInterface.php @@ -0,0 +1,41 @@ +config = $config; + $this->db_config = (array)$config->get('database'); + } +} +// End of DB.php \ No newline at end of file diff --git a/tests/AnimeClient/AnimeClientTest.php b/tests/AnimeClient/AnimeClientTest.php index ee649c85..75a89554 100644 --- a/tests/AnimeClient/AnimeClientTest.php +++ b/tests/AnimeClient/AnimeClientTest.php @@ -5,13 +5,6 @@ use Aviat\AnimeClient\AnimeClient; 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 */ @@ -19,70 +12,4 @@ class AnimeClientTest extends AnimeClient_TestCase { { $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()); - } } diff --git a/tests/AnimeClient/Model/MangaModelTest.php b/tests/AnimeClient/Model/MangaModelTest.php index d8cd4b21..bc97fca7 100644 --- a/tests/AnimeClient/Model/MangaModelTest.php +++ b/tests/AnimeClient/Model/MangaModelTest.php @@ -12,6 +12,7 @@ class MangaModelTest extends AnimeClient_TestCase { public function setUp() { parent::setUp(); + $this->container->set('util', new MockUtil($this->container)); $this->model = new Friend(new TestMangaModel($this->container)); $this->mockDir = __DIR__ . '/../../test_data/manga_list'; } diff --git a/tests/AnimeClient/UtilTest.php b/tests/AnimeClient/UtilTest.php new file mode 100644 index 00000000..67492ce5 --- /dev/null +++ b/tests/AnimeClient/UtilTest.php @@ -0,0 +1,78 @@ +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()); + } +} diff --git a/tests/AnimeClient/BaseModelTest.php b/tests/Ion/BaseModelTest.php similarity index 62% rename from tests/AnimeClient/BaseModelTest.php rename to tests/Ion/BaseModelTest.php index 01ff61b6..edc2a2f4 100644 --- a/tests/AnimeClient/BaseModelTest.php +++ b/tests/Ion/BaseModelTest.php @@ -1,12 +1,12 @@ container); + $baseModel = new BaseModel(); $this->assertTrue(is_object($baseModel)); } } \ No newline at end of file diff --git a/tests/Ion/Cache/CacheManagerTest.php b/tests/Ion/Cache/CacheManagerTest.php index bad7a202..7d88dcc1 100644 --- a/tests/Ion/Cache/CacheManagerTest.php +++ b/tests/Ion/Cache/CacheManagerTest.php @@ -15,7 +15,7 @@ class CacheManagerText extends AnimeClient_TestCase { public function 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); } diff --git a/tests/Ion/Cache/Driver/NullDriverTest.php b/tests/Ion/Cache/Driver/NullDriverTest.php index 6771fdea..df6606d4 100644 --- a/tests/Ion/Cache/Driver/NullDriverTest.php +++ b/tests/Ion/Cache/Driver/NullDriverTest.php @@ -12,6 +12,6 @@ class CacheNullDriverTest extends AnimeClient_TestCase { public function setUp() { parent::setUp(); - $this->driver = new NullDriver($this->container); + $this->driver = new NullDriver($this->container->get('config')); } } \ No newline at end of file diff --git a/tests/Ion/Cache/Driver/RedisDriver2Test.php b/tests/Ion/Cache/Driver/RedisDriver2Test.php index 35317251..4962c35a 100644 --- a/tests/Ion/Cache/Driver/RedisDriver2Test.php +++ b/tests/Ion/Cache/Driver/RedisDriver2Test.php @@ -16,16 +16,15 @@ class CacheRedisDriverTestTwo extends AnimeClient_TestCase { parent::setUp(); // Setup config with port and password - $container = new Container(); - $container->set('config', new Config([ + $config = new Config([ 'redis' => [ 'host' => (array_key_exists('REDIS_HOST', $_ENV)) ? $_ENV['REDIS_HOST'] : 'localhost', 'port' => 6379, 'password' => '', 'database' => 13, ] - ])); - $this->driver = new RedisDriver($container); + ]); + $this->driver = new RedisDriver($config); } public function tearDown() diff --git a/tests/Ion/Cache/Driver/RedisDriverTest.php b/tests/Ion/Cache/Driver/RedisDriverTest.php index 3ae45a9a..ed1209f5 100644 --- a/tests/Ion/Cache/Driver/RedisDriverTest.php +++ b/tests/Ion/Cache/Driver/RedisDriverTest.php @@ -13,7 +13,7 @@ class CacheRedisDriverTest extends AnimeClient_TestCase { { parent::setUp(); - $this->driver = new RedisDriver($this->container); + $this->driver = new RedisDriver($this->container->get('config')); } public function tearDown() diff --git a/tests/Ion/Cache/Driver/SQLDriverTest.php b/tests/Ion/Cache/Driver/SQLDriverTest.php index d23d9ec1..4cd98659 100644 --- a/tests/Ion/Cache/Driver/SQLDriverTest.php +++ b/tests/Ion/Cache/Driver/SQLDriverTest.php @@ -13,7 +13,7 @@ class CacheSQLDriverTest extends AnimeClient_TestCase { public function setUp() { parent::setUp(); - $this->driver = new SQLDriver($this->container); + $this->driver = new SQLDriver($this->container->get('config')); $friend = new Friend($this->driver); $friend->db->query('CREATE TABLE IF NOT EXISTS "cache" ("key" TEXT NULL, "value" TEXT NULL, PRIMARY KEY ("key"))'); } diff --git a/tests/mocks.php b/tests/mocks.php index 6b48a815..d4884bbe 100644 --- a/tests/mocks.php +++ b/tests/mocks.php @@ -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 { public function send() {} protected function output() @@ -157,11 +164,6 @@ class TestAnimeModel extends AnimeModel { class TestMangaModel extends MangaModel { 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) { $file = __DIR__ . '/test_data/manga_list/manga-transformed.json';