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 ...@@ -73,7 +73,7 @@ final class AttributeDistinctAction extends AbstractAction
// Create query builder with from clause using dataset information // Create query builder with from clause using dataset information
// With select, group by and order by clauses using attribute information // With select, group by and order by clauses using attribute information
$dataset = $attribute->getDataset(); $dataset = $attribute->getDataset();
$column = $attribute->getTableName() . '.' . $attribute->getName(); $column = $dataset->getTableRef() . '.' . $attribute->getName();
$connection = $this->connectionFactory->create($dataset->getProject()->getDatabase()); $connection = $this->connectionFactory->create($dataset->getProject()->getDatabase());
$queryBuilder = $connection->createQueryBuilder(); $queryBuilder = $connection->createQueryBuilder();
$queryBuilder->select($column . ' as ' . $attribute->getLabel()); $queryBuilder->select($column . ' as ' . $attribute->getLabel());
......
...@@ -40,7 +40,6 @@ final class InstanceListAction extends AbstractAction ...@@ -40,7 +40,6 @@ final class InstanceListAction extends AbstractAction
} }
if ($request->getMethod() === GET) { if ($request->getMethod() === GET) {
// Retrieve user with email adress
$instances = $this->em->getRepository('App\Entity\Instance')->findAll(); $instances = $this->em->getRepository('App\Entity\Instance')->findAll();
$payload = json_encode($instances); $payload = json_encode($instances);
} }
......
...@@ -64,7 +64,7 @@ class Option implements \JsonSerializable ...@@ -64,7 +64,7 @@ class Option implements \JsonSerializable
$this->select = $select; $this->select = $select;
} }
public function getId(): int public function getId()
{ {
return $this->id; return $this->id;
} }
......
...@@ -16,15 +16,10 @@ use PHPUnit\Framework\TestCase; ...@@ -16,15 +16,10 @@ use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\ServerRequest; use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response; use Nyholm\Psr7\Response;
use Slim\Exception\HttpNotFoundException; use Slim\Exception\HttpNotFoundException;
use App\tests\EntityManagerBuilder; use Doctrine\ORM\EntityManager;
use App\Entity\Database; use Doctrine\Persistence\ObjectRepository;
use App\Entity\Project;
use App\Entity\Instance;
use App\Entity\DatasetFamily;
use App\Entity\CriteriaFamily; use App\Entity\CriteriaFamily;
use App\Entity\OutputFamily;
use App\Entity\OutputCategory; use App\Entity\OutputCategory;
use App\Entity\Dataset;
use App\Entity\Attribute; use App\Entity\Attribute;
final class AttributeActionTest extends TestCase final class AttributeActionTest extends TestCase
...@@ -34,7 +29,7 @@ final class AttributeActionTest extends TestCase ...@@ -34,7 +29,7 @@ final class AttributeActionTest extends TestCase
protected function setUp(): void protected function setUp(): void
{ {
$this->entityManager = EntityManagerBuilder::getInstance(); $this->entityManager = $this->createMock(EntityManager::class);
$this->action = new \App\Action\AttributeAction($this->entityManager); $this->action = new \App\Action\AttributeAction($this->entityManager);
} }
...@@ -45,13 +40,12 @@ final class AttributeActionTest extends TestCase ...@@ -45,13 +40,12 @@ final class AttributeActionTest extends TestCase
$this->assertSame($response->getHeaderLine('Access-Control-Allow-Methods'), 'GET, PUT, DELETE, OPTIONS'); $this->assertSame($response->getHeaderLine('Access-Control-Allow-Methods'), 'GET, PUT, DELETE, OPTIONS');
} }
protected function tearDown(): void
{
$this->entityManager->getConnection()->close();
}
public function testAttributeIsNotFound(): void 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->expectException(HttpNotFoundException::class);
$this->expectExceptionMessage('Attribute with dataset name obs_cat and attribute id 1 is not found'); $this->expectExceptionMessage('Attribute with dataset name obs_cat and attribute id 1 is not found');
$request = $this->getRequest('GET'); $request = $this->getRequest('GET');
...@@ -61,34 +55,66 @@ final class AttributeActionTest extends TestCase ...@@ -61,34 +55,66 @@ final class AttributeActionTest extends TestCase
public function testGetAnAttributeById(): void 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'); $request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('name' => 'obs_cat', 'id' => 1)); ($this->action)($request, new Response(), array('name' => 'obs_cat', 'id' => 1));
$this->assertSame(json_encode($attribute), (string) $response->getBody());
} }
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(); $fields = $this->getEditAttributeFields();
$request = $this->getRequest('PUT')->withParsedBody($fields); $request = $this->getRequest('PUT')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array('name' => 'obs_cat', 'id' => 1)); ($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()
);
} }
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 = $this->getEditAttributeFields();
$fields['id_criteria_family'] = null; $fields['id_criteria_family'] = null;
$fields['id_output_category'] = null; $fields['id_output_category'] = null;
$request = $this->getRequest('PUT')->withParsedBody($fields); $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)); $response = ($this->action)($request, new Response(), array('name' => 'obs_cat', 'id' => 1));
$this->assertSame( $this->assertSame(
json_encode(array_merge(['id' => 1, 'name' => 'id'], $fields)), json_encode(array('message' => 'Attribute with id 1 is removed!')),
(string) $response->getBody() (string) $response->getBody()
); );
} }
...@@ -137,127 +163,4 @@ final class AttributeActionTest extends TestCase ...@@ -137,127 +163,4 @@ final class AttributeActionTest extends TestCase
'id_output_category' => 1 '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; ...@@ -15,20 +15,31 @@ namespace App\Tests\Action;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\ServerRequest; use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response; 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\Search\DBALConnectionFactory;
use App\Entity\Attribute;
use App\Entity\Database;
use App\Entity\Dataset;
use App\Entity\Project;
final class AttributeDistinctActionTest extends TestCase final class AttributeDistinctActionTest extends TestCase
{ {
private $action; private $action;
private $entityManager; private $entityManager;
private $connectionFactory;
protected function setUp(): void 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->action = new \App\Action\AttributeDistinctAction(
$this->entityManager, $this->entityManager,
$this->createMock(DBALConnectionFactory::class) $this->connectionFactory
); );
} }
...@@ -39,9 +50,48 @@ final class AttributeDistinctActionTest extends TestCase ...@@ -39,9 +50,48 @@ final class AttributeDistinctActionTest extends TestCase
$this->assertSame($response->getHeaderLine('Access-Control-Allow-Methods'), 'GET, OPTIONS'); $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 private function getRequest(string $method): ServerRequest
......
...@@ -16,13 +16,10 @@ use PHPUnit\Framework\TestCase; ...@@ -16,13 +16,10 @@ use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\ServerRequest; use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response; use Nyholm\Psr7\Response;
use Slim\Exception\HttpNotFoundException; use Slim\Exception\HttpNotFoundException;
use App\tests\EntityManagerBuilder; use Slim\Exception\HttpBadRequestException;
use App\Entity\Database; use Doctrine\ORM\EntityManager;
use App\Entity\Project; use Doctrine\Persistence\ObjectRepository;
use App\Entity\Instance;
use App\Entity\DatasetFamily;
use App\Entity\Dataset; use App\Entity\Dataset;
use App\Entity\Attribute;
final class AttributeListActionTest extends TestCase final class AttributeListActionTest extends TestCase
{ {
...@@ -31,7 +28,7 @@ final class AttributeListActionTest extends TestCase ...@@ -31,7 +28,7 @@ final class AttributeListActionTest extends TestCase
protected function setUp(): void protected function setUp(): void
{ {
$this->entityManager = EntityManagerBuilder::getInstance(); $this->entityManager = $this->createMock(EntityManager::class);
$this->action = new \App\Action\AttributeListAction($this->entityManager); $this->action = new \App\Action\AttributeListAction($this->entityManager);
} }
...@@ -53,122 +50,61 @@ final class AttributeListActionTest extends TestCase ...@@ -53,122 +50,61 @@ final class AttributeListActionTest extends TestCase
public function testGetAllAttributesOfADataset(): void public function testGetAllAttributesOfADataset(): void
{ {
$attributes = $this->addAttributes(); $dataset = $this->createMock(Dataset::class);
$request = $this->getRequest('GET'); $this->entityManager->method('find')->willReturn($dataset);
$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);
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 $request = $this->getRequest('GET');
{ ($this->action)($request, new Response(), array('name' => 'obs_cat'));
$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 public function testAddNewAttributeEmptyIdField(): void
{ {
$instance = $this->addInstance(); $dataset = $this->createMock(Dataset::class);
$this->entityManager->method('find')->willReturn($dataset);
$family = new DatasetFamily($instance); $this->expectException(HttpBadRequestException::class);
$family->setLabel('Default dataset'); $this->expectExceptionMessage('Param id needed to add a new attribute');
$family->setDisplay(10); $request = $this->getRequest('POST')->withParsedBody(array());
$this->entityManager->persist($family); $response = ($this->action)($request, new Response(), array('name' => 'obs_cat'));
$this->assertSame(201, $response->getStatusCode());
return $family;
} }
private function addADataset(): Dataset public function testAddNewAttribute(): void
{ {
$project = $this->addProject(); $dataset = $this->createMock(Dataset::class);
$family = $this->addDatasetFamily(); $this->entityManager->method('find')->willReturn($dataset);
$fields = array(
$dataset = new Dataset('obs_cat'); 'id' => 1,
$dataset->setTableRef('v_obs_cat'); 'name' => 'id',
$dataset->setLabel('Obscat label'); 'label' => 'id',
$dataset->setDescription('Obscat description'); 'form_label' => 'ID',
$dataset->setDisplay(10); 'type' => 'integer',
$dataset->setCount(10000); 'criteria_display' => 10,
$dataset->setVo(false); 'output_display' => 10,
$dataset->setDataPath('/mnt/obs_cat'); 'display_detail' => 10,
$dataset->setPublic(true); 'order_display' => 10,
$dataset->setProject($project); 'selected' => true,
$dataset->setDatasetFamily($family); 'order_by' => false,
$this->entityManager->