Merge branch 'master' into 111_exports_suite

This commit is contained in:
2022-10-24 11:06:12 +02:00
18 changed files with 595 additions and 17 deletions

View File

@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Serializer\Normalizer;
use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* @internal
* @coversNothing
*/
final class AccompanyingPeriodResourceNormalizerTest extends KernelTestCase
{
private NormalizerInterface $normalizer;
protected function setUp(): void
{
self::bootKernel();
$this->normalizer = self::$container->get(NormalizerInterface::class);
}
public function testNormalizeNullHasSameValueAsNotNull()
{
$nullResource = $this->normalizer->normalize(null, 'docgen', ['groups' => 'docgen:read', 'docgen:expects' => Resource::class]);
$notNull = $this->normalizer->normalize(new Resource(), 'docgen', ['groups' => 'docgen:read', 'docgen:expects' => Resource::class]);
$this->assertEqualsCanonicalizing(array_keys($notNull), array_keys($nullResource));
}
public function testNormalizeResource()
{
$resource = new Resource();
$resource
->setComment('blabla')
->setResource(new ThirdParty());
$expected = [
'type' => 'accompanying_period_resource',
'isNull' => false,
'comment' => 'blabla',
];
$actual = $this->normalizer->normalize($resource, 'docgen', ['groups' => 'docgen:read', 'docgen:expects' => Resource::class]);
// we do not test for sub array (person, thirdparty). We then check first for base value...
foreach ($expected as $key => $value) {
$this->assertArrayHasKey($key, $actual);
$this->assertEquals($value, $actual[$key]);
}
// ... and then for the existence of some values
$this->assertArrayHasKey('person', $actual);
$this->assertArrayHasKey('thirdParty', $actual);
}
}

View File

@@ -21,6 +21,7 @@ use Chill\DocStoreBundle\Security\Authorization\PersonDocumentVoter;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\Type\ScopePickerType;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
@@ -33,6 +34,8 @@ use Prophecy\Exception\Prediction\FailedPredictionException;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
@@ -82,7 +85,7 @@ final class PersonContextTest extends TestCase
$parameter
);
$this->assertFalse($personContext->hasPublicForm($docGen, $person));
$personContext->buildPublicForm($this->buildFormBuilder(false), $docGen, $person);
$personContext->storeGenerated(
$docGen,
@@ -126,7 +129,7 @@ final class PersonContextTest extends TestCase
$em->reveal(),
);
$this->assertTrue($personContext->hasPublicForm($docGen, $person));
$personContext->buildPublicForm($this->buildFormBuilder(true), $docGen, $person);
$personContext->storeGenerated(
$docGen,
@@ -170,7 +173,7 @@ final class PersonContextTest extends TestCase
$em->reveal(),
);
$this->assertTrue($personContext->hasPublicForm($docGen, $person));
$personContext->buildPublicForm($this->buildFormBuilder(true), $docGen, $person);
$personContext->storeGenerated(
$docGen,
@@ -180,6 +183,24 @@ final class PersonContextTest extends TestCase
);
}
private function buildFormBuilder(bool $withScope): FormBuilderInterface
{
$builder = $this->prophesize(FormBuilderInterface::class);
$builder->add('title', TextType::class, Argument::type('array'))
->shouldBeCalled(1);
if ($withScope) {
$builder->add('scope', ScopePickerType::class, Argument::type('array'))
->shouldBeCalled();
} else {
$builder->add('scope', ScopePickerType::class, Argument::type('array'))
->shouldNotBeCalled();
}
return $builder->reveal();
}
private function buildPersonContext(
?AuthorizationHelperInterface $authorizationHelper = null,
?BaseContextData $baseContextData = null,

View File

@@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Service\DocGenerator;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Service\DocGenerator\PersonContextInterface;
use Chill\PersonBundle\Service\DocGenerator\PersonContextWithThirdParty;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* @internal
* @coversNothing
*/
final class PersonContextWithThirdPartyTest extends KernelTestCase
{
use ProphecyTrait;
public function testAdminFormReverseTransform()
{
$personContext = $this->buildPersonContextWithThirdParty();
$actual = $personContext->adminFormReverseTransform(['label' => 'bloup']);
$this->assertArrayHasKey('category', $actual);
$this->assertArrayHasKey('label', $actual);
$this->assertEquals('bloup', $actual['label']);
}
public function testAdminFormTransform()
{
$personContext = $this->buildPersonContextWithThirdParty();
$actual = $personContext->adminFormTransform(['label' => 'bloup']);
$this->assertArrayHasKey('from_person', $actual);
$this->assertArrayHasKey('label', $actual);
$this->assertEquals('bloup', $actual['label']);
}
public function testGetData()
{
$personContext = $this->buildPersonContextWithThirdParty();
$actual = $personContext->getData(
(new DocGeneratorTemplate())->setOptions(['label' => 'bloup']),
new Person(),
['thirdParty' => $tp = new ThirdParty()]
);
$this->assertArrayHasKey('person', $actual);
$this->assertArrayHasKey('thirdParty', $actual);
$this->assertEquals(spl_object_hash($tp), $actual['thirdParty']['hash']);
}
private function buildPersonContextWithThirdParty(): PersonContextWithThirdParty
{
$normalizer = $this->prophesize(NormalizerInterface::class);
$normalizer->normalize(Argument::type(ThirdParty::class), 'docgen', Argument::type('array'))
->will(static function ($args): array {
return ['class' => '3party', 'hash' => spl_object_hash($args[0])];
});
$personContext = $this->prophesize(PersonContextInterface::class);
$personContext->adminFormReverseTransform(Argument::type('array'))->willReturn(
['category' => ['idInsideBundle' => 1, 'bundleId' => 'abc']]
);
$personContext->adminFormTransform(Argument::type('array'))->willReturn(
['from_person' => 'kept']
);
$personContext->getData(Argument::type(DocGeneratorTemplate::class), Argument::type(Person::class), Argument::type('array'))
->willReturn(['person' => 'data']);
return new PersonContextWithThirdParty(
$personContext->reveal(),
$normalizer->reveal()
);
}
}