DatasetActionTest.php 5.86 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
use Psr\Http\Message\ServerRequestInterface as Request;

François Agneray's avatar
François Agneray committed
8
9
10
use App\Tests\AbstractActionMetaTestCase;

final class DatasetActionTest extends AbstractActionMetaTestCase
François Agneray's avatar
François Agneray committed
11
12
13
14
15
16
{
    protected function setUp(): void
    {
        parent::setUp();
        $this->action = new \App\Action\Meta\DatasetAction(
            new \Psr\Log\NullLogger(),
François Agneray's avatar
François Agneray committed
17
            $this->metaEntityManagerFactory
François Agneray's avatar
François Agneray committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
        );
    }

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

    public function testDatasetIsNotFound(): void
    {
        $environment = \Slim\Http\Environment::mock([
            'REQUEST_METHOD' => 'GET',
            'REQUEST_URI' => '/metadata/dataset/undifined'
        ]);
        $request = \Slim\Http\Request::createFromEnvironment($environment);
François Agneray's avatar
François Agneray committed
38
39
40
41
42
        $response = ($this->action)(
            $request,
            new \Slim\Http\Response(),
            array('name' => 'undifined', 'instance' => 'default')
        );
François Agneray's avatar
François Agneray committed
43
44
45
        $this->assertEquals(404, (int) $response->getStatusCode());
        $this->assertSame((string) $response->getBody(), json_encode(array(
            'error' => 'Invalid request',
46
            'error_description' => 'Dataset with name undifined is not found'
François Agneray's avatar
François Agneray committed
47
48
49
50
51
52
53
54
55
56
        )));
    }

    public function testGetDataset(): void
    {
        $environment = \Slim\Http\Environment::mock([
            'REQUEST_METHOD' => 'GET',
            'REQUEST_URI' => '/metadata/dataset/corot_targets'
        ]);
        $request = \Slim\Http\Request::createFromEnvironment($environment);
François Agneray's avatar
François Agneray committed
57
58
59
60
61
        $response = ($this->action)(
            $request,
            new \Slim\Http\Response(),
            array('name' => 'corot_targets', 'instance' => 'default')
        );
François Agneray's avatar
François Agneray committed
62
        $this->assertEquals(200, (int) $response->getStatusCode());
François Agneray's avatar
François Agneray committed
63
        $this->assertSame((string) $response->getBody(), json_encode(array(
François Agneray's avatar
François Agneray committed
64
65
66
67
68
69
70
71
            '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
72
            'selectable_row' => false,
François Agneray's avatar
François Agneray committed
73
74
            'project_name' => 'corot',
            'id_dataset_family' => 1
François Agneray's avatar
François Agneray committed
75
        )));
François Agneray's avatar
François Agneray committed
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
    }

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

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

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

    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 testEditDataset(): void
    {
        $editedDataset = $this->getEditedDataset();
        $request = $this->getRequestForPut($editedDataset);
François Agneray's avatar
François Agneray committed
117
118
119
120
121
        $response = ($this->action)(
            $request,
            new \Slim\Http\Response(),
            array('name' => 'corot_targets', 'instance' => 'default')
        );
François Agneray's avatar
François Agneray committed
122
        $this->assertEquals(200, (int) $response->getStatusCode());
François Agneray's avatar
François Agneray committed
123
        $this->assertSame((string) $response->getBody(), json_encode($editedDataset));
François Agneray's avatar
François Agneray committed
124
125
126
127
128
129
130
131
132
133
        $this->assertSame(2, $this->getDatabaseTester()->getConnection()->getRowCount('dataset'));
    }

    public function testRemoveDataset(): void
    {
        $environment = \Slim\Http\Environment::mock([
            'REQUEST_METHOD' => 'DELETE',
            'REQUEST_URI' => '/metadata/dataset/corot_targets'
        ]);
        $request = \Slim\Http\Request::createFromEnvironment($environment);
François Agneray's avatar
François Agneray committed
134
135
136
137
138
        $response = ($this->action)(
            $request,
            new \Slim\Http\Response(),
            array('name' => 'corot_targets', 'instance' => 'default')
        );
François Agneray's avatar
François Agneray committed
139
        $this->assertEquals(200, (int) $response->getStatusCode());
François Agneray's avatar
François Agneray committed
140
141
142
143
        $this->assertSame(
            (string) $response->getBody(),
            json_encode(array('message' => 'Dataset with name corot_targets is removed!'))
        );
François Agneray's avatar
François Agneray committed
144
145
146
147
148
149
150
151
        $this->assertSame(1, $this->getDatabaseTester()->getConnection()->getRowCount('dataset'));
    }

    private function fieldIsEmpty($field): void
    {
        $editedDataset = $this->getEditedDataset();
        unset($editedDataset[$field]);
        $request = $this->getRequestForPut($editedDataset);
François Agneray's avatar
François Agneray committed
152
153
154
155
156
        $response = ($this->action)(
            $request,
            new \Slim\Http\Response(),
            array('name' => 'corot_targets', 'instance' => 'default')
        );
François Agneray's avatar
François Agneray committed
157
158
159
        $this->assertEquals(400, (int) $response->getStatusCode());
        $this->assertSame((string) $response->getBody(), json_encode(array(
            'error' => 'Invalid request',
160
            'error_description' => 'Param ' . $field . ' needed to edit the dataset'
François Agneray's avatar
François Agneray committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
        )));
    }

    private function getEditedDataset(): array
    {
        return array(
            'name' => 'corot_targets',
            'table_ref' => 'v_corot_targets',
            'label' => 'Edited dataset',
            'description' => 'Edited dataset description',
            'display' => 50,
            'count' => 100583,
            'vo' => true,
            'data_path' => '/tmp/data',
François Agneray's avatar
François Agneray committed
175
            'selectable_row' => false,
François Agneray's avatar
François Agneray committed
176
177
178
179
180
            'project_name' => 'corot',
            'id_dataset_family' => 1
        );
    }
}