From 38faaebb5feb4e267a9b17a8b8af0c2d133401de Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Wed, 6 Jan 2016 15:44:40 -0500 Subject: [PATCH] Remove unnamespaced constants, and improve some tests --- app/config/base_config.php | 15 +-- index.php | 30 +++--- src/Aviat/AnimeClient/AnimeClient.php | 34 +++++++ src/Aviat/AnimeClient/Dispatcher.php | 6 +- src/Aviat/AnimeClient/Model/Anime.php | 9 +- .../AnimeClient/Model/AnimeCollection.php | 3 +- src/Aviat/AnimeClient/Model/Manga.php | 31 ++++--- tests/AnimeClient/AnimeClientTest.php | 13 +++ .../Transformer/AnimeListTransformerTest.php | 4 +- .../Transformer/MangaListTransformerTest.php | 93 ++++++++++++++++++- .../Transformer/MangaListsZipperTest.php | 4 +- tests/AnimeClient/Model/MangaModelTest.php | 13 ++- tests/AnimeClient/RoutingBaseTest.php | 5 +- tests/AnimeClient_TestCase.php | 17 +++- tests/Ion/View/HtmlViewTest.php | 2 +- tests/bootstrap.php | 40 +++++--- tests/mocks.php | 2 +- 17 files changed, 245 insertions(+), 76 deletions(-) diff --git a/app/config/base_config.php b/app/config/base_config.php index 586cc9bd..a2e4fb5a 100644 --- a/app/config/base_config.php +++ b/app/config/base_config.php @@ -16,16 +16,19 @@ // // You shouldn't generally need to change anything below this line // ---------------------------------------------------------------------------- +$APP_DIR = realpath(__DIR__ . '/../'); +$ROOT_DIR = realpath("{$APP_DIR}/../"); + $base_config = [ // Template file path - 'view_path' => _dir(APP_DIR, 'views'), + 'view_path' => "{$APP_DIR}/views", // Cache paths - 'data_cache_path' => _dir(APP_DIR, 'cache'), - 'img_cache_path' => _dir(ROOT_DIR, 'public/images'), + 'data_cache_path' => "{$APP_DIR}/cache", + 'img_cache_path' => "{$ROOT_DIR}/public/images", // Included config files - 'database' => require __DIR__ . '/database.php', - 'menus' => require __DIR__ . '/menus.php', - 'routes' => require __DIR__ . '/routes.php', + 'database' => require 'database.php', + 'menus' => require 'menus.php', + 'routes' => require 'routes.php', ]; \ No newline at end of file diff --git a/index.php b/index.php index 1a4904f8..244b4982 100644 --- a/index.php +++ b/index.php @@ -20,13 +20,6 @@ if ($timezone === '' || $timezone === FALSE) ini_set('date.timezone', 'GMT'); } -// Define base directories -define('ROOT_DIR', __DIR__); -define('APP_DIR', ROOT_DIR . DIRECTORY_SEPARATOR . 'app'); -define('SRC_DIR', ROOT_DIR . DIRECTORY_SEPARATOR . 'src'); -define('CONF_DIR', APP_DIR . DIRECTORY_SEPARATOR . 'config'); -define('BASE_DIR', SRC_DIR . DIRECTORY_SEPARATOR . 'Base'); - /** * Joins paths together. Variadic to take an * arbitrary number of arguments @@ -38,15 +31,20 @@ function _dir() return implode(DIRECTORY_SEPARATOR, func_get_args()); } +// Define base directories +$APP_DIR = _dir(__DIR__, 'app'); +$SRC_DIR = _dir(__DIR__, 'src'); +$CONF_DIR = _dir($APP_DIR, 'config'); + /** * Set up autoloaders * * @codeCoverageIgnore * @return void */ -spl_autoload_register(function($class) { +spl_autoload_register(function($class) use ($SRC_DIR) { $class_parts = explode('\\', $class); - $ns_path = SRC_DIR . '/' . implode('/', $class_parts) . ".php"; + $ns_path = $SRC_DIR . '/' . implode('/', $class_parts) . ".php"; if (file_exists($ns_path)) { @@ -55,7 +53,7 @@ spl_autoload_register(function($class) { } }); -require _dir(ROOT_DIR, '/vendor/autoload.php'); +require _dir(__DIR__, '/vendor/autoload.php'); // ------------------------------------------------------------------------- // Setup error handling @@ -77,10 +75,16 @@ $whoops->register(); // ----------------------------------------------------------------------------- // Dependency Injection setup // ----------------------------------------------------------------------------- -require _dir(CONF_DIR, 'base_config.php'); // $base_config -require _dir(CONF_DIR, 'config.php'); // $config +require _dir($CONF_DIR, 'base_config.php'); // $base_config +require _dir($CONF_DIR, 'config.php'); // $config $config_array = array_merge($base_config, $config); -$di = require _dir(APP_DIR, 'bootstrap.php'); +$di = require _dir($APP_DIR, 'bootstrap.php'); + +// Unset 'constants' +unset($APP_DIR); +unset($SRC_DIR); +unset($CONF_DIR); + $container = $di($config_array); $container->set('error-handler', $defaultHandler); diff --git a/src/Aviat/AnimeClient/AnimeClient.php b/src/Aviat/AnimeClient/AnimeClient.php index 88636b28..bf1e6bb8 100644 --- a/src/Aviat/AnimeClient/AnimeClient.php +++ b/src/Aviat/AnimeClient/AnimeClient.php @@ -13,6 +13,9 @@ namespace Aviat\AnimeClient; +/** + * Odds and Ends class + */ class AnimeClient { use \Aviat\Ion\Di\ContainerAware; @@ -23,6 +26,7 @@ class AnimeClient { const DEFAULT_CONTROLLER_METHOD = 'index'; const NOT_FOUND_METHOD = 'not_found'; const ERROR_MESSAGE_METHOD = 'error_page'; + const SRC_DIR = __DIR__ . '/../../'; private static $form_pages = [ 'edit', @@ -57,6 +61,36 @@ class AnimeClient { return ($a !== $b) ? 'selected' : ''; } + /** + * Decode a json file into a php data structure + * + * @param string $file + * @param bool $as_array + * @return array|object + */ + public static function json_file_decode($file, $as_array=TRUE) + { + return json_decode( + file_get_contents($file), + $as_array + ); + } + + /** + * Encode json data and save to the selected file + * + * @param string $file + * @param mixed $data + * @return bool + */ + public static function json_file_encode($file, $data) + { + return file_put_contents( + $file, + json_encode($data) + ); + } + /** * Determine whether to show the sub-menu * diff --git a/src/Aviat/AnimeClient/Dispatcher.php b/src/Aviat/AnimeClient/Dispatcher.php index 607eaa1d..edcdcb43 100644 --- a/src/Aviat/AnimeClient/Dispatcher.php +++ b/src/Aviat/AnimeClient/Dispatcher.php @@ -203,7 +203,7 @@ class Dispatcher extends RoutingBase { $default_namespace = AnimeClient::DEFAULT_CONTROLLER_NAMESPACE; $path = str_replace('\\', '/', $default_namespace); $path = trim($path, '/'); - $actual_path = \_dir(SRC_DIR, $path); + $actual_path = \_dir(AnimeClient::SRC_DIR, $path); $class_files = glob("{$actual_path}/*.php"); @@ -242,10 +242,6 @@ class Dispatcher extends RoutingBase { { $controller_class = $controller_map[$route_type]; } - else - { - $controller_class = AnimeClient::DEFAULT_CONTROLLER; - } // Prepend the controller to the route parameters $route['controller'] = $controller_class; diff --git a/src/Aviat/AnimeClient/Model/Anime.php b/src/Aviat/AnimeClient/Model/Anime.php index 6ec3b7b7..b604881b 100644 --- a/src/Aviat/AnimeClient/Model/Anime.php +++ b/src/Aviat/AnimeClient/Model/Anime.php @@ -13,6 +13,7 @@ namespace Aviat\AnimeClient\Model; +use Aviat\AnimeClient\AnimeClient; use Aviat\AnimeClient\Hummingbird\Enum\AnimeWatchingStatus; use Aviat\AnimeClient\Hummingbird\Transformer\AnimeListTransformer; @@ -217,20 +218,20 @@ class Anime extends API { $transformed_cache_file = _dir($this->config->get('data_cache_path'), "anime-{$status}-transformed.json"); $cached = (file_exists($cache_file)) - ? json_decode(file_get_contents($cache_file), TRUE) + ? AnimeClient::json_file_decode($cache_file) : []; $api_data = json_decode($response->getBody(), TRUE); if ($api_data === $cached && file_exists($transformed_cache_file)) { - return json_decode(file_get_contents($transformed_cache_file), TRUE); + return AnimeClient::json_file_decode($transformed_cache_file); } else { - file_put_contents($cache_file, json_encode($api_data)); + AnimeClient::json_file_encode($cache_file, $api_data); $transformer = new AnimeListTransformer(); $transformed = $transformer->transform_collection($api_data); - file_put_contents($transformed_cache_file, json_encode($transformed)); + AnimeClient::json_file_encode($transformed_cache_file, $transformed); return $transformed; } } diff --git a/src/Aviat/AnimeClient/Model/AnimeCollection.php b/src/Aviat/AnimeClient/Model/AnimeCollection.php index befa7c65..22301d43 100644 --- a/src/Aviat/AnimeClient/Model/AnimeCollection.php +++ b/src/Aviat/AnimeClient/Model/AnimeCollection.php @@ -14,6 +14,7 @@ namespace Aviat\AnimeClient\Model; use Aviat\Ion\Di\ContainerInterface; +use Aviat\AnimeClient\AnimeClient; use Aviat\AnimeClient\Model\Anime as AnimeModel; /** @@ -305,7 +306,7 @@ class AnimeCollection extends DB { return; } - $anime = json_decode(file_get_contents("import.json")); + $anime = AnimeClient::json_file_decode("import.json"); foreach ($anime as $item) { diff --git a/src/Aviat/AnimeClient/Model/Manga.php b/src/Aviat/AnimeClient/Model/Manga.php index b1ce2dbf..5b768962 100644 --- a/src/Aviat/AnimeClient/Model/Manga.php +++ b/src/Aviat/AnimeClient/Model/Manga.php @@ -13,13 +13,14 @@ namespace Aviat\AnimeClient\Model; +use GuzzleHttp\Cookie\Cookiejar; +use GuzzleHttp\Cookie\SetCookie; + +use Aviat\AnimeClient\AnimeClient; use Aviat\AnimeClient\Model\API; use Aviat\AnimeClient\Hummingbird\Transformer; use Aviat\AnimeClient\Hummingbird\Enum\MangaReadingStatus; -use GuzzleHttp\Cookie\Cookiejar; -use GuzzleHttp\Cookie\SetCookie; - /** * Model for handling requests dealing with the manga list */ @@ -133,7 +134,9 @@ class Manga extends API { $data = $this->_check_cache($response); $output = $this->map_by_status($data); - return (array_key_exists($status, $output)) ? $output[$status] : $output; + return (array_key_exists($status, $output)) + ? $output[$status] + : $output; } /** @@ -153,25 +156,27 @@ class Manga extends API { } $cache_file = _dir($this->config->get('data_cache_path'), 'manga.json'); - $transformed_cache_file = _dir($this->config->get('data_cache_path'), 'manga-transformed.json'); - + $transformed_cache_file = _dir( + $this->config->get('data_cache_path'), + 'manga-transformed.json' + ); $cached_data = file_exists($cache_file) - ? json_decode(file_get_contents($cache_file), TRUE) + ? AnimeClient::json_file_decode($cache_file) : []; if ($cached_data === $api_data && file_exists($transformed_cache_file)) { - return json_decode(file_get_contents($transformed_cache_file), TRUE); + return AnimeClient::json_file_decode($transformed_cache_file); } else { - file_put_contents($cache_file, json_encode($api_data)); + AnimeClient::json_file_encode($cache_file, $api_data); $zippered_data = $this->zipper_lists($api_data); $transformer = new Transformer\MangaListTransformer(); $transformed_data = $transformer->transform_collection($zippered_data); - file_put_contents($transformed_cache_file, json_encode($transformed_data)); + AnimeClient::json_file_encode($transformed_cache_file, $transformed_data); return $transformed_data; } } @@ -194,7 +199,11 @@ class Manga extends API { foreach ($data as &$entry) { - $entry['manga']['image'] = $this->get_cached_image($entry['manga']['image'], $entry['manga']['slug'], 'manga'); + $entry['manga']['image'] = $this->get_cached_image( + $entry['manga']['image'], + $entry['manga']['slug'], + 'manga' + ); $key = $this->const_map[$entry['reading_status']]; $output[$key][] = $entry; } diff --git a/tests/AnimeClient/AnimeClientTest.php b/tests/AnimeClient/AnimeClientTest.php index 3ff1dd6c..ee649c85 100644 --- a/tests/AnimeClient/AnimeClientTest.php +++ b/tests/AnimeClient/AnimeClientTest.php @@ -72,4 +72,17 @@ class AnimeClientTest extends AnimeClient_TestCase { ]); $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/Hummingbird/Transformer/AnimeListTransformerTest.php b/tests/AnimeClient/Hummingbird/Transformer/AnimeListTransformerTest.php index 0ef7783b..46d78457 100644 --- a/tests/AnimeClient/Hummingbird/Transformer/AnimeListTransformerTest.php +++ b/tests/AnimeClient/Hummingbird/Transformer/AnimeListTransformerTest.php @@ -41,8 +41,8 @@ class AnimeListTransformerTest extends AnimeClient_TestCase { public function testTransform() { - $json = json_decode(file_get_contents($this->start_file), TRUE); - $expected = json_decode(file_get_contents($this->res_file), TRUE); + $json = json_file_decode($this->start_file); + $expected = json_file_decode($this->res_file); $actual = $this->transformer->transform_collection($json); //file_put_contents($this->res_file, json_encode($actual)); $this->assertEquals($expected, $actual); diff --git a/tests/AnimeClient/Hummingbird/Transformer/MangaListTransformerTest.php b/tests/AnimeClient/Hummingbird/Transformer/MangaListTransformerTest.php index b92a3d30..74d7aa05 100644 --- a/tests/AnimeClient/Hummingbird/Transformer/MangaListTransformerTest.php +++ b/tests/AnimeClient/Hummingbird/Transformer/MangaListTransformerTest.php @@ -15,10 +15,99 @@ class MangaListTransformerTest extends AnimeClient_TestCase { public function testTransform() { - $orig_json = json_decode(file_get_contents($this->start_file), TRUE); - $expected = json_decode(file_get_contents($this->res_file), TRUE); + $orig_json = json_file_decode($this->start_file); + $expected = json_file_decode($this->res_file); $actual = $this->transformer->transform_collection($orig_json); $this->assertEquals($expected, $actual); } + + public function dataUntransform() + { + return [ + 'same_rating' => [ + 'orig' => [ + 'id' => 401735, + 'manga_id' => "love-hina", + 'status' => "Plan to Read", + 'chapters_read' => 16, + 'volumes_read' => 2, + 'rereading' => true, + 'reread_count' => 1, + 'notes' => "Some text notes", + 'old_rating' => 7, + 'new_rating' => 7, + ], + 'expected' => [ + 'id' => 401735, + 'manga_id' => "love-hina", + 'status' => "Plan to Read", + 'chapters_read' => 16, + 'volumes_read' => 2, + 'rereading' => true, + 'reread_count' => 1, + 'notes' => "Some text notes", + ] + ], + 'update_rating' => [ + 'orig' => [ + 'id' => 401735, + 'manga_id' => "love-hina", + 'status' => "Plan to Read", + 'chapters_read' => 16, + 'volumes_read' => 2, + 'rereading' => true, + 'reread_count' => 1, + 'notes' => "Some text notes", + 'old_rating' => 7, + 'new_rating' => 8, + ], + 'expected' => [ + 'id' => 401735, + 'manga_id' => "love-hina", + 'status' => "Plan to Read", + 'chapters_read' => 16, + 'volumes_read' => 2, + 'rereading' => true, + 'reread_count' => 1, + 'notes' => "Some text notes", + 'rating' => 4, + ] + ], + 'remove_rating' => [ + 'orig' => [ + 'id' => 401735, + 'manga_id' => "love-hina", + 'status' => "Plan to Read", + 'chapters_read' => 16, + 'volumes_read' => 2, + 'rereading' => true, + 'reread_count' => 1, + 'notes' => "Some text notes", + 'old_rating' => 7, + 'new_rating' => 0, + ], + 'expected' => [ + 'id' => 401735, + 'manga_id' => "love-hina", + 'status' => "Plan to Read", + 'chapters_read' => 16, + 'volumes_read' => 2, + 'rereading' => true, + 'reread_count' => 1, + 'notes' => "Some text notes", + 'rating' => 3.5, + ] + ] + ]; + } + + /** + * @dataProvider dataUntransform + */ + public function testUntransform($orig, $expected) + { + $actual = $this->transformer->untransform($orig); + $this->assertEquals($expected, $actual); + } } \ No newline at end of file diff --git a/tests/AnimeClient/Hummingbird/Transformer/MangaListsZipperTest.php b/tests/AnimeClient/Hummingbird/Transformer/MangaListsZipperTest.php index f31072b7..99b0c4fc 100644 --- a/tests/AnimeClient/Hummingbird/Transformer/MangaListsZipperTest.php +++ b/tests/AnimeClient/Hummingbird/Transformer/MangaListsZipperTest.php @@ -12,13 +12,13 @@ class MangaListsZipperTest extends AnimeClient_TestCase { $this->start_file = __DIR__ . '/../../../test_data/manga_list/manga.json'; $this->res_file = __DIR__ . '/../../../test_data/manga_list/manga-zippered.json'; - $json = json_decode(file_get_contents($this->start_file), TRUE); + $json = json_file_decode($this->start_file); $this->mangaListsZipper = new MangaListsZipper($json); } public function testTransform() { - $zippered_json = json_decode(file_get_contents($this->res_file), TRUE); + $zippered_json = json_file_decode($this->res_file); $transformed = $this->mangaListsZipper->transform(); $this->assertEquals($zippered_json, $transformed); diff --git a/tests/AnimeClient/Model/MangaModelTest.php b/tests/AnimeClient/Model/MangaModelTest.php index 68675d6b..17e039fe 100644 --- a/tests/AnimeClient/Model/MangaModelTest.php +++ b/tests/AnimeClient/Model/MangaModelTest.php @@ -17,16 +17,16 @@ class MangaModelTest extends AnimeClient_TestCase { public function testZipperLists() { - $raw_data = json_decode(file_get_contents($this->mockDir . '/manga.json'), TRUE); - $expected = json_decode(file_get_contents($this->mockDir . '/manga-zippered.json'), TRUE); + $raw_data = json_file_decode($this->mockDir . '/manga.json'); + $expected = json_file_decode($this->mockDir . '/manga-zippered.json'); $this->assertEquals($expected, $this->model->zipper_lists($raw_data)); } public function testMapByStatus() { - $original = json_decode(file_get_contents($this->mockDir . '/manga-transformed.json'), TRUE); - $expected = json_decode(file_get_contents($this->mockDir . '/manga-mapped.json'), TRUE); + $original = json_file_decode($this->mockDir . '/manga-transformed.json'); + $expected = json_file_decode($this->mockDir . '/manga-mapped.json'); $actual = $this->model->map_by_status($original); $this->assertEquals($expected, $actual); @@ -43,7 +43,7 @@ class MangaModelTest extends AnimeClient_TestCase { $reflect = new ReflectionClass($this->model); $constants = $reflect->getConstants(); - $expected_all = json_decode(file_get_contents($this->mockDir . '/manga-mapped.json'), TRUE); + $expected_all = json_file_decode($this->mockDir . '/manga-mapped.json'); $this->assertEquals($expected_all, $this->model->_get_list_from_api()); @@ -62,7 +62,6 @@ $this->markTestSkipped(); $this->markTestSkipped(); } - $data = $this->model->get_all_lists(); $this->assertEquals($data['Reading'], $this->model->get_list('Reading')); } @@ -75,7 +74,7 @@ $this->markTestSkipped(); $this->markTestSkipped(); } - $data = json_decode(file_get_contents($this->mockDir . '/manga-mapped.json'), TRUE); + $data = json_file_decode($this->mockDir . '/manga-mapped.json'); foreach($data as &$val) { diff --git a/tests/AnimeClient/RoutingBaseTest.php b/tests/AnimeClient/RoutingBaseTest.php index b4636edd..dc44ad82 100644 --- a/tests/AnimeClient/RoutingBaseTest.php +++ b/tests/AnimeClient/RoutingBaseTest.php @@ -14,13 +14,13 @@ class RoutingBaseTest extends AnimeClient_TestCase { { return [ 'empty_segment' => [ - 'request_uri' => ' // ', + 'request_uri' => ' // ', 'path' => '/', 'segments' => ['', ''], 'last_segment' => NULL ], 'three_segments' => [ - 'request_uri' => '/anime/watching/list ', + 'request_uri' => '/anime/watching/list ', 'path' => '/anime/watching/list', 'segments' => ['', 'anime', 'watching', 'list'], 'last_segment' => 'list' @@ -33,7 +33,6 @@ class RoutingBaseTest extends AnimeClient_TestCase { */ public function testSegments($request_uri, $path, $segments, $last_segment) { -$this->markTestSkipped(); $this->setSuperGlobals([ '_SERVER' => [ 'REQUEST_URI' => $request_uri diff --git a/tests/AnimeClient_TestCase.php b/tests/AnimeClient_TestCase.php index ac802abb..a6a54c0c 100644 --- a/tests/AnimeClient_TestCase.php +++ b/tests/AnimeClient_TestCase.php @@ -12,6 +12,12 @@ use Aviat\AnimeClient\Config; * Base class for TestCases */ class AnimeClient_TestCase extends PHPUnit_Framework_TestCase { + // Test directory constants + const ROOT_DIR = __DIR__ . '/../'; + const SRC_DIR = __DIR__ . '/../src'; + const TEST_DATA_DIR = __DIR__ . '/test_data'; + const TEST_VIEW_DIR = __DIR__ . '/test_views'; + protected $container; protected static $staticContainer; protected static $session_handler; @@ -24,7 +30,7 @@ class AnimeClient_TestCase extends PHPUnit_Framework_TestCase { self::$session_handler = $session_handler; // Remove test cache files - $files = glob(_dir(TEST_DATA_DIR, 'cache', '*.json')); + $files = glob(_dir(self::TEST_DATA_DIR, 'cache', '*.json')); array_map('unlink', $files); } @@ -32,10 +38,13 @@ class AnimeClient_TestCase extends PHPUnit_Framework_TestCase { { parent::setUp(); + $ROOT_DIR = realpath(_dir(__DIR__, '/../')); + $APP_DIR = _dir($ROOT_DIR, 'app'); + $config_array = [ 'asset_path' => '//localhost/assets/', - 'img_cache_path' => _dir(ROOT_DIR, 'public/images'), - 'data_cache_path' => _dir(TEST_DATA_DIR, 'cache'), + 'img_cache_path' => _dir(self::ROOT_DIR, 'public/images'), + 'data_cache_path' => _dir(self::TEST_DATA_DIR, 'cache'), 'database' => [ 'collection' => [ 'type' => 'sqlite', @@ -59,7 +68,7 @@ class AnimeClient_TestCase extends PHPUnit_Framework_TestCase { ]; // Set up DI container - $di = require _dir(APP_DIR, 'bootstrap.php'); + $di = require _dir($APP_DIR, 'bootstrap.php'); $container = $di($config_array); $container->set('error-handler', new MockErrorHandler()); $container->set('session-handler', self::$session_handler); diff --git a/tests/Ion/View/HtmlViewTest.php b/tests/Ion/View/HtmlViewTest.php index 2702b70a..5599f313 100644 --- a/tests/Ion/View/HtmlViewTest.php +++ b/tests/Ion/View/HtmlViewTest.php @@ -14,7 +14,7 @@ class HtmlViewTest extends ViewTest { public function testRenderTemplate() { - $path = _dir(TEST_VIEW_DIR, 'test_view.php'); + $path = _dir(self::TEST_VIEW_DIR, 'test_view.php'); $expected = 'foo'; $actual = $this->view->render_template($path, [ 'var' => 'foo' diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d1090108..47b349f0 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,9 +2,11 @@ /** * Global setup for unit tests */ - + +use Aviat\AnimeClient\AnimeClient; + // ----------------------------------------------------------------------------- -// Autoloaders +// Global functions // ----------------------------------------------------------------------------- /** @@ -18,15 +20,26 @@ function _dir() return implode(DIRECTORY_SEPARATOR, func_get_args()); } +/** + * Decode a json file into a php data structure + * + * @param string $file + * @param bool $as_array + * @return array|object + */ +function json_file_decode($file, $as_array=TRUE) +{ + return AnimeClient::json_file_decode($file, $as_array); +} + +// ----------------------------------------------------------------------------- +// Autoloading +// ----------------------------------------------------------------------------- + +require _dir(__DIR__, 'AnimeClient_TestCase.php'); + // Define base path constants -define('ROOT_DIR', realpath(_dir(__DIR__, "/../"))); -define('APP_DIR', _dir(ROOT_DIR, 'app')); -define('CONF_DIR', _dir(APP_DIR, 'config')); -define('SRC_DIR', _dir(ROOT_DIR, 'src')); -define('BASE_DIR', _dir(SRC_DIR, 'Base')); -define('TEST_DATA_DIR', _dir(__DIR__, 'test_data')); -define('TEST_VIEW_DIR', _dir(__DIR__, 'test_views')); -require _dir(ROOT_DIR, '/vendor/autoload.php'); +require _dir(__DIR__, '../vendor/autoload.php'); /** * Set up autoloaders @@ -36,7 +49,7 @@ require _dir(ROOT_DIR, '/vendor/autoload.php'); */ spl_autoload_register(function ($class) { $class_parts = explode('\\', $class); - $ns_path = SRC_DIR . '/' . implode('/', $class_parts) . ".php"; + $ns_path = AnimeClient_TestCase::SRC_DIR . '/' . implode('/', $class_parts) . ".php"; if (file_exists($ns_path)) { @@ -44,7 +57,7 @@ spl_autoload_register(function ($class) { return; } }); - + // ----------------------------------------------------------------------------- // Ini Settings // ----------------------------------------------------------------------------- @@ -52,7 +65,7 @@ ini_set('session.use_cookies', 0); ini_set("session.use_only_cookies",0); ini_set("session.use_trans_sid",1); // Start session here to supress error about headers not sent -session_start(); +session_start(); // ----------------------------------------------------------------------------- // Load base test case and mocks @@ -65,6 +78,5 @@ $_COOKIE = []; // Request base test case and mocks require _dir(__DIR__, 'TestSessionHandler.php'); require _dir(__DIR__, 'mocks.php'); -require _dir(__DIR__, 'AnimeClient_TestCase.php'); // End of bootstrap.php \ No newline at end of file diff --git a/tests/mocks.php b/tests/mocks.php index 7ffe317b..cf45fe54 100644 --- a/tests/mocks.php +++ b/tests/mocks.php @@ -151,7 +151,7 @@ class TestMangaModel extends MangaModel { protected function _check_cache($response) { $file = __DIR__ . '/test_data/manga_list/manga-transformed.json'; - return json_decode(file_get_contents($file), TRUE); + return json_file_decode($file); } } // End of mocks.php \ No newline at end of file