Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Add a new test method to assert a field enforces the greater than or equal rule [#13](https://github.com/orca-services/cakephp-data-validation-testing/issues/13)

### Changed

### Fixed
Expand Down
41 changes: 40 additions & 1 deletion src/Traits/DataValidationTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ protected function testDataValidationNonNegativeInteger(
Table $table,
string $fieldName,
?array $expected = null,
?array $options = []
?array $options = [],
): void {
// Negative integer
$dataset = [$fieldName => '-1'];
Expand All @@ -557,6 +557,45 @@ protected function testDataValidationNonNegativeInteger(
$this->testDataValidationNoErrors($table, $fieldName, $dataset, $options);
}

/**
* Validate that a value is greater than or equal to a given threshold for a given table
*
* @param Table $table The table to test
* @param string $fieldName The field to check for data validation errors.
* @param float|int $value The threshold the field value must be greater than or equal to.
* @param array|null $expected The expected data validation errors when the value is below the threshold.
* @param array $additionalDataSet Additional data set to test.
* @param ?array $options Additional options for newEntity.
* @return void
*/
protected function testDataValidationGreaterThanOrEqual(
Table $table,
string $fieldName,
float|int $value,
?array $expected = null,
array $additionalDataSet = [],
?array $options = [],
): void {
$options ??= [];

// Invalid value: just below the threshold
$belowThreshold = is_int($value) ? $value - 1 : $value - 0.01;
$dataset = array_merge($additionalDataSet, [$fieldName => $belowThreshold]);

$expected ??= [
'greaterThanOrEqual' => sprintf(
'The provided value must be greater than or equal to `%s`',
$value,
),
];
$this->testDataValidation($table, $fieldName, $dataset, $expected, $options);

// Valid values: exactly at and just above the threshold
$aboveThreshold = is_int($value) ? $value + 1 : $value + 0.01;
$list = [$value, $aboveThreshold];
$this->testDataValidationInList($table, $list, $fieldName, [], $additionalDataSet, $options);
}

/**
* Validate the length between data validation of a field for a given table
*
Expand Down
2 changes: 2 additions & 0 deletions tests/TestApp/Model/Table/ValidationTestTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public function validationDefault(Validator $validator): Validator
->allowEmptyString('integer_field')
->nonNegativeInteger('non_negative_integer_field')
->allowEmptyString('non_negative_integer_field')
->greaterThanOrEqual('greater_than_or_equal_field', 10)
->allowEmptyString('greater_than_or_equal_field')
->lengthBetween('length_between_field', [5, 10])
->allowEmptyString('length_between_field')
->naturalNumber('natural_number_field')
Expand Down
34 changes: 34 additions & 0 deletions tests/TestCase/Traits/DataValidationTestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,40 @@ public function testTestDataValidationNonNegativeInteger(): void
$this->testDataValidationNonNegativeInteger($this->table, $field);
}

/**
* Test that testDataValidationGreaterThanOrEqual passes when the field value
* is greater than or equal to the configured threshold.
*
* @return void
* @covers ::testDataValidationGreaterThanOrEqual
*/
public function testTestDataValidationGreaterThanOrEqual(): void
{
// Ensure data validation of the field works as expected first
$threshold = 10;
$field = 'greater_than_or_equal_field';
$expectedErrors = [
'greaterThanOrEqual' => sprintf(
'The provided value must be greater than or equal to `%s`',
$threshold,
),
];

// Just below the threshold should fail
$dataSet = [$field => $threshold - 1];
$this->testDataValidation($this->table, $field, $dataSet, $expectedErrors);

// At the threshold should pass
$dataSet = [$field => $threshold];
$this->testDataValidationNoErrors($this->table, $field, $dataSet);

// Above the threshold should pass
$dataSet = [$field => $threshold + 1];
$this->testDataValidationNoErrors($this->table, $field, $dataSet);

$this->testDataValidationGreaterThanOrEqual($this->table, $field, $threshold);
}

/**
* Test that testDataValidationLengthBetween passes when the field is between the min and max length.
*
Expand Down
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
scalar_field VARCHAR(255),
decimal_field DECIMAL,
non_negative_integer_field INT,
greater_than_or_equal_field INT,
integer_field INT,
length_between_field VARCHAR(255),
natural_number_field INTEGER,
Expand Down