Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ae328b9
Check only own validation rule
Neluxx Aug 26, 2026
e4fcb67
Add unit tests for new data validation helper methods
Neluxx Aug 26, 2026
9ae88fc
Add multi_rule_field to validation_test table
Neluxx Aug 26, 2026
2cd3549
Update usage guide
Neluxx Aug 26, 2026
b5d9280
Update change log
Neluxx Aug 26, 2026
61af269
Improve parameter documentation
Murl080 Aug 26, 2026
48634c8
Ignore dataValidation in `testDataValidationIsUnique()` to correctly …
Murl080 Aug 27, 2026
f384526
Apply suggestions from code review
Neluxx Aug 27, 2026
1ef9241
Rename $expected to $notExpected in rule-dedicated methods
Neluxx Aug 27, 2026
eda5a28
Reword type validator docs to describe the asserted rule
Neluxx Aug 27, 2026
03f2db8
Note that rule-dedicated helpers assert only their own rule
Neluxx Aug 27, 2026
0a9b52d
Group custom expected tests next to their sibling test cases
Neluxx Aug 27, 2026
a8b30e6
Add dedicated test for testDataValidationContains helper
Neluxx Aug 27, 2026
ef99a91
Adjust unit tests
Neluxx Aug 27, 2026
182cca4
Add dedicated test for assertDataValidationErrorsContain helper
Neluxx Aug 27, 2026
7b01d41
Blank lines for code readability
ravage84 Sep 11, 2026
1d32199
CS
ravage84 Sep 11, 2026
98d9cd8
Rename InList methods
Neluxx Sep 15, 2026
82457d7
Revert changing the name of testDataValidationInList method
Neluxx Sep 15, 2026
f5aa9a2
Fix reverted method name in test
Neluxx Sep 15, 2026
df20f53
Merge branch 'cakephp-5.x' into 38-check-only-own-validation-rule
Neluxx Sep 21, 2026
cc93e47
Use testDataValidationListNotContains instead of testDataValidationIn…
Neluxx Sep 21, 2026
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ 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.
- `testDataValidationInListContains()` and `testDataValidationInListNotContains()` list helpers.
- Optional custom `$expected` parameter for `testDataValidationForeignKey()` and `testDataValidationIsUnique()`.

### Changed
- **BREAKING CHANGE:** All type-specific/rule-dedicated methods now assert only their own validation rule for the field, ignoring others.

### Fixed
- Ignore dataValidation in `testDataValidationIsUnique()` to correctly assert build rules.

### Dependencies

Expand Down
48 changes: 30 additions & 18 deletions docs/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The `DataValidationTestTrait` provides helper methods to simplify testing the da
## Setup

Add the trait to any PHPUnit test case:

```php
<?php
declare(strict_types=1);
Expand All @@ -31,6 +32,7 @@ class ArticlesTableTest extends TestCase
All helper methods are `protected` and callable from within your test class.

## Example

```php
public function testValidationTitle(): void
{
Expand All @@ -53,27 +55,31 @@ Each helper builds an entity, runs the validator, and asserts the expected error

## Available methods

> **Note:** The presence, emptiness and type helpers listed below are rule-dedicated: each one only asserts that
> its own validation rule is (or is not) configured for the given field and ignores any other validation errors
> present on that field. They no longer compare against the field's full error list.

### Presence and emptiness

- `testDataValidationRequired($table, $fieldName)` - field must be present.
- `testDataValidationNotRequired($table, $fieldName)` - field is optional.
- `testDataValidationNotEmpty($table, $fieldName)` - field cannot be `null` or `''`.
- `testDataValidationEmpty($table, $fieldName)` - field may be `null` or `''`.
- `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 `''`.

### Type validators

- `testDataValidationBoolean($table, $fieldName)` - field must be boolean.
- `testDataValidationURLWithProtocol($table, $fieldName)` - requires `http://` or `https://`.
- `testDataValidationDateTime($table, $fieldName)` - field must be datetime.
- `testDataValidationDate($table, $fieldName)` - field must be date.
- `testDataValidationNaturalNumber($table, $fieldName)` - positive integers only.
- `testDataValidationScalar($table, $fieldName)` - rejects non-scalar values like arrays.
- `testDataValidationDecimal($table, $fieldName)` - rejects non-decimal values like arrays.
- `testDataValidationInteger($table, $fieldName)` - rejects non-integer values like arrays.
- `testDataValidationNonNegativeInteger($table, $fieldName)` - rejects negative integer values like `-1`.
- `testDataValidationGreaterThanOrEqual($table, $fieldName)` - rejects values below the threshold.
- `testDataValidationEmail($table, $fieldName)` - rejects invalid email addresses.
- `testDataValidationUuid($table, $fieldName)` - rejects invalid UUIDs.
- `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.

### Length validators

Expand All @@ -84,9 +90,13 @@ Each helper builds an entity, runs the validator, and asserts the expected error

### Generic helpers

- `testDataValidation($table, $fieldName, $dataSet, $expected)` - the underlying helper. Use when no specialized helper fits.
- `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 same assertion for each value in a list.
- `testDataValidationInList($table, $list, $fieldName, $expected)` - runs the complete-error-array assertion for each value in a list.
Comment thread
Neluxx marked this conversation as resolved.
- `testDataValidationInListContains($table, $list, $fieldName, $expected)` - runs the `contains` assertion for each value in a list.
Comment thread
Neluxx marked this conversation as resolved.
- `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.

Expand All @@ -103,6 +113,7 @@ For application rules that run at save time (not marshalling time). These requir
## 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->Articles,
Expand All @@ -114,6 +125,7 @@ $this->testDataValidationNotEmpty(
## Passing `newEntity()` options

The `$options` parameter is forwarded to `Table::newEntity()`:

```php
$this->testDataValidationRequired(
$this->Articles,
Expand Down
Loading
Loading