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
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.