diff --git a/CHANGELOG.md b/CHANGELOG.md index ec2056e..2b82050 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Traits/DataValidationTestTrait.php b/src/Traits/DataValidationTestTrait.php index fb9d04d..b8af777 100644 --- a/src/Traits/DataValidationTestTrait.php +++ b/src/Traits/DataValidationTestTrait.php @@ -541,7 +541,7 @@ protected function testDataValidationNonNegativeInteger( Table $table, string $fieldName, ?array $expected = null, - ?array $options = [] + ?array $options = [], ): void { // Negative integer $dataset = [$fieldName => '-1']; @@ -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 * diff --git a/tests/TestApp/Model/Table/ValidationTestTable.php b/tests/TestApp/Model/Table/ValidationTestTable.php index 2dc4194..480ac04 100644 --- a/tests/TestApp/Model/Table/ValidationTestTable.php +++ b/tests/TestApp/Model/Table/ValidationTestTable.php @@ -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') diff --git a/tests/TestCase/Traits/DataValidationTestTraitTest.php b/tests/TestCase/Traits/DataValidationTestTraitTest.php index d1d393c..5f78355 100644 --- a/tests/TestCase/Traits/DataValidationTestTraitTest.php +++ b/tests/TestCase/Traits/DataValidationTestTraitTest.php @@ -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. * diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3b0e89d..b5188ce 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -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,