mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-01 20:43:49 +00:00
Add StoredObjectVersionApiController and corresponding test
Added a new class StoredObjectVersionApiController in ChillDocGeneratorBundle which lists versions of a specified stored object. Corresponding unit test has been added as well. Made modifications in `StoredObject.php` to make the versions selectable. Also updated the API specifications to include a new GET route for retrieving versions.
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
<?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\Controller;
|
||||
|
||||
use Chill\DocStoreBundle\Controller\StoredObjectVersionApiController;
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum;
|
||||
use Chill\DocStoreBundle\Serializer\Normalizer\StoredObjectVersionNormalizer;
|
||||
use Chill\MainBundle\Pagination\Paginator;
|
||||
use Chill\MainBundle\Pagination\PaginatorFactoryInterface;
|
||||
use Chill\MainBundle\Serializer\Normalizer\CollectionNormalizer;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class StoredObjectVersionApiControllerTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
public function testListVersion(): void
|
||||
{
|
||||
$storedObject = new StoredObject();
|
||||
for ($i = 0; $i < 15; ++$i) {
|
||||
$storedObject->registerVersion();
|
||||
}
|
||||
|
||||
$security = $this->prophesize(Security::class);
|
||||
$security->isGranted(StoredObjectRoleEnum::SEE->value, $storedObject)
|
||||
->willReturn(true)
|
||||
->shouldBeCalledOnce();
|
||||
|
||||
$controller = $this->buildController($security->reveal());
|
||||
|
||||
$response = $controller->listVersions($storedObject);
|
||||
$body = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
self::assertEquals($response->getStatusCode(), 200);
|
||||
self::assertIsArray($body);
|
||||
self::assertArrayHasKey('results', $body);
|
||||
self::assertCount(10, $body['results']);
|
||||
}
|
||||
|
||||
private function buildController(Security $security): StoredObjectVersionApiController
|
||||
{
|
||||
$paginator = $this->prophesize(Paginator::class);
|
||||
$paginator->getCurrentPageFirstItemNumber()->willReturn(0);
|
||||
$paginator->getItemsPerPage()->willReturn(10);
|
||||
$paginator->getTotalItems()->willReturn(15);
|
||||
$paginator->hasNextPage()->willReturn(false);
|
||||
$paginator->hasPreviousPage()->willReturn(false);
|
||||
|
||||
$paginatorFactory = $this->prophesize(PaginatorFactoryInterface::class);
|
||||
$paginatorFactory->create(Argument::type('int'))->willReturn($paginator);
|
||||
|
||||
$serializer = new Serializer([
|
||||
new StoredObjectVersionNormalizer(), new CollectionNormalizer(),
|
||||
], [new JsonEncoder()]);
|
||||
|
||||
return new StoredObjectVersionApiController($paginatorFactory->reveal(), $serializer, $security);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user