DatasetListActionTest.php 6.17 KB
Newer Older
François Agneray's avatar
François Agneray committed
1
2
3
<?php
declare(strict_types=1);

François Agneray's avatar
François Agneray committed
4
5
namespace App\Tests\Action\Meta;

François Agneray's avatar
François Agneray committed
6
7
8
9
use Psr\Http\Message\ServerRequestInterface as Request;
use Doctrine\DBAL\Connection as DBALConnection;

use App\Utils\DBALConnectionFactory;
François Agneray's avatar
François Agneray committed
10
use App\Tests\AbstractActionMetaTestCase;
François Agneray's avatar
François Agneray committed
11

François Agneray's avatar
François Agneray committed
12
final class DatasetListActionTest extends AbstractActionMetaTestCase
François Agneray's avatar
François Agneray committed
13
14
15
16
17
18
{
    protected function setUp(): void
    {
        parent::setUp();
        $this->action = new \App\Action\Meta\DatasetListAction(
            new \Psr\Log\NullLogger(),
François Agneray's avatar
François Agneray committed
19
            $this->metaEntityManagerFactory,
François Agneray's avatar
François Agneray committed
20
21
            $this->getMockDBALConnectionFactory(),
            base64_decode('r3Q8C7LgIrRcTtI8I6EPzFwrDXJ4adgnGQ9V/pWVI8M=')
François Agneray's avatar
François Agneray committed
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
        );
    }

    protected function getMockDBALConnectionFactory(): DBALConnectionFactory
    {
        $mockDBALConnection = $this->createMock(DBALConnection::class);
        $mockConnectionFactory = $this->createMock(DBALConnectionFactory::class);
        $mockConnectionFactory
            ->method('create')
            ->willReturn($mockDBALConnection);
        return $mockConnectionFactory;
    }

    protected function getRequestForPost(array $parsedBody): Request
    {
        $environment = \Slim\Http\Environment::mock([
            'REQUEST_METHOD' => 'POST',
            'REQUEST_URI' => '/metadata/dataset',
            'CONTENT_TYPE'   => 'application/json',
        ]);
        return \Slim\Http\Request::createFromEnvironment($environment)->withParsedBody($parsedBody);
    }

    public function testGetDatasetList(): void
    {
        $environment = \Slim\Http\Environment::mock([
            'REQUEST_METHOD' => 'GET',
            'REQUEST_URI' => '/metadata/dataset'
        ]);
        $request = \Slim\Http\Request::createFromEnvironment($environment);
François Agneray's avatar
François Agneray committed
52
        $response = ($this->action)($request, new \Slim\Http\Response(), array('instance' => 'default'));
François Agneray's avatar
François Agneray committed
53
        $this->assertEquals(200, (int) $response->getStatusCode());
François Agneray's avatar
François Agneray committed
54
        $this->assertSame((string) $response->getBody(), json_encode(array(
François Agneray's avatar
François Agneray committed
55
56
57
58
59
60
61
62
63
            array(
                'name' => 'corot_targets',
                'table_ref' => 'v_corot_targets',
                'label' => 'CoRoT Targets',
                'description' => 'CoRoT Targets',
                'display' => 10,
                'count' => 100582,
                'vo' => true,
                'data_path' => '/tmp/data',
François Agneray's avatar
François Agneray committed
64
                'selectable_raw' => false,
François Agneray's avatar
François Agneray committed
65
66
67
68
69
70
71
72
73
74
75
76
                'project_name' => 'corot',
                'id_dataset_family' => 1
            ),
            array(
                'name' => 'vuds_targets',
                'table_ref' => 'v_vuds_targets',
                'label' => 'VUDS Targets',
                'description' => 'VUDS Targets display',
                'display' => 20,
                'count' => 784512,
                'vo' => false,
                'data_path' => '/tmp/data',
François Agneray's avatar
François Agneray committed
77
                'selectable_raw' => false,
François Agneray's avatar
François Agneray committed
78
79
80
                'project_name' => 'vuds',
                'id_dataset_family' => 2
            )
François Agneray's avatar
François Agneray committed
81
        )));
François Agneray's avatar
François Agneray committed
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
    }

    public function testNameIsEmpty(): void
    {
        $this->fieldIsEmpty('name');
    }

    public function testTableRefIsEmpty(): void
    {
        $this->fieldIsEmpty('table_ref');
    }

    public function testLabelIsEmpty(): void
    {
        $this->fieldIsEmpty('label');
    }

    public function testDescriptionIsEmpty(): void
    {
        $this->fieldIsEmpty('description');
    }

    public function testDisplayIsEmpty(): void
    {
        $this->fieldIsEmpty('display');
    }

    public function testCountIsEmpty(): void
    {
        $this->fieldIsEmpty('count');
    }

    public function testVoIsEmpty(): void
    {
        $this->fieldIsEmpty('vo');
    }

    public function testDataPathIsEmpty(): void
    {
        $this->fieldIsEmpty('data_path');
    }

    public function testProjectNameIsEmpty(): void
    {
        $this->fieldIsEmpty('project_name');
    }

    public function testIdDatasetFamilyIsEmpty(): void
    {
        $this->fieldIsEmpty('id_dataset_family');
    }

    public function testProjectIsNotFound(): void
    {
        $newDataset = $this->getNewDataset();
        $newDataset['project_name'] = 'undifined';
        $request = $this->getRequestForPost($newDataset);
François Agneray's avatar
François Agneray committed
139
        $response = ($this->action)($request, new \Slim\Http\Response(), array('instance' => 'default'));
François Agneray's avatar
François Agneray committed
140
141
142
        $this->assertEquals(404, (int) $response->getStatusCode());
        $this->assertSame((string) $response->getBody(), json_encode(array(
            'error' => 'Invalid request',
143
            'error_description' => 'Project with name undifined is not found'
François Agneray's avatar
François Agneray committed
144
145
146
147
148
149
150
151
        )));
    }

    public function testDatasetFamilyIsNotFound(): void
    {
        $newDataset = $this->getNewDataset();
        $newDataset['id_dataset_family'] = 15;
        $request = $this->getRequestForPost($newDataset);
François Agneray's avatar
François Agneray committed
152
        $response = ($this->action)($request, new \Slim\Http\Response(), array('instance' => 'default'));
François Agneray's avatar
François Agneray committed
153
154
155
        $this->assertEquals(404, (int) $response->getStatusCode());
        $this->assertSame((string) $response->getBody(), json_encode(array(
            'error' => 'Invalid request',
156
            'error_description' => 'Dataset family with id 15 is not found'
François Agneray's avatar
François Agneray committed
157
158
159
160
161
162
163
164
        )));
    }

    private function fieldIsEmpty($field): void
    {
        $newDataset = $this->getNewDataset();
        unset($newDataset[$field]);
        $request = $this->getRequestForPost($newDataset);
François Agneray's avatar
François Agneray committed
165
        $response = ($this->action)($request, new \Slim\Http\Response(), array('instance' => 'default'));
François Agneray's avatar
François Agneray committed
166
167
168
        $this->assertEquals(400, (int) $response->getStatusCode());
        $this->assertSame((string) $response->getBody(), json_encode(array(
            'error' => 'Invalid request',
169
            'error_description' => 'Param ' . $field . ' needed to add a new dataset'
François Agneray's avatar
François Agneray committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
        )));
    }

    private function getNewDataset(): array
    {
        return array(
            'name' => 'new_dataset',
            'table_ref' => 'v_new_dataset',
            'label' => 'New dataset',
            'description' => 'New Dataset description',
            'display' => 30,
            'count' => 987456,
            'vo' => true,
            'data_path' => '/tmp/data',
François Agneray's avatar
François Agneray committed
184
            'selectable_raw' => true,
François Agneray's avatar
François Agneray committed
185
186
187
188
189
            'project_name' => 'corot',
            'id_dataset_family' => 1
        );
    }
}