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