-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResolverTest.php
201 lines (165 loc) · 5.5 KB
/
ResolverTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* PHP version 5.5
*
* This source file is subject to the license that is bundled with this package in the file LICENSE.
*
* @copyright Comunidad PHP Puebla 2015 (http://www.comunidadphppuebla.com)
*/
namespace ComPHPPuebla\Slim;
use PHPUnit_Framework_TestCase as TestCase;
use stdClass;
use Slim\Environment;
use Slim\Http\Request;
use Slim\Slim;
class TestController
{
public function test() {}
public function testWithArgument() {}
}
class ResolverTest extends TestCase
{
/** @test */
function it_should_call_a_controller_with_the_default_arguments()
{
Environment::mock([
'PATH_INFO' => '/test',
]);
$controller = $this->getMock(TestController::class);
$controller
->expects($this->once())
->method('test')
->with($this->isInstanceOf(Request::class), $this->isInstanceOf(Slim::class))
;
$resolver = new Resolver();
$app = new Slim();
$app->container->singleton('controller', function () use ($controller) {
return $controller;
});
$app->get('/test', $resolver->resolve($app, 'controller:test'));
$app->run();
}
/** @test */
function it_should_call_a_controller_with_the_default_and_route_arguments()
{
$expectedArgument = 'leonard_nimoy';
Environment::mock([
'PATH_INFO' => "/test/$expectedArgument",
]);
$controller = $this->getMock(TestController::class);
$controller
->expects($this->once())
->method('testWithArgument')
->with(
$expectedArgument,
$this->isInstanceOf(Request::class),
$this->isInstanceOf(Slim::class)
)
;
$resolver = new Resolver();
$app = new Slim();
$app->container->singleton('controller', function () use ($controller) {
return $controller;
});
$app->get('/test/:name', $resolver->resolve($app, 'controller:testWithArgument'));
$app->run();
}
/** @test */
function it_should_call_a_controller_with_arguments_modified_by_a_converter()
{
$expectedArgument = 'leonard_nimoy';
Environment::mock([
'PATH_INFO' => "/test/$expectedArgument",
]);
$controller = $this->getMock(TestController::class);
$controller
->expects($this->once())
->method('testWithArgument')
->with($expectedArgument) // Converter will remove the request and application arguments
;
$resolver = new Resolver();
$app = new Slim();
$app->container->singleton('controller', function () use ($controller) {
return $controller;
});
$app->get(
'/test/:name',
$resolver->resolve(
$app,
'controller:testWithArgument',
function(array $arguments) {
return [$arguments[0]]; // It removes the request and application arguments
}
)
);
$app->run();
}
/** @test */
function it_should_call_a_controller_with_arguments_defined_by_a_converter()
{
$expectedArgument = 'leonard_nimoy';
Environment::mock([
'PATH_INFO' => "/test/$expectedArgument",
]);
$controller = $this->getMock(TestController::class);
$controller
->expects($this->once())
->method('testWithArgument')
->with($this->isInstanceOf(stdClass::class)) // Converter will replace default arguments
;
$resolver = new Resolver();
$app = new Slim();
$app->container->singleton('controller', function () use ($controller) {
return $controller;
});
$app->get(
'/test/:name',
$resolver->resolve(
$app,
'controller:testWithArgument',
function(array $arguments) {
return [new stdClass()]; // It replaces default arguments
}
)
);
$app->run();
}
/** @test */
function it_should_extend_a_service_definition()
{
$resolver = new Resolver();
$app = new Slim();
$app->container->singleton('service', function() {
return new stdClass();
});
$resolver->extend($app, 'service', function(stdClass $service) {
$service->modified = true;
return $service;
});
$service = $app->container->get('service');
$this->assertObjectHasAttribute('modified', $service);
$this->assertTrue($service->modified);
}
/** @test */
function it_should_allow_extending_a_service_definition_more_than_once()
{
$resolver = new Resolver();
$app = new Slim();
$app->container->singleton('service', function() {
return new stdClass();
});
$resolver->extend($app, 'service', function(stdClass $service) {
$service->first = true;
return $service;
});
$resolver->extend($app, 'service', function(stdClass $service) {
$service->second = true;
return $service;
});
$service = $app->container->get('service');
$this->assertObjectHasAttribute('first', $service);
$this->assertTrue($service->first);
$this->assertObjectHasAttribute('second', $service);
$this->assertTrue($service->second);
}
}