ProjectActionTest.php 5.32 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?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 App\tests\EntityManagerBuilder;
use App\Entity\Database;
use App\Entity\Project;

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

    protected function setUp(): void
    {
        $this->entityManager = EntityManagerBuilder::getInstance();
        $this->action = new \App\Action\ProjectAction($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 testProjectIsNotFound(): void
    {
        $this->expectException(HttpNotFoundException::class);
        $this->expectExceptionMessage('Project with name anis_project is not found');
        $request = $this->getRequest('GET');
        $response = ($this->action)($request, new Response(), array('name' => 'anis_project'));
        $this->assertEquals(404, (int) $response->getStatusCode());
    }

    public function testGetAProjectByName(): void
    {
        $project = $this->addAProject();
        $request = $this->getRequest('GET');
        $response = ($this->action)($request, new Response(), array('name' => 'anis_project'));
        $this->assertSame(json_encode($project, JSON_UNESCAPED_SLASHES), (string) $response->getBody());
    }

    public function testEditAProjectEmptyLabelField(): void
    {
        $this->addAProject();
        $this->expectException(HttpBadRequestException::class);
        $this->expectExceptionMessage('Param label needed to edit the project');
        $request = $this->getRequest('PUT')->withParsedBody(array());
        $response = ($this->action)($request, new Response(), array('name' => 'anis_project'));
        $this->assertEquals(400, (int) $response->getStatusCode());
    }

    public function testEditAProjectDatabaseNotFound(): void
    {
        $this->addAProject();
        $fields = $this->getEditProjectFields();
        $fields['id_database'] = 2;
        $this->expectException(HttpBadRequestException::class);
        $this->expectExceptionMessage('Database with id 2 is not found');
        $request = $this->getRequest('PUT')->withParsedBody($fields);
        $response = ($this->action)($request, new Response(), array('name' => 'anis_project'));
        $this->assertEquals(400, (int) $response->getStatusCode());
    }

    public function testEditAProject(): void
    {
        $this->addAProject();
        $fields = $this->getEditProjectFields();
        $request = $this->getRequest('PUT')->withParsedBody($fields);
        $response = ($this->action)($request, new Response(), array('name' => 'anis_project'));
        $this->assertSame(
88
            json_encode(array_merge(['name' => 'anis_project'], $fields, ['nb_datasets' => 0]), JSON_UNESCAPED_SLASHES),
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
147
148
149
150
151
152
153
154
155
156
            (string) $response->getBody()
        );
    }

    public function testDeleteAProject(): void
    {
        $this->addAProject();
        $request = $this->getRequest('DELETE');
        $response = ($this->action)($request, new Response(), array('name' => 'anis_project'));
        $this->assertSame(
            json_encode(array('message' => 'Project anis_project is removed!')),
            (string) $response->getBody()
        );
    }

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

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

    private function getEditProjectFields(): array
    {
        return array(
            'label' => 'New label project',
            '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 addAProject(): Project
    {
        $database = $this->addDatabase();

        $project = new Project('anis_project');
        $project->setLabel('Anis Project Test');
        $project->setDescription('Anis Project Test');
        $project->setLink('http://project.com');
        $project->setManager('M. Durand');
        $project->setDatabase($database);
        $this->entityManager->persist($project);
        $this->entityManager->flush();
        return $project;
    }
}