Remove unnamespaced constants, and improve some tests

This commit is contained in:
Timothy Warren 2016-01-06 15:44:40 -05:00
parent 3c124456d0
commit 38faaebb5f
17 changed files with 245 additions and 76 deletions

View File

@ -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',
];

View File

@ -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);

View File

@ -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
*

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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)
{

View File

@ -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;
}

View File

@ -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());
}
}

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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)
{

View File

@ -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

View File

@ -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);

View File

@ -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 = '<tag>foo</tag>';
$actual = $this->view->render_template($path, [
'var' => 'foo'

View File

@ -3,8 +3,10 @@
* 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))
{
@ -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

View File

@ -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