Skip to content
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
46 changes: 40 additions & 6 deletions src/Traits/DataValidationTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
}

/**
Expand Down
7 changes: 2 additions & 5 deletions tests/TestCase/Traits/DataValidationTestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down