diff --git a/phpstan-baseline.php b/phpstan-baseline.php index c49b156548eb..2fd4d79af642 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -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\\, array\\ given\\.$#', diff --git a/tests/system/ControllerTest.php b/tests/system/ControllerTest.php index 7cbe664aabc0..add07fc20dd4 100644 --- a/tests/system/ControllerTest.php +++ b/tests/system/ControllerTest.php @@ -126,10 +126,17 @@ public function testValidateWithStringRulesNotFound(): void public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig(): void { $validation = new class () extends ValidationConfig { - public $signup = [ + /** + * @var array + */ + public array $signup = [ 'username' => 'required', ]; - public $signup_errors = [ + + /** + * @var array> + */ + public array $signup_errors = [ 'username' => [ 'required' => 'You must choose a username.', ], @@ -149,7 +156,10 @@ public function testValidateWithStringRulesFoundReadMessagesFromValidationConfig public function testValidateWithStringRulesFoundUseMessagesParameter(): void { $validation = new class () extends ValidationConfig { - public $signup = [ + /** + * @var array + */ + public array $signup = [ 'username' => 'required', ]; }; @@ -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 {