The DataValidationTestTrait provides helper methods to simplify testing the data validation configuration of your CakePHP tables.
Add the trait to any PHPUnit test case:
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Model\Table;
use Cake\TestSuite\TestCase;
use DataValidationTesting\Traits\DataValidationTestTrait;
class ArticlesTableTest extends TestCase
{
use DataValidationTestTrait;
private \App\Model\Table\ArticlesTable $Articles;
protected function setUp(): void
{
parent::setUp();
$this->Articles = $this->getTableLocator()->get('Articles');
}
}All helper methods are protected and callable from within your test class.
public function assertValidationTitle(): void
{
// Assert that the "title" field's presence is required
$this->assertValidationRequired($this->Articles, 'title');
// Assert that the field "title" cannot be empty
$this->assertValidationNotEmpty($this->Articles, 'title');
// Assert that the "title" field's maximum length is 255 characters
$this->assertValidationMaxLength($this->Articles, 'title', 255);
}
public function assertValidationIsPublished(): void
{
// Assert that the field "is_published" must have a boolean value
$this->assertValidationBoolean($this->Articles, 'is_published');
}Each helper builds an entity, runs the validator, and asserts the expected errors (or absence of errors) on the given field.
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.
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.nullor''(_empty).assertValidationEmpty($table, $fieldName)- asserts the field is configured to allow empty values, i.e.nullor''.
assertValidationBoolean($table, $fieldName)- asserts the field is configured with thebooleanvalidation rule.assertValidationURLWithProtocol($table, $fieldName)- asserts the field is configured with theurlWithProtocolvalidation rule (requireshttp://orhttps://).assertValidationDateTime($table, $fieldName)- asserts the field is configured with thedateTimevalidation rule.assertValidationDate($table, $fieldName)- asserts the field is configured with thedatevalidation rule.assertValidationNaturalNumber($table, $fieldName)- asserts the field is configured with thenaturalNumbervalidation rule (positive integers).assertValidationScalar($table, $fieldName)- asserts the field is configured with thescalarvalidation rule.assertValidationDecimal($table, $fieldName)- asserts the field is configured with thedecimalvalidation rule.assertValidationInteger($table, $fieldName)- asserts the field is configured with theintegervalidation rule.assertValidationNonNegativeInteger($table, $fieldName)- asserts the field is configured with thenonNegativeIntegervalidation rule.assertValidationGreaterThanOrEqual($table, $fieldName, $threshold)- asserts the field is configured with thegreaterThanOrEqualvalidation rule.assertValidationEmail($table, $fieldName)- asserts the field is configured with theemailvalidation rule.assertValidationUuid($table, $fieldName)- asserts the field is configured with theuuidvalidation rule.
assertValidationMaxLength($table, $fieldName, $maxLength)assertValidationMinLength($table, $fieldName, $minLength, $expected)assertValidationLengthBetween($table, $fieldName, $minLength, $maxLength)assertValidationRange($table, $fieldName, $lowerBound, $upperBound)
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 thecontainsassertion for each value in a list.assertValidationInListNotContains($table, $list, $fieldName, $rules)- runs thenot containsassertion 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.
For application rules that run at save time (not marshalling time). These require a real database connection and fixtures.
assertRulesNoErrors($table, $fieldName, $dataSet)assertRules($table, $fieldName, $dataSet, $expected)assertValidationUnique($table, $fieldName, $fieldValue)assertValidationForeignKey($table, $fieldName)
Use the $additionalDataSet parameter to supply required companion fields so your test only fails for the reason you care about:
$this->assertValidationNotEmpty(
$this->Articles,
'title',
['author_id' => 1]
);The $options parameter is forwarded to Table::newEntity():
$this->assertValidationRequired(
$this->Articles,
'title',
[],
['validate' => 'create']
);