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

DatasetFamilyListAction => done

parent 8d0f6b8c
......@@ -21,15 +21,16 @@ $app->map([OPTIONS, GET, PUT, DELETE], '/project/{name}', App\Action\ProjectActi
$app->map([OPTIONS, GET, POST], '/instance', App\Action\InstanceListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/instance/{name}', App\Action\InstanceAction::class);
$app->map([OPTIONS, GET, POST], '/instance/{name}/dataset-family', App\Action\DatasetFamilyListAction::class);
$app->map([OPTIONS, GET], '/instance/{name}/dataset', App\Action\InstanceAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/dataset-family/{id}', App\Action\DatasetFamilyAction::class);
$app->map([OPTIONS, GET, POST], '/dataset-family/{id}/dataset', App\Action\DatasetListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/dataset/{name}', App\Action\DatasetAction::class);
$app->map([OPTIONS, GET, POST], '/dataset/{name}/criteria-family', App\Action\CriteriaFamilyListAction::class);
$app->map([OPTIONS, GET, POST], '/dataset/{name}/output-family', App\Action\OutputFamilyListAction::class);
$app->map([OPTIONS, GET], '/dataset/{name}/attribute', App\Action\AttributeListAction::class);
$app->map([OPTIONS, GET, PUT], '/dataset/{name}/attribute/{id}', App\Action\AttributeAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/criteria-family/{id}', App\Action\CriteriaFamilyAction::class);
$app->map([OPTIONS, GET, POST], '/dataset/{name}/output-family', App\Action\OutputFamilyListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/output-family/{id}', App\Action\OutputFamilyAction::class);
$app->map([OPTIONS, GET, POST], '/output-family/{id}/output-category', App\Action\OutputCategoryListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/output-category/{id}', App\Action\OutputCategoryAction::class);
$app->map([OPTIONS, GET], '/dataset/{name}/attribute', App\Action\AttributeListAction::class);
$app->map([OPTIONS, GET, PUT], '/dataset/{name}/attribute/{id}', App\Action\AttributeAction::class);
$app->get('/search/{dname}', App\Action\SearchAction::class);
<?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\Instance;
use App\Entity\DatasetFamily;
final class DatasetFamilyListAction extends AbstractAction
{
/**
* `GET` Returns a list of all dataset family for a given instance
*
* @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');
}
$instance = $this->em->find('App\Entity\Instance', $args['name']);
// Returns HTTP 404 if the dataset is not found
if (is_null($instance)) {
throw new HttpNotFoundException(
$request,
'Instance with name ' . $args['name'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$families = $this->em->getRepository('App\Entity\DatasetFamily')->findByInstance($instance);
$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 dataset family'
);
}
}
$family = $this->postDatasetFamily($parsedBody, $instance);
$payload = json_encode($family);
$response = $response->withStatus(201);
}
$response->getBody()->write($payload);
return $response;
}
private function postDatasetFamily(array $parsedBody, Instance $instance): DatasetFamily
{
$family = new DatasetFamily($instance);
$family->setLabel($parsedBody['label']);
$family->setDisplay($parsedBody['display']);
$this->em->persist($family);
$this->em->flush();
return $family;
}
}
......@@ -96,7 +96,7 @@ class Dataset implements \JsonSerializable
* @var Anis\Entity\Project
*
* @ManyToOne(targetEntity="DatasetFamily")
* @JoinColumn(name="id_dataset_family", referencedColumnName="id", nullable=true)
* @JoinColumn(name="id_dataset_family", referencedColumnName="id", nullable=false)
*/
protected $datasetFamily;
......
<?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\Instance;
use App\Entity\DatasetFamily;
final class DatasetFamilyListActionTest extends TestCase
{
private $action;
private $entityManager;
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->action = new \App\Action\DatasetFamilyListAction($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 testInstanceIsNotFound(): void
{
$this->expectException(HttpNotFoundException::class);
$this->expectExceptionMessage('Instance with name default is not found');
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('name' => 'default'));
$this->assertEquals(404, (int) $response->getStatusCode());
}
public function testGetAllDatasetFamiliesOfAnInstance(): void
{
$families = $this->addFamilies();
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('name' => 'default'));
$this->assertSame(
json_encode($families),
(string) $response->getBody()
);
}
public function testAddANewDatasetFamilyEmptyLabelField(): void
{
$this->addInstance();
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Param label needed to add a new dataset family');
$request = $this->getRequest('POST')->withParsedBody(array());
$response = ($this->action)($request, new Response(), array('name' => 'default'));
$this->assertEquals(400, (int) $response->getStatusCode());
}
public function testAddANewDatasetFamily(): void
{
$fields = array(
'label' => 'Default family',
'display' => 10
);
$this->addInstance();
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array('name' => 'default'));
$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, '/instance/default/dataset-family', 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 addFamilies(): array
{
$instance = $this->addInstance();
$family = new DatasetFamily($instance);
$family->setLabel('Default dataset');
$family->setDisplay(10);
$this->entityManager->persist($family);
$family2 = new DatasetFamily($instance);
$family2->setLabel('Another family');
$family2->setDisplay(20);
$this->entityManager->persist($family2);
$this->entityManager->flush();
return array($family, $family2);
}
}
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