Commit 7635354d authored by François Agneray's avatar François Agneray
Browse files

OutputFamilyAction => done

parent 83ba9e12
......@@ -75,14 +75,6 @@ $container->set('App\Action\FamilyAction', function (ContainerInterface $c) {
return new App\Action\FamilyAction($c->get('em'));
});
$container->set('App\Action\OutputCategoryListAction', function (ContainerInterface $c) {
return new App\Action\OutputCategoryListAction($c->get('em'));
});
$container->set('App\Action\OutputCategoryAction', function (ContainerInterface $c) {
return new App\Action\OutputCategoryAction($c->get('em'));
});
$container->set('App\Action\InstanceListAction', function (ContainerInterface $c) {
return new App\Action\InstanceListAction($c->get('em'));
});
......@@ -123,6 +115,18 @@ $container->set('App\Action\OutputFamilyListAction', function (ContainerInterfac
return new App\Action\OutputFamilyListAction($c->get('em'), new App\Utils\DBALConnectionFactory());
});
$container->set('App\Action\OutputFamilyAction', function (ContainerInterface $c) {
return new App\Action\OutputFamilyAction($c->get('em'), new App\Utils\DBALConnectionFactory());
});
$container->set('App\Action\OutputCategoryListAction', function (ContainerInterface $c) {
return new App\Action\OutputCategoryListAction($c->get('em'));
});
$container->set('App\Action\OutputCategoryAction', function (ContainerInterface $c) {
return new App\Action\OutputCategoryAction($c->get('em'));
});
$container->set('App\Action\AttributeListAction', function (ContainerInterface $c) {
return new App\Action\AttributeListAction($c->get('em'));
});
......
<?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\OutputFamily;
final class OutputFamilyAction 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 output family with primary key
$outputFamily = $this->em->find('App\Entity\OutputFamily', $args['id']);
// If output family is not found 404
if (is_null($outputFamily)) {
throw new HttpNotFoundException(
$request,
'Output family with id ' . $args['id'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$payload = json_encode($outputFamily);
}
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 output family'
);
}
}
$this->editOutputFamily($outputFamily, $parsedBody);
$payload = json_encode($outputFamily);
}
if ($request->getMethod() === DELETE) {
$id = $outputFamily->getId();
$this->em->remove($outputFamily);
$this->em->flush();
$payload = json_encode(array(
'message' => 'Output family with id ' . $id . ' is removed!'
));
}
$response->getBody()->write($payload);
return $response;
}
private function editOutputFamily(OutputFamily $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\Database;
use App\Entity\Project;
use App\Entity\Instance;
use App\Entity\DatasetFamily;
use App\Entity\Dataset;
use App\Entity\OutputFamily;
final class OutputFamilyActionTest extends TestCase
{
private $action;
private $entityManager;
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->action = new \App\Action\OutputFamilyAction($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 testOutputFamilyIsNotFound(): void
{
$this->expectException(HttpNotFoundException::class);
$this->expectExceptionMessage('Output 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 testGetAnOutputFamilyById(): void
{
$outputFamily = $this->addAnOutputFamily();
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertSame(json_encode($outputFamily), (string) $response->getBody());
}
public function testEditAnOutputFamilyEmptyLabelField(): void
{
$this->addAnOutputFamily();
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Param label needed to edit the output family');
$request = $this->getRequest('PUT')->withParsedBody(array());
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertEquals(400, (int) $response->getStatusCode());
}
public function testEditAnOutputFamily(): void
{
$fields = array(
'label' => 'Modfied family',
'display' => 20
);
$this->addAnOutputFamily();
$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 testDeleteAnOutputFamily(): void
{
$this->addAnOutputFamily();
$request = $this->getRequest('DELETE');
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertSame(
json_encode(array('message' => 'Output 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, '/output-family/1', array(
'Content-Type' => 'application/json'
));
}
private function addAnOutputFamily(): OutputFamily
{
$dataset = $this->addADataset();
$family = new OutputFamily($dataset);
$family->setLabel('Default criteria');
$family->setDisplay(10);
$this->entityManager->persist($family);
$this->entityManager->flush();
return $family;
}
private function addProject(): Project
{
$database = new Database();
$database->setLabel('Test1');
$database->setDbName('test1');
$database->setType('pgsql');
$database->setHost('db');
$database->setPort(5432);
$database->setLogin('test');
$database->setPassword('test');
$this->entityManager->persist($database);
$project = new Project('anis_project');
$project->setLabel('Test project');
$project->setDescription('Test description');
$project->setLink('http://test.com');
$project->setManager('User1');
$project->setDatabase($database);
$this->entityManager->persist($project);
$this->entityManager->flush();
return $project;
}
private function addInstance(): Instance
{
$instance = new Instance('aspic', 'Aspic');
$instance->setClientUrl('http://cesam.lam.fr/aspic');
$this->entityManager->persist($instance);
$this->entityManager->flush();
return $instance;
}
private function addDatasetFamily(): DatasetFamily
{
$instance = $this->addInstance();
$family = new DatasetFamily($instance);
$family->setLabel('Default dataset');
$family->setDisplay(10);
$this->entityManager->persist($family);
$this->entityManager->flush();
return $family;
}
private function addADataset(): Dataset
{
$project = $this->addProject();
$family = $this->addDatasetFamily();
$dataset = new Dataset('obs_cat');
$dataset->setTableRef('v_obs_cat');
$dataset->setLabel('Obscat label');
$dataset->setDescription('Obscat description');
$dataset->setDisplay(10);
$dataset->setCount(10000);
$dataset->setVo(false);
$dataset->setDataPath('/mnt/obs_cat');
$dataset->setSelectableRow(false);
$dataset->setProject($project);
$dataset->setDatasetFamily($family);
$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