mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
Squashed commit of the following:
commit 977863c2dd56d5c835f2a710cad7f7d3ba42da68 Merge: 5c37b419d3eb7ffed1
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Sun Dec 12 14:45:21 2021 +0100 Merge remote-tracking branch 'origin/master' into docgen/improve-normalizer commit 5c37b419ddf0b32b9950c33042396bba1860da84 Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Sun Dec 12 14:37:05 2021 +0100 fix normalization for user and type in null value commit 4469d46cdb19051fedec86bbb84e2351e6fcb72e Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Sun Dec 12 13:24:23 2021 +0100 add civility to person commit 6cf92fbbde8f4d9f2f4763ec4ee88216257040f7 Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Sun Dec 12 13:19:36 2021 +0100 fix person normalization: add a isNull on not null person commit ed6087ff8fd47b80ea5e9526756fe5d032d478e3 Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Sun Dec 12 13:08:46 2021 +0100 fix stan and cs issues commit 8429c334c33b3545835cbde034fccaa529c134a7 Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Sun Dec 12 13:08:36 2021 +0100 fix id type commit 39ae00d172a9f29320a97abb8518b2ea48d89d9b Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Sun Dec 12 12:52:41 2021 +0100 fix test and fix null or not-null value have same keys commit 312fcc44c07affa7aa60f6c5fce58f9d1c564cc3 Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Sat Dec 11 03:27:30 2021 +0100 improve normalization wip commit f91a29635827005fc58617dc1c9d210091372be5 Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Sat Dec 11 01:15:32 2021 +0100 improve normalization commit 56060e5e6a2191ef441039fdc91a01fb4653a553 Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Sat Dec 11 00:41:09 2021 +0100 handle changelog with translatable string commit9004686a13
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Fri Dec 10 01:10:55 2021 +0100 improve docgen wip commite266fa0e5d
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Thu Dec 9 21:50:56 2021 +0100 show errors from relatorio driver commit75ba56fa09
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Thu Dec 9 21:14:12 2021 +0100 add verification tool for admin commit12d6829b98
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Thu Dec 9 14:17:42 2021 +0100 fix type with phonenumber helper commit7b5e96771f
Merge:8a9024de1
8a4748dc2
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Thu Dec 9 14:11:14 2021 +0100 Merge remote-tracking branch 'origin/master' into docgen/improve-normalizer commit8a9024de13
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Thu Dec 9 13:51:36 2021 +0100 add docgen:normalization for relation commit24a404964b
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Thu Dec 9 12:44:41 2021 +0100 docgen normalization for relation commit5d24bd4d11
Merge:70ab23214
455b225f4
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 21:08:30 2021 +0100 Merge branch 'master' into docgen/improve-normalizer commit70ab232149
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 13:58:49 2021 +0100 improve docgen, trnslations, admin commit027c01fc58
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 12:23:24 2021 +0100 fix css block commitfdc5127c74
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 11:57:16 2021 +0100 fix some error in test (wip) commitb8d48f04ae
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 11:47:50 2021 +0100 fix tests (wip) commitf1b1771d6b
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 11:35:00 2021 +0100 fix tests (wip) commit62dabbe1e7
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 11:14:46 2021 +0100 fix code style commit4101392190
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 11:13:49 2021 +0100 fix tests and type hinting commit3f1bed0b1c
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 11:05:41 2021 +0100 fix tests (wip) commit79fbdcdee4
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 11:05:29 2021 +0100 type hint User class commit3d8d79323e
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 10:56:30 2021 +0100 remove error messages commit32178e22fe
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 10:51:30 2021 +0100 fix tests (wip) commit60a8c20896
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 10:29:54 2021 +0100 update app commit9d8011da61
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 10:06:35 2021 +0100 fix loading origin commit789eeadb40
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 09:52:51 2021 +0100 fix loading fixtures for doc generator template commitf206fdb08c
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 09:52:35 2021 +0100 fix code style commit9d5409d8d9
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 09:52:06 2021 +0100 fix casting commite297d82533
Author: Julien Fastré <julien.fastre@champs-libres.coop> Date: Wed Dec 8 09:26:13 2021 +0100 fixes on tests [WIP]
This commit is contained in:
@@ -32,6 +32,19 @@ final class AccompanyingPeriodDocGenNormalizerTest extends KernelTestCase
|
||||
$this->normalizer = self::$container->get(NormalizerInterface::class);
|
||||
}
|
||||
|
||||
public function testNormalizationNullOrNotNullHaveSameKeys()
|
||||
{
|
||||
$period = new AccompanyingPeriod();
|
||||
$notNullData = $this->normalizer->normalize($period, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]);
|
||||
$nullData = $this->normalizer->normalize(null, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]);
|
||||
|
||||
$this->assertEqualsCanonicalizing(
|
||||
array_keys($notNullData),
|
||||
array_keys($nullData),
|
||||
'test that the data returned by null value and an accompanying period have the same keys'
|
||||
);
|
||||
}
|
||||
|
||||
public function testNormalize()
|
||||
{
|
||||
$period = new AccompanyingPeriod();
|
||||
@@ -43,10 +56,14 @@ final class AccompanyingPeriodDocGenNormalizerTest extends KernelTestCase
|
||||
$period->addScope((new Scope())->setName(['fr' => 'scope2']));
|
||||
$period->addSocialIssue((new SocialIssue())->setTitle(['fr' => 'issue1']));
|
||||
$period->addSocialIssue((new SocialIssue())->setTitle(['fr' => 'issue2']));
|
||||
$period->addPerson(new Person());
|
||||
$period->addPerson(new Person());
|
||||
$data = $this->normalizer->normalize($period, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]);
|
||||
|
||||
$expected = [
|
||||
'id' => null,
|
||||
'type' => 'accompanying_period',
|
||||
'isNull' => false,
|
||||
'closingDate' => '@ignored',
|
||||
'confidential' => true,
|
||||
'confidentialText' => 'confidentiel',
|
||||
@@ -56,7 +73,9 @@ final class AccompanyingPeriodDocGenNormalizerTest extends KernelTestCase
|
||||
'emergencyText' => 'Urgent',
|
||||
'openingDate' => '@ignored',
|
||||
'originText' => 'origin',
|
||||
'origin' => '@ignored',
|
||||
'requestorAnonymous' => false,
|
||||
'resources' => [],
|
||||
'socialIssues' => '@ignored',
|
||||
'intensity' => 'ponctuel',
|
||||
'step' => 'Brouillon',
|
||||
@@ -66,10 +85,18 @@ final class AccompanyingPeriodDocGenNormalizerTest extends KernelTestCase
|
||||
'scopesText' => 'scope1, scope2',
|
||||
'ref' => '@ignored',
|
||||
'participations' => '@ignored',
|
||||
'currentParticipations' => '@ignored',
|
||||
'isClosed' => false,
|
||||
'hasRef' => false,
|
||||
'hasRequestor' => false,
|
||||
'requestorKind' => 'none',
|
||||
'hasRequestorPerson' => false,
|
||||
'hasRequestorThirdParty' => false,
|
||||
'requestorPerson' => '@ignored',
|
||||
'requestorThirdParty' => '@ignored',
|
||||
];
|
||||
|
||||
$this->assertIsArray($data);
|
||||
$this->markTestSkipped('still in specification');
|
||||
$this->assertEqualsCanonicalizing(array_keys($expected), array_keys($data));
|
||||
|
||||
foreach ($expected as $key => $item) {
|
||||
@@ -77,8 +104,10 @@ final class AccompanyingPeriodDocGenNormalizerTest extends KernelTestCase
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->assertEquals($item, $data[$key]);
|
||||
$this->assertEquals($item, $data[$key], "test key {$key}");
|
||||
}
|
||||
|
||||
$this->assertCount(2, $data['participations']);
|
||||
}
|
||||
|
||||
public function testNormalizeNull()
|
||||
@@ -87,16 +116,19 @@ final class AccompanyingPeriodDocGenNormalizerTest extends KernelTestCase
|
||||
|
||||
$expected = [
|
||||
'id' => '',
|
||||
'type' => 'accompanying_period',
|
||||
'closingDate' => '@ignored',
|
||||
'confidential' => '',
|
||||
'confidential' => false,
|
||||
'confidentialText' => '',
|
||||
'createdAt' => '@ignored',
|
||||
'createdBy' => '@ignored',
|
||||
'emergency' => '',
|
||||
'emergency' => false,
|
||||
'emergencyText' => '',
|
||||
'openingDate' => '@ignored',
|
||||
'originText' => '',
|
||||
'requestorAnonymous' => '',
|
||||
'origin' => '@ignored',
|
||||
'requestorAnonymous' => false,
|
||||
'resources' => [],
|
||||
'socialIssues' => '@ignored',
|
||||
'intensity' => '',
|
||||
'step' => '',
|
||||
@@ -106,10 +138,19 @@ final class AccompanyingPeriodDocGenNormalizerTest extends KernelTestCase
|
||||
'scopesText' => '',
|
||||
'ref' => '@ignored',
|
||||
'participations' => '@ignored',
|
||||
'currentParticipations' => '@ignored',
|
||||
'isClosed' => false,
|
||||
'hasRef' => false,
|
||||
'hasRequestor' => false,
|
||||
'requestorKind' => 'none',
|
||||
'hasRequestorPerson' => false,
|
||||
'hasRequestorThirdParty' => false,
|
||||
'requestorPerson' => '@ignored',
|
||||
'requestorThirdParty' => '@ignored',
|
||||
'isNull' => true,
|
||||
];
|
||||
|
||||
$this->assertIsArray($data);
|
||||
$this->markTestSkipped('still in specification');
|
||||
$this->assertEqualsCanonicalizing(array_keys($expected), array_keys($data));
|
||||
|
||||
foreach ($expected as $key => $item) {
|
||||
@@ -117,7 +158,7 @@ final class AccompanyingPeriodDocGenNormalizerTest extends KernelTestCase
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->assertEquals($item, $data[$key]);
|
||||
$this->assertEquals($item, $data[$key], "test the key {$key}");
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -11,9 +11,21 @@ declare(strict_types=1);
|
||||
|
||||
namespace Serializer\Normalizer;
|
||||
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
||||
use Chill\PersonBundle\Entity\Household\Household;
|
||||
use Chill\PersonBundle\Entity\Household\HouseholdMember;
|
||||
use Chill\PersonBundle\Entity\Household\Position;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Entity\Relationships\Relation;
|
||||
use Chill\PersonBundle\Entity\Relationships\Relationship;
|
||||
use Chill\PersonBundle\Repository\Relationships\RelationshipRepository;
|
||||
use Chill\PersonBundle\Serializer\Normalizer\PersonDocGenNormalizer;
|
||||
use Chill\PersonBundle\Templating\Entity\PersonRender;
|
||||
use Prophecy\Argument;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use function array_merge;
|
||||
|
||||
/**
|
||||
@@ -27,9 +39,13 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
|
||||
'lastname' => '',
|
||||
'altNames' => '',
|
||||
'text' => '',
|
||||
'isNull' => true,
|
||||
'type' => 'person',
|
||||
'birthdate' => ['short' => '', 'long' => ''],
|
||||
'deathdate' => ['short' => '', 'long' => ''],
|
||||
'gender' => '',
|
||||
'civility' => '@ignored',
|
||||
'address' => '@ignored',
|
||||
'maritalStatus' => '',
|
||||
'maritalStatusDate' => ['short' => '', 'long' => ''],
|
||||
'email' => '',
|
||||
@@ -69,6 +85,19 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
|
||||
yield [null, self::BLANK, 'normalization for a null person'];
|
||||
}
|
||||
|
||||
public function testNormalizationNullOrNotNullHaveSameKeys()
|
||||
{
|
||||
$period = new Person();
|
||||
$notNullData = $this->buildPersonNormalizer()->normalize($period, 'docgen', ['docgen:expects' => Person::class]);
|
||||
$nullData = $this->buildPersonNormalizer()->normalize(null, 'docgen', ['docgen:expects' => Person::class]);
|
||||
|
||||
$this->assertEqualsCanonicalizing(
|
||||
array_keys($notNullData),
|
||||
array_keys($nullData),
|
||||
'test that the data returned by null value and a Person have the same keys'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider generateData
|
||||
*
|
||||
@@ -77,8 +106,133 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
|
||||
*/
|
||||
public function testNormalize(?Person $person, $expected, $msg)
|
||||
{
|
||||
$normalized = $this->normalizer->normalize($person, 'docgen', ['docgen:expects' => Person::class]);
|
||||
$normalized = $this->normalizer->normalize($person, 'docgen', [
|
||||
'docgen:expects' => Person::class,
|
||||
'groups' => 'docgen:read',
|
||||
]);
|
||||
|
||||
$this->assertEquals($expected, $normalized, $msg);
|
||||
$this->assertIsArray($normalized);
|
||||
|
||||
foreach ($normalized as $key => $value) {
|
||||
if ('@ignored' === $value) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->assertEquals($value, $normalized[$key]);
|
||||
}
|
||||
$this->assertEqualsCanonicalizing(array_keys($expected), array_keys($normalized), $msg);
|
||||
}
|
||||
|
||||
public function testNormalizePersonWithHousehold()
|
||||
{
|
||||
$household = new Household();
|
||||
$person = new Person();
|
||||
$person
|
||||
->setFirstName('Renaud')
|
||||
->setLastName('Mégane');
|
||||
$householdMember = new HouseholdMember();
|
||||
$householdMember
|
||||
->setPosition((new Position())->setAllowHolder(true)->setLabel(['fr' => 'position'])
|
||||
->setShareHousehold(true))
|
||||
->setHolder(true);
|
||||
$person->addHouseholdParticipation($householdMember);
|
||||
$household->addMember($householdMember);
|
||||
|
||||
$person = new Person();
|
||||
$person
|
||||
->setFirstName('Citroen')
|
||||
->setLastName('Xsara');
|
||||
$householdMember = new HouseholdMember();
|
||||
$householdMember
|
||||
->setPosition((new Position())->setAllowHolder(true)->setLabel(['fr' => 'position2'])
|
||||
->setShareHousehold(true))
|
||||
->setHolder(false);
|
||||
$person->addHouseholdParticipation($householdMember);
|
||||
$household->addMember($householdMember);
|
||||
|
||||
$actual = $this->normalizer->normalize($person, 'docgen', [
|
||||
'groups' => 'docgen:read',
|
||||
'docgen:expects' => Person::class,
|
||||
'docgen:person:with-household' => true,
|
||||
]);
|
||||
|
||||
$this->assertCount(2, $household->getMembers());
|
||||
$this->assertIsArray($actual);
|
||||
$this->assertArrayHasKey('household', $actual);
|
||||
$this->assertCount(2, $actual['household']['currentMembers']);
|
||||
$this->assertCount(2, $actual['household']['members']);
|
||||
}
|
||||
|
||||
public function testNormalizePersonWithRelationships()
|
||||
{
|
||||
$person = (new Person())->setFirstName('Renaud')->setLastName('megane');
|
||||
$father = (new Person())->setFirstName('Clément')->setLastName('megane');
|
||||
$mother = (new Person())->setFirstName('Mireille')->setLastName('Mathieu');
|
||||
$sister = (new Person())->setFirstName('Callie')->setLastName('megane');
|
||||
|
||||
$relations = [
|
||||
(new Relationship())->setFromPerson($person)->setToPerson($father)
|
||||
->setReverse(false)->setRelation((new Relation())->setTitle(['fr' => 'Père'])
|
||||
->setReverseTitle(['fr' => 'Fils'])),
|
||||
(new Relationship())->setFromPerson($person)->setToPerson($mother)
|
||||
->setReverse(false)->setRelation((new Relation())->setTitle(['fr' => 'Mère'])
|
||||
->setReverseTitle(['fr' => 'Fils'])),
|
||||
(new Relationship())->setFromPerson($person)->setToPerson($sister)
|
||||
->setReverse(true)->setRelation((new Relation())->setTitle(['fr' => 'Frère'])
|
||||
->setReverseTitle(['fr' => 'Soeur'])),
|
||||
];
|
||||
|
||||
$repository = $this->prophesize(RelationshipRepository::class);
|
||||
$repository->findByPerson($person)->willReturn($relations);
|
||||
|
||||
$normalizer = $this->buildPersonNormalizer(null, $repository->reveal(), null, null);
|
||||
|
||||
$actual = $normalizer->normalize($person, 'docgen', [
|
||||
'groups' => 'docgen:read',
|
||||
'docgen:expects' => Person::class,
|
||||
'docgen:person:with-relations' => true,
|
||||
]);
|
||||
|
||||
$this->assertIsArray($actual);
|
||||
$this->assertArrayHasKey('relations', $actual);
|
||||
$this->assertCount(3, $actual['relations']);
|
||||
}
|
||||
|
||||
private function buildPersonNormalizer(
|
||||
?PersonRender $personRender = null,
|
||||
?RelationshipRepository $relationshipRepository = null,
|
||||
?TranslatorInterface $translator = null,
|
||||
?TranslatableStringHelper $translatableStringHelper = null
|
||||
): PersonDocGenNormalizer {
|
||||
$normalizer = new PersonDocGenNormalizer(
|
||||
$personRender ?? self::$container->get(PersonRender::class),
|
||||
$relationshipRepository ?? self::$container->get(RelationshipRepository::class),
|
||||
$translator ?? self::$container->get(TranslatorInterface::class),
|
||||
$translatableStringHelper ?? self::$container->get(TranslatableStringHelperInterface::class)
|
||||
);
|
||||
$normalizerManager = $this->prophesize(NormalizerInterface::class);
|
||||
$normalizerManager->supportsNormalization(Argument::any(), 'docgen', Argument::any())->willReturn(true);
|
||||
$normalizerManager->normalize(Argument::type(Person::class), 'docgen', Argument::any())
|
||||
->will(static function ($args) use ($normalizer) {
|
||||
return $normalizer->normalize($args[0], $args[1], $args[2]);
|
||||
});
|
||||
$normalizerManager->normalize(Argument::any(), 'docgen', Argument::any())->will(
|
||||
static function ($args) {
|
||||
if (is_iterable($args[0])) {
|
||||
$r = [];
|
||||
|
||||
foreach ($args[0] as $i) {
|
||||
$r[] = ['fake' => true, 'hash' => spl_object_hash($i)];
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
return ['fake' => true, 'hash' => null !== $args[0] ? spl_object_hash($args[0]) : null];
|
||||
}
|
||||
);
|
||||
$normalizer->setNormalizer($normalizerManager->reveal());
|
||||
|
||||
return $normalizer;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Serializer\Normalizer;
|
||||
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Entity\Relationships\Relation;
|
||||
use Chill\PersonBundle\Entity\Relationships\Relationship;
|
||||
use Chill\PersonBundle\Serializer\Normalizer\RelationshipDocGenNormalizer;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use function is_object;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
final class RelationshipDocGenNormalizerTest extends TestCase
|
||||
{
|
||||
public function testNormalizeRelationshipNull()
|
||||
{
|
||||
$relationship = null;
|
||||
|
||||
$normalizer = $this->buildNormalizer();
|
||||
|
||||
$this->assertTrue($normalizer->supportsNormalization($relationship, 'docgen', [
|
||||
'docgen:expects' => Relationship::class,
|
||||
]));
|
||||
$this->assertFalse($normalizer->supportsNormalization($relationship, 'docgen', [
|
||||
'docgen:expects' => Person::class,
|
||||
]));
|
||||
|
||||
$actual = $normalizer->normalize($relationship, 'docgen', [
|
||||
'docgen:expects' => Relationship::class,
|
||||
]);
|
||||
$this->assertIsArray($actual);
|
||||
$this->assertEqualsCanonicalizing(
|
||||
['fromPerson', 'toPerson', 'id', 'relationId', 'text', 'opposite'],
|
||||
array_keys($actual),
|
||||
'check that the expected keys are present'
|
||||
);
|
||||
}
|
||||
|
||||
public function testNormalizeRelationshipWithCounterPart()
|
||||
{
|
||||
$relationship = new Relationship();
|
||||
$relationship
|
||||
->setFromPerson($person1 = new Person())
|
||||
->setToPerson($person2 = new Person())
|
||||
->setRelation(
|
||||
(new Relation())->setTitle(['fr' => 'title'])
|
||||
->setReverseTitle(['fr' => 'reverse title'])
|
||||
)
|
||||
->setReverse(false);
|
||||
|
||||
$normalizer = $this->buildNormalizer();
|
||||
|
||||
$this->assertTrue($normalizer->supportsNormalization($relationship, 'docgen', []));
|
||||
$this->assertFalse($normalizer->supportsNormalization($person1, 'docgen', []));
|
||||
|
||||
$actual = $normalizer->normalize($relationship, 'docgen', [
|
||||
'docgen:expects' => Relationship::class,
|
||||
'docgen:relationship:counterpart' => $person1,
|
||||
]);
|
||||
|
||||
$this->assertIsArray($actual);
|
||||
$this->assertEqualsCanonicalizing(
|
||||
['fromPerson', 'toPerson', 'id', 'relationId', 'text', 'opposite'],
|
||||
array_keys($actual),
|
||||
'check that the expected keys are present'
|
||||
);
|
||||
$this->assertEquals(spl_object_hash($person2), $actual['opposite']['hash']);
|
||||
}
|
||||
|
||||
public function testNormalizeRelationshipWithoutCounterPart()
|
||||
{
|
||||
$relationship = new Relationship();
|
||||
$relationship
|
||||
->setFromPerson($person1 = new Person())
|
||||
->setToPerson($person2 = new Person())
|
||||
->setRelation(
|
||||
(new Relation())->setTitle(['fr' => 'title'])
|
||||
->setReverseTitle(['fr' => 'reverse title'])
|
||||
)
|
||||
->setReverse(false);
|
||||
|
||||
$normalizer = $this->buildNormalizer();
|
||||
|
||||
$this->assertTrue($normalizer->supportsNormalization($relationship, 'docgen', []));
|
||||
$this->assertFalse($normalizer->supportsNormalization($person1, 'docgen', []));
|
||||
|
||||
$actual = $normalizer->normalize($relationship, 'docgen', [
|
||||
'docgen:expects' => Relationship::class,
|
||||
]);
|
||||
$this->assertIsArray($actual);
|
||||
$this->assertEqualsCanonicalizing(
|
||||
['fromPerson', 'toPerson', 'id', 'relationId', 'text', 'opposite'],
|
||||
array_keys($actual),
|
||||
'check that the expected keys are present'
|
||||
);
|
||||
$this->assertEquals(null, $actual['opposite']);
|
||||
}
|
||||
|
||||
private function buildNormalizer(): RelationshipDocGenNormalizer
|
||||
{
|
||||
$translatableStringHelper = $this->prophesize(TranslatableStringHelperInterface::class);
|
||||
$translatableStringHelper->localize(Argument::type('array'))->will(
|
||||
static function ($args) { return $args[0][array_keys($args[0])[0]]; }
|
||||
);
|
||||
|
||||
$normalizer = new RelationshipDocGenNormalizer(
|
||||
$translatableStringHelper->reveal()
|
||||
);
|
||||
|
||||
$normalizerManager = $this->prophesize(NormalizerInterface::class);
|
||||
$normalizerManager->supportsNormalization(Argument::any(), 'docgen', Argument::any())->willReturn(true);
|
||||
$normalizerManager->normalize(Argument::type(Relationship::class), 'docgen', Argument::any())
|
||||
->will(static function ($args) use ($normalizer) {
|
||||
return $normalizer->normalize($args[0], $args[1], $args[2]);
|
||||
});
|
||||
$normalizerManager->normalize(Argument::any(), 'docgen', Argument::any())->will(
|
||||
static function ($args) {
|
||||
if (null === $args[0]) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (is_iterable($args[0])) {
|
||||
$r = [];
|
||||
|
||||
foreach ($args[0] as $i) {
|
||||
$r[] = ['fake' => true, 'hash' => spl_object_hash($i)];
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
if (is_object($args[0])) {
|
||||
return ['fake' => true, 'hash' => null !== $args[0] ? spl_object_hash($args[0]) : null];
|
||||
}
|
||||
|
||||
return $args[0];
|
||||
}
|
||||
);
|
||||
$normalizer->setNormalizer($normalizerManager->reveal());
|
||||
|
||||
return $normalizer;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user