mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
build base context data
This commit is contained in:
parent
6501a0148e
commit
17a81d7e66
@ -0,0 +1,53 @@
|
||||
<?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 Chill\DocGeneratorBundle\Service\Context;
|
||||
|
||||
use Chill\MainBundle\Entity\Location;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use DateTimeImmutable;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
class BaseContextData
|
||||
{
|
||||
private NormalizerInterface $normalizer;
|
||||
|
||||
private Security $security;
|
||||
|
||||
public function __construct(Security $security, NormalizerInterface $normalizer)
|
||||
{
|
||||
$this->security = $security;
|
||||
$this->normalizer = $normalizer;
|
||||
}
|
||||
|
||||
public function getData(): array
|
||||
{
|
||||
$data = [];
|
||||
$user = $this->security->getUser();
|
||||
|
||||
$data['creator'] = $this->normalizer->normalize(
|
||||
$user instanceof User ? $user : null,
|
||||
'docgen',
|
||||
['docgen:expects' => User::class, 'groups' => ['docgen:read']]
|
||||
);
|
||||
$data['createdAt'] = $this->normalizer->normalize(new DateTimeImmutable(), 'docgen', [
|
||||
'docgen:expects' => DateTimeImmutable::class, 'groups' => ['docgen:read'],
|
||||
]);
|
||||
$data['location'] = $this->normalizer->normalize(
|
||||
$user instanceof User ? $user->getCurrentLocation() : null,
|
||||
'docgen',
|
||||
['docgen:expects' => Location::class, 'groups' => ['docgen:expects']]
|
||||
);
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
@ -34,6 +34,11 @@ services:
|
||||
autowire: true
|
||||
autoconfigure: true
|
||||
|
||||
Chill\DocGeneratorBundle\Service\Context\:
|
||||
resource: "../Service/Context/"
|
||||
autowire: true
|
||||
autoconfigure: true
|
||||
|
||||
Chill\DocGeneratorBundle\GeneratorDriver\:
|
||||
resource: "../GeneratorDriver/"
|
||||
autowire: true
|
||||
|
@ -0,0 +1,66 @@
|
||||
<?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 Chill\DocGeneratorBundle\tests\Service\Context;
|
||||
|
||||
use Chill\DocGeneratorBundle\Service\Context\BaseContextData;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
final class BaseContextDataTest extends KernelTestCase
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
self::bootKernel();
|
||||
}
|
||||
|
||||
public function testGenerateNoContext()
|
||||
{
|
||||
$context = $this->buildBaseContext();
|
||||
|
||||
$actual = $context->getData();
|
||||
|
||||
$this->assertIsArray($actual);
|
||||
$this->assertArrayHasKey('creator', $actual);
|
||||
$this->assertArrayHasKey('createdAt', $actual);
|
||||
}
|
||||
|
||||
public function testGenerateWithUser()
|
||||
{
|
||||
$security = $this->prophesize(Security::class);
|
||||
$security->getUser()->willReturn(new User());
|
||||
|
||||
$context = $this->buildBaseContext($security->reveal());
|
||||
|
||||
$actual = $context->getData();
|
||||
|
||||
$this->assertIsArray($actual);
|
||||
$this->assertArrayHasKey('creator', $actual);
|
||||
$this->assertArrayHasKey('createdAt', $actual);
|
||||
}
|
||||
|
||||
private function buildBaseContext(
|
||||
?Security $security = null,
|
||||
?NormalizerInterface $normalizer = null
|
||||
): BaseContextData {
|
||||
return new BaseContextData(
|
||||
$security ?? self::$container->get(Security::class),
|
||||
$normalizer ?? self::$container->get(NormalizerInterface::class)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user