Completed
Push — 1.0-after-release ( a67a34 )
by Kamil
91:32 queued 64:06
created

BookApiTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 182
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A it_allows_creating_a_book() 0 18 1
A it_allows_updating_a_book() 0 23 1
A it_allows_updating_partial_information_about_a_book() 0 15 1
A it_allows_removing_a_book() 0 8 1
A it_allows_showing_a_book() 0 8 1
A it_allows_indexing_books() 0 8 1
A it_allows_paginating_the_index_of_books() 0 8 1
A it_does_not_allow_showing_resource_if_it_not_exists() 0 8 1
A it_does_not_apply_sorting_for_un_existing_field() 0 9 1
A it_does_not_apply_filtering_for_un_existing_field() 0 9 1
A it_applies_sorting_for_existing_field() 0 9 1
A it_applies_filtering_for_existing_field() 0 9 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
/*
14
 * This file is part of the Sylius package.
15
 *
16
 * (c) Paweł Jędrzejewski
17
 *
18
 * For the full copyright and license information, please view the LICENSE
19
 * file that was distributed with this source code.
20
 */
21
22
namespace AppBundle\Tests\Controller;
23
24
use Lakion\ApiTestCase\JsonApiTestCase;
25
use Symfony\Component\HttpFoundation\Response;
26
27
class BookApiTest extends JsonApiTestCase
28
{
29
    /**
30
     * @test
31
     */
32
    public function it_allows_creating_a_book()
33
    {
34
        $data =
35
<<<EOT
36
        {
37
            "translations": {
38
                "en_US": {
39
                    "title": "Star Wars: Dark Disciple"
40
                }
41
            },
42
            "author": "Christie Golden"
43
        }
44
EOT;
45
46
        $this->client->request('POST', '/books/', [], [], ['CONTENT_TYPE' => 'application/json'], $data);
47
        $response = $this->client->getResponse();
48
        $this->assertResponse($response, 'books/create_response', Response::HTTP_CREATED);
49
    }
50
51
    /**
52
     * @test
53
     */
54
    public function it_allows_updating_a_book()
55
    {
56
        $objects = $this->loadFixturesFromFile('books.yml');
57
58
        $data =
59
<<<EOT
60
        {
61
             "translations": {
62
                "en_US": {
63
                    "title": "Star Wars: Dark Disciple"
64
                },
65
                "pl_PL": {
66
                    "title": "Gwiezdne Wojny: Mroczny Uczeń"
67
                }
68
            },
69
            "author": "Christie Golden"
70
        }
71
EOT;
72
73
        $this->client->request('PUT', '/books/' . $objects['book1']->getId(), [], [], ['CONTENT_TYPE' => 'application/json'], $data);
74
        $response = $this->client->getResponse();
75
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
76
    }
77
78
    /**
79
     * @test
80
     */
81
    public function it_allows_updating_partial_information_about_a_book()
82
    {
83
        $objects = $this->loadFixturesFromFile('books.yml');
84
85
        $data =
86
 <<<EOT
87
        {
88
            "author": "Christie Golden"
89
        }
90
EOT;
91
92
        $this->client->request('PATCH', '/books/' . $objects['book1']->getId(), [], [], ['CONTENT_TYPE' => 'application/json'], $data);
93
        $response = $this->client->getResponse();
94
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
95
    }
96
97
    /**
98
     * @test
99
     */
100
    public function it_allows_removing_a_book()
101
    {
102
        $objects = $this->loadFixturesFromFile('books.yml');
103
104
        $this->client->request('DELETE', '/books/' . $objects['book1']->getId());
105
        $response = $this->client->getResponse();
106
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
107
    }
108
109
    /**
110
     * @test
111
     */
112
    public function it_allows_showing_a_book()
113
    {
114
        $objects = $this->loadFixturesFromFile('books.yml');
115
116
        $this->client->request('GET', '/books/' . $objects['book1']->getId());
117
        $response = $this->client->getResponse();
118
        $this->assertResponse($response, 'books/show_response');
119
    }
120
121
    /**
122
     * @test
123
     */
124
    public function it_allows_indexing_books()
125
    {
126
        $this->loadFixturesFromFile('books.yml');
127
128
        $this->client->request('GET', '/books/');
129
        $response = $this->client->getResponse();
130
        $this->assertResponse($response, 'books/index_response');
131
    }
132
133
    /**
134
     * @test
135
     */
136
    public function it_allows_paginating_the_index_of_books()
137
    {
138
        $this->loadFixturesFromFile('more_books.yml');
139
140
        $this->client->request('GET', '/books/', ['page' => 2]);
141
        $response = $this->client->getResponse();
142
        $this->assertResponse($response, 'books/paginated_index_response');
143
    }
144
145
    /**
146
     * @test
147
     */
148
    public function it_does_not_allow_showing_resource_if_it_not_exists()
149
    {
150
        $this->loadFixturesFromFile('books.yml');
151
152
        $this->client->request('GET', '/books/3');
153
        $response = $this->client->getResponse();
154
        $this->assertResponseCode($response, Response::HTTP_NOT_FOUND);
155
    }
156
157
    /**
158
     * @test
159
     */
160
    public function it_does_not_apply_sorting_for_un_existing_field()
161
    {
162
        $this->loadFixturesFromFile('more_books.yml');
163
164
        $this->client->request('GET', '/books/sortable/', ['sorting' => ['name' => 'DESC']]);
165
        $response = $this->client->getResponse();
166
167
        $this->assertResponseCode($response, Response::HTTP_OK);
168
    }
169
170
    /**
171
     * @test
172
     */
173
    public function it_does_not_apply_filtering_for_un_existing_field()
174
    {
175
        $this->loadFixturesFromFile('more_books.yml');
176
177
        $this->client->request('GET', '/books/filterable/', ['criteria' => ['name' => 'John']]);
178
        $response = $this->client->getResponse();
179
180
        $this->assertResponseCode($response, Response::HTTP_OK);
181
    }
182
183
    /**
184
     * @test
185
     */
186
    public function it_applies_sorting_for_existing_field()
187
    {
188
        $this->loadFixturesFromFile('more_books.yml');
189
190
        $this->client->request('GET', '/books/sortable/', ['sorting' => ['id' => 'DESC']]);
191
        $response = $this->client->getResponse();
192
193
        $this->assertResponseCode($response, Response::HTTP_OK);
194
    }
195
196
    /**
197
     * @test
198
     */
199
    public function it_applies_filtering_for_existing_field()
200
    {
201
        $this->loadFixturesFromFile('more_books.yml');
202
203
        $this->client->request('GET', '/books/filterable/', ['criteria' => ['author' => 'J.R.R. Tolkien']]);
204
        $response = $this->client->getResponse();
205
206
        $this->assertResponseCode($response, Response::HTTP_OK);
207
    }
208
}
209