Commit 89dace27 authored by François Agneray's avatar François Agneray
Browse files

DatasetFamilyAction => done

parent cca62613
......@@ -99,6 +99,10 @@ $container->set('App\Action\DatasetListByInstanceAction', function (ContainerInt
return new App\Action\DatasetListByInstanceAction($c->get('em'));
});
$container->set('App\Action\DatasetFamilyAction', function (ContainerInterface $c) {
return new App\Action\DatasetFamilyAction($c->get('em'));
});
$container->set('App\Action\DatasetListAction', function (ContainerInterface $c) {
return new App\Action\DatasetListAction($c->get('em'), new App\Utils\DBALConnectionFactory());
});
......
<?php
/*
* This file is part of Anis Server.
*
* (c) Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Action;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
use App\Entity\DatasetFamily;
final class DatasetFamilyAction extends AbstractAction
{
public function __invoke(Request $request, Response $response, array $args): Response
{
if ($request->getMethod() === OPTIONS) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, PUT, DELETE, OPTIONS');
}
// Search the correct dataset family with primary key
$datasetFamily = $this->em->find('App\Entity\DatasetFamily', $args['id']);
// If dataset family is not found 404
if (is_null($datasetFamily)) {
throw new HttpNotFoundException(
$request,
'Dataset family with id ' . $args['id'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$payload = json_encode($datasetFamily);
}
if ($request->getMethod() === PUT) {
$parsedBody = $request->getParsedBody();
$fields = array('label', 'display');
foreach ($fields as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
throw new HttpBadRequestException(
$request,
'Param ' . $a . ' needed to edit the dataset family'
);
}
}
$this->editDatasetFamily($datasetFamily, $parsedBody);
$payload = json_encode($datasetFamily);
}
if ($request->getMethod() === DELETE) {
$id = $datasetFamily->getId();
$this->em->remove($datasetFamily);
$this->em->flush();
$payload = json_encode(array(
'message' => 'Dataset family with id ' . $id . ' is removed!'
));
}
$response->getBody()->write($payload);
return $response;
}
private function editDatasetFamily(DatasetFamily $family, array $parsedBody): void
{
$family->setLabel($parsedBody['label']);
$family->setDisplay($parsedBody['display']);
$this->em->flush();
}
}
<?php
/*
* This file is part of Anis Server.
*
* (c) Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Tests\Action;
use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
use Slim\Exception\HttpNotFoundException;
use Slim\Exception\HttpBadRequestException;
use App\tests\EntityManagerBuilder;
use App\Entity\Instance;
use App\Entity\DatasetFamily;
final class DatasetFamilyActionTest extends TestCase
{
private $action;
private $entityManager;
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->action = new \App\Action\DatasetFamilyAction($this->entityManager);
}
public function testOptionsHttpMethod(): void
{
$request = $this->getRequest('OPTIONS');
$response = ($this->action)($request, new Response(), array());
$this->assertSame($response->getHeaderLine('Access-Control-Allow-Methods'), 'GET, PUT, DELETE, OPTIONS');
}
public function testDatasetFamilyIsNotFound(): void
{
$this->expectException(HttpNotFoundException::class);
$this->expectExceptionMessage('Dataset family with id 1 is not found');
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertEquals(404, (int) $response->getStatusCode());
}
public function testGetADatasetFamilyById(): void
{
$datasetFamily = $this->addADatasetFamily();
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertSame(json_encode($datasetFamily), (string) $response->getBody());
}
public function testEditADatasetFamilyEmptyLabelField(): void
{
$this->addADatasetFamily();
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Param label needed to edit the dataset family');
$request = $this->getRequest('PUT')->withParsedBody(array());
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertEquals(400, (int) $response->getStatusCode());
}
public function testEditADatasetFamily(): void
{
$fields = array(
'label' => 'Modfied family',
'display' => 20
);
$this->addADatasetFamily();
$request = $this->getRequest('PUT')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertSame(json_encode(array_merge(['id' => 1], $fields)), (string) $response->getBody());
}
public function testDeleteADatasetFamily(): void
{
$this->addADatasetFamily();
$request = $this->getRequest('DELETE');
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertSame(
json_encode(array('message' => 'Dataset family with id 1 is removed!')),
(string) $response->getBody()
);
}
protected function tearDown(): void
{
$this->entityManager->getConnection()->close();
}
private function getRequest(string $method): ServerRequest
{
return new ServerRequest($method, '/dataset-family/1', array(
'Content-Type' => 'application/json'
));
}
private function addInstance(): Instance
{
$instance = new Instance('default', 'Default instance');
$instance->setClientUrl('http://anis.lam.fr');
$this->entityManager->persist($instance);
$this->entityManager->flush();
return $instance;
}
private function addADatasetFamily(): DatasetFamily
{
$instance = $this->addInstance();
$dataset = new DatasetFamily($instance);
$dataset->setLabel('Test1');
$dataset->setDisplay(10);
$this->entityManager->persist($dataset);
$this->entityManager->flush();
return $dataset;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment