Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?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 Doctrine\ORM\EntityManager;
use App\Entity\Instance;
final class InstanceActionTest extends TestCase
{
private $action;
private $entityManager;
protected function setUp(): void
{
$this->entityManager = $this->createMock(EntityManager::class);
$this->action = new \App\Action\InstanceAction($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 testInstanceIsNotFound(): void
{
$this->expectException(HttpNotFoundException::class);
$this->expectExceptionMessage('Instance with name aspic is not found');
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('name' => 'aspic'));
$this->assertEquals(404, (int) $response->getStatusCode());
}
public function testGetAnInstanceByName(): void
{
$instance->expects($this->once())->method('jsonSerialize');
$this->entityManager->method('find')->willReturn($instance);
$request = $this->getRequest('GET');
($this->action)($request, new Response(), array('name' => 'aspic'));
}
public function testEditAnInstanceEmptyLabelField(): void
{
$this->entityManager->method('find')->willReturn($instance);
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Param label needed to edit the instance');
$request = $this->getRequest('PUT')->withParsedBody(array());
$response = ($this->action)($request, new Response(), array('name' => 'aspic'));
$this->assertEquals(400, (int) $response->getStatusCode());
}
public function testEditAnInstance(): void
{
$this->entityManager->method('find')->willReturn($instance);
$this->entityManager->expects($this->once())->method('flush');
$fields = array(
'name' => 'aspic',
'label' => 'Aspic',
'description' => 'Test',
'scientific_manager' => 'M. Durand',
'instrument' => 'Multiple',
'wavelength_domain' => 'Visible imaging',
'files_path' => '/INSTANCE_FILES',
'portal_logo' => '',
'design_color' => '#7AC29A',
'design_background_color' => '#FFFFFF',
'design_logo' => 'logo.png',
'design_favicon' => 'favicon.ico',
'navbar_background_color' => '#F8F9FA',
'navbar_border_bottom_color' => '#DEE2E6',
'navbar_color_href' => '#000000',
'navbar_font_family' => 'auto',
'navbar_sign_in_btn_color' => '#28A745',
'navbar_user_btn_color' => '#7AC29A',
'footer_background_color' => '#F8F9FA',
'footer_border_top_color' => '#DEE2E6',
'footer_text_color' => '#000000',
'family_border_color' => '#DFDFDF',
'family_header_background_color' => '#F7F7F7',
'family_title_color' => '#007BFF',
'family_title_bold' => false,
'family_background_color' => '#FFFFFF',
'family_color' => '#212529',
'progress_bar_subtitle' => 'Select a dataset, add criteria, select output columns and display the result.',
'progress_bar_step_dataset_title' => 'Dataset selection',
'progress_bar_step_criteria_title' => 'Search criteria',
'progress_bar_step_output_title' => 'Output columns',
'progress_bar_step_result_title' => 'Result table',
'progress_bar_color' => '#E9ECEF',
'progress_bar_active_color' => '#7AC29A',
'progress_bar_circle_color' => '#FFFFFF',
'progress_bar_circle_icon_color' => '#CCCCCC',
'progress_bar_circle_icon_active_color' => '#FFFFFF',
'result_header_background_color' => '#E9ECEF',
'result_header_text_color' => '#000000',
'result_header_btn_color' => '#007BFF',
'result_header_btn_hover_color' => '#0069D9',
'result_header_btn_text_color' => '#FFFFFF',
'result_datatable_bordered' => true,
'result_datatable_border_color' => '#DEE2E6',
'result_datatable_header_background_color' => '#FFFFFF',
'result_datatable_header_text_color' => '#000000',
'result_datatable_rows_background_color' => '#FFFFFF',
'result_datatable_rows_text_color' => '#000000',
'result_datatable_sorted_color' => '#C5C5C5',
'result_datatable_sorted_active_color' => '#000000',
'result_datatable_link_color' => '#007BFF',
'result_datatable_link_hover_color' => '#0056B3',
'result_datatable_rows_selected_color' => '#7AC29A',
'search_by_criteria_allowed' => true,
'search_by_criteria_label' => 'Search',
'search_multiple_allowed' => false,
'search_multiple_label' => 'Search multiple',
'search_multiple_all_datasets_selected' => false,
'documentation_allowed' => false,
'documentation_label' => ''
);
$request = $this->getRequest('PUT')->withParsedBody($fields);
($this->action)($request, new Response(), array('name' => 'aspic'));
}
public function testDeleteAnInstance(): void
{
$instance->method('getName')->willReturn('aspic');
$this->entityManager->method('find')->willReturn($instance);
$request = $this->getRequest('DELETE');
$response = ($this->action)($request, new Response(), array('name' => 'aspic'));
$this->assertSame(
json_encode(array('message' => 'Instance with name aspic is removed!')),
(string) $response->getBody()
);
}
private function getRequest(string $method): ServerRequest
{
return new ServerRequest($method, '/instance/aspic', array(
'Content-Type' => 'application/json'
));
}
/**
* @return Instance|\PHPUnit\Framework\MockObject\MockObject
*/
private function getInstanceMock()
{
return $this->createMock(Instance::class);
}