Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -83,9 +89,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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InList`` is actually a type validation.

https://api.cakephp.org/5.3/class-Cake.Validation.Validation.html#inList()

Let's move it.

- `testDataValidationInListContains($table, $list, $fieldName, $expected)` - runs the `contains` assertion for each value in a list.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since InList is an actual validation method, I think these two methods should be renamed. I think just taking away the In would prevent mix-ups.

- `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 @@ -102,6 +112,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 @@ -113,6 +124,7 @@ $this->testDataValidationNotEmpty(
## Passing `newEntity()` options

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

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