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

Refactor unit tests to remove the database (mock)

parent 48dd80e8
Pipeline #5253 passed with stages
in 2 minutes and 32 seconds
......@@ -73,7 +73,7 @@ final class AttributeDistinctAction extends AbstractAction
// Create query builder with from clause using dataset information
// With select, group by and order by clauses using attribute information
$dataset = $attribute->getDataset();
$column = $attribute->getTableName() . '.' . $attribute->getName();
$column = $dataset->getTableRef() . '.' . $attribute->getName();
$connection = $this->connectionFactory->create($dataset->getProject()->getDatabase());
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder->select($column . ' as ' . $attribute->getLabel());
......
......@@ -40,7 +40,6 @@ final class InstanceListAction extends AbstractAction
}
if ($request->getMethod() === GET) {
// Retrieve user with email adress
$instances = $this->em->getRepository('App\Entity\Instance')->findAll();
$payload = json_encode($instances);
}
......
......@@ -64,7 +64,7 @@ class Option implements \JsonSerializable
$this->select = $select;
}
public function getId(): int
public function getId()
{
return $this->id;
}
......
......@@ -16,15 +16,10 @@ use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
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 Doctrine\ORM\EntityManager;
use Doctrine\Persistence\ObjectRepository;
use App\Entity\CriteriaFamily;
use App\Entity\OutputFamily;
use App\Entity\OutputCategory;
use App\Entity\Dataset;
use App\Entity\Attribute;
final class AttributeActionTest extends TestCase
......@@ -34,7 +29,7 @@ final class AttributeActionTest extends TestCase
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->entityManager = $this->createMock(EntityManager::class);
$this->action = new \App\Action\AttributeAction($this->entityManager);
}
......@@ -45,13 +40,12 @@ final class AttributeActionTest extends TestCase
$this->assertSame($response->getHeaderLine('Access-Control-Allow-Methods'), 'GET, PUT, DELETE, OPTIONS');
}
protected function tearDown(): void
{
$this->entityManager->getConnection()->close();
}
public function testAttributeIsNotFound(): void
{
$repository = $this->createMock(ObjectRepository::class);
$repository->method('findOneBy')->willReturn(null);
$this->entityManager->method('getRepository')->willReturn($repository);
$this->expectException(HttpNotFoundException::class);
$this->expectExceptionMessage('Attribute with dataset name obs_cat and attribute id 1 is not found');
$request = $this->getRequest('GET');
......@@ -61,34 +55,66 @@ final class AttributeActionTest extends TestCase
public function testGetAnAttributeById(): void
{
$attribute = $this->addAnAttribute();
$attribute = $this->createMock(Attribute::class);
$attribute->expects($this->once())->method('jsonSerialize');
$repository = $this->createMock(ObjectRepository::class);
$repository->method('findOneBy')->willReturn($attribute);
$this->entityManager->method('getRepository')->willReturn($repository);
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('name' => 'obs_cat', 'id' => 1));
$this->assertSame(json_encode($attribute), (string) $response->getBody());
($this->action)($request, new Response(), array('name' => 'obs_cat', 'id' => 1));
}
public function testEditADatabase(): void
public function testEditAnAttribute(): void
{
$this->addAnAttribute();
$attribute = $this->createMock(Attribute::class);
$attribute->expects($this->once())->method('jsonSerialize');
$repository = $this->createMock(ObjectRepository::class);
$repository->method('findOneBy')->willReturn($attribute);
$this->entityManager->method('getRepository')->willReturn($repository);
$criteriaFamily = $this->createMock(CriteriaFamily::class);
$outputCategory = $this->createMock(OutputCategory::class);
$this->entityManager->method('find')->willReturnOnConsecutiveCalls($criteriaFamily, $outputCategory);
$this->entityManager->expects($this->once())->method('flush');
$fields = $this->getEditAttributeFields();
$request = $this->getRequest('PUT')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array('name' => 'obs_cat', 'id' => 1));
$this->assertSame(
json_encode(array_merge(['id' => 1, 'name' => 'id'], $fields)),
(string) $response->getBody()
);
($this->action)($request, new Response(), array('name' => 'obs_cat', 'id' => 1));
}
public function testEditADatabaseWithCriteriaFamilyAndOutputCategoryNull(): void
public function testEditAnAttributeWithCriteriaFamilyAndOutputCategoryNull(): void
{
$this->addAnAttribute();
$attribute = $this->createMock(Attribute::class);
$attribute->expects($this->once())->method('jsonSerialize');
$repository = $this->createMock(ObjectRepository::class);
$repository->method('findOneBy')->willReturn($attribute);
$this->entityManager->method('getRepository')->willReturn($repository);
$this->entityManager->expects($this->once())->method('flush');
$fields = $this->getEditAttributeFields();
$fields['id_criteria_family'] = null;
$fields['id_output_category'] = null;
$request = $this->getRequest('PUT')->withParsedBody($fields);
($this->action)($request, new Response(), array('name' => 'obs_cat', 'id' => 1));
}
public function testDeleteAnAttribute(): void
{
$attribute = $this->createMock(Attribute::class);
$attribute->method('getId')->willReturn(1);
$repository = $this->createMock(ObjectRepository::class);
$repository->method('findOneBy')->willReturn($attribute);
$this->entityManager->method('getRepository')->willReturn($repository);
$this->entityManager->expects($this->once())->method('remove')->with($attribute);
$request = $this->getRequest('DELETE');
$response = ($this->action)($request, new Response(), array('name' => 'obs_cat', 'id' => 1));
$this->assertSame(
json_encode(array_merge(['id' => 1, 'name' => 'id'], $fields)),
json_encode(array('message' => 'Attribute with id 1 is removed!')),
(string) $response->getBody()
);
}
......@@ -137,127 +163,4 @@ final class AttributeActionTest extends TestCase
'id_output_category' => 1
);
}
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);
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);
return $family;
}
private function addCriteriaFamily(Dataset $dataset): CriteriaFamily
{
$family = new CriteriaFamily($dataset);
$family->setLabel('Default criteria');
$family->setDisplay(10);
$this->entityManager->persist($family);
$this->entityManager->flush();
return $family;
}
private function addOutputCategory(Dataset $dataset): OutputCategory
{
$outputFamily = $this->addOutputFamily($dataset);
$outputCategory = new OutputCategory();
$outputCategory->setLabel('Default output category');
$outputCategory->setDisplay(10);
$outputCategory->setOutputFamily($outputFamily);
$this->entityManager->persist($outputCategory);
$this->entityManager->flush();
return $outputCategory;
}
private function addOutputFamily(Dataset $dataset): OutputFamily
{
$family = new OutputFamily($dataset);
$family->setLabel('Default output');
$family->setDisplay(10);
$this->entityManager->persist($family);
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;
}
private function addAnAttribute(): Attribute
{
$dataset = $this->addADataset();
$this->addCriteriaFamily($dataset);
$this->addOutputCategory($dataset);
$attribute = new Attribute(1, $dataset);
$attribute->setName('id');
$attribute->setLabel('ID');
$attribute->setFormLabel('ID');
$attribute->setType('integer');
$attribute->setCriteriaDisplay(10);
$attribute->setOutputDisplay(10);
$attribute->setDisplayDetail(10);
$attribute->setOrderDisplay(10);
$attribute->setSelected(true);
$this->entityManager->persist($attribute);
$this->entityManager->flush();
return $attribute;
}
}
......@@ -15,20 +15,31 @@ namespace App\Tests\Action;
use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
use App\tests\EntityManagerBuilder;
use Slim\Exception\HttpNotFoundException;
use Doctrine\ORM\EntityManager;
use Doctrine\Persistence\ObjectRepository;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Driver\ResultStatement;
use App\Search\DBALConnectionFactory;
use App\Entity\Attribute;
use App\Entity\Database;
use App\Entity\Dataset;
use App\Entity\Project;
final class AttributeDistinctActionTest extends TestCase
{
private $action;
private $entityManager;
private $connectionFactory;
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->entityManager = $this->createMock(EntityManager::class);
$this->connectionFactory = $this->createMock(DBALConnectionFactory::class);
$this->action = new \App\Action\AttributeDistinctAction(
$this->entityManager,
$this->createMock(DBALConnectionFactory::class)
$this->connectionFactory
);
}
......@@ -39,9 +50,48 @@ final class AttributeDistinctActionTest extends TestCase
$this->assertSame($response->getHeaderLine('Access-Control-Allow-Methods'), 'GET, OPTIONS');
}
protected function tearDown(): void
public function testAttributeIsNotFound(): void
{
$this->entityManager->getConnection()->close();
$repository = $this->createMock(ObjectRepository::class);
$repository->method('findOneBy')->willReturn(null);
$this->entityManager->method('getRepository')->willReturn($repository);
$this->expectException(HttpNotFoundException::class);
$this->expectExceptionMessage('Attribute with dataset name obs_cat and attribute id 1 is not found');
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('name' => 'obs_cat', 'id' => 1));
$this->assertEquals(404, (int) $response->getStatusCode());
}
public function testGetDistinctValues(): void
{
$database = $this->createMock(Database::class);
$project = $this->createMock(Project::class);
$project->method('getDatabase')->willReturn($database);
$dataset = $this->createMock(Dataset::class);
$dataset->method('getProject')->willReturn($project);
$attribute = $this->createMock(Attribute::class);
$attribute->method('getDataset')->willReturn($dataset);
$repository = $this->createMock(ObjectRepository::class);
$repository->method('findOneBy')->willReturn($attribute);
$this->entityManager->method('getRepository')->willReturn($repository);
$stmt = $this->createMock(ResultStatement::class);
$stmt->method('fetchAll')->willReturn(array('value1', 'value2'));
$queryBuilder = $this->createMock(QueryBuilder::class);
$queryBuilder->method('execute')->willReturn($stmt);
$connection = $this->createMock(Connection::class);
$connection->method('createQueryBuilder')->willReturn($queryBuilder);
$this->connectionFactory->method('create')->willReturn($connection);
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('name' => 'obs_cat', 'id' => 1));
$this->assertSame(
'["value1","value2"]',
(string) $response->getBody()
);
}
private function getRequest(string $method): ServerRequest
......
......@@ -16,13 +16,10 @@ use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
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 Slim\Exception\HttpBadRequestException;
use Doctrine\ORM\EntityManager;
use Doctrine\Persistence\ObjectRepository;
use App\Entity\Dataset;
use App\Entity\Attribute;
final class AttributeListActionTest extends TestCase
{
......@@ -31,7 +28,7 @@ final class AttributeListActionTest extends TestCase
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->entityManager = $this->createMock(EntityManager::class);
$this->action = new \App\Action\AttributeListAction($this->entityManager);
}
......@@ -53,122 +50,61 @@ final class AttributeListActionTest extends TestCase
public function testGetAllAttributesOfADataset(): void
{
$attributes = $this->addAttributes();
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('name' => 'obs_cat'));
$this->assertSame(
json_encode($attributes),
(string) $response->getBody()
);
}
protected function tearDown(): void
{
$this->entityManager->getConnection()->close();
}
private function getRequest(string $method): ServerRequest
{
return new ServerRequest($method, '/dataset/obs_cat/attribute', array(
'Content-Type' => 'application/json'
));
}
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);
$dataset = $this->createMock(Dataset::class);
$this->entityManager->method('find')->willReturn($dataset);
return $project;
}
$repository = $this->createMock(ObjectRepository::class);
$repository->expects($this->once())->method('findBy')->with(
array('dataset' => $dataset),
array('id' => 'ASC')
);
$this->entityManager->method('getRepository')->with('App\Entity\Attribute')->willReturn($repository);
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;
$request = $this->getRequest('GET');
($this->action)($request, new Response(), array('name' => 'obs_cat'));
}
private function addDatasetFamily(): DatasetFamily
public function testAddNewAttributeEmptyIdField(): void
{
$instance = $this->addInstance();
$dataset = $this->createMock(Dataset::class);
$this->entityManager->method('find')->willReturn($dataset);
$family = new DatasetFamily($instance);
$family->setLabel('Default dataset');
$family->setDisplay(10);
$this->entityManager->persist($family);
return $family;
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Param id needed to add a new attribute');
$request = $this->getRequest('POST')->withParsedBody(array());
$response = ($this->action)($request, new Response(), array('name' => 'obs_cat'));
$this->assertSame(201, $response->getStatusCode());
}
private function addADataset(): Dataset
public function testAddNewAttribute(): void
{
$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();
$dataset = $this->createMock(Dataset::class);
$this->entityManager->method('find')->willReturn($dataset);
$fields = array(
'id' => 1,
'name' => 'id',
'label' => 'id',
'form_label' => 'ID',
'type' => 'integer',
'criteria_display' => 10,
'output_display' => 10,
'display_detail' => 10,
'order_display' => 10,
'selected' => true,
'order_by' => false,
'detail' => false
);
$this->entityManager->expects($this->once())->method('persist');
return $dataset;
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array('name' => 'obs_cat'));
$this->assertSame(201, $response->getStatusCode());
}
private function addAttributes(): array
private function getRequest(string $method): ServerRequest
{
$dataset = $this->addADataset();
$attribute1 = new Attribute(1, $dataset);
$attribute1->setName('id');
$attribute1->setLabel('ID');
$attribute1->setFormLabel('ID');
$attribute1->setType('integer');
$attribute1->setCriteriaDisplay(10);
$attribute1->setOutputDisplay(10);
$attribute1->setDisplayDetail(10);
$attribute1->setOrderDisplay(10);
$attribute1->setSelected(true);
$this->entityManager->persist($attribute1);
$attribute2 = new Attribute(2, $dataset);
$attribute2->setName('ra');
$attribute2->setLabel('RA');
$attribute2->setFormLabel('RA');
$attribute2->setType('float');
$attribute2->setCriteriaDisplay(20);
$attribute2->setOutputDisplay(20);
$attribute2->setDisplayDetail(20);
$attribute2->setOrderDisplay(20);
$attribute2->setSelected(true);
$this->entityManager->persist($attribute2);
$this->entityManager->flush();
return array($attribute1, $attribute2);
return new ServerRequest($method, '/dataset/obs_cat/attribute', array(
'Content-Type' => 'application/json'
));
}
}
......@@ -17,12 +17,7 @@ use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
use Slim\Exception\HttpNotFoundException;
use Slim\Exception\HttpBadRequestException;
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 Doctrine\ORM\EntityManager;
use App\Entity\CriteriaFamily;
final class CriteriaFamilyActionTest extends TestCase
......@@ -32,7 +27,7 @@ final class CriteriaFamilyActionTest extends TestCase
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->entityManager = $this->createMock(EntityManager::class);
$this->action = new \App\Action\CriteriaFamilyAction($this->entityManager);
}
......@@ -54,15 +49,19 @@ final class CriteriaFamilyActionTest extends TestCase
public function testGetACriteriaFamilyById(): void
{
$criteriaFamily = $this->addACriteriaFamily();
$criteriaFamily = $this->createMock(CriteriaFamily::class);
$criteriaFamily->expects($this->once())->method('jsonSerialize');
$this->entityManager->method('find')->willReturn($criteriaFamily);