From 275b0eea40c44c6249c99ed77fc59f568de2d8ff Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Thu, 7 Jan 2016 13:45:43 -0500 Subject: [PATCH] Update codebase to use new Json class --- src/Aviat/AnimeClient/AnimeClient.php | 30 ------------------- src/Aviat/AnimeClient/Model/Anime.php | 18 +++++------ .../AnimeClient/Model/AnimeCollection.php | 3 +- src/Aviat/AnimeClient/Model/Manga.php | 14 ++++----- src/Aviat/Ion/Json.php | 7 +++-- src/Aviat/Ion/View/JsonView.php | 3 +- .../Transformer/AnimeListTransformerTest.php | 5 ++-- .../Transformer/MangaListTransformerTest.php | 5 ++-- .../Transformer/MangaListsZipperTest.php | 5 ++-- tests/AnimeClient/Model/MangaModelTest.php | 13 ++++---- tests/Ion/ViewTest.php | 4 +++ tests/bootstrap.php | 13 +------- tests/mocks.php | 3 +- 13 files changed, 48 insertions(+), 75 deletions(-) diff --git a/src/Aviat/AnimeClient/AnimeClient.php b/src/Aviat/AnimeClient/AnimeClient.php index 8b7a85d6..940ed517 100644 --- a/src/Aviat/AnimeClient/AnimeClient.php +++ b/src/Aviat/AnimeClient/AnimeClient.php @@ -63,36 +63,6 @@ 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/Model/Anime.php b/src/Aviat/AnimeClient/Model/Anime.php index b604881b..58685326 100644 --- a/src/Aviat/AnimeClient/Model/Anime.php +++ b/src/Aviat/AnimeClient/Model/Anime.php @@ -13,7 +13,7 @@ namespace Aviat\AnimeClient\Model; -use Aviat\AnimeClient\AnimeClient; +use Aviat\Ion\Json; use Aviat\AnimeClient\Hummingbird\Enum\AnimeWatchingStatus; use Aviat\AnimeClient\Hummingbird\Transformer\AnimeListTransformer; @@ -70,7 +70,7 @@ class Anime extends API { return [ 'statusCode' => $response->getStatusCode(), - 'body' => json_decode($response->getBody(), TRUE) + 'body' => Json::decode($response->getBody(), TRUE) ]; } @@ -138,7 +138,7 @@ class Anime extends API { $response = $this->client->get("anime/{$anime_id}", $config); - return json_decode($response->getBody(), TRUE); + return Json::decode($response->getBody(), TRUE); } /** @@ -165,7 +165,7 @@ class Anime extends API { throw new RuntimeException($response->getEffectiveUrl()); } - return json_decode($response->getBody(), TRUE); + return Json::decode($response->getBody(), TRUE); } /** @@ -218,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)) - ? AnimeClient::json_file_decode($cache_file) + ? Json::decodeFile($cache_file) : []; - $api_data = json_decode($response->getBody(), TRUE); + $api_data = Json::decode($response->getBody(), TRUE); if ($api_data === $cached && file_exists($transformed_cache_file)) { - return AnimeClient::json_file_decode($transformed_cache_file); + return Json::decodeFile($transformed_cache_file); } else { - AnimeClient::json_file_encode($cache_file, $api_data); + Json::encodeFile($cache_file, $api_data); $transformer = new AnimeListTransformer(); $transformed = $transformer->transform_collection($api_data); - AnimeClient::json_file_encode($transformed_cache_file, $transformed); + Json::encodeFile($transformed_cache_file, $transformed); return $transformed; } } diff --git a/src/Aviat/AnimeClient/Model/AnimeCollection.php b/src/Aviat/AnimeClient/Model/AnimeCollection.php index 22301d43..ea386764 100644 --- a/src/Aviat/AnimeClient/Model/AnimeCollection.php +++ b/src/Aviat/AnimeClient/Model/AnimeCollection.php @@ -13,6 +13,7 @@ namespace Aviat\AnimeClient\Model; +use Aviat\Ion\Json; use Aviat\Ion\Di\ContainerInterface; use Aviat\AnimeClient\AnimeClient; use Aviat\AnimeClient\Model\Anime as AnimeModel; @@ -306,7 +307,7 @@ class AnimeCollection extends DB { return; } - $anime = AnimeClient::json_file_decode("import.json"); + $anime = Json::decodeFile("import.json"); foreach ($anime as $item) { diff --git a/src/Aviat/AnimeClient/Model/Manga.php b/src/Aviat/AnimeClient/Model/Manga.php index 5b768962..a34475c1 100644 --- a/src/Aviat/AnimeClient/Model/Manga.php +++ b/src/Aviat/AnimeClient/Model/Manga.php @@ -16,7 +16,7 @@ namespace Aviat\AnimeClient\Model; use GuzzleHttp\Cookie\Cookiejar; use GuzzleHttp\Cookie\SetCookie; -use Aviat\AnimeClient\AnimeClient; +use Aviat\Ion\Json; use Aviat\AnimeClient\Model\API; use Aviat\AnimeClient\Hummingbird\Transformer; use Aviat\AnimeClient\Hummingbird\Enum\MangaReadingStatus; @@ -80,7 +80,7 @@ class Manga extends API { return [ 'statusCode' => $result->getStatusCode(), - 'body' => json_decode($result->getBody(), TRUE) + 'body' => Json::decode($result->getBody(), TRUE) ]; } @@ -149,7 +149,7 @@ class Manga extends API { private function _check_cache($response) { // Bail out early if there isn't any manga data - $api_data = json_decode($response->getBody(), TRUE); + $api_data = Json::decode($response->getBody(), TRUE); if ( ! array_key_exists('manga', $api_data)) { return []; @@ -162,21 +162,21 @@ class Manga extends API { ); $cached_data = file_exists($cache_file) - ? AnimeClient::json_file_decode($cache_file) + ? Json::decodeFile($cache_file) : []; if ($cached_data === $api_data && file_exists($transformed_cache_file)) { - return AnimeClient::json_file_decode($transformed_cache_file); + return Json::decodeFile($transformed_cache_file); } else { - AnimeClient::json_file_encode($cache_file, $api_data); + Json::encodeFile($cache_file, $api_data); $zippered_data = $this->zipper_lists($api_data); $transformer = new Transformer\MangaListTransformer(); $transformed_data = $transformer->transform_collection($zippered_data); - AnimeClient::json_file_encode($transformed_cache_file, $transformed_data); + Json::encodeFile($transformed_cache_file, $transformed_data); return $transformed_data; } } diff --git a/src/Aviat/Ion/Json.php b/src/Aviat/Ion/Json.php index 831f941e..a74d9552 100644 --- a/src/Aviat/Ion/Json.php +++ b/src/Aviat/Ion/Json.php @@ -69,12 +69,15 @@ class Json { * Decode json data loaded from the passed filename * * @param string $filename + * @param bool $assoc + * @param int $depth + * @param int $options * @return mixed */ - public static function decodeFile($filename) + public static function decodeFile($filename, $assoc = TRUE, $depth = 512, $options = 0) { $json = file_get_contents($filename); - return self::decode($json); + return self::decode($json, $assoc, $depth, $options); } /** diff --git a/src/Aviat/Ion/View/JsonView.php b/src/Aviat/Ion/View/JsonView.php index ea2737b1..a65dd558 100644 --- a/src/Aviat/Ion/View/JsonView.php +++ b/src/Aviat/Ion/View/JsonView.php @@ -12,6 +12,7 @@ namespace Aviat\Ion\View; +use Aviat\Ion\Json; use Aviat\Ion\View\HttpView; use Aviat\Ion\View as BaseView; @@ -37,7 +38,7 @@ class JsonView extends HttpView { { if ( ! is_string($string)) { - $string = json_encode($string); + $string = Json::encode($string); } return parent::setOutput($string); diff --git a/tests/AnimeClient/Hummingbird/Transformer/AnimeListTransformerTest.php b/tests/AnimeClient/Hummingbird/Transformer/AnimeListTransformerTest.php index 46d78457..26e98ecc 100644 --- a/tests/AnimeClient/Hummingbird/Transformer/AnimeListTransformerTest.php +++ b/tests/AnimeClient/Hummingbird/Transformer/AnimeListTransformerTest.php @@ -1,6 +1,7 @@ start_file); - $expected = json_file_decode($this->res_file); + $json = Json::decodeFile($this->start_file); + $expected = Json::decodeFile($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 74d7aa05..65c4d244 100644 --- a/tests/AnimeClient/Hummingbird/Transformer/MangaListTransformerTest.php +++ b/tests/AnimeClient/Hummingbird/Transformer/MangaListTransformerTest.php @@ -1,5 +1,6 @@ start_file); - $expected = json_file_decode($this->res_file); + $orig_json = Json::decodeFile($this->start_file); + $expected = Json::decodeFile($this->res_file); $actual = $this->transformer->transform_collection($orig_json); $this->assertEquals($expected, $actual); diff --git a/tests/AnimeClient/Hummingbird/Transformer/MangaListsZipperTest.php b/tests/AnimeClient/Hummingbird/Transformer/MangaListsZipperTest.php index 99b0c4fc..eb105e91 100644 --- a/tests/AnimeClient/Hummingbird/Transformer/MangaListsZipperTest.php +++ b/tests/AnimeClient/Hummingbird/Transformer/MangaListsZipperTest.php @@ -1,5 +1,6 @@ start_file = __DIR__ . '/../../../test_data/manga_list/manga.json'; $this->res_file = __DIR__ . '/../../../test_data/manga_list/manga-zippered.json'; - $json = json_file_decode($this->start_file); + $json = Json::decodeFile($this->start_file); $this->mangaListsZipper = new MangaListsZipper($json); } public function testTransform() { - $zippered_json = json_file_decode($this->res_file); + $zippered_json = Json::decodeFile($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 17e039fe..fa0d93f8 100644 --- a/tests/AnimeClient/Model/MangaModelTest.php +++ b/tests/AnimeClient/Model/MangaModelTest.php @@ -2,6 +2,7 @@ use GuzzleHttp\Psr7\Response; use Aviat\Ion\Friend; +use Aviat\Ion\Json; use Aviat\Ion\Di\ContainerInterface; use Aviat\AnimeClient\Model\Manga as MangaModel; use Aviat\AnimeClient\Hummingbird\Enum\MangaReadingStatus; @@ -17,16 +18,16 @@ class MangaModelTest extends AnimeClient_TestCase { public function testZipperLists() { - $raw_data = json_file_decode($this->mockDir . '/manga.json'); - $expected = json_file_decode($this->mockDir . '/manga-zippered.json'); + $raw_data = Json::decodeFile($this->mockDir . '/manga.json'); + $expected = Json::decodeFile($this->mockDir . '/manga-zippered.json'); $this->assertEquals($expected, $this->model->zipper_lists($raw_data)); } public function testMapByStatus() { - $original = json_file_decode($this->mockDir . '/manga-transformed.json'); - $expected = json_file_decode($this->mockDir . '/manga-mapped.json'); + $original = Json::decodeFile($this->mockDir . '/manga-transformed.json'); + $expected = Json::decodeFile($this->mockDir . '/manga-mapped.json'); $actual = $this->model->map_by_status($original); $this->assertEquals($expected, $actual); @@ -43,7 +44,7 @@ class MangaModelTest extends AnimeClient_TestCase { $reflect = new ReflectionClass($this->model); $constants = $reflect->getConstants(); - $expected_all = json_file_decode($this->mockDir . '/manga-mapped.json'); + $expected_all = Json::decodeFile($this->mockDir . '/manga-mapped.json'); $this->assertEquals($expected_all, $this->model->_get_list_from_api()); @@ -74,7 +75,7 @@ $this->markTestSkipped(); $this->markTestSkipped(); } - $data = json_file_decode($this->mockDir . '/manga-mapped.json'); + $data = Json::decodeFile($this->mockDir . '/manga-mapped.json'); foreach($data as &$val) { diff --git a/tests/Ion/ViewTest.php b/tests/Ion/ViewTest.php index de4268c5..e51c49c4 100644 --- a/tests/Ion/ViewTest.php +++ b/tests/Ion/ViewTest.php @@ -17,6 +17,10 @@ class ViewTest extends AnimeClient_TestCase { { $this->friend->output = 'foo'; $this->assertEquals($this->friend->output, $this->friend->getOutput()); + $this->assertFalse($this->friend->hasRendered); + + $this->assertEquals($this->friend->getOutput(), $this->friend->__toString()); + $this->assertTrue($this->friend->hasRendered); } public function testSetOutput() diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 6a64201e..b988e8c1 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -3,6 +3,7 @@ * Global setup for unit tests */ +use Aviat\Ion\Json; use Aviat\AnimeClient\AnimeClient; // ----------------------------------------------------------------------------- @@ -20,18 +21,6 @@ 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 // ----------------------------------------------------------------------------- diff --git a/tests/mocks.php b/tests/mocks.php index cf45fe54..d9dffd4c 100644 --- a/tests/mocks.php +++ b/tests/mocks.php @@ -5,6 +5,7 @@ use Aviat\Ion\Enum; use Aviat\Ion\Friend; +use Aviat\Ion\Json; use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\Transformer\AbstractTransformer; use Aviat\Ion\View; @@ -151,7 +152,7 @@ class TestMangaModel extends MangaModel { protected function _check_cache($response) { $file = __DIR__ . '/test_data/manga_list/manga-transformed.json'; - return json_file_decode($file); + return Json::decodeFile($file); } } // End of mocks.php \ No newline at end of file