diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b23f1b..3188f41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,15 +6,16 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased](https://github.com/orca-services/cakephp-data-validation-testing) ### Added -- `testDataValidationNotContains()` to assert that specific validation rules are absent, ignoring others on the same field. -- `testDataValidationListContains()` and `testDataValidationListNotContains()` list helpers. -- Optional custom `$expected` parameter for `testDataValidationForeignKey()` and `testDataValidationIsUnique()`. +- `assertValidationNotContains()` to assert that specific validation rules are absent, ignoring others on the same field. +- `assertValidationListContains()` and `assertValidationListNotContains()` list helpers. +- Optional custom `$expected` parameter for `assertValidationForeignKey()` and `assertValidationIsUnique()`. ### Changed +- **BREAKING CHANGE:** Replace `testDataValidation` prefix in all test trait method names with `assertValidation`. E.g. `testDataValidationNotEmpty()` becomes `assertValidationNotEmpty()`. - **BREAKING CHANGE:** All type-specific/rule-dedicated methods now assert only their own validation rule for the field, ignoring others. ### Fixed -- Ignore data validation in `testDataValidationIsUnique()` to correctly assert build rules. +- Ignore data validation in `assertValidationIsUnique()` to correctly assert build rules. ### Dependencies diff --git a/docs/Migration.md b/docs/Migration.md new file mode 100644 index 0000000..548cf47 --- /dev/null +++ b/docs/Migration.md @@ -0,0 +1,61 @@ +# Upgrading CakePHP Data Validation Testing to 3.x + +This major version bundles two breaking changes: + +1. **Method renames** — [PR #45](https://github.com/orca-services/cakephp-data-validation-testing/pull/45) (closes [#44](https://github.com/orca-services/cakephp-data-validation-testing/issues/44)) +2. **Rule-dedicated methods now check only their own rule** — [PR #40](https://github.com/orca-services/cakephp-data-validation-testing/pull/40) (closes [#38](https://github.com/orca-services/cakephp-data-validation-testing/issues/38)) + +--- + +## 1. Method renames + +All `test`/`testData`-prefixed methods on `DataValidationTestTrait` are now prefixed with `assert` instead. +This avoids PHPUnit mistaking them for actual test methods. **No logic changed** only the method names. + +### Rename table + +| Old | New | +|------------------------------------------|----------------------------------------| +| `testDataValidationNotEmpty()` | `assertValidationNotEmpty()` | +| `testDataValidationEmpty()` | `assertValidationEmpty()` | +| `testDataValidationRequired()` | `assertValidationRequired()` | +| `testDataValidationNotRequired()` | `assertValidationNotRequired()` | +| `testDataValidationBoolean()` | `assertValidationBoolean()` | +| `testDataValidationURLWithProtocol()` | `assertValidationURLWithProtocol()` | +| `testDataValidationDateTime()` | `assertValidationDateTime()` | +| `testDataValidationDate()` | `assertValidationDate()` | +| `testDataValidationInList()` | `assertValidationInList()` | +| `testDataValidation()` | `assertValidation()` | +| `testDataValidationNoErrors()` | `assertValidationNoErrors()` | +| `testFullDataValidation()` | `assertValidationTableErrors()` | +| `testFullDataValidationNoErrors()` | `assertValidationTableNoErrors()` | +| `testDataValidationContains()` | `assertValidationContains()` | +| `testDataValidationNotContains()` | `assertValidationNotContains()` | +| `assertDataValidationErrorsContain()` | `assertValidationErrorsContain()` | +| `testDataValidationListContains()` | `assertValidationListContains()` | +| `testDataValidationListNotContains()` | `assertValidationListNotContains()` | +| `testDataRules()` | `assertRules()` | +| `testRules()` | Removed. Use `assertRules()` instead | +| `testDataRulesNoErrors()` | `assertRulesNoErrors()` | +| `testDataValidationMaxLength()` | `assertValidationMaxLength()` | +| `testDataValidationMinLength()` | `assertValidationMinLength()` | +| `testDataValidationScalar()` | `assertValidationScalar()` | +| `testDataValidationDecimal()` | `assertValidationDecimal()` | +| `testDataValidationInteger()` | `assertValidationInteger()` | +| `testDataValidationNonNegativeInteger()` | `assertValidationNonNegativeInteger()` | +| `testDataValidationGreaterThanOrEqual()` | `assertValidationGreaterThanOrEqual()` | +| `testDataValidationEmail()` | `assertValidationEmail()` | +| `testDataValidationUuid()` | `assertValidationUuid()` | +| `testDataValidationLengthBetween()` | `assertValidationLengthBetween()` | +| `testDataValidationRange()` | `assertValidationRange()` | +| `testDataValidationNaturalNumber()` | `assertValidationNaturalNumber()` | +| `testDataValidationForeignKey()` | `assertValidationForeignKey()` | +| `testDataValidationIsUnique()` | `assertValidationIsUnique()` | + +--- + +## 2. Rule-dedicated methods now check only their own rule + +Previously, methods like `assertValidationBoolean()`, `assertValidationEmail()`, `assertValidationInteger()`, etc. compared the **entire** error array for a field against an expected array (or `[]` for valid values). If a field had multiple validation errors, this could hide unrelated errors or cause false failures. + +Now these methods assert **only their own rule key** (present or absent), ignoring any other errors on the same field. diff --git a/docs/Usage.md b/docs/Usage.md index 35ed2d9..1f27fbb 100644 --- a/docs/Usage.md +++ b/docs/Usage.md @@ -34,20 +34,20 @@ All helper methods are `protected` and callable from within your test class. ## Example ```php -public function testValidationTitle(): void +public function assertValidationTitle(): void { // Assert that the "title" field's presence is required - $this->testDataValidationRequired($this->Articles, 'title'); + $this->assertValidationRequired($this->Articles, 'title'); // Assert that the field "title" cannot be empty - $this->testDataValidationNotEmpty($this->Articles, 'title'); + $this->assertValidationNotEmpty($this->Articles, 'title'); // Assert that the "title" field's maximum length is 255 characters - $this->testDataValidationMaxLength($this->Articles, 'title', 255); + $this->assertValidationMaxLength($this->Articles, 'title', 255); } -public function testValidationIsPublished(): void +public function assertValidationIsPublished(): void { // Assert that the field "is_published" must have a boolean value - $this->testDataValidationBoolean($this->Articles, 'is_published'); + $this->assertValidationBoolean($this->Articles, 'is_published'); } ``` @@ -61,61 +61,60 @@ Each helper builds an entity, runs the validator, and asserts the expected error ### Presence and emptiness -- `testDataValidationRequired($table, $fieldName)` - asserts the field is configured to require presence (`_required`). -- `testDataValidationNotRequired($table, $fieldName)` - asserts the field is not configured to require presence. -- `testDataValidationNotEmpty($table, $fieldName)` - asserts the field is configured to disallow empty values, i.e. `null` or `''` (`_empty`). -- `testDataValidationEmpty($table, $fieldName)` - asserts the field is configured to allow empty values, i.e. `null` or `''`. +- `assertValidationRequired($table, $fieldName)` - asserts the field is configured to require presence (`_required`). +- `assertValidationNotRequired($table, $fieldName)` - asserts the field is not configured to require presence. +- `assertValidationNotEmpty($table, $fieldName)` - asserts the field is configured to disallow empty values, i.e. `null` or `''` (`_empty`). +- `assertValidationEmpty($table, $fieldName)` - asserts the field is configured to allow empty values, i.e. `null` or `''`. ### Type validators -- `testDataValidationBoolean($table, $fieldName)` - asserts the field is configured with the `boolean` validation rule. -- `testDataValidationURLWithProtocol($table, $fieldName)` - asserts the field is configured with the `urlWithProtocol` validation rule (requires `http://` or `https://`). -- `testDataValidationDateTime($table, $fieldName)` - asserts the field is configured with the `dateTime` validation rule. -- `testDataValidationDate($table, $fieldName)` - asserts the field is configured with the `date` validation rule. -- `testDataValidationNaturalNumber($table, $fieldName)` - asserts the field is configured with the `naturalNumber` validation rule (positive integers). -- `testDataValidationScalar($table, $fieldName)` - asserts the field is configured with the `scalar` validation rule. -- `testDataValidationDecimal($table, $fieldName)` - asserts the field is configured with the `decimal` validation rule. -- `testDataValidationInteger($table, $fieldName)` - asserts the field is configured with the `integer` validation rule. -- `testDataValidationNonNegativeInteger($table, $fieldName)` - asserts the field is configured with the `nonNegativeInteger` validation rule. -- `testDataValidationGreaterThanOrEqual($table, $fieldName, $threshold)` - asserts the field is configured with the `greaterThanOrEqual` validation rule. -- `testDataValidationEmail($table, $fieldName)` - asserts the field is configured with the `email` validation rule. -- `testDataValidationUuid($table, $fieldName)` - asserts the field is configured with the `uuid` validation rule. +- `assertValidationBoolean($table, $fieldName)` - asserts the field is configured with the `boolean` validation rule. +- `assertValidationURLWithProtocol($table, $fieldName)` - asserts the field is configured with the `urlWithProtocol` validation rule (requires `http://` or `https://`). +- `assertValidationDateTime($table, $fieldName)` - asserts the field is configured with the `dateTime` validation rule. +- `assertValidationDate($table, $fieldName)` - asserts the field is configured with the `date` validation rule. +- `assertValidationNaturalNumber($table, $fieldName)` - asserts the field is configured with the `naturalNumber` validation rule (positive integers). +- `assertValidationScalar($table, $fieldName)` - asserts the field is configured with the `scalar` validation rule. +- `assertValidationDecimal($table, $fieldName)` - asserts the field is configured with the `decimal` validation rule. +- `assertValidationInteger($table, $fieldName)` - asserts the field is configured with the `integer` validation rule. +- `assertValidationNonNegativeInteger($table, $fieldName)` - asserts the field is configured with the `nonNegativeInteger` validation rule. +- `assertValidationGreaterThanOrEqual($table, $fieldName, $threshold)` - asserts the field is configured with the `greaterThanOrEqual` validation rule. +- `assertValidationEmail($table, $fieldName)` - asserts the field is configured with the `email` validation rule. +- `assertValidationUuid($table, $fieldName)` - asserts the field is configured with the `uuid` validation rule. ### Length validators -- `testDataValidationMaxLength($table, $fieldName, $maxLength)` -- `testDataValidationMinLength($table, $fieldName, $minLength, $expected)` -- `testDataValidationLengthBetween($table, $fieldName, $minLength, $maxLength)` -- `testDataValidationRange($table, $fieldName, $lowerBound, $upperBound)` +- `assertValidationMaxLength($table, $fieldName, $maxLength)` +- `assertValidationMinLength($table, $fieldName, $minLength, $expected)` +- `assertValidationLengthBetween($table, $fieldName, $minLength, $maxLength)` +- `assertValidationRange($table, $fieldName, $lowerBound, $upperBound)` ### Generic helpers -- `testDataValidation($table, $fieldName, $dataSet, $expected)` - the underlying helper. Use when no specialized helper fits. Compares the field's **complete** error array against `$expected`. -- `testDataValidationContains($table, $fieldName, $dataSet, $expected)` - asserts the given `"rule name" => "message"` pairs are present on the field, ignoring any other errors. -- `testDataValidationNotContains($table, $fieldName, $dataSet, $rules)` - asserts the given rule names are **not** present on the field, ignoring any other errors. -- `testDataValidationNoErrors($table, $fieldName, $dataSet)` - asserts a data set produces no errors on the field. -- `testDataValidationInList($table, $list, $fieldName, $expected)` - runs the complete-error-array assertion for each value in a list. -- `testDataValidationInListContains($table, $list, $fieldName, $expected)` - runs the `contains` assertion for each value in a list. -- `testDataValidationInListNotContains($table, $list, $fieldName, $rules)` - runs the `not contains` assertion for each value in a list. -- `testFullDataValidation($table, $dataSet, $expected)` - asserts errors across all fields. -- `testFullDataValidationNoErrors($table, $dataSet)` - asserts a full data set produces no errors at all. +- `assertValidation($table, $fieldName, $dataSet, $expected)` - the underlying helper. Use when no specialized helper fits. Compares the field's **complete** error array against `$expected`. +- `assertValidationContains($table, $fieldName, $dataSet, $expected)` - asserts the given `"rule name" => "message"` pairs are present on the field, ignoring any other errors. +- `assertValidationNotContains($table, $fieldName, $dataSet, $rules)` - asserts the given rule names are **not** present on the field, ignoring any other errors. +- `assertValidationNoErrors($table, $fieldName, $dataSet)` - asserts a data set produces no errors on the field. +- `assertValidationInList($table, $list, $fieldName, $expected)` - runs the complete-error-array assertion for each value in a list. +- `assertValidationInListContains($table, $list, $fieldName, $expected)` - runs the `contains` assertion for each value in a list. +- `assertValidationInListNotContains($table, $list, $fieldName, $rules)` - runs the `not contains` assertion for each value in a list. +- `assertValidationTableErrors($table, $dataSet, $expected)` - asserts errors across all fields. +- `assertValidationTableNoErrors($table, $dataSet)` - asserts a full data set produces no errors at all. ### Rules helpers For application rules that run at save time (not marshalling time). These require a real database connection and fixtures. -- `testDataRules($table, $fieldName, $dataSet, $expected)` -- `testDataRulesNoErrors($table, $fieldName, $dataSet)` -- `testRules($table, $fieldName, $dataSet, $expected)` -- `testDataValidationUnique($table, $fieldName, $fieldValue)` -- `testDataValidationForeignKey($table, $fieldName)` +- `assertRulesNoErrors($table, $fieldName, $dataSet)` +- `assertRules($table, $fieldName, $dataSet, $expected)` +- `assertValidationUnique($table, $fieldName, $fieldValue)` +- `assertValidationForeignKey($table, $fieldName)` ## Dependent fields Use the `$additionalDataSet` parameter to supply required companion fields so your test only fails for the reason you care about: ```php -$this->testDataValidationNotEmpty( +$this->assertValidationNotEmpty( $this->Articles, 'title', ['author_id' => 1] @@ -127,7 +126,7 @@ $this->testDataValidationNotEmpty( The `$options` parameter is forwarded to `Table::newEntity()`: ```php -$this->testDataValidationRequired( +$this->assertValidationRequired( $this->Articles, 'title', [], diff --git a/src/Traits/DataValidationTestTrait.php b/src/Traits/DataValidationTestTrait.php index f8f9537..f06ce04 100644 --- a/src/Traits/DataValidationTestTrait.php +++ b/src/Traits/DataValidationTestTrait.php @@ -31,7 +31,7 @@ trait DataValidationTestTrait * @see \Cake\Validation\Validator::notEmptyString() * @see \Cake\Validation\Validator::notEmptyTime() */ - protected function testDataValidationNotEmpty( + protected function assertValidationNotEmpty( Table $table, string $fieldName, array $additionalDataSet = [], @@ -40,7 +40,7 @@ protected function testDataValidationNotEmpty( $list = [null, '']; $expected = ['_empty' => 'This field cannot be left empty']; - $this->testDataValidationListContains($table, $list, $fieldName, $expected, $additionalDataSet, $options); + $this->assertValidationListContains($table, $list, $fieldName, $expected, $additionalDataSet, $options); } /** @@ -59,7 +59,7 @@ protected function testDataValidationNotEmpty( * @see \Cake\Validation\Validator::allowEmptyString() * @see \Cake\Validation\Validator::allowEmptyTime() */ - protected function testDataValidationEmpty( + protected function assertValidationEmpty( Table $table, string $fieldName, array $additionalDataSet = [], @@ -68,7 +68,7 @@ protected function testDataValidationEmpty( $list = [null, '']; $notExpected = ['_empty']; - $this->testDataValidationListNotContains( + $this->assertValidationListNotContains( $table, $list, $fieldName, @@ -88,14 +88,14 @@ protected function testDataValidationEmpty( * @return void * @see \Cake\Validation\Validator::requirePresence() */ - protected function testDataValidationRequired( + protected function assertValidationRequired( Table $table, string $fieldName, array $dataSet = [], array $options = [], ): void { $expected = ['_required' => 'This field is required']; - $this->testDataValidationContains($table, $fieldName, $dataSet, $expected, $options); + $this->assertValidationContains($table, $fieldName, $dataSet, $expected, $options); } /** @@ -108,14 +108,14 @@ protected function testDataValidationRequired( * @return void * @see \Cake\Validation\Validator::requirePresence() */ - protected function testDataValidationNotRequired( + protected function assertValidationNotRequired( Table $table, string $fieldName, array $dataSet = [], array $options = [], ): void { $notExpected = ['_required']; - $this->testDataValidationNotContains($table, $fieldName, $dataSet, $notExpected, $options); + $this->assertValidationNotContains($table, $fieldName, $dataSet, $notExpected, $options); } /** @@ -128,7 +128,7 @@ protected function testDataValidationNotRequired( * @return void * @see \Cake\Validation\Validator::boolean() */ - protected function testDataValidationBoolean( + protected function assertValidationBoolean( Table $table, string $fieldName, array $additionalDataSet = [], @@ -137,7 +137,7 @@ protected function testDataValidationBoolean( // Valid values $list = [true, false, 1, 0]; $notExpected = ['boolean']; - $this->testDataValidationListNotContains( + $this->assertValidationListNotContains( $table, $list, $fieldName, @@ -149,7 +149,7 @@ protected function testDataValidationBoolean( // Invalid values $list = ['Not a boolean', 123, []]; $expected = ['boolean' => 'The provided value must be a boolean']; - $this->testDataValidationListContains($table, $list, $fieldName, $expected, $additionalDataSet, $options); + $this->assertValidationListContains($table, $list, $fieldName, $expected, $additionalDataSet, $options); } /** @@ -162,7 +162,7 @@ protected function testDataValidationBoolean( * @return void * @see \Cake\Validation\Validator::urlWithProtocol() */ - protected function testDataValidationURLWithProtocol( + protected function assertValidationURLWithProtocol( Table $table, string $fieldName, array $additionalDataSet = [], @@ -171,7 +171,7 @@ protected function testDataValidationURLWithProtocol( // Valid values $list = ['https://valid.com', 'http://valid.com']; $notExpected = ['urlWithProtocol']; - $this->testDataValidationListNotContains( + $this->assertValidationListNotContains( $table, $list, $fieldName, @@ -183,7 +183,7 @@ protected function testDataValidationURLWithProtocol( // Invalid values $list = ['no-protocol.com', 'htp://foo.com']; $expected = ['urlWithProtocol' => 'The provided value must be a URL with protocol']; - $this->testDataValidationListContains($table, $list, $fieldName, $expected, $additionalDataSet, $options); + $this->assertValidationListContains($table, $list, $fieldName, $expected, $additionalDataSet, $options); } /** @@ -196,7 +196,7 @@ protected function testDataValidationURLWithProtocol( * @return void * @see \Cake\Validation\Validator::dateTime() */ - protected function testDataValidationDateTime( + protected function assertValidationDateTime( Table $table, string $fieldName, array $additionalDataSet = [], @@ -211,7 +211,7 @@ protected function testDataValidationDateTime( new FrozenTime(), ]; $notExpected = ['dateTime']; - $this->testDataValidationListNotContains( + $this->assertValidationListNotContains( $table, $list, $fieldName, @@ -229,7 +229,7 @@ protected function testDataValidationDateTime( '123', // Numeric ]; $expected = ['dateTime' => 'The provided value must be a date and time of one of these formats: `ymd`']; - $this->testDataValidationListContains($table, $list, $fieldName, $expected, $additionalDataSet, $options); + $this->assertValidationListContains($table, $list, $fieldName, $expected, $additionalDataSet, $options); } /** @@ -242,7 +242,7 @@ protected function testDataValidationDateTime( * @return void * @see \Cake\Validation\Validator::date() */ - protected function testDataValidationDate( + protected function assertValidationDate( Table $table, string $fieldName, array $additionalDataSet = [], @@ -259,7 +259,7 @@ protected function testDataValidationDate( new FrozenTime(), ]; $notExpected = ['date']; - $this->testDataValidationListNotContains( + $this->assertValidationListNotContains( $table, $list, $fieldName, @@ -277,7 +277,7 @@ protected function testDataValidationDate( $expected = [ 'date' => 'The provided value must be a date of one of these formats: `ymd`', ]; - $this->testDataValidationListContains($table, $list, $fieldName, $expected, $additionalDataSet, $options); + $this->assertValidationListContains($table, $list, $fieldName, $expected, $additionalDataSet, $options); } /** @@ -292,7 +292,7 @@ protected function testDataValidationDate( * @return void * @see \Cake\Validation\Validator::inList() */ - protected function testDataValidationInList( + protected function assertValidationInList( Table $table, array $list, string $fieldName, @@ -302,7 +302,7 @@ protected function testDataValidationInList( ): void { foreach ($list as $value) { $dataSet = array_merge($additionalDataSet, [$fieldName => $value]); - $this->testDataValidation($table, $fieldName, $dataSet, $expected, $options); + $this->assertValidation($table, $fieldName, $dataSet, $expected, $options); } } @@ -316,14 +316,36 @@ protected function testDataValidationInList( * @return void * @see \Cake\Validation\Validator::validate() */ - protected function testDataValidationNoErrors( + protected function assertValidationNoErrors( Table $table, string $fieldName, array $dataSet, array $options = [], ): void { $expected = []; - $this->testDataValidation($table, $fieldName, $dataSet, $expected, $options); + $this->assertValidation($table, $fieldName, $dataSet, $expected, $options); + } + + /** + * Validate that a given data set for a given table leads to the expected errors + * + * @param Table $table The table to test. + * @param array $dataSet The data set to test. + * @param array $expected The expected errors. + * @param array $options Additional options for newEntity. + * @return void + * @see \Cake\Validation\Validator::validate() + */ + protected function assertValidationTableErrors( + Table $table, + array $dataSet, + array $expected, + array $options = [], + ): void { + $entity = $table->newEntity($dataSet, $options); + $errors = $entity->getErrors(); + + static::assertEquals($expected, $errors); } /** @@ -335,10 +357,10 @@ protected function testDataValidationNoErrors( * @return void * @see \Cake\Validation\Validator::validate() */ - protected function testFullDataValidationNoErrors(Table $table, array $dataSet, array $options = []): void + protected function assertValidationTableNoErrors(Table $table, array $dataSet, array $options = []): void { $expected = []; - $this->testFullDataValidation($table, $dataSet, $expected, $options); + $this->assertValidationTableErrors($table, $dataSet, $expected, $options); } /** @@ -352,7 +374,7 @@ protected function testFullDataValidationNoErrors(Table $table, array $dataSet, * @return void * @see \Cake\Validation\Validator::validate() */ - protected function testDataValidation( + protected function assertValidation( Table $table, string $fieldName, array $dataSet, @@ -377,7 +399,7 @@ protected function testDataValidation( * @return void * @see \Cake\Validation\Validator::validate() */ - protected function testDataValidationContains( + protected function assertValidationContains( Table $table, string $fieldName, array $dataSet, @@ -387,7 +409,7 @@ protected function testDataValidationContains( $entity = $table->newEntity($dataSet, $options); $errors = $entity->getError($fieldName); - $this->assertDataValidationErrorsContain($fieldName, $errors, $expected); + $this->assertValidationErrorsContain($fieldName, $errors, $expected); } /** @@ -403,7 +425,7 @@ protected function testDataValidationContains( * @return void * @see \Cake\Validation\Validator::validate() */ - protected function testDataValidationNotContains( + protected function assertValidationNotContains( Table $table, string $fieldName, array $dataSet, @@ -436,7 +458,7 @@ protected function testDataValidationNotContains( * @param array $expected The expected errors ("rule name" => "message") that must be present. * @return void */ - protected function assertDataValidationErrorsContain(string $fieldName, array $errors, array $expected): void + protected function assertValidationErrorsContain(string $fieldName, array $errors, array $expected): void { foreach ($expected as $rule => $message) { static::assertArrayHasKey( @@ -473,7 +495,7 @@ protected function assertDataValidationErrorsContain(string $fieldName, array $e * @param array $options Additional options for newEntity. * @return void */ - protected function testDataValidationListContains( + protected function assertValidationListContains( Table $table, array $list, string $fieldName, @@ -483,7 +505,7 @@ protected function testDataValidationListContains( ): void { foreach ($list as $value) { $dataSet = array_merge($additionalDataSet, [$fieldName => $value]); - $this->testDataValidationContains($table, $fieldName, $dataSet, $expected, $options); + $this->assertValidationContains($table, $fieldName, $dataSet, $expected, $options); } } @@ -500,7 +522,7 @@ protected function testDataValidationListContains( * @param array $options Additional options for newEntity. * @return void */ - protected function testDataValidationListNotContains( + protected function assertValidationListNotContains( Table $table, array $list, string $fieldName, @@ -510,7 +532,7 @@ protected function testDataValidationListNotContains( ): void { foreach ($list as $value) { $dataSet = array_merge($additionalDataSet, [$fieldName => $value]); - $this->testDataValidationNotContains($table, $fieldName, $dataSet, $rules, $options); + $this->assertValidationNotContains($table, $fieldName, $dataSet, $rules, $options); } } @@ -525,7 +547,7 @@ protected function testDataValidationListNotContains( * @return void * @todo Move to a rules dedicated helper class. */ - protected function testDataRules( + protected function assertRules( Table $table, string $fieldName, array $dataSet, @@ -544,43 +566,21 @@ protected function testDataRules( } /** - * Validate that a given data set for a given table leads to the expected table rules errors + * Validate that a given data set for a given table does not lead to data rules errors * * @param Table $table The table to test. - * @param string $fieldName The field to check for table rules errors. + * @param string $fieldName The field to check for errors. * @param array $dataSet The data set to test. - * @param array $expected The expected table rules errors. * @param array $options Additional options for newEntity. * @return void + * @todo Move to a rules dedicated helper class. */ - protected function testRules( + protected function assertRulesNoErrors( Table $table, string $fieldName, array $dataSet, - array $expected, array $options = [], ): void { - $entity = $table->newEntity($dataSet, $options); - $errors = $entity->getError($fieldName); - static::assertEmpty($errors); - - $table->save($entity); - $errors = $entity->getError($fieldName); - static::assertEquals($expected, $errors); - } - - /** - * Validate that a given data set for a given table does not lead to data rules errors - * - * @param Table $table The table to test. - * @param string $fieldName The field to check for errors. - * @param array $dataSet The data set to test. - * @param array $options Additional options for newEntity. - * @return void - * @todo Move to a rules dedicated helper class. - */ - protected function testDataRulesNoErrors(Table $table, string $fieldName, array $dataSet, array $options = []): void - { $defaultOptions = ['validate' => false]; $options = $defaultOptions + $options; $entity = $table->newEntity($dataSet, $options); @@ -603,7 +603,7 @@ protected function testDataRulesNoErrors(Table $table, string $fieldName, array * @return void * @see \Cake\Validation\Validator::maxLength() */ - protected function testDataValidationMaxLength( + protected function assertValidationMaxLength( Table $table, string $fieldName, int $maxLength, @@ -614,7 +614,7 @@ protected function testDataValidationMaxLength( $dataset = [$fieldName => $tooLongFieldContent]; $expected ??= ['maxLength' => sprintf('The provided value must be at most `%d` characters long', $maxLength)]; - $this->testDataValidationContains($table, $fieldName, $dataset, $expected, $options); + $this->assertValidationContains($table, $fieldName, $dataset, $expected, $options); } /** @@ -628,7 +628,7 @@ protected function testDataValidationMaxLength( * @return void * @see \Cake\Validation\Validator::minLength() */ - protected function testDataValidationMinLength( + protected function assertValidationMinLength( Table $table, string $fieldName, int $minLength, @@ -638,7 +638,7 @@ protected function testDataValidationMinLength( $tooShortFieldContent = str_repeat('A', $minLength - 1); $dataset = [$fieldName => $tooShortFieldContent]; $expected ??= ['minLength' => sprintf('The provided value must be at least `%d` characters long', $minLength)]; - $this->testDataValidationContains($table, $fieldName, $dataset, $expected, $options); + $this->assertValidationContains($table, $fieldName, $dataset, $expected, $options); } /** @@ -651,7 +651,7 @@ protected function testDataValidationMinLength( * @return void * @see \Cake\Validation\Validator::scalar() */ - protected function testDataValidationScalar( + protected function assertValidationScalar( Table $table, string $fieldName, ?array $expected = null, @@ -660,7 +660,7 @@ protected function testDataValidationScalar( $dataset = [$fieldName => []]; // A non-scalar value $expected ??= ['scalar' => 'The provided value must be scalar']; - $this->testDataValidationContains($table, $fieldName, $dataset, $expected, $options); + $this->assertValidationContains($table, $fieldName, $dataset, $expected, $options); } /** @@ -673,7 +673,7 @@ protected function testDataValidationScalar( * @return void * @see \Cake\Validation\Validator::decimal() */ - protected function testDataValidationDecimal( + protected function assertValidationDecimal( Table $table, string $fieldName, ?array $expected = null, @@ -693,12 +693,12 @@ protected function testDataValidationDecimal( $expected ??= [ 'decimal' => 'The provided value must be decimal with any number of decimal places, including none', ]; - $this->testDataValidationListContains($table, $list, $fieldName, $expected, [], $options); + $this->assertValidationListContains($table, $list, $fieldName, $expected, [], $options); // Valid values $list = [-99.0, 0.099]; $notExpected = ['decimal']; - $this->testDataValidationListNotContains($table, $list, $fieldName, $notExpected, [], $options); + $this->assertValidationListNotContains($table, $list, $fieldName, $notExpected, [], $options); } /** @@ -711,7 +711,7 @@ protected function testDataValidationDecimal( * @return void * @see \Cake\Validation\Validator::integer() */ - protected function testDataValidationInteger( + protected function assertValidationInteger( Table $table, string $fieldName, ?array $expected = null, @@ -729,12 +729,12 @@ protected function testDataValidationInteger( 'ab0,099', ]; $expected ??= ['integer' => 'The provided value must be an integer']; - $this->testDataValidationListContains($table, $list, $fieldName, $expected, [], $options); + $this->assertValidationListContains($table, $list, $fieldName, $expected, [], $options); // Valid values $list = [-99, 99]; $notExpected = ['integer']; - $this->testDataValidationListNotContains($table, $list, $fieldName, $notExpected, [], $options); + $this->assertValidationListNotContains($table, $list, $fieldName, $notExpected, [], $options); } /** @@ -747,7 +747,7 @@ protected function testDataValidationInteger( * @return void * @see \Cake\Validation\Validator::nonNegativeInteger() */ - protected function testDataValidationNonNegativeInteger( + protected function assertValidationNonNegativeInteger( Table $table, string $fieldName, ?array $expected = null, @@ -757,13 +757,13 @@ protected function testDataValidationNonNegativeInteger( $dataset = [$fieldName => '-1']; $expected ??= ['nonNegativeInteger' => 'The provided value must be a non-negative integer']; - $this->testDataValidationContains($table, $fieldName, $dataset, $expected, $options); + $this->assertValidationContains($table, $fieldName, $dataset, $expected, $options); // Non-negative integer $dataset = [$fieldName => '0']; $notExpected = ['nonNegativeInteger']; - $this->testDataValidationNotContains($table, $fieldName, $dataset, $notExpected, $options); + $this->assertValidationNotContains($table, $fieldName, $dataset, $notExpected, $options); } /** @@ -778,7 +778,7 @@ protected function testDataValidationNonNegativeInteger( * @return void * @see \Cake\Validation\Validator::greaterThanOrEqual() */ - protected function testDataValidationGreaterThanOrEqual( + protected function assertValidationGreaterThanOrEqual( Table $table, string $fieldName, float|int $threshold, @@ -798,13 +798,13 @@ protected function testDataValidationGreaterThanOrEqual( $threshold, ), ]; - $this->testDataValidationContains($table, $fieldName, $dataset, $expected, $options); + $this->assertValidationContains($table, $fieldName, $dataset, $expected, $options); // Valid values: exactly at and just above the threshold $aboveThreshold = is_int($threshold) ? $threshold + 1 : $threshold + 0.01; $list = [$threshold, $aboveThreshold]; $notExpected = ['greaterThanOrEqual']; - $this->testDataValidationListNotContains( + $this->assertValidationListNotContains( $table, $list, $fieldName, @@ -824,7 +824,7 @@ protected function testDataValidationGreaterThanOrEqual( * @return void * @see \Cake\Validation\Validator::email() */ - protected function testDataValidationEmail( + protected function assertValidationEmail( Table $table, string $fieldName, ?array $expected = null, @@ -838,7 +838,7 @@ protected function testDataValidationEmail( 'in@valid.1', ]; $expected ??= ['email' => 'The provided value must be an e-mail address']; - $this->testDataValidationListContains($table, $list, $fieldName, $expected, [], $options); + $this->assertValidationListContains($table, $list, $fieldName, $expected, [], $options); // Valid values $list = [ @@ -848,7 +848,7 @@ protected function testDataValidationEmail( 'va_lid.123+spamfolder@email.test', ]; $notExpected = ['email']; - $this->testDataValidationListNotContains($table, $list, $fieldName, $notExpected, [], $options); + $this->assertValidationListNotContains($table, $list, $fieldName, $notExpected, [], $options); } /** @@ -861,7 +861,7 @@ protected function testDataValidationEmail( * @return void * @see \Cake\Validation\Validator::uuid() */ - protected function testDataValidationUuid( + protected function assertValidationUuid( Table $table, string $fieldName, ?array $expected = null, @@ -875,7 +875,7 @@ protected function testDataValidationUuid( 'notAUuid', // Not a UUID ]; $expected ??= ['uuid' => 'The provided value must be a UUID']; - $this->testDataValidationListContains($table, $list, $fieldName, $expected, [], $options); + $this->assertValidationListContains($table, $list, $fieldName, $expected, [], $options); // Valid values $list = [ @@ -890,7 +890,7 @@ protected function testDataValidationUuid( 'fc93ab0e-c99e-4b58-975e-9c5e68c53624', // GUID ]; $notExpected = ['uuid']; - $this->testDataValidationListNotContains($table, $list, $fieldName, $notExpected, [], $options); + $this->assertValidationListNotContains($table, $list, $fieldName, $notExpected, [], $options); } /** @@ -905,7 +905,7 @@ protected function testDataValidationUuid( * @return void * @see \Cake\Validation\Validator::lengthBetween() */ - protected function testDataValidationLengthBetween( + protected function assertValidationLengthBetween( Table $table, string $fieldName, int $minLength, @@ -925,7 +925,7 @@ protected function testDataValidationLengthBetween( $maxlength, ), ]; - $this->testDataValidationContains($table, $fieldName, $dataset, $expected, $options); + $this->assertValidationContains($table, $fieldName, $dataset, $expected, $options); } // Too long @@ -939,7 +939,7 @@ protected function testDataValidationLengthBetween( $maxlength, ), ]; - $this->testDataValidationContains($table, $fieldName, $dataset, $expected, $options); + $this->assertValidationContains($table, $fieldName, $dataset, $expected, $options); } /** @@ -954,7 +954,7 @@ protected function testDataValidationLengthBetween( * @return void * @see \Cake\Validation\Validator::range() */ - protected function testDataValidationRange( + protected function assertValidationRange( Table $table, string $fieldName, int|float $lowerBound, @@ -984,7 +984,7 @@ protected function testDataValidationRange( $upperBound, ), ]; - $this->testDataValidationListContains($table, $list, $fieldName, $expected, [], $options); + $this->assertValidationListContains($table, $list, $fieldName, $expected, [], $options); // Valid $list = [ @@ -995,7 +995,7 @@ protected function testDataValidationRange( ]; $notExpected = ['range']; - $this->testDataValidationListNotContains($table, $list, $fieldName, $notExpected, [], $options); + $this->assertValidationListNotContains($table, $list, $fieldName, $notExpected, [], $options); } /** @@ -1008,7 +1008,7 @@ protected function testDataValidationRange( * @return void * @see \Cake\Validation\Validator::naturalNumber() */ - protected function testDataValidationNaturalNumber( + protected function assertValidationNaturalNumber( Table $table, string $fieldName, array $additionalDataSet = [], @@ -1017,12 +1017,12 @@ protected function testDataValidationNaturalNumber( // Invalid values $list = [0, -1]; $expected = ['naturalNumber' => 'The provided value must be a natural number']; - $this->testDataValidationListContains($table, $list, $fieldName, $expected, $additionalDataSet, $options); + $this->assertValidationListContains($table, $list, $fieldName, $expected, $additionalDataSet, $options); // Valid value $list = [1]; $notExpected = ['naturalNumber']; - $this->testDataValidationListNotContains( + $this->assertValidationListNotContains( $table, $list, $fieldName, @@ -1032,24 +1032,6 @@ protected function testDataValidationNaturalNumber( ); } - /** - * Validate that a given data set for a given table leads to the expected errors - * - * @param Table $table The table to test. - * @param array $dataSet The data set to test. - * @param array $expected The expected errors. - * @param array $options Additional options for newEntity. - * @return void - * @see \Cake\Validation\Validator::validate() - */ - protected function testFullDataValidation(Table $table, array $dataSet, array $expected, array $options = []): void - { - $entity = $table->newEntity($dataSet, $options); - $errors = $entity->getErrors(); - - static::assertEquals($expected, $errors); - } - /** * Validate the present foreign key to another table * @@ -1059,7 +1041,7 @@ protected function testFullDataValidation(Table $table, array $dataSet, array $e * @param array|null $expected The expected rule errors ("rule name" => "message") that must be present. * @return void */ - protected function testDataValidationForeignKey( + protected function assertValidationForeignKey( Table $table, string $fieldName, ?int $notExistingForeignKey = 999999, @@ -1073,7 +1055,7 @@ protected function testDataValidationForeignKey( $expected ??= ['_existsIn' => 'This value does not exist']; - $this->assertDataValidationErrorsContain($fieldName, $entity->getError($fieldName), $expected); + $this->assertValidationErrorsContain($fieldName, $entity->getError($fieldName), $expected); } /** @@ -1086,7 +1068,7 @@ protected function testDataValidationForeignKey( * @param array|null $expected The expected rule errors ("rule name" => "message") that must be present. * @return void */ - protected function testDataValidationIsUnique( + protected function assertValidationIsUnique( Table $table, string $fieldName, mixed $fieldValue, @@ -1113,6 +1095,6 @@ protected function testDataValidationIsUnique( $expected ??= ['_isUnique' => 'This value is already in use']; - $this->assertDataValidationErrorsContain($fieldName, $entity->getError($fieldName), $expected); + $this->assertValidationErrorsContain($fieldName, $entity->getError($fieldName), $expected); } } diff --git a/tests/TestCase/Traits/DataValidationTestTraitTest.php b/tests/TestCase/Traits/DataValidationTestTraitTest.php index dd8148c..be959a7 100644 --- a/tests/TestCase/Traits/DataValidationTestTraitTest.php +++ b/tests/TestCase/Traits/DataValidationTestTraitTest.php @@ -45,12 +45,12 @@ protected function tearDown(): void } /** - * Test the testDataValidation base method + * Test the assertValidation base method * * @return void - * @covers ::testDataValidation + * @covers ::assertValidation */ - public function testTestDataValidation(): void + public function testAssertValidation(): void { // Ensure data validation of some field works as expected first $fieldName = 'not_empty_field'; @@ -60,16 +60,16 @@ public function testTestDataValidation(): void $errors = $entity->getError($fieldName); static::assertSame($expectedErrors, $errors); - $this->testDataValidation($this->table, $fieldName, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $fieldName, $dataSet, $expectedErrors); } /** - * Test the testDataValidationNoErrors base method + * Test the assertValidationNoErrors base method * * @return void - * @covers ::testDataValidationNoErrors + * @covers ::assertValidationNoErrors */ - public function testTestDataValidationNoErrors(): void + public function testAssertValidationNoErrors(): void { // Ensure data validation of some field works as expected first $fieldName = 'empty_field'; @@ -79,272 +79,272 @@ public function testTestDataValidationNoErrors(): void $errors = $entity->getError($fieldName); static::assertSame($expectedErrors, $errors); - $this->testDataValidationNoErrors($this->table, $fieldName, $dataSet); + $this->assertValidationNoErrors($this->table, $fieldName, $dataSet); } /** - * Test that testDataValidationNotEmpty passes when the field is not empty. + * Test that assertValidationNotEmpty passes when the field is not empty. * * @return void - * @covers ::testDataValidationNotEmpty + * @covers ::assertValidationNotEmpty */ - public function testTestDataValidationNotEmpty(): void + public function testAssertValidationNotEmpty(): void { // Ensure data validation of the field works as expected first $field = 'not_empty_field'; $expectedErrors = ['_empty' => 'This field cannot be left empty']; $dataSet = [$field => '']; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); - $this->testDataValidationNotEmpty($this->table, $field); + $this->assertValidationNotEmpty($this->table, $field); } /** - * Test that testDataValidationEmpty passes when the field is empty. + * Test that assertValidationEmpty passes when the field is empty. * * @return void - * @covers ::testDataValidationEmpty + * @covers ::assertValidationEmpty */ - public function testTestDataValidationEmpty(): void + public function testAssertValidationEmpty(): void { // Ensure data validation of the field works as expected first $field = 'empty_field'; $dataSet = [$field => '']; - $this->testDataValidationNoErrors($this->table, $field, $dataSet); + $this->assertValidationNoErrors($this->table, $field, $dataSet); - $this->testDataValidationEmpty($this->table, $field); + $this->assertValidationEmpty($this->table, $field); } /** - * Test that testDataValidationRequired passes when the field is required. + * Test that assertValidationRequired passes when the field is required. * * @return void - * @covers ::testDataValidationRequired + * @covers ::assertValidationRequired */ - public function testTestDataValidationRequired(): void + public function testAssertValidationRequired(): void { // Ensure data validation of the field works as expected first $field = 'required_field'; $expectedErrors = ['_required' => 'This field is required']; $dataSet = []; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); - $this->testDataValidationRequired($this->table, $field); + $this->assertValidationRequired($this->table, $field); } /** - * Test that testDataValidationNotRequired passes when the field is empty. + * Test that assertValidationNotRequired passes when the field is empty. * * @return void - * @covers ::testDataValidationNotRequired + * @covers ::assertValidationNotRequired */ - public function testTestDataValidationNotRequired(): void + public function testAssertValidationNotRequired(): void { // Ensure data validation of the field works as expected first $field = 'empty_field'; $dataSet = []; - $this->testDataValidationNoErrors($this->table, $field, $dataSet); + $this->assertValidationNoErrors($this->table, $field, $dataSet); - $this->testDataValidationNotRequired($this->table, $field); + $this->assertValidationNotRequired($this->table, $field); } /** - * Test that testDataValidationBoolean passes when the field is boolean. + * Test that assertValidationBoolean passes when the field is boolean. * * @return void - * @covers ::testDataValidationBoolean + * @covers ::assertValidationBoolean */ - public function testTestDataValidationBoolean(): void + public function testAssertValidationBoolean(): void { // Ensure data validation of the field works as expected first $field = 'boolean_field'; $expectedErrors = ['boolean' => 'The provided value must be a boolean']; $dataSet = [$field => 'Not a boolean']; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); - $this->testDataValidationBoolean($this->table, $field); + $this->assertValidationBoolean($this->table, $field); } /** - * Test that testDataValidationURLWithProtocol passes when the field is url. + * Test that assertValidationURLWithProtocol passes when the field is url. * * @return void - * @covers ::testDataValidationURLWithProtocol + * @covers ::assertValidationURLWithProtocol */ - public function testTestDataValidationURLWithProtocol(): void + public function testAssertValidationURLWithProtocol(): void { // Ensure data validation of the field works as expected first $field = 'url_field'; $expectedErrors = ['urlWithProtocol' => 'The provided value must be a URL with protocol']; $dataSet = [$field => 'no-protocol.com']; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); - $this->testDataValidationURLWithProtocol($this->table, $field); + $this->assertValidationURLWithProtocol($this->table, $field); } /** - * Test that testDataValidationDateTime passes when the field is datetime. + * Test that assertValidationDateTime passes when the field is datetime. * * @return void - * @covers ::testDataValidationDateTime + * @covers ::assertValidationDateTime */ - public function testTestDataValidationDateTime(): void + public function testAssertValidationDateTime(): void { // Ensure data validation of the field works as expected first $field = 'datetime_field'; $expectedErrors = ['dateTime' => 'The provided value must be a date and time of one of these formats: `ymd`']; $dataSet = [$field => 'Not a date/time']; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); - $this->testDataValidationDateTime($this->table, $field); + $this->assertValidationDateTime($this->table, $field); } /** - * Test that testDataValidationDate passes when the field is date. + * Test that assertValidationDate passes when the field is date. * * @return void - * @covers ::testDataValidationDate + * @covers ::assertValidationDate */ - public function testTestDataValidationDate(): void + public function testAssertValidationDate(): void { // Ensure data validation of the field works as expected first $field = 'date_field'; $expectedErrors = ['date' => 'The provided value must be a date of one of these formats: `ymd`']; $dataSet = [$field => 'Not a date']; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); - $this->testDataValidationDate($this->table, $field); + $this->assertValidationDate($this->table, $field); } /** - * Test that testDataValidationInList passes when the field is datetime. + * Test that assertValidationInList passes when the field is datetime. * * @return void - * @covers ::testDataValidationInList + * @covers ::assertValidationInList */ - public function testTestDataValidationInList(): void + public function testAssertValidationInList(): void { // Ensure data validation of the field works as expected first $field = 'datetime_field'; $expectedErrors = ['dateTime' => 'The provided value must be a date and time of one of these formats: `ymd`']; $invalidValues = ['Not a date/time', '123']; - $this->testDataValidationInList($this->table, $invalidValues, $field, $expectedErrors); + $this->assertValidationInList($this->table, $invalidValues, $field, $expectedErrors); $validValues = ['1900-01-01 00:00:00', '2022-10-12 11:50:32']; - $this->testDataValidationInList($this->table, $validValues, $field); + $this->assertValidationInList($this->table, $validValues, $field); } /** - * Test that testDataValidationMaxLength passes when the value is less than the max length. + * Test that assertValidationMaxLength passes when the value is less than the max length. * * @return void - * @covers ::testDataValidationMaxLength + * @covers ::assertValidationMaxLength */ - public function testTestDataValidationMaxLength(): void + public function testAssertValidationMaxLength(): void { // Ensure data validation of the field works as expected first $maxLength = 10; $field = 'max_length_field'; $expectedErrors = ['maxLength' => 'The provided value must be at most `10` characters long']; $dataSet = [$field => str_repeat('A', $maxLength + 1)]; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); - $this->testDataValidationMaxLength($this->table, $field, $maxLength); + $this->assertValidationMaxLength($this->table, $field, $maxLength); } /** - * Test that testDataValidationMinLength passes when the value is greater than the min length. + * Test that assertValidationMinLength passes when the value is greater than the min length. * * @return void - * @covers ::testDataValidationMinLength + * @covers ::assertValidationMinLength */ - public function testTestDataValidationMinLength(): void + public function testAssertValidationMinLength(): void { // Ensure data validation of the field works as expected first $minLength = 5; $field = 'min_length_field'; $expectedErrors = ['minLength' => 'The provided value must be at least `5` characters long']; $dataSet = [$field => str_repeat('A', $minLength - 1)]; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); - $this->testDataValidationMinLength($this->table, $field, $minLength); + $this->assertValidationMinLength($this->table, $field, $minLength); } /** - * Test that testDataValidationScalar passes when the field is scalar. + * Test that assertValidationScalar passes when the field is scalar. * * @return void - * @covers ::testDataValidationScalar + * @covers ::assertValidationScalar */ - public function testTestDataValidationScalar(): void + public function testAssertValidationScalar(): void { // Ensure data validation of the field works as expected first $field = 'scalar_field'; $entity = $this->table->newEntity([$field => []]); static::assertArrayHasKey('scalar', $entity->getError($field)); - $this->testDataValidationScalar($this->table, $field); + $this->assertValidationScalar($this->table, $field); } /** - * Test that testDataValidationDecimal passes when the field is decimal. + * Test that assertValidationDecimal passes when the field is decimal. * * @return void - * @covers ::testDataValidationDecimal + * @covers ::assertValidationDecimal */ - public function testTestDataValidationDecimal(): void + public function testAssertValidationDecimal(): void { // Ensure data validation of the field works as expected first $field = 'decimal_field'; $expectedErrors = ['decimal' => 'The provided value must be decimal with any number of decimal places, including none']; $dataSet = [$field => 'not a decimal']; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); - $this->testDataValidationDecimal($this->table, $field); + $this->assertValidationDecimal($this->table, $field); } /** - * Test that testDataValidationInteger passes when the field is integer. + * Test that assertValidationInteger passes when the field is integer. * * @return void - * @covers ::testDataValidationInteger + * @covers ::assertValidationInteger */ - public function testTestDataValidationInteger(): void + public function testAssertValidationInteger(): void { // Ensure data validation of the field works as expected first $field = 'integer_field'; $expectedErrors = ['integer' => 'The provided value must be an integer']; $dataSet = [$field => 'not a integer']; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); - $this->testDataValidationInteger($this->table, $field); + $this->assertValidationInteger($this->table, $field); } /** - * Test that testDataValidationNonNegativeInteger passes when the field is a non-negative integer. + * Test that assertValidationNonNegativeInteger passes when the field is a non-negative integer. * * @return void - * @covers ::testDataValidationNonNegativeInteger + * @covers ::assertValidationNonNegativeInteger */ - public function testTestDataValidationNonNegativeInteger(): void + public function testAssertValidationNonNegativeInteger(): void { // Ensure data validation of the field works as expected first $field = 'non_negative_integer_field'; $expectedErrors = ['nonNegativeInteger' => 'The provided value must be a non-negative integer']; $dataSet = [$field => 'not a integer']; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); - $this->testDataValidationNonNegativeInteger($this->table, $field); + $this->assertValidationNonNegativeInteger($this->table, $field); } /** - * Test that testDataValidationGreaterThanOrEqual passes when the field value + * Test that assertValidationGreaterThanOrEqual passes when the field value * is greater than or equal to the configured threshold. * * @return void - * @covers ::testDataValidationGreaterThanOrEqual + * @covers ::assertValidationGreaterThanOrEqual */ - public function testTestDataValidationGreaterThanOrEqual(): void + public function testAssertValidationGreaterThanOrEqual(): void { // Ensure data validation of the field works as expected first $threshold = 10; @@ -358,26 +358,26 @@ public function testTestDataValidationGreaterThanOrEqual(): void // Just below the threshold should fail $dataSet = [$field => $threshold - 1]; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); // At the threshold should pass $dataSet = [$field => $threshold]; - $this->testDataValidationNoErrors($this->table, $field, $dataSet); + $this->assertValidationNoErrors($this->table, $field, $dataSet); // Above the threshold should pass $dataSet = [$field => $threshold + 1]; - $this->testDataValidationNoErrors($this->table, $field, $dataSet); + $this->assertValidationNoErrors($this->table, $field, $dataSet); - $this->testDataValidationGreaterThanOrEqual($this->table, $field, $threshold); + $this->assertValidationGreaterThanOrEqual($this->table, $field, $threshold); } /** - * Test that testDataValidationEmail passes when the field is a valid email. + * Test that assertValidationEmail passes when the field is a valid email. * * @return void - * @covers ::testDataValidationEmail + * @covers ::assertValidationEmail */ - public function testTestDataValidationEmail(): void + public function testAssertValidationEmail(): void { // Ensure data validation of the field works as expected first $field = 'email_field'; @@ -385,18 +385,18 @@ public function testTestDataValidationEmail(): void 'email' => 'The provided value must be an e-mail address', ]; $dataSet = [$field => 'Not an email']; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); - $this->testDataValidationEmail($this->table, $field); + $this->assertValidationEmail($this->table, $field); } /** - * Test that testDataValidationUuid passes when the field is a valid uuid. + * Test that assertValidationUuid passes when the field is a valid uuid. * * @return void - * @covers ::testDataValidationUuid + * @covers ::assertValidationUuid */ - public function testTestDataValidationUuid(): void + public function testAssertValidationUuid(): void { // Ensure data validation of the field works as expected first $field = 'uuid_field'; @@ -404,18 +404,18 @@ public function testTestDataValidationUuid(): void 'uuid' => 'The provided value must be a UUID', ]; $dataSet = [$field => 'Not a uuid']; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); - $this->testDataValidationUuid($this->table, $field); + $this->assertValidationUuid($this->table, $field); } /** - * Test that testDataValidationLengthBetween passes when the field is between the min and max length. + * Test that assertValidationLengthBetween passes when the field is between the min and max length. * * @return void - * @covers ::testDataValidationLengthBetween + * @covers ::assertValidationLengthBetween */ - public function testTestDataValidationLengthBetween(): void + public function testAssertValidationLengthBetween(): void { // Ensure data validation of the field works as expected first $minLength = 5; @@ -423,20 +423,20 @@ public function testTestDataValidationLengthBetween(): void $field = 'length_between_field'; $expectedErrors = ['lengthBetween' => 'The length of the provided value must be between `5` and `10`, inclusively']; $dataSet = [$field => str_repeat('A', $minLength - 1)]; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); $dataSet = [$field => str_repeat('A', $maxLength + 1)]; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); - $this->testDataValidationLengthBetween($this->table, $field, $minLength, $maxLength); + $this->assertValidationLengthBetween($this->table, $field, $minLength, $maxLength); } /** - * Test that testDataValidationRange passes when the field is between the lower and upper bound. + * Test that assertValidationRange passes when the field is between the lower and upper bound. * * @return void - * @covers ::testDataValidationRange + * @covers ::assertValidationRange */ - public function testTestDataValidationRange(): void + public function testAssertValidationRange(): void { // Ensure data validation of the field works as expected first $lowerBound = -30.0; @@ -445,66 +445,66 @@ public function testTestDataValidationRange(): void $expectedErrors = ['range' => 'The provided value must be between `-30` and `30`, inclusively']; $dataSet = [$field => $lowerBound - 1]; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); $dataSet = [$field => $upperBound + 1]; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); - $this->testDataValidationRange($this->table, $field, $lowerBound, $upperBound); + $this->assertValidationRange($this->table, $field, $lowerBound, $upperBound); } /** - * Test that testDataValidationNaturalNumber passes when the field is a natural number. + * Test that assertValidationNaturalNumber passes when the field is a natural number. * * @return void - * @covers ::testDataValidationNaturalNumber + * @covers ::assertValidationNaturalNumber */ - public function testTestDataValidationNaturalNumber(): void + public function testAssertValidationNaturalNumber(): void { // Ensure data validation of the field works as expected first $field = 'natural_number_field'; $expectedErrors = ['naturalNumber' => 'The provided value must be a natural number']; $dataSet = [$field => -1]; - $this->testDataValidation($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidation($this->table, $field, $dataSet, $expectedErrors); - $this->testDataValidationNaturalNumber($this->table, $field); + $this->assertValidationNaturalNumber($this->table, $field); } /** - * Test that testFullDataValidationNoErrors passes when the full data set has no errors. + * Test that assertValidationTableNoErrors passes when the full data set has no errors. * * @return void - * @covers ::testFullDataValidationNoErrors + * @covers ::assertValidationTableNoErrors */ - public function testTestFullDataValidationNoErrors(): void + public function testAssertValidationTableNoErrors(): void { $dataSet = ['required_field' => 'required', 'multi_rule_field' => 1]; - $this->testFullDataValidationNoErrors($this->table, $dataSet); + $this->assertValidationTableNoErrors($this->table, $dataSet); } /** - * Test that testFullDataValidation reports all errors. + * Test that assertValidationTableErrors reports all errors. * * @return void - * @covers ::testFullDataValidation + * @covers ::assertValidationTableErrors */ - public function testTestFullDataValidation(): void + public function testAssertValidationTableErrors(): void { $dataSet = ['not_empty_field' => '', 'multi_rule_field' => 1]; $expectedErrors = [ 'not_empty_field' => ['_empty' => 'This field cannot be left empty'], 'required_field' => ['_required' => 'This field is required'], ]; - $this->testFullDataValidation($this->table, $dataSet, $expectedErrors); + $this->assertValidationTableErrors($this->table, $dataSet, $expectedErrors); } /** - * Test that testDataRules passes when saving leads to the expected rule errors. + * Test that assertRules passes when saving leads to the expected rule errors. * * @return void - * @covers ::testDataRules + * @covers ::assertRules */ - public function testTestDataRules(): void + public function testAssertRules(): void { $field = 'unique_field'; $dataSet = [$field => 'duplicate']; @@ -518,16 +518,16 @@ public function testTestDataRules(): void static::assertFalse($this->table->save($duplicate)); static::assertSame($expectedErrors, $duplicate->getError($field)); - $this->testDataRules($this->table, $field, $dataSet, $expectedErrors); + $this->assertRules($this->table, $field, $dataSet, $expectedErrors); } /** - * Test that testDataRulesNoErrors passes when saving leads to no rule errors. + * Test that assertRulesNoErrors passes when saving leads to no rule errors. * * @return void - * @covers ::testDataRulesNoErrors + * @covers ::assertRulesNoErrors */ - public function testTestDataRulesNoErrors(): void + public function testAssertRulesNoErrors(): void { // Ensure the rule works as expected first $field = 'unique_field'; @@ -537,16 +537,16 @@ public function testTestDataRulesNoErrors(): void static::assertNotFalse($this->table->save($entity)); static::assertEmpty($entity->getError($field)); - $this->testDataRulesNoErrors($this->table, $field, [$field => 'another-unique-' . uniqid('', true)]); + $this->assertRulesNoErrors($this->table, $field, [$field => 'another-unique-' . uniqid('', true)]); } /** - * Test that testDataValidationForeignKey passes when the foreign key does not exist. + * Test that assertValidationForeignKey passes when the foreign key does not exist. * * @return void - * @covers ::testDataValidationForeignKey + * @covers ::assertValidationForeignKey */ - public function testTestDataValidationForeignKey(): void + public function testAssertValidationForeignKey(): void { // Ensure the rule works as expected first $field = 'parent_id'; @@ -557,42 +557,42 @@ public function testTestDataValidationForeignKey(): void static::assertFalse($this->table->checkRules($entity)); static::assertSame($expectedErrors, $entity->getError($field)); - $this->testDataValidationForeignKey($this->table, $field, $notExistingForeignKey); + $this->assertValidationForeignKey($this->table, $field, $notExistingForeignKey); } /** - * Test that testDataValidationForeignKey passes with the default not existing foreign key. + * Test that assertValidationForeignKey passes with the default not existing foreign key. * * @return void - * @covers ::testDataValidationForeignKey + * @covers ::assertValidationForeignKey */ - public function testTestDataValidationForeignKeyDefault(): void + public function testAssertValidationForeignKeyDefault(): void { $field = 'parent_id'; - $this->testDataValidationForeignKey($this->table, $field); + $this->assertValidationForeignKey($this->table, $field); } /** - * Test that testDataValidationForeignKey accepts a custom expected error. + * Test that assertValidationForeignKey accepts a custom expected error. * * @return void - * @covers ::testDataValidationForeignKey + * @covers ::assertValidationForeignKey */ - public function testTestDataValidationForeignKeyCustomExpected(): void + public function testAssertValidationForeignKeyCustomExpected(): void { $field = 'parent_id'; $expectedErrors = ['_existsIn' => 'This value does not exist']; - $this->testDataValidationForeignKey($this->table, $field, 999999, $expectedErrors); + $this->assertValidationForeignKey($this->table, $field, 999999, $expectedErrors); } /** - * Test that testDataValidationIsUnique passes when the field value is not unique. + * Test that assertValidationIsUnique passes when the field value is not unique. * * @return void - * @covers ::testDataValidationIsUnique + * @covers ::assertValidationIsUnique */ - public function testTestDataValidationIsUnique(): void + public function testAssertValidationIsUnique(): void { // Ensure the rule works as expected first $field = 'unique_field'; @@ -612,16 +612,16 @@ public function testTestDataValidationIsUnique(): void // Use a different value since the trait method will also save a record $this->table->deleteAll([]); - $this->testDataValidationIsUnique($this->table, $field, 'another-duplicate-value', $dataset); + $this->assertValidationIsUnique($this->table, $field, 'another-duplicate-value', $dataset); } /** - * Test that testDataValidationIsUnique accepts a custom expected error. + * Test that assertValidationIsUnique accepts a custom expected error. * * @return void - * @covers ::testDataValidationIsUnique + * @covers ::assertValidationIsUnique */ - public function testTestDataValidationIsUniqueCustomExpected(): void + public function testAssertValidationIsUniqueCustomExpected(): void { $field = 'unique_field'; $dataset = [ @@ -630,41 +630,16 @@ public function testTestDataValidationIsUniqueCustomExpected(): void ]; $expectedErrors = ['_isUnique' => 'This value is already in use']; - $this->testDataValidationIsUnique($this->table, $field, 'custom-duplicate-value', $dataset, $expectedErrors); + $this->assertValidationIsUnique($this->table, $field, 'custom-duplicate-value', $dataset, $expectedErrors); } /** - * Test that testRules passes when saving leads to the expected rule errors. + * Test the assertValidationContains base method. * * @return void - * @covers ::testRules + * @covers ::assertValidationContains */ - public function testTestRules(): void - { - $field = 'unique_field'; - $dataSet = ['required_field' => 'required', $field => 'duplicate', 'multi_rule_field' => 1]; - $expectedErrors = ['_isUnique' => 'This value is already in use']; - - // Ensure a first record exists so the unique rule will fail on the second - $existing = $this->table->newEntity($dataSet); - static::assertNotFalse($this->table->save($existing)); - - // Ensure the rule works as expected first - $duplicate = $this->table->newEntity($dataSet); - static::assertEmpty($duplicate->getError($field)); - static::assertFalse($this->table->save($duplicate)); - static::assertSame($expectedErrors, $duplicate->getError($field)); - - $this->testRules($this->table, $field, $dataSet, $expectedErrors); - } - - /** - * Test the testDataValidationContains base method. - * - * @return void - * @covers ::testDataValidationContains - */ - public function testTestDataValidationContains(): void + public function testAssertValidationContains(): void { // An invalid scalar value must produce a `scalar` error $field = 'scalar_field'; @@ -673,61 +648,61 @@ public function testTestDataValidationContains(): void $entity = $this->table->newEntity($dataSet); static::assertArrayHasKey('scalar', $entity->getError($field)); - $this->testDataValidationContains($this->table, $field, $dataSet, $expectedErrors); + $this->assertValidationContains($this->table, $field, $dataSet, $expectedErrors); } /** - * Test the testDataValidationNotContains base method. + * Test the assertValidationNotContains base method. * * @return void - * @covers ::testDataValidationNotContains + * @covers ::assertValidationNotContains */ - public function testTestDataValidationNotContains(): void + public function testAssertValidationNotContains(): void { // A valid boolean value must not produce a `boolean` error $field = 'boolean_field'; $entity = $this->table->newEntity([$field => true]); static::assertArrayNotHasKey('boolean', $entity->getError($field)); - $this->testDataValidationNotContains($this->table, $field, [$field => true], ['boolean']); + $this->assertValidationNotContains($this->table, $field, [$field => true], ['boolean']); } /** - * Test the testDataValidationListContains base method. + * Test the assertValidationListContains base method. * * @return void - * @covers ::testDataValidationListContains + * @covers ::assertValidationListContains */ - public function testTestDataValidationListContains(): void + public function testAssertValidationListContains(): void { $field = 'boolean_field'; $expectedErrors = ['boolean' => 'The provided value must be a boolean']; $invalidValues = ['Not a boolean', 123]; - $this->testDataValidationListContains($this->table, $invalidValues, $field, $expectedErrors); + $this->assertValidationListContains($this->table, $invalidValues, $field, $expectedErrors); } /** - * Test the testDataValidationListNotContains base method. + * Test the assertValidationListNotContains base method. * * @return void - * @covers ::testDataValidationListNotContains + * @covers ::assertValidationListNotContains */ - public function testTestDataValidationListNotContains(): void + public function testAssertValidationListNotContains(): void { $field = 'boolean_field'; $validValues = [true, false, 1, 0]; - $this->testDataValidationListNotContains($this->table, $validValues, $field, ['boolean']); + $this->assertValidationListNotContains($this->table, $validValues, $field, ['boolean']); } /** - * Test the assertDataValidationErrorsContain base method. + * Test the assertValidationErrorsContain base method. * * @return void - * @covers ::assertDataValidationErrorsContain + * @covers ::assertValidationErrorsContain */ - public function testAssertDataValidationErrorsContain(): void + public function testAssertValidationErrorsContain(): void { $field = 'boolean_field'; // The errors contain the expected rule alongside an unrelated one, which must be ignored @@ -737,14 +712,14 @@ public function testAssertDataValidationErrorsContain(): void ]; $expected = ['boolean' => 'The provided value must be a boolean']; - $this->assertDataValidationErrorsContain($field, $errors, $expected); + $this->assertValidationErrorsContain($field, $errors, $expected); } /** * Test that a type-specific method only asserts its own rule and ignores unrelated errors. * * @return void - * @covers ::testDataValidationInteger + * @covers ::assertValidationInteger */ public function testTypeSpecificMethodIgnoresUnrelatedErrors(): void { @@ -757,6 +732,6 @@ public function testTypeSpecificMethodIgnoresUnrelatedErrors(): void static::assertArrayHasKey('maxLength', $errors); // The type-specific method still passes because it checks for the `integer` rule, only - $this->testDataValidationInteger($this->table, $field); + $this->assertValidationInteger($this->table, $field); } }