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

Adding unit tests

parent a7acb85a
Pipeline #4699 passed with stages
in 2 minutes and 47 seconds
......@@ -32,7 +32,7 @@ test:
- vendor
policy: pull
script:
- phpdbg -qrr ./vendor/bin/phpunit --bootstrap ./tests/bootstrap.php --whitelist src --coverage-clover ./coverage/clover.xml --log-junit ./coverage/junit-logfile.xml --colors=never ./tests
- phpdbg -qrr ./vendor/bin/phpunit --bootstrap ./tests/bootstrap.php --whitelist src --coverage-clover ./coverage/clover.xml --log-junit ./coverage/junit-logfile.xml --coverage-text --colors=never ./tests
artifacts:
paths:
- coverage
......
# Anis Server
# Anis Server [![pipeline status](https://gitlab.lam.fr/anis/anis-server/badges/develop/pipeline.svg)](https://gitlab.lam.fr/anis/anis-server/-/commits/develop) [![coverage report](https://gitlab.lam.fr/anis/anis-server/badges/develop/coverage.svg)](https://gitlab.lam.fr/anis/anis-server/-/commits/develop)
## Introduction
......@@ -38,10 +38,6 @@ To list all operations availables just type `make` in your terminal at the root
- To execute php code sniffer : `make phpcs`
- To create the metamodel database : `make create-db`
## Open API
You can find an open api documentation into the `anis-server.yaml` file.
## Production mode
To activate the production mode with no error details and doctrine cache, you must configure the following environment variables :
......
......@@ -27,8 +27,7 @@
"firebase/php-jwt": "^5.2"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpspec/prophecy-phpunit": "^2.0"
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
......
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "df10cb89c262e08d94c642228bf6607f",
"content-hash": "4962ae03113a07c92977cf2d8324b554",
"packages": [
{
"name": "composer/package-versions-deprecated",
......@@ -3298,58 +3298,6 @@
},
"time": "2021-03-17T13:42:18+00:00"
},
{
"name": "phpspec/prophecy-phpunit",
"version": "v2.0.1",
"source": {
"type": "git",
"url": "https://github.com/phpspec/prophecy-phpunit.git",
"reference": "2d7a9df55f257d2cba9b1d0c0963a54960657177"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpspec/prophecy-phpunit/zipball/2d7a9df55f257d2cba9b1d0c0963a54960657177",
"reference": "2d7a9df55f257d2cba9b1d0c0963a54960657177",
"shasum": ""
},
"require": {
"php": "^7.3 || ^8",
"phpspec/prophecy": "^1.3",
"phpunit/phpunit": "^9.1"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0-dev"
}
},
"autoload": {
"psr-4": {
"Prophecy\\PhpUnit\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Christophe Coevoet",
"email": "stof@notk.org"
}
],
"description": "Integrating the Prophecy mocking library in PHPUnit test cases",
"homepage": "http://phpspec.net",
"keywords": [
"phpunit",
"prophecy"
],
"support": {
"issues": "https://github.com/phpspec/prophecy-phpunit/issues",
"source": "https://github.com/phpspec/prophecy-phpunit/tree/v2.0.1"
},
"time": "2020-07-09T08:33:42+00:00"
},
{
"name": "phpunit/php-code-coverage",
"version": "9.2.5",
......
This diff is collapsed.
This diff is collapsed.
......@@ -61,7 +61,7 @@ final class DownloadFileAction extends AbstractAction
if (is_null($dataset)) {
throw new HttpNotFoundException(
$request,
'Dataset with name ' . $args['name'] . ' is not found'
'Dataset with name ' . $args['dname'] . ' is not found'
);
}
......
<?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 Slim\Exception\HttpNotFoundException;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
use App\tests\EntityManagerBuilder;
use App\Utils\DBALConnectionFactory;
use App\Utils\Operator\OperatorFactory;
final class AttributeDistinctActionTest extends TestCase
{
private $action;
private $entityManager;
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->action = new \App\Action\AttributeDistinctAction(
$this->entityManager,
$this->createMock(DBALConnectionFactory::class)
);
}
public function testOptionsHttpMethod(): void
{
$request = $this->getRequest('OPTIONS');
$response = ($this->action)($request, new Response(), array());
$this->assertSame($response->getHeaderLine('Access-Control-Allow-Methods'), 'GET, OPTIONS');
}
protected function tearDown(): void
{
$this->entityManager->getConnection()->close();
}
private function getRequest(string $method): ServerRequest
{
return new ServerRequest($method, '/dataset/obs_cat/attribute/1/distinct', array(
'Content-Type' => 'application/json'
));
}
}
<?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 Slim\Exception\HttpNotFoundException;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
use App\tests\EntityManagerBuilder;
final class DownloadFileActionTest extends TestCase
{
private $action;
private $entityManager;
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->action = new \App\Action\DownloadFileAction($this->entityManager, array());
}
public function testOptionsHttpMethod(): void
{
$request = $this->getRequest('OPTIONS');
$response = ($this->action)($request, new Response(), array());
$this->assertSame($response->getHeaderLine('Access-Control-Allow-Methods'), 'GET, OPTIONS');
}
public function testDatasetIsNotFound(): void
{
$this->expectException(HttpNotFoundException::class);
$this->expectExceptionMessage('Dataset with name obs_cat is not found');
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('dname' => 'obs_cat'));
$this->assertEquals(404, (int) $response->getStatusCode());
}
protected function tearDown(): void
{
$this->entityManager->getConnection()->close();
}
private function getRequest(string $method): ServerRequest
{
return new ServerRequest($method, '/download-file/obs_cat/file.fits', array(
'Content-Type' => 'application/json'
));
}
}
<?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 Slim\Exception\HttpNotFoundException;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
use App\tests\EntityManagerBuilder;
use App\Utils\DBALConnectionFactory;
use App\Utils\Operator\OperatorFactory;
final class SearchActionTest extends TestCase
{
private $action;
private $entityManager;
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->action = new \App\Action\SearchAction(
$this->entityManager,
$this->createMock(DBALConnectionFactory::class),
$this->createMock(OperatorFactory::class),
array()
);
}
public function testOptionsHttpMethod(): void
{
$request = $this->getRequest('OPTIONS');
$response = ($this->action)($request, new Response(), array());
$this->assertSame($response->getHeaderLine('Access-Control-Allow-Methods'), 'GET, OPTIONS');
}
public function testDatasetIsNotFound(): void
{
$this->expectException(HttpNotFoundException::class);
$this->expectExceptionMessage('Dataset with name obs_cat is not found');
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('dname' => 'obs_cat'));
$this->assertEquals(404, (int) $response->getStatusCode());
}
protected function tearDown(): void
{
$this->entityManager->getConnection()->close();
}
private function getRequest(string $method): ServerRequest
{
return new ServerRequest($method, '/search/obs_cat?a=1,2,3', array(
'Content-Type' => 'application/json'
));
}
}
......@@ -13,7 +13,6 @@ declare(strict_types=1);
namespace App\Tests\Handlers;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Slim\Interfaces\CallableResolverInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Log\LoggerInterface;
......@@ -21,8 +20,6 @@ use ReflectionClass;
final class LogErrorHandlerTest extends TestCase
{
use ProphecyTrait;
private $logErrorHandler;
protected function setUp(): void
......@@ -42,10 +39,12 @@ final class LogErrorHandlerTest extends TestCase
public function testLogError(): void
{
$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->once())
->method('info');
$this->logErrorHandler->setLogger($logger);
$logError = self::getMethod('logError');
$logger = $this->prophesize(LoggerInterface::class);
$logger->info('Log test')->shouldBeCalled();
$this->logErrorHandler->setLogger($logger->reveal());
$logError->invokeArgs($this->logErrorHandler, array('Log test'));
}
}
<?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\Middleware;
use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
final class AdminMiddlewareTest extends TestCase
{
public function testOptionsHttpMethod(): void
{
$request = new ServerRequest('OPTIONS', '/');
$requestHandler = $this->getMockBuilder(RequestHandler::class)
->disableOriginalConstructor()
->setMethods(['handle'])
->getMock();
$requestHandler->method('handle')
->with($this->identicalTo($request))
->will($this->returnValue(new Response(200, [], 'Hello world')));
$adminMiddleware = new \App\Middleware\AdminMiddleware(array(), true);
$response = $adminMiddleware->process($request, $requestHandler);
$this->assertSame((string) $response->getBody(), 'Hello world');
}
}
<?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\Middleware;
use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
final class AuthorizationMiddlewareTest extends TestCase
{
public function testOptionsHttpMethod(): void
{
$request = new ServerRequest('OPTIONS', '/');
$requestHandler = $this->getMockBuilder(RequestHandler::class)
->disableOriginalConstructor()
->setMethods(['handle'])
->getMock();
$requestHandler->method('handle')
->with($this->identicalTo($request))
->will($this->returnValue(new Response(200, [], 'Hello world')));
$authorizationMiddleware = new \App\Middleware\AuthorizationMiddleware(array(), true);
$response = $authorizationMiddleware->process($request, $requestHandler);
$this->assertSame((string) $response->getBody(), 'Hello world');
}
}
......@@ -19,11 +19,9 @@ use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
final class ContentTypeJsonMiddlewareTest extends TestCase
{
public function testContentTypeJson()
public function testContentTypeJson(): void
{
$request = new ServerRequest('GET', '/', array(
'Content-Type' => 'application/json'
));
$request = new ServerRequest('GET', '/');
$requestHandler = $this->getMockBuilder(RequestHandler::class)
->disableOriginalConstructor()
......@@ -38,4 +36,24 @@ final class ContentTypeJsonMiddlewareTest extends TestCase
$response = $contentTypeJsonMiddleware->process($request, $requestHandler);
$this->assertSame((string) $response->getHeaderLine('Content-Type'), 'application/json');
}
public function testContentTypeHtml(): void
{
$request = new ServerRequest('GET', '/', array(
'Content-Type' => 'text/html'
));
$requestHandler = $this->getMockBuilder(RequestHandler::class)
->disableOriginalConstructor()
->setMethods(['handle'])
->getMock();
$requestHandler->method('handle')
->with($this->identicalTo($request))
->will($this->returnValue((new Response())->withHeader('Content-Type', 'text/html')));
$contentTypeJsonMiddleware = new \App\Middleware\ContentTypeJsonMiddleware();
$response = $contentTypeJsonMiddleware->process($request, $requestHandler);
$this->assertSame((string) $response->getHeaderLine('Content-Type'), 'text/html');
}
}
......@@ -19,7 +19,7 @@ use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
final class CorsMiddlewareTest extends TestCase
{
public function testCorsHeadersForOptionsMethod()
public function testCorsHeadersForOptionsMethod(): void
{
$request = new ServerRequest('OPTIONS', '/');
......@@ -41,7 +41,7 @@ final class CorsMiddlewareTest extends TestCase
);
}
public function testCorsHeadersForGetMethod()
public function testCorsHeadersForGetMethod(): void
{
$request = new ServerRequest('GET', '/');
......
<?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\Middleware;
use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
final class JsonBodyParserMiddlewareTest extends TestCase
{
public function testJsonBodyParser(): void
{
$request = new ServerRequest('GET', '/');
$requestHandler = $this->getMockBuilder(RequestHandler::class)
->disableOriginalConstructor()
->setMethods(['handle'])
->getMock();
$requestHandler->method('handle')
->with($this->identicalTo($request))
->will($this->returnValue(new Response(200, [], 'Hello world')));
$jsonBodyParserMiddleware = new \App\Middleware\JsonBodyParserMiddleware(array(), true);
$response = $jsonBodyParserMiddleware->process($request, $requestHandler);
$this->assertSame((string) $response->getBody(), 'Hello world');
}
}
<?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\Middleware;
use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
use Psr\Log\LoggerInterface;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\DBAL\Logging\SQLLogger;
final class MetamodelSqlLoggerMiddlewareTest extends TestCase
{
public function testMetamodelSqlLogger(): void
{
$request = new ServerRequest('GET', '/');
$requestHandler = $this->getMockBuilder(RequestHandler::class)
->disableOriginalConstructor()
->setMethods(['handle'])
->getMock();
$requestHandler->method('handle')
->with($this->identicalTo($request))
->will($this->returnValue(new Response(200, [], 'Hello world')));
$metamodelSqlLoggerMiddleware = new \App\Middleware\MetamodelSqlLoggerMiddleware(
$this->createMock(LoggerInterface::class),
$this->getEntityManager()
);
$response = $metamodelSqlLoggerMiddleware->process($request, $requestHandler);
$this->assertSame((string) $response->getBody(), 'Hello world');
}
private function getEntityManager(): EntityManager
{
$sqlLogger = $this->createMock(SQLLogger::class);
$sqlLogger->queries = [1 => 'sql1', 2 => 'sql2'];
$configuration = $this->getMockBuilder(Configuration::class)
->setMethods(['getSQLLogger'])
->getMock();
$configuration->method('getSQLLogger')
->will($this->returnValue($sqlLogger));
$em = $this->getMockBuilder(EntityManager::class)
->disableOriginalConstructor()
->setMethods(['getConfiguration'])
->getMock();
$em->method('getConfiguration')
->will($this->returnValue($configuration));
return $em;
}
}
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