forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerTest.php
More file actions
285 lines (246 loc) · 9.21 KB
/
ControllerTest.php
File metadata and controls
285 lines (246 loc) · 9.21 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter;
use CodeIgniter\Config\Factories;
use CodeIgniter\HTTP\Exceptions\RedirectException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Validation\Exceptions\ValidationException;
use Config\App;
use Config\Validation as ValidationConfig;
use PHPUnit\Framework\Attributes\BackupGlobals;
use PHPUnit\Framework\Attributes\Group;
use Psr\Log\LoggerInterface;
/**
* Exercise our core Controller class.
* Not a lot of business logic, so concentrate on making sure
* we can exercise everything without blowing up :-/
*
* @internal
*/
#[BackupGlobals(true)]
#[Group('Others')]
final class ControllerTest extends CIUnitTestCase
{
private ?Controller $controller = null;
/**
* Current request.
*/
private Request $request;
/**
* Current response.
*/
private Response $response;
private LoggerInterface $logger;
protected function setUp(): void
{
parent::setUp();
$config = new App();
$this->request = new IncomingRequest($config, new SiteURI($config), null, new UserAgent());
$this->response = new Response($config);
$this->logger = service('logger');
}
public function testConstructor(): void
{
// make sure we can instantiate one
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);
$this->assertInstanceOf(Controller::class, $this->controller);
}
public function testConstructorHTTPS(): void
{
$original = $_SERVER;
$_SERVER = ['HTTPS' => 'on'];
// make sure we can instantiate one
try {
$this->controller = new class () extends Controller {
protected $forceHTTPS = 1;
};
$this->controller->initController($this->request, $this->response, $this->logger);
} catch (RedirectException) {
}
$this->assertInstanceOf(Controller::class, $this->controller);
$_SERVER = $original; // restore so code coverage doesn't break
}
public function testCachePage(): void
{
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);
$method = self::getPrivateMethodInvoker($this->controller, 'cachePage');
$this->assertNull($method(10));
}
public function testValidate(): void
{
// make sure we can instantiate one
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);
// and that we can attempt validation, with no rules
$method = self::getPrivateMethodInvoker($this->controller, 'validate');
$this->assertFalse($method([]));
}
public function testValidateWithStringRulesNotFound(): void
{
$this->expectException(ValidationException::class);
// make sure we can instantiate one
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);
$method = self::getPrivateMethodInvoker($this->controller, 'validate');
$this->assertFalse($method('signup'));
}
public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig(): void
{
$validation = new class () extends ValidationConfig {
/**
* @var array<string, string>
*/
public array $signup = [
'username' => 'required',
];
/**
* @var array<string, array<string, string>>
*/
public array $signup_errors = [
'username' => [
'required' => 'You must choose a username.',
],
];
};
Factories::injectMock('config', 'Validation', $validation);
// make sure we can instantiate one
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);
$method = self::getPrivateMethodInvoker($this->controller, 'validate');
$this->assertFalse($method('signup'));
$this->assertSame('You must choose a username.', service('validation')->getError('username'));
}
public function testValidateWithStringRulesFoundUseMessagesParameter(): void
{
$validation = new class () extends ValidationConfig {
/**
* @var array<string, string>
*/
public array $signup = [
'username' => 'required',
];
};
Factories::injectMock('config', 'Validation', $validation);
// make sure we can instantiate one
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);
$method = self::getPrivateMethodInvoker($this->controller, 'validate');
$this->assertFalse($method('signup', [
'username' => [
'required' => 'You must choose a username.',
],
]));
$this->assertSame('You must choose a username.', service('validation')->getError('username'));
}
public function testValidateData(): void
{
// make sure we can instantiate one
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);
$method = self::getPrivateMethodInvoker($this->controller, 'validateData');
$data = [
'username' => 'mike',
'password' => '123',
];
$rule = [
'username' => 'required',
'password' => 'required|min_length[10]',
];
$this->assertFalse($method($data, $rule));
$this->assertSame(
'The password field must be at least 10 characters in length.',
service('validation')->getError('password'),
);
}
public function testValidateDataWithCustomErrorMessage(): void
{
// make sure we can instantiate one
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);
$method = self::getPrivateMethodInvoker($this->controller, 'validateData');
$data = [
'username' => 'a',
'password' => '123',
];
$rules = [
'username' => 'required|min_length[3]',
'password' => 'required|min_length[10]',
];
$errors = [
'username' => [
'required' => 'Please fill "{field}".',
'min_length' => '"{field}" must be {param} letters or longer.',
],
];
$this->assertFalse($method($data, $rules, $errors));
$this->assertSame(
'"username" must be 3 letters or longer.',
service('validation')->getError('username'),
);
$this->assertSame(
'The password field must be at least 10 characters in length.',
service('validation')->getError('password'),
);
}
public function testValidateDataWithCustomErrorMessageLabeledStyle(): void
{
// make sure we can instantiate one
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);
$method = self::getPrivateMethodInvoker($this->controller, 'validateData');
$data = [
'username' => 'a',
'password' => '123',
];
$rules = [
'username' => [
'label' => 'Username',
'rules' => 'required|min_length[3]',
'errors' => [
'required' => 'Please fill "{field}".',
'min_length' => '"{field}" must be {param} letters or longer.',
],
],
'password' => [
'required|min_length[10]',
'label' => 'Password',
'rules' => 'required|min_length[10]',
],
];
$this->assertFalse($method($data, $rules));
$this->assertSame(
'"Username" must be 3 letters or longer.',
service('validation')->getError('username'),
);
$this->assertSame(
'The Password field must be at least 10 characters in length.',
service('validation')->getError('password'),
);
}
public function testHelpers(): void
{
$this->controller = new class () extends Controller {
protected $helpers = [
'cookie',
'text',
];
};
$this->controller->initController($this->request, $this->response, $this->logger);
$this->assertInstanceOf(Controller::class, $this->controller);
}
}