mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
114 lines
3.4 KiB
PHP
114 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Chill\DocGeneratorBundle\Tests\Serializer\Encoder;
|
|
|
|
use Chill\DocGeneratorBundle\Serializer\Encoder\DocGenEncoder;
|
|
use Doctrine\ORM\EntityRepository;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
|
|
|
class DocGenEncoderTest extends TestCase
|
|
{
|
|
private DocGenEncoder $encoder;
|
|
|
|
protected function setUp()
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->encoder = new DocGenEncoder();
|
|
}
|
|
|
|
/**
|
|
* @dataProvider generateEncodeData
|
|
*/
|
|
public function testEncode($expected, $data, string $msg)
|
|
{
|
|
$generated = $this->encoder->encode($data, 'docgen');
|
|
$this->assertEquals($expected, $generated, $msg);
|
|
}
|
|
|
|
public function testEmbeddedLoopsThrowsException()
|
|
{
|
|
$this->expectException(UnexpectedValueException::class);
|
|
|
|
$data = [
|
|
'data' => [
|
|
['item' => 'one'],
|
|
[
|
|
'embedded' => [
|
|
[
|
|
['subitem' => 'two'],
|
|
['subitem' => 'three']
|
|
]
|
|
]
|
|
],
|
|
]
|
|
];
|
|
|
|
$this->encoder->encode($data, 'docgen');
|
|
}
|
|
|
|
public function generateEncodeData()
|
|
{
|
|
yield [ ['tests' => 'ok'], ['tests' => 'ok'], "A simple test with a simple array"];
|
|
|
|
yield [
|
|
// expected:
|
|
['item_subitem' => 'value'],
|
|
// data:
|
|
['item' => ['subitem' => 'value']],
|
|
"A test with multidimensional array"
|
|
];
|
|
|
|
yield [
|
|
// expected:
|
|
[ 'data' => [['item' => 'one'], ['item' => 'two']] ],
|
|
// data:
|
|
[ 'data' => [['item' => 'one'], ['item' => 'two']] ],
|
|
"a list of items"
|
|
];
|
|
|
|
yield [
|
|
// expected:
|
|
[ 'data' => [['item_subitem' => 'alpha'], ['item' => 'two']] ],
|
|
// data:
|
|
[ 'data' => [['item' => ['subitem' => 'alpha']], ['item' => 'two'] ] ],
|
|
"a list of items with multidimensional array inside item"
|
|
];
|
|
|
|
yield [
|
|
// expected:
|
|
[
|
|
'persons' => [
|
|
[
|
|
'firstname' => 'Jonathan',
|
|
'lastname' => 'Dupont',
|
|
'dateOfBirth_long' => '16 juin 1981',
|
|
'dateOfBirth_short' => '16/06/1981',
|
|
'father_firstname' => 'Marcel',
|
|
'father_lastname' => 'Dupont',
|
|
'father_dateOfBirth_long' => '10 novembre 1953',
|
|
'father_dateOfBirth_short' => '10/11/1953'
|
|
],
|
|
]
|
|
],
|
|
// data:
|
|
[
|
|
'persons' => [
|
|
[
|
|
'firstname' => 'Jonathan',
|
|
'lastname' => 'Dupont',
|
|
'dateOfBirth' => [ 'long' => '16 juin 1981', 'short' => '16/06/1981'],
|
|
'father' => [
|
|
'firstname' => 'Marcel',
|
|
'lastname' => 'Dupont',
|
|
'dateOfBirth' => ['long' => '10 novembre 1953', 'short' => '10/11/1953']
|
|
]
|
|
],
|
|
]
|
|
],
|
|
"a longer list, with near real data inside and embedded associative arrays"
|
|
];
|
|
}
|
|
}
|