ProjectListActionTest.php 4.71 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?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\HttpBadRequestException;
use App\tests\EntityManagerBuilder;
use App\Entity\Database;
use App\Entity\Project;

final class ProjectListActionTest extends TestCase
{
    private $action;
    private $entityManager;

    protected function setUp(): void
    {
        $this->entityManager = EntityManagerBuilder::getInstance();
        $this->action = new \App\Action\ProjectListAction($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, POST, OPTIONS');
    }

    public function testGetAllProjects(): void
    {
        $projects = $this->addProjects();
        $request = $this->getRequest('GET');
        $response = ($this->action)($request, new Response(), array());
        $this->assertSame(
            json_encode($projects, JSON_UNESCAPED_SLASHES),
            (string) $response->getBody()
        );
    }

    public function testAddANewProjectEmptyNameField(): void
    {
        $this->expectException(HttpBadRequestException::class);
        $this->expectExceptionMessage('Param name needed to add a new project');
        $request = $this->getRequest('POST')->withParsedBody(array());
        $response = ($this->action)($request, new Response(), array());
        $this->assertEquals(400, (int) $response->getStatusCode());
    }

    public function testAddANewProjectDatabaseNotFound(): void
    {
        $this->expectException(HttpBadRequestException::class);
        $this->expectExceptionMessage('Database with id 1 is not found');
        $fields = $this->getNewProjectFields();
        $request = $this->getRequest('POST')->withParsedBody($fields);
        $response = ($this->action)($request, new Response(), array());
        $this->assertEquals(400, (int) $response->getStatusCode());
    }

    public function testAddANewProject(): void
    {
        $this->addDatabase();
74
        $fields = array_merge($this->getNewProjectFields(), ['nb_datasets' => 0]);
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
        $request = $this->getRequest('POST')->withParsedBody($fields);
        $response = ($this->action)($request, new Response(), array());
        $this->assertSame(
            json_encode($fields, JSON_UNESCAPED_SLASHES),
            (string) $response->getBody()
        );
        $this->assertEquals(201, (int) $response->getStatusCode());
    }

    protected function tearDown(): void
    {
        $this->entityManager->getConnection()->close();
    }

    private function getRequest(string $method): ServerRequest
    {
        return new ServerRequest($method, '/project', array(
            'Content-Type' => 'application/json'
        ));
    }

    private function getNewProjectFields(): array
    {
        return array(
            'name' => 'anis_project',
            'label' => 'Anis Project Test',
            'description' => 'Anis Project Test',
            'link' => 'http://project.com',
            'manager' => 'M. Durand',
            'id_database' => 1
        );
    }

    private function addDatabase(): Database
    {
        $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);
        $this->entityManager->flush();
        return $database;
    }

    private function addProjects(): array
    {
        $database = $this->addDatabase();

        $project = new Project('test');
        $project->setLabel('Test project');
        $project->setDescription('Test description');
        $project->setLink('http://test.com');
        $project->setManager('User1');
        $project->setDatabase($database);
        $this->entityManager->persist($project);

        $project2 = new Project('test2');
        $project2->setLabel('Test2 project');
        $project2->setDescription('Test2 description');
        $project2->setLink('http://test2.com');
        $project2->setManager('User2');
        $project2->setDatabase($database);
        $this->entityManager->persist($project2);

        $this->entityManager->flush();
        return array($project, $project2);
    }
}