Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for validateData() with custom error messages #9102

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading