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

OutputFamilyListAction => done

parent 48415814
......@@ -119,6 +119,10 @@ $container->set('App\Action\CriteriaFamilyAction', function (ContainerInterface
return new App\Action\CriteriaFamilyAction($c->get('em'), new App\Utils\DBALConnectionFactory());
});
$container->set('App\Action\OutputFamilyListAction', function (ContainerInterface $c) {
return new App\Action\OutputFamilyListAction($c->get('em'), new App\Utils\DBALConnectionFactory());
});
$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\Dataset;
use App\Entity\OutputFamily;
final class OutputFamilyListAction extends AbstractAction
{
/**
* `GET` Returns a list of all output family for a given dataset
* `POST` Add a new dataset output family to a given dataset
*
* @param ServerRequestInterface $request PSR-7 This object represents the HTTP request
* @param ResponseInterface $response PSR-7 This object represents the HTTP response
* @param string[] $args This table contains information transmitted in the URL (see routes.php)
*
* @return ResponseInterface
*/
public function __invoke(Request $request, Response $response, array $args): Response
{
if ($request->getMethod() === OPTIONS) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
}
$dataset = $this->em->find('App\Entity\Dataset', $args['name']);
// Returns HTTP 404 if the dataset is not found
if (is_null($dataset)) {
throw new HttpNotFoundException(
$request,
'Dataset with name ' . $args['name'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$families = $this->em->getRepository('App\Entity\OutputFamily')->findByDataset($dataset);
$payload = json_encode($families);
}
if ($request->getMethod() === POST) {
$parsedBody = $request->getParsedBody();
// To work this action needs information
foreach (array('label', 'display') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
throw new HttpBadRequestException(
$request,
'Param ' . $a . ' needed to add a new output family'
);
}
}
$family = $this->postOutputFamily($parsedBody, $dataset);
$payload = json_encode($family);
$response = $response->withStatus(201);
}
$response->getBody()->write($payload);
return $response;
}
private function postOutputFamily(array $parsedBody, Dataset $dataset): OutputFamily
{
$family = new OutputFamily($dataset);
$family->setLabel($parsedBody['label']);
$family->setDisplay($parsedBody['display']);
$this->em->persist($family);
$this->em->flush();
return $family;
}
}
......@@ -84,8 +84,7 @@ class OutputFamily implements \JsonSerializable
return [
'id' => $this->getId(),
'label' => $this->getLabel(),
'display' => $this->getDisplay(),
'type' => 'output'
'display' => $this->getDisplay()
];
}
}
<?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\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
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 OutputFamilyListActionTest extends TestCase
{
private $action;
private $entityManager;
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->action = new \App\Action\OutputFamilyListAction($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, POST, OPTIONS');
}
public function testDatasetIsNotFound(): void
{
$this->expectException(HttpNotFoundException::class);
$this->expectExceptionMessage('Dataset with name obs_cat is not found');
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('name' => 'obs_cat'));
$this->assertEquals(404, (int) $response->getStatusCode());
}
public function testGetAllOutputFamiliesOfADataset(): void
{
$families = $this->addFamilies();
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('name' => 'obs_cat'));
$this->assertSame(
json_encode($families),
(string) $response->getBody()
);
}
public function testAddANewOutputFamilyEmptyLabelField(): void
{
$this->addADataset();
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Param label needed to add a new output family');
$request = $this->getRequest('POST')->withParsedBody(array());
$response = ($this->action)($request, new Response(), array('name' => 'obs_cat'));
$this->assertEquals(400, (int) $response->getStatusCode());
}
public function testAddANewOutputFamily(): void
{
$fields = array(
'label' => 'Default output family',
'display' => 10
);
$this->addADataset();
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array('name' => 'obs_cat'));
$this->assertSame(
json_encode(array_merge(['id' => 1], $fields)),
(string) $response->getBody()
);
$this->assertEquals(201, (int) $response->getStatusCode());
}
protected function tearDown(): void
{
$this->entityManager->getConnection()->close();
}
private function getRequest(string $method): ServerRequest
{
return new ServerRequest($method, '/dataset/obs_cat/output-family', array(
'Content-Type' => 'application/json'
));
}
private function addFamilies(): array
{
$dataset = $this->addADataset();
$family = new OutputFamily($dataset);
$family->setLabel('Default output');
$family->setDisplay(10);
$this->entityManager->persist($family);
$family2 = new OutputFamily($dataset);
$family2->setLabel('Another family');
$family2->setDisplay(20);
$this->entityManager->persist($family2);
$this->entityManager->flush();
return array($family, $family2);
}
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