Commit 17988738 authored by François Agneray's avatar François Agneray

GroupListActionTest => ok

parent 38db59d9
......@@ -45,7 +45,7 @@ final class GroupListAction extends AbstractAction
$parsedBody = $request->getParsedBody();
// To work this action needs group information
foreach (array('label') as $a) {
foreach (array('label', 'datasets') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
throw new HttpBadRequestException(
$request,
......@@ -70,10 +70,9 @@ final class GroupListAction extends AbstractAction
*/
private function postGroup(array $parsedBody): Group
{
$group = new Group(
$this->getDatasets($parsedBody['datasets'])
);
$group = new Group();
$group->setLabel($parsedBody['label']);
$group->setDatasets($this->getDatasets($parsedBody['datasets']));
$this->em->persist($group);
$this->em->flush();
......
......@@ -12,9 +12,6 @@ declare(strict_types=1);
namespace App\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity
* @Table(name="anis_group")
......@@ -49,11 +46,6 @@ class Group implements \JsonSerializable
*/
protected $datasets;
public function __construct(array $datasets)
{
$this->datasets = new ArrayCollection($datasets);
}
public function getId()
{
return $this->id;
......
<?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\Group;
use App\Entity\Database;
use App\Entity\Project;
use App\Entity\Instance;
use App\Entity\DatasetFamily;
use App\Entity\Dataset;
final class GroupListActionTest extends TestCase
{
private $action;
private $entityManager;
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->action = new \App\Action\GroupListAction($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 testGetAllGroups(): void
{
$groups = $this->addGroups();
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array());
$this->assertSame(
json_encode($groups),
(string) $response->getBody()
);
}
public function testAddANewGroupEmptyLabelField(): void
{
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Param label needed to add a new group');
$request = $this->getRequest('POST')->withParsedBody(array());
$response = ($this->action)($request, new Response(), array());
$this->assertEquals(400, (int) $response->getStatusCode());
}
public function testAddANewGroupWithoutDataset(): void
{
$fields = array(
'label' => 'group1',
'datasets' => []
);
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array());
$this->assertSame(
json_encode(array_merge(['id' => 1], $fields)),
(string) $response->getBody()
);
$this->assertEquals(201, (int) $response->getStatusCode());
}
public function testAddANewGroupWithDataset(): void
{
$dataset = $this->addADataset();
$fields = array(
'label' => 'group1',
'datasets' => [$dataset->getName()]
);
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array());
$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, '/group', array(
'Content-Type' => 'application/json'
));
}
private function addGroups(): array
{
$group = new Group();
$group->setLabel('Group1');
$group->setDatasets(array());
$this->entityManager->persist($group);
$group2 = new Group();
$group2->setLabel('Group2');
$group2->setDatasets(array());
$this->entityManager->persist($group2);
$this->entityManager->flush();
return array($group, $group2);
}
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->setPublic(true);
$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