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

Update and running tests

parent 206be0aa
......@@ -53,7 +53,7 @@ phpunit:
phpcs:
@docker run --init -it --rm --user $(UID):$(GID) \
-v $(CURDIR):/project \
-w /project jakzal/phpqa phpcs --standard=PSR2 --extensions=php --colors src
-w /project jakzal/phpqa phpcs --standard=PSR2 --extensions=php --colors src tests
anis-init:
@docker-compose exec php sh ./conf-dev/anis_init.sh
......
......@@ -73,8 +73,12 @@ final class DatasetListAction
* @param DBALConnectionFactory $dcf
* @param string $encryptionKey
*/
public function __construct(LoggerInterface $logger, MetaEntityManagerFactory $memf, DBALConnectionFactory $dcf, string $encryptionKey)
{
public function __construct(
LoggerInterface $logger,
MetaEntityManagerFactory $memf,
DBALConnectionFactory $dcf,
string $encryptionKey
) {
$this->logger = $logger;
$this->memf = $memf;
$this->dcf = $dcf;
......@@ -200,7 +204,7 @@ final class DatasetListAction
/**
* Access to the business database dans get all the columns of the table (table_ref)
* and makes the corresponding doctrine attribute objects
*
*
* @param Dataset $dataset Contains the new dataset doctrine object
*/
private function postAttributes(Dataset $dataset): void
......
......@@ -121,7 +121,7 @@ final class FamilyAction
private function getEntityClass(string $type): string
{
return 'App\Entity\\' . ucfirst($type) . 'Family';
return 'App\Entity\Metamodel\\' . ucfirst($type) . 'Family';
}
private function editFamily(Object $family, array $parsedBody): void
......
......@@ -43,7 +43,7 @@ final class FileListAction
// Create the doctrine Entity Manager object for the instance metamodel database
$this->memf->createMetaEntityManager($args['instance']);
$dataset = $this->memf->getMetaEntityManager()->find('App\Entity\Dataset', $args['name']);
$dataset = $this->memf->getMetaEntityManager()->find('App\Entity\Metamodel\Dataset', $args['name']);
if (array_key_exists('type', $request->getQueryParams())) {
$type = $request->getQueryParams()['type'];
}
......@@ -63,7 +63,7 @@ final class FileListAction
if (isset($type)) {
$types['type'] = $type;
}
$files = $this->memf->getMetaEntityManager()->getRepository('App\Entity\File')->findBy($types);
$files = $this->memf->getMetaEntityManager()->getRepository('App\Entity\Metamodel\File')->findBy($types);
$newResponse = $response->withJson($files);
}
......
......@@ -22,7 +22,7 @@ use App\Entity\Metamodel\OutputFamily;
/**
* Route: /metadata/output-category/{id}
* {id}: Output category id
*
*
* This action is used to manage one output category
*
* @author François Agneray <francois.agneray@lam.fr>
......@@ -147,8 +147,11 @@ final class OutputCategoryAction
* @param string[] $parsedBody Contains the new values ​​of the output category sent by the user
* @param OutputFamily $outputFamily Contains the output family doctrine object to set to the output category
*/
private function editOutputCategory(OutputCategory $outputCategory, array $parsedBody, OutputFamily $outputFamily): void
{
private function editOutputCategory(
OutputCategory $outputCategory,
array $parsedBody,
OutputFamily $outputFamily
): void {
$outputCategory->setLabel($parsedBody['label']);
$outputCategory->setDisplay($parsedBody['display']);
$outputCategory->setOutputFamily($outputFamily);
......
......@@ -44,7 +44,7 @@ final class ProjectListAction
if ($request->isGet()) {
$projects = $this->memf->getMetaEntityManager()->getRepository('App\Entity\Metamodel\Project')->findAll();
$newResponse = $response->withJson($projects);
$newResponse = $response->withJson($projects, 200, JSON_UNESCAPED_SLASHES);
}
if ($request->isPost()) {
......@@ -75,7 +75,7 @@ final class ProjectListAction
}
$project = $this->postProject($parsedBody, $database);
$newResponse = $response->withJson($project, 201);
$newResponse = $response->withJson($project, 201, JSON_UNESCAPED_SLASHES);
}
return $newResponse;
......
......@@ -55,7 +55,7 @@ final class TableListAction
}
$this->memf->createMetaEntityManager($args['instance']);
$database = $this->memf->getMetaEntityManager()->find('App\Entity\Database', $args['id']);
$database = $this->memf->getMetaEntityManager()->find('App\Entity\Metamodel\Database', $args['id']);
if (is_null($database)) {
return $this->dispatchHttpError(
......
......@@ -44,8 +44,13 @@ class Between extends Operator
* @param string $value1
* @param string $value2
*/
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value1, string $value2)
{
public function __construct(
ExpressionBuilder $expr,
string $column,
string $columnType,
string $value1,
string $value2
) {
parent::__construct($expr, $column, $columnType);
$this->verifyTypeCompatibility($value1);
$this->verifyTypeCompatibility($value2);
......
......@@ -31,5 +31,11 @@ interface IOperatorFactory
*
* @return IOperator
*/
public function create(string $type, ExpressionBuilder $expr, string $column, string $columnType, array $parameters): IOperator;
public function create(
string $type,
ExpressionBuilder $expr,
string $column,
string $columnType,
array $parameters
): IOperator;
}
......@@ -40,8 +40,10 @@ class OperatorException extends SearchException
*
* @return OperatorException
*/
public static function operatorBadNumberOfParameters(string $operator, int $numberOfParametersRequired): OperatorException
{
public static function operatorBadNumberOfParameters(
string $operator,
int $numberOfParametersRequired
): OperatorException {
return new self("Operator '" . $operator . "' needs " . $numberOfParametersRequired . ' values to work');
}
......
<?php
declare(strict_types=1);
namespace App\Tests;
use PHPUnit\DbUnit\TestCase;
use PHPUnit\DbUnit\DataSet\YamlDataSet;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
abstract class AbstractActionTestCase extends TestCase
abstract class AbstractActionAdminTestCase extends TestCase
{
protected $entityManager;
protected $mailer;
......@@ -26,7 +28,7 @@ abstract class AbstractActionTestCase extends TestCase
protected function getConnection()
{
$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/../src/Entity"), $isDevMode);
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/../src/Entity/Admin"), $isDevMode);
$conn = array(
'driver' => 'pdo_sqlite',
'memory' => true
......@@ -40,6 +42,6 @@ abstract class AbstractActionTestCase extends TestCase
protected function getDataSet()
{
return new YamlDataset(__DIR__ . "/database.yaml");
return new YamlDataset(__DIR__ . "/admin.yaml");
}
}
\ No newline at end of file
}
<?php
declare(strict_types=1);
namespace App\Tests;
use PHPUnit\DbUnit\TestCase;
use PHPUnit\DbUnit\DataSet\YamlDataSet;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use App\Utils\MetaEntityManagerFactory;
abstract class AbstractActionMetaTestCase extends TestCase
{
protected $metaEntityManagerFactory;
protected $entityManager;
protected $mailer;
protected $action;
protected function setUp(): void
{
parent::setUp();
$transport = new \Swift_Transport_SpoolTransport(
new \Swift_Events_SimpleEventDispatcher(),
new \Swift_MemorySpool
);
$this->mailer = new \Swift_Mailer($transport);
$this->metaEntityManagerFactory = $this->getMockBuilder(MetaEntityManagerFactory::class)
->disableOriginalConstructor()
->setMethods(['getMetaEntityManager', 'createMetaEntityManager'])
->getMock();
$this->metaEntityManagerFactory->method('getMetaEntityManager')->willReturn($this->entityManager);
}
protected function getConnection()
{
$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/../src/Entity/Metamodel"), $isDevMode);
$conn = array(
'driver' => 'pdo_sqlite',
'memory' => true
);
$this->entityManager = EntityManager::create($conn, $config);
$schemaTool = new SchemaTool($this->entityManager);
$schemaTool->createSchema($this->entityManager->getMetadataFactory()->getAllMetadata());
$pdo = $this->entityManager->getConnection()->getWrappedConnection();
return $this->createDefaultDBConnection($pdo, ':memory');
}
protected function getDataSet()
{
return new YamlDataset(__DIR__ . "/database.yaml");
}
}
<?php
declare(strict_types=1);
namespace App\Tests\Action\Login;
use Psr\Http\Message\ServerRequestInterface as Request;
final class ActivateAccountActionTest extends AbstractActionTestCase
use App\Tests\AbstractActionAdminTestCase;
final class ActivateAccountActionTest extends AbstractActionAdminTestCase
{
protected function setUp(): void
{
......@@ -74,6 +78,9 @@ final class ActivateAccountActionTest extends AbstractActionTestCase
$request = $this->getRequest('email=user1@anis.fr&activation_key=noioioi');
$response = ($this->action)($request, new \Slim\Http\Response(), array());
$this->assertEquals(200, (int) $response->getStatusCode());
$this->assertSame((string) $response->getBody(), '{"email":"user1@anis.fr","activated":true,"adminsi":false,"superuser":false,"id_group":1}');
$this->assertSame(
(string) $response->getBody(),
'{"email":"user1@anis.fr","activated":true,"adminsi":false,"superuser":false}'
);
}
}
\ No newline at end of file
}
<?php
declare(strict_types=1);
namespace App\Tests\Action\Login;
use Psr\Http\Message\ServerRequestInterface as Request;
final class ChangePasswordActionTest extends AbstractActionTestCase
use App\Tests\AbstractActionAdminTestCase;
final class ChangePasswordActionTest extends AbstractActionAdminTestCase
{
protected function setUp(): void
{
......@@ -60,7 +64,11 @@ final class ChangePasswordActionTest extends AbstractActionTestCase
public function testUserDoNotExist(): void
{
$request = $this->getRequest(['email' => 'nobody@anis.fr', 'password' => 'password', 'new_password' => 'new_password']);
$request = $this->getRequest(array(
'email' => 'nobody@anis.fr',
'password' => 'password',
'new_password' => 'new_password'
));
$response = ($this->action)($request, new \Slim\Http\Response(), array());
$this->assertEquals(400, (int) $response->getStatusCode());
$this->assertSame((string) $response->getBody(), json_encode(array(
......@@ -71,7 +79,11 @@ final class ChangePasswordActionTest extends AbstractActionTestCase
public function testAccountNotActivated(): void
{
$request = $this->getRequest(['email' => 'user1@anis.fr', 'password' => 'password', 'new_password' => 'new_password']);
$request = $this->getRequest(array(
'email' => 'user1@anis.fr',
'password' => 'password',
'new_password' => 'new_password'
));
$response = ($this->action)($request, new \Slim\Http\Response(), array());
$this->assertEquals(400, (int) $response->getStatusCode());
$this->assertSame((string) $response->getBody(), json_encode(array(
......@@ -82,7 +94,11 @@ final class ChangePasswordActionTest extends AbstractActionTestCase
public function testBadPassword(): void
{
$request = $this->getRequest(['email' => 'user2@anis.fr', 'password' => 'badpassword', 'new_password' => 'new_password']);
$request = $this->getRequest(array(
'email' => 'user2@anis.fr',
'password' => 'badpassword',
'new_password' => 'new_password'
));
$response = ($this->action)($request, new \Slim\Http\Response(), array());
$this->assertEquals(400, (int) $response->getStatusCode());
$this->assertSame((string) $response->getBody(), json_encode(array(
......@@ -93,7 +109,11 @@ final class ChangePasswordActionTest extends AbstractActionTestCase
public function testChangePassword(): void
{
$request = $this->getRequest(['email' => 'user2@anis.fr', 'password' => 'password', 'new_password' => 'new_password']);
$request = $this->getRequest(array(
'email' => 'user2@anis.fr',
'password' => 'password',
'new_password' => 'new_password'
));
$response = ($this->action)($request, new \Slim\Http\Response(), array());
$this->assertEquals(200, (int) $response->getStatusCode());
$this->assertSame((string) $response->getBody(), json_encode(array('message' => 'Password changed!')));
......
<?php
declare(strict_types=1);
namespace App\Tests\Action\Login;
use Psr\Http\Message\ServerRequestInterface as Request;
final class NewPasswordActionTest extends AbstractActionTestCase
use App\Tests\AbstractActionAdminTestCase;
final class NewPasswordActionTest extends AbstractActionAdminTestCase
{
protected function setUp(): void
{
......@@ -22,7 +26,7 @@ final class NewPasswordActionTest extends AbstractActionTestCase
'REQUEST_URI' => '/new-password',
'CONTENT_TYPE' => 'application/json',
]);
return \Slim\Http\Request::createFromEnvironment($environment)->withParsedBody($parsedBody);;
return \Slim\Http\Request::createFromEnvironment($environment)->withParsedBody($parsedBody);
}
public function testEmailIsEmpty(): void
......
<?php
declare(strict_types=1);
namespace App\Tests\Action\Login;
use Psr\Http\Message\ServerRequestInterface as Request;
final class RegisterActionTest extends AbstractActionTestCase
use App\Tests\AbstractActionAdminTestCase;
final class RegisterActionTest extends AbstractActionAdminTestCase
{
protected function setUp(): void
{
......@@ -69,9 +73,10 @@ final class RegisterActionTest extends AbstractActionTestCase
]);
$response = ($this->action)($request, new \Slim\Http\Response(), array());
$this->assertEquals(400, (int) $response->getStatusCode());
$error_description = 'Bad email adress; a well-defined email address is needed to finalize the registration';
$this->assertSame((string) $response->getBody(), json_encode(array(
'error' => 'Invalid email',
'error_description' => 'Bad email adress; a well-defined email address is needed to finalize the registration'
'error_description' => $error_description
)));
}
......@@ -84,7 +89,10 @@ final class RegisterActionTest extends AbstractActionTestCase
]);
$response = ($this->action)($request, new \Slim\Http\Response(), array());
$this->assertEquals(201, (int) $response->getStatusCode());
$this->assertSame((string) $response->getBody(), '{"email":"test@anis.fr","activated":false,"adminsi":false,"superuser":false,"id_group":1}');
$this->assertSame(
(string) $response->getBody(),
'{"email":"test@anis.fr","activated":false,"adminsi":false,"superuser":false}'
);
$this->assertSame(3, $this->getDatabaseTester()->getConnection()->getRowCount('anis_user'));
}
}
\ No newline at end of file
}
<?php
declare(strict_types=1);
namespace App\Tests\Action\Login;
use Psr\Http\Message\ServerRequestInterface as Request;
use \Lcobucci\JWT\Builder as JwtBuilder;
use \Lcobucci\JWT\Signer as JwtSigner;
use \Lcobucci\JWT\Token;
use Lcobucci\JWT\Builder as JwtBuilder;
use Lcobucci\JWT\Signer as JwtSigner;
use Lcobucci\JWT\Token;
use App\Tests\AbstractActionAdminTestCase;
final class TokenActionTest extends AbstractActionTestCase
final class TokenActionTest extends AbstractActionAdminTestCase
{
protected function setUp(): void
{
......@@ -113,4 +117,4 @@ final class TokenActionTest extends AbstractActionTestCase
$this->assertEquals(200, (int) $response->getStatusCode());
$this->assertSame((string) $response->getBody(), '{"email":"user2@anis.fr","token":"supertoken"}');
}
}
\ No newline at end of file
}
<?php
declare(strict_types=1);
namespace App\Tests\Action\Meta;
use Psr\Http\Message\ServerRequestInterface as Request;
final class DatabaseActionTest extends AbstractActionTestCase
use App\Tests\AbstractActionMetaTestCase;
final class DatabaseActionTest extends AbstractActionMetaTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->action = new \App\Action\Meta\DatabaseAction(
new \Psr\Log\NullLogger(),
$this->entityManager,
$this->metaEntityManagerFactory,
base64_decode('r3Q8C7LgIrRcTtI8I6EPzFwrDXJ4adgnGQ9V/pWVI8M=')
);
}
......@@ -32,7 +36,7 @@ final class DatabaseActionTest extends AbstractActionTestCase
'REQUEST_URI' => '/metadata/database/15'
]);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = ($this->action)($request, new \Slim\Http\Response(), array('id' => 15));
$response = ($this->action)($request, new \Slim\Http\Response(), array('id' => 15, 'instance' => 'default'));
$this->assertEquals(404, (int) $response->getStatusCode());
$this->assertSame((string) $response->getBody(), json_encode(array(
'error' => 'Invalid request',
......@@ -57,7 +61,7 @@ final class DatabaseActionTest extends AbstractActionTestCase
'REQUEST_URI' => '/metadata/database/1'
]);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = ($this->action)($request, new \Slim\Http\Response(), array('id' => 1));
$response = ($this->action)($request, new \Slim\Http\Response(), array('id' => 1, 'instance' => 'default'));
$this->assertEquals(200, (int) $response->getStatusCode());
$this->assertSame((string) $response->getBody(), json_encode($database));
}
......@@ -101,7 +105,7 @@ final class DatabaseActionTest extends AbstractActionTestCase
{
$editedDatabase = $this->getEditedDatabase();
$request = $this->getRequestForPut($editedDatabase);
$response = ($this->action)($request, new \Slim\Http\Response(), array('id' => 1));
$response = ($this->action)($request, new \Slim\Http\Response(), array('id' => 1, 'instance' => 'default'));
$this->assertEquals(200, (int) $response->getStatusCode());
$arrayResponse = json_decode((string) $response->getBody(), true);
$arrayResponse['dbpassword'] = 'password';
......@@ -116,9 +120,12 @@ final class DatabaseActionTest extends AbstractActionTestCase
'REQUEST_URI' => '/metadata/database/1'
]);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = ($this->action)($request, new \Slim\Http\Response(), array('id' => 1));
$response = ($this->action)($request, new \Slim\Http\Response(), array('id' => 1, 'instance' => 'default'));
$this->assertEquals(200, (int) $response->getStatusCode());
$this->assertSame((string) $response->getBody(), json_encode(array('message' => 'Database with id 1 is removed!')));
$this->assertSame(
(string) $response->getBody(),
json_encode(array('message' => 'Database with id 1 is removed!'))
);
$this->assertSame(1, $this->getDatabaseTester()->getConnection()->getRowCount('database'));
}
......@@ -127,7 +134,7 @@ final class DatabaseActionTest extends AbstractActionTestCase
$editedDatabase = $this->getEditedDatabase();
unset($editedDatabase[$field]);
$request = $this->getRequestForPut($editedDatabase);
$response = ($this->action)($request, new \Slim\Http\Response(), array('id' => 1));
$response = ($this->action)($request, new \Slim\Http\Response(), array('id' => 1, 'instance' => 'default'));
$this->assertEquals(400, (int) $response->getStatusCode());
$this->assertSame((string) $response->getBody(), json_encode(array(
'error' => 'Invalid request',
......
<?php
declare(strict_types=1);
namespace App\Tests\Action\Meta;
use Psr\Http\Message\ServerRequestInterface as Request;
final class DatabaseListActionTest extends AbstractActionTestCase
use App\Tests\AbstractActionMetaTestCase;
final class DatabaseListActionTest extends AbstractActionMetaTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->action = new \App\Action\Meta\DatabaseListAction(
new \Psr\Log\NullLogger(),
$this->entityManager,
$this->metaEntityManagerFactory,
base64_decode('r3Q8C7LgIrRcTtI8I6EPzFwrDXJ4adgnGQ9V/pWVI8M=')
);
}
......@@ -32,9 +36,31 @@ final class DatabaseListActionTest extends AbstractActionTestCase
'REQUEST_URI' => '/metadata/database'
]);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = ($this->action)($request, new \Slim\Http\Response(), array());
$response = ($this->action)($request, new \Slim\Http\Response(), array('instance' => 'default'));
$this->assertEquals(200, (int) $response->getStatusCode());
$this->assertSame((string) $response->getBody(), '[{"id":1,"label":"Cosmology","dbname":"cosmologydb","dbtype":"pgsql","dbhost":"pgserver","dbport":5432,"dblogin":"consult","dbpassword":"password"},{"id":2,"label":"ExoDat","dbname":"exodat_new","dbtype":"pgsql","dbhost":"pgserver","dbport":5432,"dblogin":"consult","dbpassword":"exopassword"}]');
$json = array(
[
"id" => 1,
"label" => "Cosmology",
"dbname" => "cosmologydb",
"dbtype" => "pgsql",
"dbhost" => "pgserver",
"dbport" => 5432,
"dblogin" => "consult",
"dbpassword" => "password"
],
[
"id" => 2,
"label" => "ExoDat",
"dbname" => "exodat_new",
"dbtype" => "pgsql",
"dbhost" => "pgserver",
"dbport" => 5432,
"dblogin" => "consult",
"dbpassword" => "exopassword"
]
);
$this->assertSame((string) $response->getBody(), json_encode($json));
}
public function testLabelIsEmpty(): void
......@@ -76,7 +102,7 @@ final class DatabaseListActionTest extends AbstractActionTestCase
{
$newDatabase = $this->getNewDatabase();
$request = $this->getRequestForPost($newDatabase);
$response = ($this->action)($request, new \Slim\Http\Response(), array());
$response = ($this->action)($request, new \Slim\Http\Response(), array('instance' => 'default'));
$this->assertEquals(201, (int) $response->getStatusCode());
$arrayResponse = json_decode((string) $response->getBody(), true);
$arrayResponse['dbpassword'] = 'password';
......@@ -89,7 +115,7 @@ final class DatabaseListActionTest extends AbstractActionTestCase
$newDatabase = $this->getNewDatabase();
unset($newDatabase[$field]);
$request = $this->getRequestForPost($newDatabase);
$response = ($this->action)($request, new \Slim\Http\Response(), array());
$response = ($this->action)($request, new \Slim\Http\Response(), array('instance' => 'default'));
$this->assertEquals(400, (int) $response->getStatusCode());
$this->assertSame((string) $response->getBody(), json_encode(array(
'error' => 'Invalid request',
......
<?php
declare(strict_types=1);
namespace App\Tests\Action\Meta;
use Psr\Http\Message\ServerRequestInterface as Request;
final class DatasetActionTest extends AbstractActionTestCase
use App\Tests\AbstractActionMetaTestCase;
final class DatasetActionTest extends AbstractActionMetaTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->action = new \App\Action\Meta\DatasetAction(
new \Psr\Log\NullLogger(),
$this->entityManager
$this->metaEntityManagerFactory
);