Add new file for StoredObjectType tests and update class definitions

This commit adds a new file, StoredObjectTypeTest.php, to ChillDocStoreBundle Tests. It contains unit tests for the StoredObjectType class. Changes are also made in StoredObjectNormalizer and StoredObjectDataMapper classes, making JWTDavTokenProviderInterface and UrlGeneratorInterface as readonly in StoredObjectNormalizer and removing unnecessary EntityManagerInterface and debug commands on StoredObjectDataMapper. These changes improve test coverage and optimize the code for better performance.
This commit is contained in:
Julien Fastré 2024-05-28 12:08:02 +02:00
parent 775535e683
commit 111a21fcec
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
3 changed files with 110 additions and 12 deletions

View File

@ -12,16 +12,14 @@ declare(strict_types=1);
namespace Chill\DocStoreBundle\Form\DataMapper;
use Chill\DocStoreBundle\Entity\StoredObject;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception;
use Symfony\Component\Form\FormInterface;
class StoredObjectDataMapper implements DataMapperInterface
{
public function __construct(
private EntityManagerInterface $entityManager,
) {
public function __construct()
{
}
/**
@ -55,10 +53,7 @@ class StoredObjectDataMapper implements DataMapperInterface
throw new Exception\UnexpectedTypeException($viewData, StoredObject::class);
}
dump($forms['stored_object']->getData(), $viewData);
if (null === $forms['stored_object']->getData()) {
return;
}
@ -76,7 +71,5 @@ class StoredObjectDataMapper implements DataMapperInterface
if (array_key_exists('title', $forms)) {
$viewData->setTitle($forms['title']->getData());
}
dump($viewData);
}
}

View File

@ -24,15 +24,15 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
*
* Normalizes a StoredObject entity to an array of data.
*/
class StoredObjectNormalizer implements NormalizerInterface, NormalizerAwareInterface
final class StoredObjectNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
public const ADD_DAV_SEE_LINK_CONTEXT = 'dav-see-link-context';
public const ADD_DAV_EDIT_LINK_CONTEXT = 'dav-edit-link-context';
public function __construct(
private JWTDavTokenProviderInterface $JWTDavTokenProvider,
private UrlGeneratorInterface $urlGenerator
private readonly JWTDavTokenProviderInterface $JWTDavTokenProvider,
private readonly UrlGeneratorInterface $urlGenerator
) {
}

View File

@ -0,0 +1,105 @@
<?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 Chill\DocStoreBundle\Tests\Form;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Form\DataMapper\StoredObjectDataMapper;
use Chill\DocStoreBundle\Form\DataTransformer\StoredObjectDataTransformer;
use Chill\DocStoreBundle\Form\StoredObjectType;
use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum;
use Chill\DocStoreBundle\Security\Guard\JWTDavTokenProviderInterface;
use Chill\DocStoreBundle\Serializer\Normalizer\StoredObjectNormalizer;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Serializer;
/**
* @internal
*
* @coversNothing
*/
class StoredObjectTypeTest extends TypeTestCase
{
use ProphecyTrait;
public function testChangeTitleValue(): void
{
$formData = ['title' => $newTitle = 'new title', 'stored_object' => <<<'JSON'
{"datas":[],"filename":"","id":null,"iv":[],"keyInfos":[],"title":"","type":"","uuid":"3c6a28fe-f913-40b9-a201-5eccc4f2d312","status":"ready","createdAt":null,"createdBy":null,"creationDate":null,"_links":{"dav_link":{"href":"http:\/\/url\/fake","expiration":"1716889578"}}}
JSON];
$model = new StoredObject();
$form = $this->factory->create(StoredObjectType::class, $model, ['has_title' => true]);
$form->submit($formData);
$this->assertTrue($form->isSynchronized());
$this->assertEquals($newTitle, $model->getTitle());
}
public function testReplaceByAnotherObject(): void
{
$formData = ['title' => $newTitle = 'new title', 'stored_object' => <<<'JSON'
{"filename":"abcdef","iv":[10, 15, 20, 30],"keyInfos":[],"type":"text/html","status":"object_store_created"}
JSON];
$model = new StoredObject();
$originalObjectId = spl_object_id($model);
$form = $this->factory->create(StoredObjectType::class, $model, ['has_title' => true]);
$form->submit($formData);
$this->assertTrue($form->isSynchronized());
$model = $form->getData();
$this->assertNotEquals($originalObjectId, spl_object_hash($model));
$this->assertEquals('abcdef', $model->getFilename());
$this->assertEquals([10, 15, 20, 30], $model->getIv());
$this->assertEquals('text/html', $model->getType());
$this->assertEquals($newTitle, $model->getTitle());
}
protected function getExtensions()
{
$jwtTokenProvider = $this->prophesize(JWTDavTokenProviderInterface::class);
$jwtTokenProvider->createToken(Argument::type(StoredObject::class), Argument::type(StoredObjectRoleEnum::class))
->willReturn('token');
$jwtTokenProvider->getTokenExpiration('token')->willReturn(new \DateTimeImmutable());
$urlGenerator = $this->prophesize(UrlGeneratorInterface::class);
$urlGenerator->generate('chill_docstore_dav_document_get', Argument::type('array'), UrlGeneratorInterface::ABSOLUTE_URL)
->willReturn('http://url/fake');
$serializer = new Serializer(
[
new StoredObjectNormalizer(
$jwtTokenProvider->reveal(),
$urlGenerator->reveal(),
),
],
[
new JsonEncoder(),
]
);
$dataTransformer = new StoredObjectDataTransformer($serializer);
$dataMapper = new StoredObjectDataMapper();
$type = new StoredObjectType(
$dataTransformer,
$dataMapper,
);
return [
new PreloadedExtension([$type], []),
];
}
}