1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace User\Tests\Controller; |
5
|
|
|
|
6
|
|
|
use Common\Response as View; |
7
|
|
|
use Doctrine\Common\DataFixtures\Executor\ORMExecutor; |
8
|
|
|
use Doctrine\Common\DataFixtures\Loader; |
9
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
10
|
|
|
use Silex\WebTestCase; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* User Controller Test Case |
14
|
|
|
* |
15
|
|
|
* @author Thiago Paes <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class UserTest extends WebTestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \Silex\Application |
21
|
|
|
*/ |
22
|
|
|
protected static $application; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $header = [ |
28
|
|
|
'HTTP_Content-type' => 'application/json' |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class Bootstrap |
33
|
|
|
* |
34
|
|
|
* Load fixtures before controllers tests |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public static function setUpBeforeClass() |
39
|
|
|
{ |
40
|
|
|
if (!empty(self::$application) && self::$application['doctrine_orm.em']->isOpen()) { |
41
|
|
|
return self::$application; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$app = include __DIR__ . '/../../../../bootstrap.php'; |
45
|
|
|
|
46
|
|
|
/* @var $metadata array */ |
47
|
|
|
$metadata = $app['doctrine_orm.em']->getMetadataFactory()->getAllMetadata(); |
48
|
|
|
|
49
|
|
|
/* @var $tool SchemaTool */ |
50
|
|
|
$tool = new SchemaTool($app['doctrine_orm.em']); |
51
|
|
|
$tool->createSchema($metadata); |
52
|
|
|
|
53
|
|
|
/* @var $loader Loader */ |
54
|
|
|
$loader = new Loader(); |
55
|
|
|
$loader->loadFromDirectory(__DIR__ . '/../../../../fixtures'); |
56
|
|
|
|
57
|
|
|
/* @var $executor ORMExecutor */ |
58
|
|
|
$executor = new ORMExecutor($app['doctrine_orm.em']); |
59
|
|
|
$executor->execute($loader->getFixtures(), true); |
60
|
|
|
|
61
|
|
|
$app['logger']->addDebug('Fixtures loaded to ' . static::class); |
62
|
|
|
|
63
|
|
|
self::$application = $app; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Test bootstrap |
68
|
|
|
* |
69
|
|
|
* @return \Silex\Application |
70
|
|
|
*/ |
71
|
|
|
public function createApplication() |
72
|
|
|
{ |
73
|
|
|
return $this->app = self::$application; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Data Provider |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function validObjects() |
82
|
|
|
{ |
83
|
|
|
return [ |
84
|
|
|
[ |
85
|
|
|
[ |
86
|
|
|
"name" => "Fooo Bar bar", |
87
|
|
|
"email" => "[email protected]" |
88
|
|
|
] |
89
|
|
|
] |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Data Provider |
95
|
|
|
* |
96
|
|
|
* @return multitype:multitype:multitype:string |
|
|
|
|
97
|
|
|
*/ |
98
|
|
|
public function validProfileIds() |
99
|
|
|
{ |
100
|
|
|
return |
101
|
|
|
[ |
102
|
|
|
[ |
103
|
|
|
1 |
104
|
|
|
] |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @test |
110
|
|
|
*/ |
111
|
|
|
public function getWithoutHeader() |
112
|
|
|
{ |
113
|
|
|
/* @var $client \Symfony\Component\HttpKernel\Client */ |
114
|
|
|
$client = $this->createClient(); |
115
|
|
|
|
116
|
|
|
/* @var $crawler \Symfony\Component\DomCrawler\Crawler */ |
117
|
|
|
$crawler = $client->request('GET', '/user/'); |
|
|
|
|
118
|
|
|
|
119
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
120
|
|
|
$response = $client->getResponse(); |
121
|
|
|
|
122
|
|
|
/* @var $content string */ |
123
|
|
|
$content = $response->getContent(); |
124
|
|
|
|
125
|
|
|
/* @var $object \stdClass */ |
126
|
|
|
$object = json_decode($content); |
127
|
|
|
|
128
|
|
|
$this->assertJson($content); |
129
|
|
|
$this->assertEquals(View::HTTP_OK, $response->getStatusCode()); |
130
|
|
|
$this->assertEquals(View::HTTP_OK, $object->code); |
131
|
|
|
$this->assertTrue(is_array($object->data)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @test |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function getWithHeader() |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
/* @var $client \Symfony\Component\HttpKernel\Client */ |
140
|
|
|
$client = $this->createClient(); |
141
|
|
|
|
142
|
|
|
/* @var $crawler \Symfony\Component\DomCrawler\Crawler */ |
143
|
|
|
$crawler = $client->request('GET', '/user/', [], [], $this->header); |
|
|
|
|
144
|
|
|
|
145
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
146
|
|
|
$response = $client->getResponse(); |
147
|
|
|
|
148
|
|
|
/* @var $content string */ |
149
|
|
|
$content = $response->getContent(); |
150
|
|
|
|
151
|
|
|
/* @var $object \stdClass */ |
152
|
|
|
$object = json_decode($content); |
153
|
|
|
|
154
|
|
|
$this->assertJson($content); |
155
|
|
|
$this->assertEquals(View::HTTP_OK, $response->getStatusCode()); |
156
|
|
|
$this->assertEquals(View::HTTP_OK, $object->code); |
157
|
|
|
$this->assertTrue(is_array($object->data)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @test |
162
|
|
|
* @dataProvider validProfileIds |
163
|
|
|
*/ |
164
|
|
View Code Duplication |
public function getWithUserIdWithoutHeader($id) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
/* @var $client \Symfony\Component\HttpKernel\Client */ |
167
|
|
|
$client = $this->createClient(); |
168
|
|
|
|
169
|
|
|
/* @var $crawler \Symfony\Component\DomCrawler\Crawler */ |
170
|
|
|
$crawler = $client->request('GET', '/user/' . $id); |
|
|
|
|
171
|
|
|
|
172
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
173
|
|
|
$response = $client->getResponse(); |
174
|
|
|
|
175
|
|
|
/* @var $content string */ |
176
|
|
|
$content = $response->getContent(); |
177
|
|
|
|
178
|
|
|
/* @var $object \stdClass */ |
179
|
|
|
$object = json_decode($content); |
180
|
|
|
|
181
|
|
|
$this->assertJson($content); |
182
|
|
|
$this->assertEquals(View::HTTP_OK, $response->getStatusCode()); |
183
|
|
|
$this->assertEquals(View::HTTP_OK, $object->code); |
184
|
|
|
$this->assertNotEmpty($object->data); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @test |
189
|
|
|
* @dataProvider validProfileIds |
190
|
|
|
*/ |
191
|
|
View Code Duplication |
public function getUserUserIdWithHeader($id) |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
/* @var $client \Symfony\Component\HttpKernel\Client */ |
194
|
|
|
$client = $this->createClient(); |
195
|
|
|
|
196
|
|
|
/* @var $crawler \Symfony\Component\DomCrawler\Crawler */ |
197
|
|
|
$crawler = $client->request('GET', '/user/' . $id, [], [], $this->header); |
|
|
|
|
198
|
|
|
|
199
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
200
|
|
|
$response = $client->getResponse(); |
201
|
|
|
|
202
|
|
|
/* @var $content string */ |
203
|
|
|
$content = $response->getContent(); |
204
|
|
|
|
205
|
|
|
/* @var $object \stdClass */ |
206
|
|
|
$object = json_decode($content); |
207
|
|
|
|
208
|
|
|
$this->assertJson($content); |
209
|
|
|
$this->assertEquals(View::HTTP_OK, $response->getStatusCode()); |
210
|
|
|
$this->assertEquals(View::HTTP_OK, $object->code); |
211
|
|
|
$this->assertNotEmpty($object->data); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.