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

GroupActionTest => ok

parent 17988738
<?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\Group;
use App\Entity\Database;
use App\Entity\Project;
use App\Entity\Instance;
use App\Entity\DatasetFamily;
use App\Entity\Dataset;
final class GroupActionTest extends TestCase
{
private $action;
private $entityManager;
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->action = new \App\Action\GroupAction($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 testGroupIsNotFound(): void
{
$this->expectException(HttpNotFoundException::class);
$this->expectExceptionMessage('Group 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 testGetAGroupById(): void
{
$group = $this->addAGroup();
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertSame(json_encode($group), (string) $response->getBody());
}
public function testEditAGroupEmptyLabelField(): void
{
$this->addAGroup();
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Param label needed to edit the group');
$request = $this->getRequest('PUT')->withParsedBody(array());
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertEquals(400, (int) $response->getStatusCode());
}
public function testEditAGroupWithoutDataset(): void
{
$fields = array(
'label' => 'New_label',
'datasets' => array()
);
$this->addAGroup();
$request = $this->getRequest('PUT')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertSame(json_encode(array_merge(['id' => 1], $fields)), (string) $response->getBody());
}
public function testEditAGroupWithDataset(): void
{
$dataset = $this->addADataset();
$fields = array(
'label' => 'New_label',
'datasets' => array($dataset->getName())
);
$this->addAGroup();
$request = $this->getRequest('PUT')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertSame(json_encode(array_merge(['id' => 1], $fields)), (string) $response->getBody());
}
public function testDeleteAGroup(): void
{
$this->addAGroup();
$request = $this->getRequest('DELETE');
$response = ($this->action)($request, new Response(), array('id' => 1));
$this->assertSame(
json_encode(array('message' => 'Group 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, '/group/1', array(
'Content-Type' => 'application/json'
));
}
private function addAGroup(): Group
{
$group = new Group();
$group->setLabel('Group1');
$group->setDatasets(array());
$this->entityManager->persist($group);
$this->entityManager->flush();
return $group;
}
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;
}
}
......@@ -82,7 +82,7 @@ final class GroupListActionTest extends TestCase
$dataset = $this->addADataset();
$fields = array(
'label' => 'group1',
'datasets' => [$dataset->getName()]
'datasets' => array($dataset->getName())
);
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array());
......
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