1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Seblegall\ApiValidatorBundle\Tests\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
6
|
|
|
|
7
|
|
|
class ExampleApiControllerTest extends WebTestCase |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @expectedException \InvalidArgumentException |
11
|
|
|
* @expectedExceptionMessage Invalid json message received |
12
|
|
|
*/ |
13
|
|
|
public function testRoutingApiParametersWithMalformedJson() |
14
|
|
|
{ |
15
|
|
|
$client = static::createClient(); |
16
|
|
|
|
17
|
|
|
$client->request( |
18
|
|
|
'POST', |
19
|
|
|
'/example/api/ws-route?page=1&max=20&sort=name', |
20
|
|
|
array(), |
21
|
|
|
array(), |
22
|
|
|
array('CONTENT_TYPE' => 'application/json'), |
23
|
|
|
'{"created_at":"2014-01-01' |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
$response = $client->getResponse(); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
View Code Duplication |
public function testRoutingApiParametersWithValidation() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$client = static::createClient(); |
32
|
|
|
|
33
|
|
|
$client->request('POST', '/example/api/ws-route-validation?page=test&max=20&sort=address', array('created_at' => '08/11/2014')); |
34
|
|
|
$response = $client->getResponse(); |
35
|
|
|
|
36
|
|
|
$this->assertEquals(400, $response->getStatusCode()); |
37
|
|
|
|
38
|
|
|
$this->assertEquals(array( |
39
|
|
|
'errors' => array( |
40
|
|
|
'page' => 'This value should be a valid number.', |
41
|
|
|
'sort' => 'The value you selected is not a valid choice.', |
42
|
|
|
'created_at' => 'This value is not a valid date.', |
43
|
|
|
), |
44
|
|
|
), json_decode($response->getContent(), true)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function testAnnotationsApiParameters() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$client = static::createClient(); |
50
|
|
|
|
51
|
|
|
$client->request('POST', '/example/api/ws-annotations?page=1&max=20&sort=name', array('created_at' => '2014-01-01')); |
52
|
|
|
$response = $client->getResponse(); |
53
|
|
|
$this->assertEquals(array( |
54
|
|
|
'page' => '1', |
55
|
|
|
'max' => '20', |
56
|
|
|
'sort' => 'name', |
57
|
|
|
'created_at' => '2014-01-01', |
58
|
|
|
), json_decode($response->getContent(), true)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
public function testAnnotationsApiParametersWithValidation() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$client = static::createClient(); |
64
|
|
|
|
65
|
|
|
$client->request('POST', '/example/api/ws-annotations-validation?page=test&max=20&sort=address', array('created_at' => '08/11/2014')); |
66
|
|
|
$response = $client->getResponse(); |
67
|
|
|
|
68
|
|
|
$this->assertEquals(400, $response->getStatusCode()); |
69
|
|
|
|
70
|
|
|
$this->assertEquals(array( |
71
|
|
|
'errors' => array( |
72
|
|
|
'page' => 'This value should be a valid number.', |
73
|
|
|
'sort' => 'The value you selected is not a valid choice.', |
74
|
|
|
'created_at' => 'This value is not a valid date.', |
75
|
|
|
), |
76
|
|
|
), json_decode($response->getContent(), true)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testAnnotationsApiParametersWithValidationXml() |
80
|
|
|
{ |
81
|
|
|
$client = static::createClient(); |
82
|
|
|
|
83
|
|
|
$client->request('POST', '/example/api/ws-annotations-validation?_format=xml&page=test&max=20&sort=address', array('created_at' => '08/11/2014')); |
84
|
|
|
$response = $client->getResponse(); |
85
|
|
|
|
86
|
|
|
$this->assertEquals(400, $response->getStatusCode()); |
87
|
|
|
|
88
|
|
|
$this->assertXmlStringEqualsXmlString( |
89
|
|
|
'<result> |
90
|
|
|
<errors> |
91
|
|
|
<page>This value should be a valid number.</page> |
92
|
|
|
<sort>The value you selected is not a valid choice.</sort> |
93
|
|
|
<created_at>This value is not a valid date.</created_at> |
94
|
|
|
</errors> |
95
|
|
|
</result>', |
96
|
|
|
$response->getContent() |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
View Code Duplication |
public function testAnnotationsAsParam() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$client = static::createClient(); |
103
|
|
|
|
104
|
|
|
$client->request('POST', '/example/api/ws-annotations-as?page=1&max=20&sort=name', array('created_at' => '2014-01-01')); |
105
|
|
|
$response = $client->getResponse(); |
106
|
|
|
$this->assertEquals(array( |
107
|
|
|
'page' => '1', |
108
|
|
|
'max' => '20', |
109
|
|
|
'sort' => 'name', |
110
|
|
|
'created_at' => '2014-01-01', |
111
|
|
|
), json_decode($response->getContent(), true)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
View Code Duplication |
public function testRoutinAsParam() |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
$client = static::createClient(); |
117
|
|
|
|
118
|
|
|
$client->request('POST', '/example/api/ws-routing-as?page=1&max=20&sort=name', array('created_at' => '2014-01-01')); |
119
|
|
|
$response = $client->getResponse(); |
120
|
|
|
$this->assertEquals(array( |
121
|
|
|
'page' => '1', |
122
|
|
|
'max' => '20', |
123
|
|
|
'sort' => 'name', |
124
|
|
|
'created_at' => '2014-01-01', |
125
|
|
|
), json_decode($response->getContent(), true)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testErrorsWithSubCollections() |
129
|
|
|
{ |
130
|
|
|
$client = static::createClient(); |
131
|
|
|
|
132
|
|
|
$postArray = array( |
133
|
|
|
'filters' => array( |
134
|
|
|
'lastname' => 'test', |
135
|
|
|
), |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
$client->request( |
139
|
|
|
'POST', |
140
|
|
|
'/example/api/sub-collection-test', |
141
|
|
|
$postArray |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$response = $client->getResponse(); |
145
|
|
|
$data = json_decode($response->getContent(), true); |
146
|
|
|
|
147
|
|
|
$errorData = array( |
148
|
|
|
'errors' => array( |
149
|
|
|
'page' => 'search.missing_fields', |
150
|
|
|
'filters' => array( |
151
|
|
|
'firstname' => 'filter.missing_fields', |
152
|
|
|
), |
153
|
|
|
), |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
$this->assertEquals($errorData, $data); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.