Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased](https://github.com/orca-services/cakephp-data-validation-testing)

### Added
- Add validation trait method for numeric range

### Changed

Expand Down
1 change: 1 addition & 0 deletions docs/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Each helper builds an entity, runs the validator, and asserts the expected error
- `testDataValidationMaxLength($table, $fieldName, $maxLength)`
- `testDataValidationMinLength($table, $fieldName, $minLength, $expected)`
- `testDataValidationLengthBetween($table, $fieldName, $minLength, $maxLength)`
- `testDataValidationRange($table, $fieldName, $lowerBound, $upperBound)`

### Generic helpers

Expand Down
82 changes: 73 additions & 9 deletions src/Traits/DataValidationTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,16 +352,24 @@ protected function testDataValidationContains(
$errors = $entity->getError($fieldName);

foreach ($expected as $rule => $message) {
static::assertArrayHasKey($rule, $errors, sprintf(
'Field `%s` does not have expected validation error `%s`.',
$fieldName,
static::assertArrayHasKey(
$rule,
));
static::assertSame($message, $errors[$rule], sprintf(
'Validation error message for field `%s` and rule `%s` does not match expected.',
$fieldName,
$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,
),
);
}
}

Expand Down Expand Up @@ -792,6 +800,62 @@ protected function testDataValidationLengthBetween(
$this->testDataValidation($table, $fieldName, $dataset, $expected, $options);
}

/**
* Validate the range data validation of a field for a given table
*
* @param Table $table The table to test.
* @param string $fieldName The field to check the range.
* @param float|int $lowerBound The lower bound to check with.
* @param float|int $upperBound The upper bound to check with.
* @param array|null $expected The expected data validation errors.
* @param ?array $options Additional options for newEntity.
* @return void
* @see \Cake\Validation\Validator::range()
Comment thread
Murl080 marked this conversation as resolved.
*/
protected function testDataValidationRange(
Comment thread
Murl080 marked this conversation as resolved.
Table $table,
string $fieldName,
int|float $lowerBound,
int|float $upperBound,
?array $expected = null,
?array $options = [],
): void {
// Too low
$tooLowFloat = $lowerBound - 0.1;
$tooLowInt = $lowerBound - 1;

// Too high
$tooHighFloat = $upperBound + 0.1;
$tooHighInt = $upperBound + 1;

$list = [
$tooLowFloat,
$tooLowInt,
$tooHighFloat,
$tooHighInt,
];

$expected ??= [
'range' => sprintf(
'The provided value must be between `%s` and `%s`, inclusively',
$lowerBound,
$upperBound,
),
];
$this->testDataValidationInList($table, $list, $fieldName, $expected, $options);

// Valid
$list = [
(float)$lowerBound,
(int)$lowerBound,
(float)$upperBound,
(int)$upperBound,
];

$expected = [];
$this->testDataValidationInList($table, $list, $fieldName, $expected, $options);
}

/**
* Validate that a given field is validated as a natural number (positive integers only)
*
Expand Down
3 changes: 2 additions & 1 deletion tests/TestApp/Model/Table/ValidationTestTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public function validationDefault(Validator $validator): Validator
->allowEmptyString('parent_id')
->integer('parent_id')
->email('email_field')
->uuid('uuid_field');
->uuid('uuid_field')
->range('range_field', [-30.0, 30.0]);

return $validator;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/TestCase/Traits/DataValidationTestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,29 @@ public function testTestDataValidationLengthBetween(): void
$this->testDataValidationLengthBetween($this->table, $field, $minLength, $maxLength);
}

/**
* Test that testDataValidationRange passes when the field is between the lower and upper bound.
*
* @return void
* @covers ::testDataValidationRange
*/
public function testTestDataValidationRange(): void
{
// Ensure data validation of the field works as expected first
$lowerBound = -30.0;
$upperBound = 30.0;
$field = 'range_field';
$expectedErrors = ['range' => 'The provided value must be between `-30` and `30`, inclusively'];

$dataSet = [$field => $lowerBound - 1];
$this->testDataValidation($this->table, $field, $dataSet, $expectedErrors);

$dataSet = [$field => $upperBound + 1];
$this->testDataValidation($this->table, $field, $dataSet, $expectedErrors);

$this->testDataValidationRange($this->table, $field, $lowerBound, $upperBound);
}

/**
* Test that testDataValidationNaturalNumber passes when the field is a natural number.
*
Expand Down
3 changes: 2 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
natural_number_field INTEGER,
unique_field VARCHAR(255) UNIQUE,
email_field VARCHAR(255),
uuid VARCHAR(36)
uuid VARCHAR(36),
range_field INT
)
');

Expand Down