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
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
61 changes: 61 additions & 0 deletions docs/Migration.md
Original file line number Diff line number Diff line change
@@ -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.
83 changes: 41 additions & 42 deletions docs/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
```

Expand All @@ -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]
Expand All @@ -127,7 +126,7 @@ $this->testDataValidationNotEmpty(
The `$options` parameter is forwarded to `Table::newEntity()`:

```php
$this->testDataValidationRequired(
$this->assertValidationRequired(
$this->Articles,
'title',
[],
Expand Down
Loading
Loading