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

OutputCategoryAction => done

parent c34ac693
<?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\Family;
final class FamilyAction 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');
}
$type = $this->verifyType($args['type']);
if (empty($type)) {
throw new HttpBadRequestException(
$request,
'Type ' . $args['type'] . ' is not defined'
);
}
$entityClass = $this->getEntityClass($type);
$family = $this->em->find($entityClass, $args['id']);
if (is_null($family)) {
throw new HttpNotFoundException(
$request,
ucfirst($type) . ' family with id ' . $args['id'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$payload = json_encode($family);
}
if ($request->getMethod() === PUT) {
$parsedBody = $request->getParsedBody();
// Vérification des champs vides
$fields = array('label', 'display');
foreach ($fields as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
throw new HttpBadRequestException(
$request,
'Param ' . $a . ' needed to edit the ' . $type . ' family'
);
}
}
$this->editFamily($family, $parsedBody);
$payload = json_encode($family);
}
if ($request->getMethod() === DELETE) {
$id = $family->getId();
$this->em->remove($family);
$this->em->flush();
$payload = json_encode(array(
'message' => ucfirst($type) . ' family with id ' . $id . ' is removed!'
));
}
$response->getBody()->write($payload);
return $response;
}
private function verifyType(string $type): string
{
$t = strtolower($type);
if ($t == 'dataset' || $t == 'output' || $t == 'criteria') {
return $t;
} else {
return '';
}
}
private function getEntityClass(string $type): string
{
return 'App\Entity\\' . ucfirst($type) . 'Family';
}
private function editFamily(object $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\Action;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Exception\HttpBadRequestException;
use App\Entity\Family;
final class FamilyListAction extends AbstractAction
{
/**
* `GET` Returns a list of all families by type listed in the metamodel database
* `POST` Add a new family
*
* @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');
}
$type = $this->verifyType($args['type']);
if (empty($type)) {
throw new HttpBadRequestException(
$request,
'Type ' . $args['type'] . ' is not defined'
);
}
if ($request->getMethod() === GET) {
$families = $this->em->getRepository($this->getEntityClass($type))->findAll();
$payload = json_encode($families);
}
if ($request->getMethod() === POST) {
$parsedBody = $request->getParsedBody();
// To work this action needs information to update
foreach (array('label', 'display') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
throw new HttpBadRequestException(
$request,
'Param ' . $a . ' needed to add a new family'
);
}
}
$family = $this->postFamily($parsedBody, $this->getEntityClass($type));
$payload = json_encode($family);
$response = $response->withStatus(201);
}
$response->getBody()->write($payload);
return $response;
}
private function verifyType(string $type): string
{
$t = strtolower($type);
if ($t == 'dataset' || $t == 'output' || $t == 'criteria') {
return $t;
} else {
return '';
}
}
private function getEntityClass(string $type): string
{
return 'App\Entity\\' . ucfirst($type) . 'Family';
}
private function postFamily(array $parsedBody, string $class): object
{
$family = new $class();
$family->setLabel($parsedBody['label']);
$family->setDisplay($parsedBody['display']);
$this->em->persist($family);
$this->em->flush();
return $family;
}
}
<?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\DatasetFamily;
final class FamilyActionTest extends TestCase
{
private $action;
private $entityManager;
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->action = new \App\Action\FamilyAction($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 testTypeIsNotDefined(): void
{
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Type undifined is not defined');
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('type' => 'undifined'));
$this->assertEquals(400, (int) $response->getStatusCode());
}
public function testDatasetFamilyIsNotFound(): void
{
$this->expectException(HttpNotFoundException::class);
$this->expectExceptionMessage('Dataset family with id 1 is not found');
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('type' => 'dataset', 'id' => 1));
$this->assertEquals(404, (int) $response->getStatusCode());
}
public function testGetADatasetFamilyById(): void
{
$family = $this->addADatasetFamily();
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('type' => 'dataset', 'id' => 1));
$this->assertSame(json_encode($family), (string) $response->getBody());
}
public function testEditADatasetFamilyEmptyLabelField(): void
{
$this->addADatasetFamily();
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Param label needed to edit the dataset family');
$request = $this->getRequest('PUT')->withParsedBody(array());
$response = ($this->action)($request, new Response(), array('type' => 'dataset', 'id' => 1));
$this->assertEquals(400, (int) $response->getStatusCode());
}
public function testEditADatasetFamily(): void
{
$fields = array(
'label' => 'New_label',
'display' => 20
);
$this->addADatasetFamily();
$request = $this->getRequest('PUT')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array('type' => 'dataset', 'id' => 1));
$this->assertSame(
json_encode(array_merge(['id' => 1], $fields, ['type' => 'dataset'])),
(string) $response->getBody()
);
}
public function testDeleteADatasetFamily(): void
{
$this->addADatasetFamily();
$request = $this->getRequest('DELETE');
$response = ($this->action)($request, new Response(), array('type' => 'dataset', 'id' => 1));
$this->assertSame(
json_encode(array('message' => 'Dataset 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, '/family/dataset/1', array(
'Content-Type' => 'application/json'
));
}
private function addADatasetFamily(): DatasetFamily
{
$family = new DatasetFamily();
$family->setLabel('Default dataset');
$family->setDisplay(10);
$this->entityManager->persist($family);
$this->entityManager->flush();
return $family;
}
}
<?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 App\tests\EntityManagerBuilder;
use App\Entity\DatasetFamily;
final class FamilyListActionTest extends TestCase
{
private $action;
private $entityManager;
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->action = new \App\Action\FamilyListAction($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 testTypeIsNotDefined(): void
{
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Type undifined is not defined');
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('type' => 'undifined'));
$this->assertEquals(400, (int) $response->getStatusCode());
}
public function testGetAllDatasetFamilies(): void
{
$families = $this->addDatasetFamilies();
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('type' => 'dataset'));
$this->assertSame(
json_encode($families),
(string) $response->getBody()
);
}
public function testAddANewFamilyEmptyLabelField(): void
{
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Param label needed to add a new family');
$request = $this->getRequest('POST')->withParsedBody(array());
$response = ($this->action)($request, new Response(), array('type' => 'dataset'));
$this->assertEquals(400, (int) $response->getStatusCode());
}
public function testAddANewDatasetFamily(): void
{
$fields = array(
'label' => 'Default family',
'display' => 10
);
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array('type' => 'dataset'));
$this->assertSame(
json_encode(array_merge(['id' => 1], $fields, ['type' => 'dataset'])),
(string) $response->getBody()
);
$this->assertEquals(201, (int) $response->getStatusCode());
}
public function testAddANewCriteriaFamily(): void
{
$fields = array(
'label' => 'Default criteria',
'display' => 10
);
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array('type' => 'criteria'));
$this->assertSame(
json_encode(array_merge(['id' => 1], $fields, ['type' => 'criteria'])),
(string) $response->getBody()
);
$this->assertEquals(201, (int) $response->getStatusCode());
}
public function testAddANewOutputFamily(): void
{
$fields = array(
'label' => 'Default output',
'display' => 10
);
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array('type' => 'output'));
$this->assertSame(
json_encode(array_merge(['id' => 1], $fields, ['type' => 'output'])),
(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, '/family/dataset', array(
'Content-Type' => 'application/json'
));
}
private function addDatasetFamilies(): array
{
$family1 = new DatasetFamily();
$family1->setLabel('Default dataset');
$family1->setDisplay(10);
$this->entityManager->persist($family1);
$family2 = new DatasetFamily();
$family2->setLabel('My family dataset');
$family2->setDisplay(20);
$this->entityManager->persist($family2);
$this->entityManager->flush();
return array($family1, $family2);
}
}
......@@ -18,8 +18,13 @@ use Nyholm\Psr7\Response;
use Slim\Exception\HttpNotFoundException;
use Slim\Exception\HttpBadRequestException;
use App\tests\EntityManagerBuilder;
use App\Entity\OutputCategory;
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 OutputCategoryActionTest extends TestCase
{
......@@ -123,7 +128,9 @@ final class OutputCategoryActionTest extends TestCase
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);
......@@ -131,6 +138,73 @@ final class OutputCategoryActionTest 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 addAnOutputCategory(): OutputCategory
{
$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