Skip to content

Commit

Permalink
Merge pull request #9102 from kenjis/test-controller-validateData
Browse files Browse the repository at this point in the history
test: add tests for `validateData()` with custom error messages
  • Loading branch information
kenjis committed Aug 6, 2024
2 parents 2b3b8ed + 72c92e0 commit 0e96b3c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 21 deletions.
18 changes: 0 additions & 18 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -12955,24 +12955,6 @@
'count' => 1,
'path' => __DIR__ . '/tests/system/ControllerTest.php',
];
$ignoreErrors[] = [
// identifier: missingType.property
'message' => '#^Property class@anonymous/tests/system/ControllerTest\\.php\\:128\\:\\:\\$signup has no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/tests/system/ControllerTest.php',
];
$ignoreErrors[] = [
// identifier: missingType.property
'message' => '#^Property class@anonymous/tests/system/ControllerTest\\.php\\:128\\:\\:\\$signup_errors has no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/tests/system/ControllerTest.php',
];
$ignoreErrors[] = [
// identifier: missingType.property
'message' => '#^Property class@anonymous/tests/system/ControllerTest\\.php\\:151\\:\\:\\$signup has no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/tests/system/ControllerTest.php',
];
$ignoreErrors[] = [
// identifier: argument.type
'message' => '#^Parameter \\#1 \\$cookies of class CodeIgniter\\\\Cookie\\\\CookieStore constructor expects array\\<CodeIgniter\\\\Cookie\\\\Cookie\\>, array\\<int, DateTimeImmutable\\> given\\.$#',
Expand Down
87 changes: 84 additions & 3 deletions tests/system/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,17 @@ public function testValidateWithStringRulesNotFound(): void
public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig(): void
{
$validation = new class () extends ValidationConfig {
public $signup = [
/**
* @var array<string, string>
*/
public array $signup = [
'username' => 'required',
];
public $signup_errors = [

/**
* @var array<string, array<string, string>>
*/
public array $signup_errors = [
'username' => [
'required' => 'You must choose a username.',
],
Expand All @@ -149,7 +156,10 @@ public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig
public function testValidateWithStringRulesFoundUseMessagesParameter(): void
{
$validation = new class () extends ValidationConfig {
public $signup = [
/**
* @var array<string, string>
*/
public array $signup = [
'username' => 'required',
];
};
Expand Down Expand Up @@ -191,6 +201,77 @@ public function testValidateData(): void
);
}

public function testValidateDataWithCustomErrorMessage(): void
{
// make sure we can instantiate one
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);

$method = $this->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.',
Services::validation()->getError('username')
);
$this->assertSame(
'The password field must be at least 10 characters in length.',
Services::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 = $this->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.',
Services::validation()->getError('username')
);
$this->assertSame(
'The Password field must be at least 10 characters in length.',
Services::validation()->getError('password')
);
}

public function testHelpers(): void
{
$this->controller = new class () extends Controller {
Expand Down

0 comments on commit 0e96b3c

Please sign in to comment.