HummingBirdAnimeClient/tests/Ion/Transformer/AbstractTransformerTest.php

164 lines
4.0 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
/**
* @param mixed $original
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataTransformCollection')]
public function testTransformCollection($original, mixed $expected)
{
$actual = $this->transformer->transformCollection($original);
$this->assertSame($expected, $actual);
}
2023-05-09 12:46:52 -04:00
/**
* @param mixed $original
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataUnTransformCollection')]
public function testUntransformCollection($original, mixed $expected)
{
$actual = $this->untransformer->untransformCollection($original);
$this->assertSame($expected, $actual);
}
2023-05-09 12:46:52 -04:00
/**
* @param mixed $original
* @param mixed $expected
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataUnTransformCollection')]
public function testUntransformCollectionWithException($original, $expected)
{
$this->expectException(BadMethodCallException::class);
$this->transformer->untransformCollection($original);
}
2022-03-04 12:19:47 -05:00
}