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

OutputCategoryListByDatasetAction => done

parent 2a8a23ef
......@@ -100,23 +100,27 @@ $container->set('App\Action\DatasetListAction', function (ContainerInterface $c)
});
$container->set('App\Action\DatasetAction', function (ContainerInterface $c) {
return new App\Action\DatasetAction($c->get('em'), new App\Utils\DBALConnectionFactory());
return new App\Action\DatasetAction($c->get('em'));
});
$container->set('App\Action\CriteriaFamilyListAction', function (ContainerInterface $c) {
return new App\Action\CriteriaFamilyListAction($c->get('em'), new App\Utils\DBALConnectionFactory());
return new App\Action\CriteriaFamilyListAction($c->get('em'));
});
$container->set('App\Action\CriteriaFamilyAction', function (ContainerInterface $c) {
return new App\Action\CriteriaFamilyAction($c->get('em'), new App\Utils\DBALConnectionFactory());
return new App\Action\CriteriaFamilyAction($c->get('em'));
});
$container->set('App\Action\OutputFamilyListAction', function (ContainerInterface $c) {
return new App\Action\OutputFamilyListAction($c->get('em'), new App\Utils\DBALConnectionFactory());
return new App\Action\OutputFamilyListAction($c->get('em'));
});
$container->set('App\Action\OutputFamilyAction', function (ContainerInterface $c) {
return new App\Action\OutputFamilyAction($c->get('em'), new App\Utils\DBALConnectionFactory());
return new App\Action\OutputFamilyAction($c->get('em'));
});
$container->set('App\Action\OutputCategoryListByDatasetAction', function (ContainerInterface $c) {
return new App\Action\OutputCategoryListByDatasetAction($c->get('em'));
});
$container->set('App\Action\OutputCategoryListAction', function (ContainerInterface $c) {
......
......@@ -29,6 +29,7 @@ $app->map([OPTIONS, GET, POST], '/dataset/{name}/criteria-family', App\Action\Cr
$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], '/dataset/{name}/output-category', App\Action\OutputCategoryListByDatasetAction::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);
......
<?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\HttpNotFoundException;
final class OutputCategoryListByDatasetAction extends AbstractAction
{
/**
* `GET` Returns a list of all output categories for 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, 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) {
$qb = $this->em->createQueryBuilder();
$qb->select('o')
->from('App\Entity\OutputCategory', 'o')
->join('o.outputFamily', 'f')
->where($qb->expr()->eq('IDENTITY(f.dataset)', ':datasetName'))
->setParameter('datasetName', $dataset->getName());
$datasets = $qb->getQuery()->getResult();
$payload = json_encode($datasets);
}
$response->getBody()->write($payload);
return $response;
}
}
......@@ -12,6 +12,8 @@ declare(strict_types=1);
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity
* @Table(name="dataset")
......
<?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;
use App\Entity\OutputCategory;
final class OutputCategoryListByDatasetActionTest extends TestCase
{
private $action;
private $entityManager;
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->action = new \App\Action\OutputCategoryListByDatasetAction($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, 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 testGetAllOutputCategoriesForADataset(): void
{
$outputCategories = $this->addOutputCategories();
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('name' => 'obs_cat'));
$this->assertSame(
json_encode($outputCategories),
(string) $response->getBody()
);
}
protected function tearDown(): void
{
$this->entityManager->getConnection()->close();
}
private function getRequest(string $method): ServerRequest
{
return new ServerRequest($method, '/dataset/obs_cat/output-category', array(
'Content-Type' => 'application/json'
));
}
private function addOutputFamily(): OutputFamily
{
$dataset = $this->addADataset();
$family = new OutputFamily($dataset);
$family->setLabel('Default output family');
$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;
}
private function addOutputCategories(): array
{
$outputFamily = $this->addOutputFamily();
$outputCategory1 = new OutputCategory();
$outputCategory1->setLabel('Default output category');
$outputCategory1->setDisplay(10);
$outputCategory1->setOutputFamily($outputFamily);
$this->entityManager->persist($outputCategory1);
$outputCategory2 = new OutputCategory();
$outputCategory2->setLabel('My output category');
$outputCategory2->setDisplay(20);
$outputCategory2->setOutputFamily($outputFamily);
$this->entityManager->persist($outputCategory2);
$this->entityManager->flush();
return array($outputCategory1, $outputCategory2);
}
}
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