diff --git a/src/Aviat/AnimeClient/AnimeClient.php b/src/Aviat/AnimeClient/AnimeClient.php index bf1e6bb8..8b7a85d6 100644 --- a/src/Aviat/AnimeClient/AnimeClient.php +++ b/src/Aviat/AnimeClient/AnimeClient.php @@ -13,6 +13,8 @@ namespace Aviat\AnimeClient; +define('SRC_DIR', realpath(__DIR__ . '/../../')); + /** * Odds and Ends class */ @@ -26,7 +28,7 @@ class AnimeClient { const DEFAULT_CONTROLLER_METHOD = 'index'; const NOT_FOUND_METHOD = 'not_found'; const ERROR_MESSAGE_METHOD = 'error_page'; - const SRC_DIR = __DIR__ . '/../../'; + const SRC_DIR = SRC_DIR; private static $form_pages = [ 'edit', diff --git a/src/Aviat/AnimeClient/Dispatcher.php b/src/Aviat/AnimeClient/Dispatcher.php index edcdcb43..05117c6e 100644 --- a/src/Aviat/AnimeClient/Dispatcher.php +++ b/src/Aviat/AnimeClient/Dispatcher.php @@ -203,8 +203,7 @@ class Dispatcher extends RoutingBase { $default_namespace = AnimeClient::DEFAULT_CONTROLLER_NAMESPACE; $path = str_replace('\\', '/', $default_namespace); $path = trim($path, '/'); - $actual_path = \_dir(AnimeClient::SRC_DIR, $path); - + $actual_path = realpath(\_dir(AnimeClient::SRC_DIR, $path)); $class_files = glob("{$actual_path}/*.php"); $controllers = []; @@ -238,6 +237,10 @@ class Dispatcher extends RoutingBase { unset($route['path']); $controller_map = $this->get_controller_list(); + $controller_class = (array_key_exists($route_type, $controller_map)) + ? $controller_map[$route_type] + : AnimeClient::DEFAULT_CONTROLLER; + if (array_key_exists($route_type, $controller_map)) { $controller_class = $controller_map[$route_type]; diff --git a/tests/AnimeClient/DispatcherTest.php b/tests/AnimeClient/DispatcherTest.php index f0b2c0b7..564568df 100644 --- a/tests/AnimeClient/DispatcherTest.php +++ b/tests/AnimeClient/DispatcherTest.php @@ -128,8 +128,8 @@ class DispatcherTest extends AnimeClient_TestCase { ) ]; - $data['manga_default_routing_anime']['config']['routing']['default_list'] = 'manga'; - $data['manga_default_routing_manga']['config']['routing']['default_list'] = 'manga'; + $data['manga_default_routing_anime']['config']['routes']['route_config']['default_list'] = 'manga'; + $data['manga_default_routing_manga']['config']['routes']['route_config']['default_list'] = 'manga'; return $data; } diff --git a/tests/AnimeClient_TestCase.php b/tests/AnimeClient_TestCase.php index a6a54c0c..8c25ed08 100644 --- a/tests/AnimeClient_TestCase.php +++ b/tests/AnimeClient_TestCase.php @@ -6,17 +6,22 @@ use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; +use Aviat\AnimeClient\AnimeClient; use Aviat\AnimeClient\Config; +define('ROOT_DIR', __DIR__ . '/../'); +define('TEST_DATA_DIR', __DIR__ . '/test_data'); +define('TEST_VIEW_DIR', __DIR__ . '/test_views'); + /** * 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'; + const ROOT_DIR = ROOT_DIR; + const SRC_DIR = AnimeClient::SRC_DIR; + const TEST_DATA_DIR = TEST_DATA_DIR; + const TEST_VIEW_DIR = TEST_VIEW_DIR; protected $container; protected static $staticContainer; @@ -30,7 +35,7 @@ class AnimeClient_TestCase extends PHPUnit_Framework_TestCase { self::$session_handler = $session_handler; // Remove test cache files - $files = glob(_dir(self::TEST_DATA_DIR, 'cache', '*.json')); + $files = glob(_dir(TEST_DATA_DIR, 'cache', '*.json')); array_map('unlink', $files); } @@ -43,8 +48,8 @@ class AnimeClient_TestCase extends PHPUnit_Framework_TestCase { $config_array = [ 'asset_path' => '//localhost/assets/', - 'img_cache_path' => _dir(self::ROOT_DIR, 'public/images'), - 'data_cache_path' => _dir(self::TEST_DATA_DIR, 'cache'), + 'img_cache_path' => _dir(ROOT_DIR, 'public/images'), + 'data_cache_path' => _dir(TEST_DATA_DIR, 'cache'), 'database' => [ 'collection' => [ 'type' => 'sqlite', diff --git a/tests/Ion/JsonTest.php b/tests/Ion/JsonTest.php new file mode 100644 index 00000000..44cb4bbe --- /dev/null +++ b/tests/Ion/JsonTest.php @@ -0,0 +1,68 @@ + [1, 2, 3, 4] + ]; + $expected = '{"foo":[1,2,3,4]}'; + $this->assertEquals($expected, Json::encode($data)); + } + + public function dataEncodeDecode() + { + return [ + 'set1' => [ + 'data' => [ + 'apple' => [ + 'sauce' => ['foo','bar','baz'] + ] + ], + 'expected_size' => 39, + 'expected_json' => '{"apple":{"sauce":["foo","bar","baz"]}}' + ] + ]; + } + + /** + * @dataProvider dataEncodeDecode + */ + public function testEncodeDecodeFile($data, $expected_size, $expected_json) + { + $target_file = _dir(self::TEST_DATA_DIR, 'json_write.json'); + + $actual_size = Json::encodeFile($target_file, $data); + $actual_json = file_get_contents($target_file); + + $this->assertTrue(Json::isJson($actual_json)); + $this->assertEquals($expected_size, $actual_size); + $this->assertEquals($expected_json, $actual_json); + + $this->assertEquals($data, Json::decodeFile($target_file)); + + unlink($target_file); + } + + public function testDecode() + { + $json = '{"foo":[1,2,3,4]}'; + $expected = [ + 'foo' => [1, 2, 3, 4] + ]; + $this->assertEquals($expected, Json::decode($json)); + $this->assertEquals((object)$expected, Json::decode($json, false)); + + $badJson = '{foo:{1|2}}'; + $this->setExpectedException( + 'Aviat\Ion\JsonException', + 'JSON_ERROR_SYNTAX - Syntax error', + JSON_ERROR_SYNTAX + ); + Json::decode($badJson); + } +} \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 47b349f0..6a64201e 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -49,7 +49,7 @@ require _dir(__DIR__, '../vendor/autoload.php'); */ spl_autoload_register(function ($class) { $class_parts = explode('\\', $class); - $ns_path = AnimeClient_TestCase::SRC_DIR . '/' . implode('/', $class_parts) . ".php"; + $ns_path = realpath(__DIR__ . '/../src') . '/' . implode('/', $class_parts) . ".php"; if (file_exists($ns_path)) { diff --git a/tests/test_data/invalid_json.json b/tests/test_data/invalid_json.json new file mode 100644 index 00000000..8a3cefa6 --- /dev/null +++ b/tests/test_data/invalid_json.json @@ -0,0 +1 @@ +[}] \ No newline at end of file diff --git a/tests/test_data/valid_json.json b/tests/test_data/valid_json.json new file mode 100644 index 00000000..56b0789b --- /dev/null +++ b/tests/test_data/valid_json.json @@ -0,0 +1,5 @@ +[{ + "foo": { + "bar": [1,2,3] + } +}] \ No newline at end of file