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

OutputCategoryListAction => done

parent 7635354d
......@@ -14,6 +14,7 @@ namespace App\Action;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Exception\HttpNotFoundException;
use Slim\Exception\HttpBadRequestException;
use App\Entity\OutputCategory;
use App\Entity\OutputFamily;
......@@ -36,8 +37,20 @@ final class OutputCategoryListAction extends AbstractAction
return $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
}
$outputFamily = $this->em->find('App\Entity\OutputFamily', $args['id']);
// Returns HTTP 404 if the output family is not found
if (is_null($outputFamily)) {
throw new HttpNotFoundException(
$request,
'Output family with id ' . $args['id'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$outputCategories = $this->em->getRepository('App\Entity\OutputCategory')->findAll();
$outputCategories = $this->em->getRepository('App\Entity\OutputCategory')->findByOutputFamily(
$outputFamily
);
$payload = json_encode($outputCategories);
}
......@@ -45,7 +58,7 @@ final class OutputCategoryListAction extends AbstractAction
$parsedBody = $request->getParsedBody();
// Verif mandatories fields
foreach (array('label', 'display', 'id_output_family') as $a) {
foreach (array('label', 'display') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
throw new HttpBadRequestException(
$request,
......@@ -54,15 +67,6 @@ final class OutputCategoryListAction extends AbstractAction
}
}
// Output family is mandatory
$outputFamily = $this->em->find('App\Entity\OutputFamily', $parsedBody['id_output_family']);
if (is_null($outputFamily)) {
throw new HttpBadRequestException(
$request,
'Output family with id ' . $parsedBody['id_output_family'] . ' is not found'
);
}
$outputCategory = $this->postOutputCategory($parsedBody, $outputFamily);
$payload = json_encode($outputCategory);
$response = $response->withStatus(201);
......
......@@ -16,7 +16,13 @@ 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;
......@@ -38,11 +44,20 @@ final class OutputCategoryListActionTest extends TestCase
$this->assertSame($response->getHeaderLine('Access-Control-Allow-Methods'), 'GET, POST, 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 testGetAllOutputCategories(): void
{
$outputCategories = $this->addOutputCategories();
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array());
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertSame(
json_encode($outputCategories),
(string) $response->getBody()
......@@ -51,24 +66,11 @@ final class OutputCategoryListActionTest extends TestCase
public function testAddANewOutputCategoryEmptyLabelField(): void
{
$this->addOutputFamily();
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Param label needed to add a new output category');
$request = $this->getRequest('POST')->withParsedBody(array());
$response = ($this->action)($request, new Response(), array());
$this->assertEquals(400, (int) $response->getStatusCode());
}
public function testAddANewOutputCategoryOutputFamilyIsNotFound(): void
{
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Output family with id 1 is not found');
$fields = array(
'label' => 'Default output category',
'display' => 10,
'id_output_family' => 1
);
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array());
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertEquals(400, (int) $response->getStatusCode());
}
......@@ -77,13 +79,12 @@ final class OutputCategoryListActionTest extends TestCase
$this->addOutputFamily();
$fields = array(
'label' => 'Default output category',
'display' => 10,
'id_output_family' => 1
'display' => 10
);
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array());
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertSame(
json_encode(array_merge(['id' => 1], $fields)),
json_encode(array_merge(['id' => 1], $fields, ['id_output_family' => 1])),
(string) $response->getBody()
);
$this->assertEquals(201, (int) $response->getStatusCode());
......@@ -96,14 +97,16 @@ final class OutputCategoryListActionTest extends TestCase
private function getRequest(string $method): ServerRequest
{
return new ServerRequest($method, '/output-category', array(
return new ServerRequest($method, '/output-family/1/output-category', array(
'Content-Type' => 'application/json'
));
}
private function addOutputFamily(): OutputFamily
{
$family = new OutputFamily();
$dataset = $this->addADataset();
$family = new OutputFamily($dataset);
$family->setLabel('Default output family');
$family->setDisplay(10);
$this->entityManager->persist($family);
......@@ -111,6 +114,73 @@ final class OutputCategoryListActionTest extends TestCase
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();
......
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