HummingBirdAnimeClient/tests/Ion/Transformer/AbstractTransformerTest.php

154 lines
3.9 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
/**
2020-03-12 11:45:11 -04:00
* Hummingbird Anime List Client
*
2020-03-12 11:45:11 -04:00
* An API client for Kitsu to manage anime and manga watch lists
*
2021-02-04 11:57:01 -05:00
* PHP version 8
*
2022-03-04 15:50:35 -05:00
* @copyright 2015 - 2022 Timothy J. Warren <tim@timshome.page>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2020-12-10 17:06:50 -05:00
* @version 5.2
2022-03-04 15:50:35 -05:00
* @link https://git.timshome.page/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\Ion\Tests\Transformer;
2020-03-12 09:52:45 -04:00
use Aviat\Ion\Tests\IonTestCase;
use Aviat\Ion\Tests\{TestTransformer, TestTransformerUntransform};
2022-03-04 12:19:47 -05:00
use BadMethodCallException;
2022-03-04 12:19:47 -05:00
/**
* @internal
*/
final class AbstractTransformerTest extends IonTestCase
{
protected $transformer;
protected $untransformer;
2022-03-04 12:19:47 -05:00
protected function setUp(): void
{
$this->transformer = new TestTransformer();
$this->untransformer = new TestTransformerUntransform();
}
2023-05-09 12:46:52 -04:00
public static function dataTransformCollection()
{
return [
'object' => [
'original' => [
2022-03-04 12:19:47 -05:00
(object) [
['name' => 'Comedy'],
['name' => 'Romance'],
['name' => 'School'],
2022-03-04 12:19:47 -05:00
['name' => 'Harem'],
],
2022-03-04 12:19:47 -05:00
(object) [
['name' => 'Action'],
['name' => 'Comedy'],
['name' => 'Magic'],
['name' => 'Fantasy'],
2022-03-04 12:19:47 -05:00
['name' => 'Mahou Shoujo'],
],
2022-03-04 12:19:47 -05:00
(object) [
['name' => 'Comedy'],
2022-03-04 12:19:47 -05:00
['name' => 'Sci-Fi'],
],
],
'expected' => [
['Comedy', 'Romance', 'School', 'Harem'],
['Action', 'Comedy', 'Magic', 'Fantasy', 'Mahou Shoujo'],
2022-03-04 12:19:47 -05:00
['Comedy', 'Sci-Fi'],
],
],
'array' => [
'original' => [
[
['name' => 'Comedy'],
['name' => 'Romance'],
['name' => 'School'],
2022-03-04 12:19:47 -05:00
['name' => 'Harem'],
],
[
['name' => 'Action'],
['name' => 'Comedy'],
['name' => 'Magic'],
['name' => 'Fantasy'],
2022-03-04 12:19:47 -05:00
['name' => 'Mahou Shoujo'],
],
[
['name' => 'Comedy'],
2022-03-04 12:19:47 -05:00
['name' => 'Sci-Fi'],
],
],
'expected' => [
['Comedy', 'Romance', 'School', 'Harem'],
['Action', 'Comedy', 'Magic', 'Fantasy', 'Mahou Shoujo'],
2022-03-04 12:19:47 -05:00
['Comedy', 'Sci-Fi'],
],
],
];
}
2023-05-09 12:46:52 -04:00
public static function dataUnTransformCollection()
{
return [
'object' => [
'original' => [
2022-03-04 12:19:47 -05:00
(object) ['Comedy', 'Romance', 'School', 'Harem'],
(object) ['Action', 'Comedy', 'Magic', 'Fantasy', 'Mahou Shoujo'],
(object) ['Comedy', 'Sci-Fi'],
],
'expected' => [
['Comedy', 'Romance', 'School', 'Harem'],
['Action', 'Comedy', 'Magic', 'Fantasy', 'Mahou Shoujo'],
2022-03-04 12:19:47 -05:00
['Comedy', 'Sci-Fi'],
],
],
'array' => [
'original' => [
['Comedy', 'Romance', 'School', 'Harem'],
['Action', 'Comedy', 'Magic', 'Fantasy', 'Mahou Shoujo'],
2022-03-04 12:19:47 -05:00
['Comedy', 'Sci-Fi'],
],
'expected' => [
['Comedy', 'Romance', 'School', 'Harem'],
['Action', 'Comedy', 'Magic', 'Fantasy', 'Mahou Shoujo'],
2022-03-04 12:19:47 -05:00
['Comedy', 'Sci-Fi'],
],
],
];
}
public function testTransform()
{
$data = $this->dataTransformCollection();
$original = $data['object']['original'][0];
$expected = $data['object']['expected'][0];
$actual = $this->transformer->transform($original);
2022-03-04 12:19:47 -05:00
$this->assertSame($expected, $actual);
}
2023-05-09 12:46:52 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('dataTransformCollection')]
2023-05-19 10:56:23 -04:00
public function testTransformCollection(mixed $original, mixed $expected)
2023-05-09 12:46:52 -04:00
{
$actual = $this->transformer->transformCollection($original);
$this->assertSame($expected, $actual);
}
2023-05-09 12:46:52 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('dataUnTransformCollection')]
2023-05-19 10:56:23 -04:00
public function testUntransformCollection(mixed $original, mixed $expected)
2023-05-09 12:46:52 -04:00
{
$actual = $this->untransformer->untransformCollection($original);
$this->assertSame($expected, $actual);
}
2023-05-09 12:46:52 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('dataUnTransformCollection')]
2023-05-19 10:56:23 -04:00
public function testUntransformCollectionWithException(mixed $original, mixed $expected)
2023-05-09 12:46:52 -04:00
{
$this->expectException(BadMethodCallException::class);
$this->transformer->untransformCollection($original);
}
2022-03-04 12:19:47 -05:00
}