diff --git a/CHANGELOG.md b/CHANGELOG.md index deab1ed..f446d6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,10 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased](https://github.com/orca-services/cakephp-data-validation-testing) ### Added +- `testDataValidationContains()` to assert that specific validation errors are present, ignoring others on the same field. ### Changed +- `testDataValidationScalar()` now checks only the `scalar` rule by default. ### Fixed diff --git a/src/Traits/DataValidationTestTrait.php b/src/Traits/DataValidationTestTrait.php index 51a51be..8d991fd 100644 --- a/src/Traits/DataValidationTestTrait.php +++ b/src/Traits/DataValidationTestTrait.php @@ -328,6 +328,43 @@ protected function testDataValidation( static::assertEquals($expected, $errors); } + /** + * Validate that a field's data validation error array contains the expected "rule name" => "message" pair(s) + * + * Other validation errors will be ignored. + * + * @param Table $table The table to test. + * @param string $fieldName The field to check for data validation errors. + * @param array $dataSet The data set to test. + * @param array $expected The expected data validation errors ("rule name" => "message") that must be present. + * @param array $options Additional options for newEntity. + * @return void + * @see \Cake\Validation\Validator::validate() + */ + protected function testDataValidationContains( + Table $table, + string $fieldName, + array $dataSet, + array $expected, + array $options = [], + ): void { + $entity = $table->newEntity($dataSet, $options); + $errors = $entity->getError($fieldName); + + foreach ($expected as $rule => $message) { + static::assertArrayHasKey($rule, $errors, sprintf( + 'Field `%s` does not have expected validation error `%s`.', + $fieldName, + $rule, + )); + static::assertSame($message, $errors[$rule], sprintf( + 'Validation error message for field `%s` and rule `%s` does not match expected.', + $fieldName, + $rule, + )); + } + } + /** * Validate that a given data set for a given table leads to the expected rule errors * @@ -473,13 +510,10 @@ protected function testDataValidationScalar( ?array $expected = null, ?array $options = [], ): void { - $dataset = [$fieldName => []]; + $dataset = [$fieldName => []]; // A non-scalar value - $expected ??= [ - 'scalar' => 'The provided value must be scalar', - 'maxLength' => 'The provided value must be at most `50` characters long', - ]; - $this->testDataValidation($table, $fieldName, $dataset, $expected, $options); + $expected ??= ['scalar' => 'The provided value must be scalar']; + $this->testDataValidationContains($table, $fieldName, $dataset, $expected, $options); } /** diff --git a/tests/TestCase/Traits/DataValidationTestTraitTest.php b/tests/TestCase/Traits/DataValidationTestTraitTest.php index 444e5b0..e415bef 100644 --- a/tests/TestCase/Traits/DataValidationTestTraitTest.php +++ b/tests/TestCase/Traits/DataValidationTestTraitTest.php @@ -280,12 +280,9 @@ public function testTestDataValidationScalar(): void { // Ensure data validation of the field works as expected first $field = 'scalar_field'; - $expectedErrors = [ - 'scalar' => 'The provided value must be scalar', - 'maxLength' => 'The provided value must be at most `50` characters long', - ]; + $expectedErrors = ['scalar' => 'The provided value must be scalar']; $dataSet = [$field => []]; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->testDataValidationContains($this->table, $field, $dataSet, $expectedErrors); $this->testDataValidationScalar($this->table, $field); }