From b444648a3d5299e41aed762063808f43d6210947 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Thu, 6 Apr 2017 11:59:53 -0400 Subject: [PATCH] Use snapshots library with tests to simplify testcases --- composer.json | 3 +- .../Transformer/AnimeListTransformerTest.php | 50 ++++--------------- ...eListTransformerTest__testTransform__1.php | 45 +++++++++++++++++ ...t__testUntransform with data set #0__1.php | 14 ++++++ ...t__testUntransform with data set #1__1.php | 14 ++++++ tests/AnimeClientTestCase.php | 4 ++ 6 files changed, 88 insertions(+), 42 deletions(-) create mode 100644 tests/API/Kitsu/Transformer/__snapshots__/AnimeListTransformerTest__testTransform__1.php create mode 100644 tests/API/Kitsu/Transformer/__snapshots__/AnimeListTransformerTest__testUntransform with data set #0__1.php create mode 100644 tests/API/Kitsu/Transformer/__snapshots__/AnimeListTransformerTest__testUntransform with data set #1__1.php diff --git a/composer.json b/composer.json index 26e7ae82..57ad8fa1 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,8 @@ "henrikbjorn/lurker": "^1.1.0", "symfony/var-dumper": "^3.2", "squizlabs/php_codesniffer": "^3.0.0@beta", - "phpstan/phpstan": "^0.6.4" + "phpstan/phpstan": "^0.6.4", + "spatie/phpunit-snapshot-assertions": "^0.4.1" }, "scripts": { "build": "vendor/bin/robo build", diff --git a/tests/API/Kitsu/Transformer/AnimeListTransformerTest.php b/tests/API/Kitsu/Transformer/AnimeListTransformerTest.php index aca91cb3..39c07430 100644 --- a/tests/API/Kitsu/Transformer/AnimeListTransformerTest.php +++ b/tests/API/Kitsu/Transformer/AnimeListTransformerTest.php @@ -22,33 +22,27 @@ use Aviat\Ion\Friend; use Aviat\Ion\Json; class AnimeListTransformerTest extends AnimeClientTestCase { - protected $dir; protected $beforeTransform; protected $afterTransform; protected $transformer; - + public function setUp() { parent::setUp(); $this->dir = AnimeClientTestCase::TEST_DATA_DIR . '/Kitsu'; - + $this->beforeTransform = Json::decodeFile("{$this->dir}/animeListItemBeforeTransform.json"); - $this->afterTransform = Json::decodeFile("{$this->dir}/animeListItemAfterTransform.json"); - + $this->transformer = new AnimeListTransformer(); } - + public function testTransform() { - $expected = $this->afterTransform; $actual = $this->transformer->transform($this->beforeTransform); - - // Json::encodeFile("{$this->dir}/animeListItemAfterTransform.json", $actual); - - $this->assertEquals($expected, $actual); + $this->assertMatchesSnapshot($actual); } - + public function dataUntransform() { return [[ @@ -60,19 +54,6 @@ class AnimeListTransformerTest extends AnimeClientTestCase { 'rewatched' => 0, 'notes' => 'Very formulaic.', 'edit' => true - ], - 'expected' => [ - 'id' => 14047981, - 'mal_id' => null, - 'data' => [ - 'status' => 'current', - 'rating' => 4, - 'reconsuming' => false, - 'reconsumeCount' => 0, - 'notes' => 'Very formulaic.', - 'progress' => 38, - 'private' => false - ] ] ], [ 'input' => [ @@ -86,29 +67,16 @@ class AnimeListTransformerTest extends AnimeClientTestCase { 'edit' => 'true', 'private' => 'On', 'rewatching' => 'On' - ], - 'expected' => [ - 'id' => 14047981, - 'mal_id' => '12345', - 'data' => [ - 'status' => 'current', - 'rating' => 4, - 'reconsuming' => true, - 'reconsumeCount' => 0, - 'notes' => 'Very formulaic.', - 'progress' => 38, - 'private' => true, - ] ] ]]; } - + /** * @dataProvider dataUntransform */ - public function testUntransform($input, $expected) + public function testUntransform($input) { $actual = $this->transformer->untransform($input); - $this->assertEquals($expected, $actual); + $this->assertMatchesSnapshot($actual); } } \ No newline at end of file diff --git a/tests/API/Kitsu/Transformer/__snapshots__/AnimeListTransformerTest__testTransform__1.php b/tests/API/Kitsu/Transformer/__snapshots__/AnimeListTransformerTest__testTransform__1.php new file mode 100644 index 00000000..6796b0a5 --- /dev/null +++ b/tests/API/Kitsu/Transformer/__snapshots__/AnimeListTransformerTest__testTransform__1.php @@ -0,0 +1,45 @@ + '15839442', + 'mal_id' => '33206', + 'episodes' => + array ( + 'watched' => 0, + 'total' => '-', + 'length' => NULL, + ), + 'airing' => + array ( + 'status' => 'Currently Airing', + 'started' => '2017-01-12', + 'ended' => NULL, + ), + 'anime' => + array ( + 'age_rating' => NULL, + 'title' => 'Kobayashi-san Chi no Maid Dragon', + 'titles' => + array ( + 0 => 'Kobayashi-san Chi no Maid Dragon', + 1 => 'Miss Kobayashi\'s Dragon Maid', + 2 => '小林さんちのメイドラゴン', + ), + 'slug' => 'kobayashi-san-chi-no-maid-dragon', + 'type' => 'TV', + 'image' => 'https://media.kitsu.io/anime/poster_images/12243/small.jpg?1481144116', + 'genres' => + array ( + 0 => 'Comedy', + 1 => 'Fantasy', + 2 => 'Slice of Life', + ), + 'streaming_links' => + array ( + ), + ), + 'watching_status' => 'current', + 'notes' => NULL, + 'rewatching' => false, + 'rewatched' => 0, + 'user_rating' => '-', + 'private' => false, +); diff --git a/tests/API/Kitsu/Transformer/__snapshots__/AnimeListTransformerTest__testUntransform with data set #0__1.php b/tests/API/Kitsu/Transformer/__snapshots__/AnimeListTransformerTest__testUntransform with data set #0__1.php new file mode 100644 index 00000000..2db06bc8 --- /dev/null +++ b/tests/API/Kitsu/Transformer/__snapshots__/AnimeListTransformerTest__testUntransform with data set #0__1.php @@ -0,0 +1,14 @@ + 14047981, + 'mal_id' => NULL, + 'data' => + array ( + 'status' => 'current', + 'reconsuming' => false, + 'reconsumeCount' => 0, + 'notes' => 'Very formulaic.', + 'progress' => 38, + 'private' => false, + 'rating' => 4, + ), +); diff --git a/tests/API/Kitsu/Transformer/__snapshots__/AnimeListTransformerTest__testUntransform with data set #1__1.php b/tests/API/Kitsu/Transformer/__snapshots__/AnimeListTransformerTest__testUntransform with data set #1__1.php new file mode 100644 index 00000000..de278585 --- /dev/null +++ b/tests/API/Kitsu/Transformer/__snapshots__/AnimeListTransformerTest__testUntransform with data set #1__1.php @@ -0,0 +1,14 @@ + 14047981, + 'mal_id' => '12345', + 'data' => + array ( + 'status' => 'current', + 'reconsuming' => true, + 'reconsumeCount' => 0, + 'notes' => 'Very formulaic.', + 'progress' => 38, + 'private' => true, + 'rating' => 4, + ), +); diff --git a/tests/AnimeClientTestCase.php b/tests/AnimeClientTestCase.php index f531273f..7c4e0804 100644 --- a/tests/AnimeClientTestCase.php +++ b/tests/AnimeClientTestCase.php @@ -23,6 +23,7 @@ use function Aviat\Ion\_dir; use Aura\Web\WebFactory; use Aviat\Ion\Json; use PHPUnit\Framework\TestCase; +use Spatie\Snapshots\MatchesSnapshots; use Zend\Diactoros\{ Response as HttpResponse, ServerRequestFactory @@ -36,6 +37,9 @@ define('TEST_VIEW_DIR', __DIR__ . '/test_views'); * Base class for TestCases */ class AnimeClientTestCase extends TestCase { + use MatchesSnapshots; + + // Test directory constants const ROOT_DIR = ROOT_DIR; const SRC_DIR = SRC_DIR;