Skip to content

Commit c408acf

Browse files
committed
test: add tests for validateData() with custom error messages
1 parent 3143cf3 commit c408acf

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

tests/system/ControllerTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,77 @@ public function testValidateData(): void
191191
);
192192
}
193193

194+
public function testValidateDataWithCustomErrorMessage(): void
195+
{
196+
// make sure we can instantiate one
197+
$this->controller = new Controller();
198+
$this->controller->initController($this->request, $this->response, $this->logger);
199+
200+
$method = $this->getPrivateMethodInvoker($this->controller, 'validateData');
201+
202+
$data = [
203+
'username' => 'a',
204+
'password' => '123',
205+
];
206+
$rules = [
207+
'username' => 'required|min_length[3]',
208+
'password' => 'required|min_length[10]',
209+
];
210+
$errors = [
211+
'username' => [
212+
'required' => 'Please fill "{field}".',
213+
'min_length' => '"{field}" must be {param} letters or longer.',
214+
],
215+
];
216+
$this->assertFalse($method($data, $rules, $errors));
217+
$this->assertSame(
218+
'"username" must be 3 letters or longer.',
219+
Services::validation()->getError('username')
220+
);
221+
$this->assertSame(
222+
'The password field must be at least 10 characters in length.',
223+
Services::validation()->getError('password')
224+
);
225+
}
226+
227+
public function testValidateDataWithCustomErrorMessageLabeledStyle(): void
228+
{
229+
// make sure we can instantiate one
230+
$this->controller = new Controller();
231+
$this->controller->initController($this->request, $this->response, $this->logger);
232+
233+
$method = $this->getPrivateMethodInvoker($this->controller, 'validateData');
234+
235+
$data = [
236+
'username' => 'a',
237+
'password' => '123',
238+
];
239+
$rules = [
240+
'username' => [
241+
'label' => 'Username',
242+
'rules' => 'required|min_length[3]',
243+
'errors' => [
244+
'required' => 'Please fill "{field}".',
245+
'min_length' => '"{field}" must be {param} letters or longer.',
246+
],
247+
],
248+
'password' => [
249+
'required|min_length[10]',
250+
'label' => 'Password',
251+
'rules' => 'required|min_length[10]',
252+
],
253+
];
254+
$this->assertFalse($method($data, $rules));
255+
$this->assertSame(
256+
'"Username" must be 3 letters or longer.',
257+
Services::validation()->getError('username')
258+
);
259+
$this->assertSame(
260+
'The Password field must be at least 10 characters in length.',
261+
Services::validation()->getError('password')
262+
);
263+
}
264+
194265
public function testHelpers(): void
195266
{
196267
$this->controller = new class () extends Controller {

0 commit comments

Comments
 (0)