diff --git a/.php_cs b/.php_cs index 873f663..13061e4 100644 --- a/.php_cs +++ b/.php_cs @@ -96,13 +96,11 @@ $rules = [ 'header_comment' => [ 'header' => $header, - 'separate' => 'bottom', - 'comment_type' => 'PHPDoc', ] ]; $finder = PhpCsFixer\Finder::create() - ->in(__DIR__ . '/src'); + ->in([__DIR__ . '/src', __DIR__ . '/tests']); return PhpCsFixer\Config::create() ->setRules($rules) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30b5de4..1f474a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [4.2.2] - 2019-03-28 +- Fix error if custom validator doesn't provides a message. +- Removed broken and deprecated `RequestSchema::loadSchema` method. +- 100% Test coverage ([#24]) + ## [4.2.1] - 2019-01-13 - Fix issue with ResourceLocator @@ -34,6 +39,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - Implement equals, not_equals, telephone, uri, and username rules +[4.2.2]: https://github.com/userfrosting/fortress/compare/4.2.1...4.2.2 [4.2.1]: https://github.com/userfrosting/fortress/compare/4.2.0...4.2.1 [4.2.0]: https://github.com/userfrosting/fortress/compare/4.1.2...4.2.0 [4.1.3]: https://github.com/userfrosting/fortress/compare/4.1.2...4.1.3 @@ -41,3 +47,4 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a [4.1.1]: https://github.com/userfrosting/fortress/compare/4.1.0...v4.1.1 [4.1.0]: https://github.com/userfrosting/fortress/compare/4.0.1...4.1.0 [4.0.1]: https://github.com/userfrosting/fortress/compare/4.0.0...4.0.1 +[#24]: https://github.com/userfrosting/fortress/issues/24 diff --git a/README.md b/README.md index 5f270c9..20b96c6 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,29 @@ [![Latest Version](https://img.shields.io/github/release/userfrosting/fortress.svg)](https://github.com/userfrosting/fortress/releases) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE.md) -[![Build Status](https://travis-ci.org/userfrosting/fortress.svg?branch=master)](https://travis-ci.org/userfrosting/fortress) -[![codecov](https://codecov.io/gh/userfrosting/fortress/branch/master/graph/badge.svg)](https://codecov.io/gh/userfrosting/fortress) [![Join the chat at https://chat.userfrosting.com/channel/support](https://demo.rocket.chat/images/join-chat.svg)](https://chat.userfrosting.com/channel/support) [![Donate](https://img.shields.io/badge/Open%20Collective-Donate-blue.svg)](https://opencollective.com/userfrosting#backer) +| Branch | Build | Coverage | Style | +| ------ |:-----:|:--------:|:-----:| +| [master][Fortress] | [![][fortress-master-build]][fortress-travis] | [![][fortress-master-codecov]][fortress-codecov] | [![][fortress-style-master]][fortress-style] | +| [develop][fortress-develop] | [![][fortress-develop-build]][fortress-travis] | [![][fortress-develop-codecov]][fortress-codecov] | [![][fortress-style-develop]][fortress-style] | + + +[Fortress]: https://github.com/userfrosting/fortress +[fortress-develop]: https://github.com/userfrosting/fortress/tree/develop +[fortress-version]: https://img.shields.io/github/release/userfrosting/fortress.svg +[fortress-master-build]: https://travis-ci.org/userfrosting/fortress.svg?branch=master +[fortress-master-codecov]: https://codecov.io/gh/userfrosting/fortress/branch/master/graph/badge.svg +[fortress-develop-build]: https://travis-ci.org/userfrosting/fortress.svg?branch=develop +[fortress-develop-codecov]: https://codecov.io/gh/userfrosting/fortress/branch/develop/graph/badge.svg +[fortress-releases]: https://github.com/userfrosting/fortress/releases +[fortress-travis]: https://travis-ci.org/userfrosting/fortress +[fortress-codecov]: https://codecov.io/gh/userfrosting/fortress +[fortress-style-master]: https://github.styleci.io/repos/30551954/shield?branch=master&style=flat +[fortress-style-develop]: https://github.styleci.io/repos/30551954/shield?branch=develop&style=flat +[fortress-style]: https://github.styleci.io/repos/30551954 + If you simply want to show that you like this project, or want to remember it for later, you should **star**, not **fork**, this repository. Forking is only for when you are ready to create your own copy of the code to work on. ### By [Alex Weissman](https://alexanderweissman.com) @@ -85,7 +103,7 @@ message: { "require": { "php": ">=5.6.0", - "userfrosting/fortress": "^4.1.0" + "userfrosting/fortress": "^4.2.0" } } ``` diff --git a/composer.json b/composer.json index ae574ac..e294ba2 100644 --- a/composer.json +++ b/composer.json @@ -26,5 +26,10 @@ "psr-4": { "UserFrosting\\Fortress\\": "src" } + }, + "autoload-dev": { + "psr-4": { + "UserFrosting\\Fortress\\Tests\\": "tests" + } } } diff --git a/src/Adapter/ClientSideValidationAdapter.php b/src/Adapter/ClientSideValidationAdapter.php index 4f35c50..cb33378 100644 --- a/src/Adapter/ClientSideValidationAdapter.php +++ b/src/Adapter/ClientSideValidationAdapter.php @@ -1,5 +1,6 @@ schema->getSchema() as $fieldName => $field) { + foreach ($this->schema->all() as $fieldName => $field) { $clientRules[$fieldName] = []; $clientRules[$fieldName]['validators'] = []; @@ -70,7 +72,7 @@ public function formValidationRulesHtml5() { $clientRules = []; $implicitRules = []; - foreach ($this->schema->getSchema() as $fieldName => $field) { + foreach ($this->schema->all() as $fieldName => $field) { $fieldRules = ''; $validators = $field['validators']; @@ -243,13 +245,13 @@ private function transformValidator($fieldName, $validatorName, array $validator break; case 'member_of': if (isset($validator['values'])) { - $params['regexp'] = '^' . implode('|', $validator['values']) . '$'; + $params['regexp'] = '^'.implode('|', $validator['values']).'$'; } $transformedValidatorJson['regexp'] = $params; break; case 'not_member_of': if (isset($validator['values'])) { - $params['regexp'] = '^(?!' . implode('|', $validator['values']) . '$).*$'; + $params['regexp'] = '^(?!'.implode('|', $validator['values']).'$).*$'; } $transformedValidatorJson['regexp'] = $params; break; @@ -273,8 +275,6 @@ public function html5Attributes($validator, $prefix) $msg = ''; if (isset($validator['message'])) { $msg = $validator['message']; - } else { - return $attr; } $attr .= "$prefix-message=\"$msg\" "; } diff --git a/src/Adapter/JqueryValidationAdapter.php b/src/Adapter/JqueryValidationAdapter.php index e3e013a..a123bf8 100644 --- a/src/Adapter/JqueryValidationAdapter.php +++ b/src/Adapter/JqueryValidationAdapter.php @@ -1,5 +1,6 @@ schema->all() as $fieldNameO => $field) { $fieldNameOnly = $fieldNameO; if ($arrayPrefix != '') { - $fieldName = $arrayPrefix . '[' . $fieldNameO . ']'; + $fieldName = $arrayPrefix.'['.$fieldNameO.']'; } else { $fieldName = $fieldNameO; } @@ -69,7 +73,7 @@ public function rules($format = 'json', $stringEncode = false, $arrayPrefix = '' } $result = [ 'rules' => $clientRules, - 'messages' => $clientMessages + 'messages' => $clientMessages, ]; if ($stringEncode) { @@ -106,7 +110,7 @@ private function transformValidator($fieldName, $validatorName, array $validator if (isset($validator['min']) && isset($validator['max'])) { $transformedValidatorJson['rangelength'] = [ $validator['min'], - $validator['max'] + $validator['max'], ]; } elseif (isset($validator['min'])) { $transformedValidatorJson['minlength'] = $validator['min']; @@ -152,7 +156,7 @@ private function transformValidator($fieldName, $validatorName, array $validator if (isset($validator['min']) && isset($validator['max'])) { $transformedValidatorJson['range'] = [ $validator['min'], - $validator['max'] + $validator['max'], ]; } elseif (isset($validator['min'])) { $transformedValidatorJson['min'] = $validator['min']; diff --git a/src/RequestDataTransformer.php b/src/RequestDataTransformer.php index 12a4a42..c74b008 100644 --- a/src/RequestDataTransformer.php +++ b/src/RequestDataTransformer.php @@ -1,5 +1,6 @@ values. + * @param array $data The array of data to be transformed. + * @param string $onUnexpectedVar[optional] Determines what to do when a field is encountered that is not in the schema. Set to one of: + * "allow": Treat the field as any other, allowing the value through. + * "error": Raise an exception. + * "skip" (default): Quietly ignore the field. It will not be part of the transformed data array. + * + * @return array The array of transformed data, mapping field names => values. */ public function transform(array $data, $onUnexpectedVar); /** * Transform a raw field value. * - * @param string $name The name of the field to transform, as specified in the schema. - * @param string $value The value to be transformed. + * @param string $name The name of the field to transform, as specified in the schema. + * @param string $value The value to be transformed. + * * @return string The transformed value. */ public function transformField($name, $value); diff --git a/src/RequestSchema.php b/src/RequestSchema.php index 8ca0372..37c5f54 100644 --- a/src/RequestSchema.php +++ b/src/RequestSchema.php @@ -1,5 +1,6 @@ items; } - - /** - * @deprecated since 4.1 - * @param string $path Path to the schema file. - * @throws Exception The file does not exist or is not a valid format. - */ - public function loadSchema() - { - return $this->load($path); - } } diff --git a/src/RequestSchema/RequestSchemaInterface.php b/src/RequestSchema/RequestSchemaInterface.php index 11c3b5c..3102ea8 100644 --- a/src/RequestSchema/RequestSchemaInterface.php +++ b/src/RequestSchema/RequestSchemaInterface.php @@ -1,5 +1,6 @@ parameter value (e.g. [ "min" => 50 ]) + * + * @param string $field The name of the field for this validator (e.g., "user_name") + * @param string $validatorName A validator rule, as specified in https://github.com/alexweissman/wdvss (e.g. "length") + * @param array $parameters An array of parameters, hashed as parameter_name => parameter value (e.g. [ "min" => 50 ]) + * * @return RequestSchemaInterface This schema object. */ public function addValidator($field, $validatorName, array $parameters = []); @@ -59,8 +65,9 @@ public function addValidator($field, $validatorName, array $parameters = []); /** * Remove a validator for a specified field. * - * @param string $field The name of the field for this validator (e.g., "user_name") - * @param string $validatorName A validator rule, as specified in https://github.com/alexweissman/wdvss (e.g. "length") + * @param string $field The name of the field for this validator (e.g., "user_name") + * @param string $validatorName A validator rule, as specified in https://github.com/alexweissman/wdvss (e.g. "length") + * * @return RequestSchemaInterface This schema object. */ public function removeValidator($field, $validatorName); @@ -69,8 +76,10 @@ public function removeValidator($field, $validatorName); * Set a sequence of transformations for a specified field. * * If the specified field does not exist in the schema, add it. - * @param string $field The name of the field for this transformation (e.g., "user_name") - * @param string|array $transformations An array of transformations, as specified in https://github.com/alexweissman/wdvss (e.g. "purge") + * + * @param string $field The name of the field for this transformation (e.g., "user_name") + * @param string|array $transformations An array of transformations, as specified in https://github.com/alexweissman/wdvss (e.g. "purge") + * * @return RequestSchemaInterface This schema object. */ public function setTransformations($field, $transformations = []); diff --git a/src/RequestSchema/RequestSchemaRepository.php b/src/RequestSchema/RequestSchemaRepository.php index cde5eac..96f1bbf 100644 --- a/src/RequestSchema/RequestSchemaRepository.php +++ b/src/RequestSchema/RequestSchemaRepository.php @@ -1,5 +1,6 @@ message($messageSet); } @@ -279,7 +286,7 @@ private function generateSchemaRules() } // Regex validator if ($validatorName == 'regex') { - $this->ruleWithMessage('regex', $messageSet, $fieldName, '/' . $validator['regex'] . '/'); + $this->ruleWithMessage('regex', $messageSet, $fieldName, '/'.$validator['regex'].'/'); } // Required validator if ($validatorName == 'required') { diff --git a/src/ServerSideValidatorInterface.php b/src/ServerSideValidatorInterface.php index 1db9d33..f5607f8 100644 --- a/src/ServerSideValidatorInterface.php +++ b/src/ServerSideValidatorInterface.php @@ -1,5 +1,6 @@ basePath = __DIR__ . '/data'; + $this->basePath = __DIR__.'/data'; // Arrange $this->locator = new ResourceLocator($this->basePath); @@ -39,40 +49,40 @@ public function testExtendYamlSchema() // Assert $this->assertEquals([ - "name" => [ - "validators" => [ - "length" => [ - "min" => 1, - "max" => 200, - "message" => "Please enter a name between 1 and 200 characters." + 'name' => [ + 'validators' => [ + 'length' => [ + 'min' => 1, + 'max' => 200, + 'message' => 'Please enter a name between 1 and 200 characters.', ], - "required" => [ - "message" => "Please specify your name." - ] - ] + 'required' => [ + 'message' => 'Please specify your name.', + ], + ], ], - "email" => [ - "validators" => [ - "length" => [ - "min" => 1, - "max" => 150, - "message" => "Please enter an email address between 1 and 150 characters." + 'email' => [ + 'validators' => [ + 'length' => [ + 'min' => 1, + 'max' => 150, + 'message' => 'Please enter an email address between 1 and 150 characters.', + ], + 'email' => [ + 'message' => 'That does not appear to be a valid email address.', ], - "email" => [ - "message" => "That does not appear to be a valid email address." + 'required' => [ + 'message' => 'Please specify your email address.', + ], + ], + ], + 'message' => [ + 'validators' => [ + 'required' => [ + 'message' => 'Please enter a message', ], - "required" => [ - "message" => "Please specify your email address." - ] - ] + ], ], - "message" => [ - "validators" => [ - "required" => [ - "message" => "Please enter a message" - ] - ] - ] ], $result); } } diff --git a/tests/FormValidationAdapterTest.php b/tests/FormValidationAdapterTest.php new file mode 100644 index 0000000..905fddb --- /dev/null +++ b/tests/FormValidationAdapterTest.php @@ -0,0 +1,684 @@ +translator = new MessageTranslator(); + } + + public function testValidateEmail() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'email' => [ + 'validators' => [ + 'email' => [ + 'message' => 'Not a valid email address...we think.', + ], + ], + ], + ]); + + $expectedResult = [ + 'email' => [ + 'validators' => [ + 'emailAddress' => [ + 'message' => 'Not a valid email address...we think.', + ], + ], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + // Assert + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT), $result); + + // Test with stringEncode as true + $result = $adapter->rules('json', false); + $this->assertEquals($expectedResult, $result); + + // Test with html5 format + $result = $adapter->rules('html5'); + $expectedResult = ['email' => 'data-fv-emailaddress=true data-fv-emailaddress-message="Not a valid email address...we think." ']; + $this->assertEquals($expectedResult, $result); + } + + /** + * N.B.: equals is not a supported validator in FormValidationAdapter. + * Let's test what's happening when this happens. + */ + public function testValidateEquals() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'voles' => [ + 'validators' => [ + 'equals' => [ + 'value' => 8, + 'caseSensitive' => false, + 'message' => 'Voles must be equal to {{value}}.', + ], + ], + ], + ]); + + $expectedResult = [ + 'voles' => [ + 'validators' => [ + ], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT), $result); + + // Test with html5 format + $result = $adapter->rules('html5'); + $expectedResult = ['voles' => '']; + $this->assertEquals($expectedResult, $result); + } + + public function testValidateRequired() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'species' => [ + 'validators' => [ + 'required' => [ + 'message' => 'Please tell us your species.', + ], + ], + ], + ]); + + $expectedResult = [ + 'species' => [ + 'validators' => [ + 'notEmpty' => [ + 'message' => 'Please tell us your species.', + ], + ], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + // Assert + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT), $result); + + // Test with html5 format + $result = $adapter->rules('html5'); + $expectedResult = ['species' => 'data-fv-notempty=true data-fv-notempty-message="Please tell us your species." ']; + $this->assertEquals($expectedResult, $result); + } + + public function testValidateLengthBetween() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'screech' => [ + 'validators' => [ + 'length' => [ + 'min' => 5, + 'max' => 10, + 'message' => 'Your screech must be between {{min}} and {{max}} characters long.', + ], + ], + ], + ]); + + $expectedResult = [ + 'screech' => [ + 'validators' => [ + 'stringLength' => [ + 'message' => 'Your screech must be between 5 and 10 characters long.', + 'min' => 5, + 'max' => 10, + ], + ], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + // Assert + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT), $result); + + // Test with html5 format + $result = $adapter->rules('html5'); + $expectedResult = ['screech' => 'data-fv-stringlength=true data-fv-stringlength-message="Your screech must be between {{min}} and {{max}} characters long." data-fv-stringlength-min=5 data-fv-stringlength-max=10 ']; + $this->assertEquals($expectedResult, $result); + } + + public function testValidateLengthMin() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'screech' => [ + 'validators' => [ + 'length' => [ + 'min' => 5, + 'message' => 'Your screech must be at least {{min}} characters long.', + ], + ], + ], + ]); + + $expectedResult = [ + 'screech' => [ + 'validators' => [ + 'stringLength' => [ + 'message' => 'Your screech must be at least 5 characters long.', + 'min' => 5, + ], + ], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + // Assert + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT), $result); + + // Test with html5 format + $result = $adapter->rules('html5'); + $expectedResult = ['screech' => 'data-fv-stringlength=true data-fv-stringlength-message="Your screech must be at least {{min}} characters long." data-fv-stringlength-min=5 ']; + $this->assertEquals($expectedResult, $result); + } + + public function testValidateLengthMax() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'screech' => [ + 'validators' => [ + 'length' => [ + 'max' => 10, + 'message' => 'Your screech must be no more than {{max}} characters long.', + ], + ], + ], + ]); + + $expectedResult = [ + 'screech' => [ + 'validators' => [ + 'stringLength' => [ + 'message' => 'Your screech must be no more than 10 characters long.', + 'max' => 10, + ], + ], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + // Assert + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT), $result); + + // Test with html5 format + $result = $adapter->rules('html5'); + $expectedResult = ['screech' => 'data-fv-stringlength=true data-fv-stringlength-message="Your screech must be no more than {{max}} characters long." data-fv-stringlength-max=10 ']; + $this->assertEquals($expectedResult, $result); + } + + public function testValidateInteger() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'voles' => [ + 'validators' => [ + 'integer' => [ + 'message' => 'Voles must be numeric.', + ], + ], + ], + ]); + + $expectedResult = [ + 'voles' => [ + 'validators' => [ + 'integer' => [ + 'message' => 'Voles must be numeric.', + ], + ], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + // Assert + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT), $result); + + // Test with html5 format + $result = $adapter->rules('html5'); + $expectedResult = ['voles' => 'data-fv-integer=true data-fv-integer-message="Voles must be numeric." ']; + $this->assertEquals($expectedResult, $result); + } + + public function testValidateNumeric() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'accuracy' => [ + 'validators' => [ + 'numeric' => [ + 'message' => 'Sorry, your strike accuracy must be a number.', + ], + ], + ], + ]); + + $expectedResult = [ + 'accuracy' => [ + 'validators' => [ + 'numeric' => [ + 'message' => 'Sorry, your strike accuracy must be a number.', + ], + ], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + // Assert + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT), $result); + } + + public function testValidateRange() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'voles' => [ + 'validators' => [ + 'range' => [ + 'min' => 5, + 'max' => 10, + 'message' => 'You must catch {{min}} - {{max}} voles.', + ], + ], + ], + ]); + + $expectedResult = [ + 'voles' => [ + 'validators' => [ + 'between' => [ + 'message' => 'You must catch 5 - 10 voles.', + 'min' => 5, + 'max' => 10, + ], + ], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + // Assert + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT), $result); + + // Test with html5 format + $result = $adapter->rules('html5'); + $expectedResult = ['voles' => 'data-fv-between=true data-fv-between-message="You must catch {{min}} - {{max}} voles." data-fv-between-min=5 data-fv-between-max=10 ']; + $this->assertEquals($expectedResult, $result); + } + + public function testValidateRangeMin() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'voles' => [ + 'validators' => [ + 'range' => [ + 'min' => 5, + 'message' => 'You must catch at least {{min}} voles.', + ], + ], + ], + ]); + + $expectedResult = [ + 'voles' => [ + 'validators' => [ + 'greaterThan' => [ + 'message' => 'You must catch at least 5 voles.', + 'min' => 5, + ], + ], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + // Assert + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT), $result); + + // Test with html5 format + $result = $adapter->rules('html5'); + $expectedResult = ['voles' => 'data-fv-greaterthan=true data-fv-greaterthan-message="You must catch at least {{min}} voles." data-fv-greaterthan-value=5 ']; + $this->assertEquals($expectedResult, $result); + } + + public function testValidateRangeMax() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'voles' => [ + 'validators' => [ + 'range' => [ + 'max' => 10, + 'message' => 'You must catch no more than {{max}} voles.', + ], + ], + ], + ]); + + $expectedResult = [ + 'voles' => [ + 'validators' => [ + 'lessThan' => [ + 'message' => 'You must catch no more than 10 voles.', + 'max' => 10, + ], + ], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + // Assert + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT), $result); + + // Test with html5 format + $result = $adapter->rules('html5'); + $expectedResult = ['voles' => 'data-fv-lessthan=true data-fv-lessthan-message="You must catch no more than {{max}} voles." data-fv-lessthan-value=10 ']; + $this->assertEquals($expectedResult, $result); + } + + public function testValidateArray() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'voles' => [ + 'validators' => [ + 'array' => [ + 'min' => 5, + 'max' => 10, + 'message' => 'You must choose between {{min}} and {{max}} voles.', + ], + ], + ], + ]); + + $expectedResult = [ + 'voles' => [ + 'validators' => [ + 'choice' => [ + 'message' => 'You must choose between 5 and 10 voles.', + 'min' => 5, + 'max' => 10, + ], + ], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + // Assert + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT), $result); + + // Test with html5 format + $result = $adapter->rules('html5'); + $expectedResult = ['voles' => 'data-fv-choice=true data-fv-choice-message="You must choose between {{min}} and {{max}} voles." data-fv-choice-min=5 data-fv-choice-max=10 ']; + $this->assertEquals($expectedResult, $result); + } + + public function testValidateMatches() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'password' => [ + 'validators' => [ + 'matches' => [ + 'field' => 'passwordc', + 'message' => "The value of this field does not match the value of the '{{field}}' field.", + ], + ], + ], + 'passwordc' => [ + 'validators' => [], + ], + ]); + + $expectedResult = [ + 'password' => [ + 'validators' => [ + 'identical' => [ + 'message' => "The value of this field does not match the value of the 'passwordc' field.", + 'field' => 'passwordc', + ], + ], + ], + 'passwordc' => [ + 'validators' => [], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + // Assert + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT), $result); + + // Test with html5 format + $result = $adapter->rules('html5'); + $expectedResult = [ + 'password' => 'data-fv-identical=true data-fv-identical-message="The value of this field does not match the value of the \'{{field}}\' field." ', + 'passwordc' => 'data-fv-identical=true data-fv-identical-message="The value of this field does not match the value of the \'{{field}}\' field." data-fv-identical-field=password ', + ]; + $this->assertEquals($expectedResult, $result); + } + + public function testValidateMatchesNoFields() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'password' => [ + 'validators' => [ + 'matches' => [ + 'message' => "The value of this field does not match the value of the '{{field}}' field.", + ], + ], + ], + ]); + + $expectedResult = [ + 'password' => [ + 'validators' => [ + 'identical' => [ + 'message' => "The value of this field does not match the value of the '' field.", + ], + ], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + // Assert + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT), $result); + + // Test with html5 format + $result = $adapter->rules('html5'); + $this->assertEquals(null, $result); + } + + public function testValidateNotMatches() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'password' => [ + 'validators' => [ + 'not_matches' => [ + 'field' => 'user_name', + 'message' => 'Your password cannot be the same as your username.', + ], + ], + ], + ]); + + $expectedResult = [ + 'password' => [ + 'validators' => [ + 'different' => [ + 'message' => 'Your password cannot be the same as your username.', + 'field' => 'user_name', + ], + ], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + // Assert + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT), $result); + } + + public function testValidateMemberOf() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'genus' => [ + 'validators' => [ + 'member_of' => [ + 'values' => ['Megascops', 'Bubo', 'Glaucidium', 'Tyto', 'Athene'], + 'message' => 'Sorry, that is not one of the permitted genuses.', + ], + ], + ], + ]); + + $expectedResult = [ + 'genus' => [ + 'validators' => [ + 'regexp' => [ + 'message' => 'Sorry, that is not one of the permitted genuses.', + 'regexp' => '^Megascops|Bubo|Glaucidium|Tyto|Athene$', + ], + ], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + // Assert + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT), $result); + } + + public function testValidateNotMemberOf() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'genus' => [ + 'validators' => [ + 'not_member_of' => [ + 'values' => ['Myodes', 'Microtus', 'Neodon', 'Alticola'], + 'message' => 'Sorry, it would appear that you are not an owl.', + ], + ], + ], + ]); + + $expectedResult = [ + 'genus' => [ + 'validators' => [ + 'regexp' => [ + 'message' => 'Sorry, it would appear that you are not an owl.', + 'regexp' => '^(?!Myodes|Microtus|Neodon|Alticola$).*$', + ], + ], + ], + ]; + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + // Assert + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT), $result); + } + + public function testDomainRulesServerOnly() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'plumage' => [ + 'validators' => [ + 'required' => [ + 'domain' => 'server', + 'message' => "Are you sure you don't want to show us your plumage?", + ], + ], + ], + ]); + + // Act + $adapter = new FormValidationAdapter($schema, $this->translator); + + // Test with html5 format + $result = $adapter->rules('html5'); + $expectedResult = ['plumage' => '']; + $this->assertEquals($expectedResult, $result); + } +} diff --git a/tests/JqueryValidationAdapterTest.php b/tests/JqueryValidationAdapterTest.php index 5b9b903..f1ec874 100644 --- a/tests/JqueryValidationAdapterTest.php +++ b/tests/JqueryValidationAdapterTest.php @@ -1,10 +1,19 @@ translator = new MessageTranslator(); } @@ -23,28 +32,34 @@ public function testValidateEmail() 'email' => [ 'validators' => [ 'email' => [ - 'message' => 'Not a valid email address...we think.' - ] - ] - ] + 'message' => 'Not a valid email address...we think.', + ], + ], + ], ]); - // Act - $adapter = new JqueryValidationAdapter($schema, $this->translator); - $result = $adapter->rules(); - - $this->assertEquals([ + $expectedResult = [ 'rules' => [ 'email' => [ - 'email' => true - ] + 'email' => true, + ], ], 'messages' => [ 'email' => [ - 'email' => 'Not a valid email address...we think.' - ] - ] - ], $result); + 'email' => 'Not a valid email address...we think.', + ], + ], + ]; + + // Act + $adapter = new JqueryValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + + $this->assertEquals($expectedResult, $result); + + // Test with stringEncode as true + $result = $adapter->rules('json', true); + $this->assertEquals(json_encode($expectedResult, JSON_PRETTY_PRINT), $result); } public function testValidateEquals() @@ -54,12 +69,12 @@ public function testValidateEquals() 'voles' => [ 'validators' => [ 'equals' => [ - 'value' => 8, + 'value' => 8, 'caseSensitive' => false, - 'message' => 'Voles must be equal to {{value}}.' - ] - ] - ] + 'message' => 'Voles must be equal to {{value}}.', + ], + ], + ], ]); // Act @@ -70,17 +85,17 @@ public function testValidateEquals() 'rules' => [ 'voles' => [ 'equals' => [ - 'value' => 8, + 'value' => 8, 'caseSensitive' => false, - 'message' => 'Voles must be equal to {{value}}.' - ] - ] + 'message' => 'Voles must be equal to {{value}}.', + ], + ], ], 'messages' => [ 'voles' => [ - 'equals' => 'Voles must be equal to 8.' - ] - ] + 'equals' => 'Voles must be equal to 8.', + ], + ], ], $result); } @@ -91,10 +106,10 @@ public function testValidateInteger() 'voles' => [ 'validators' => [ 'integer' => [ - 'message' => 'Voles must be numeric.' - ] - ] - ] + 'message' => 'Voles must be numeric.', + ], + ], + ], ]); // Act @@ -104,14 +119,14 @@ public function testValidateInteger() $this->assertEquals([ 'rules' => [ 'voles' => [ - 'digits' => true - ] + 'digits' => true, + ], ], 'messages' => [ 'voles' => [ - 'digits' => 'Voles must be numeric.' - ] - ] + 'digits' => 'Voles must be numeric.', + ], + ], ], $result); } @@ -122,12 +137,12 @@ public function testValidateLengthBetween() 'screech' => [ 'validators' => [ 'length' => [ - 'min' => 5, - 'max' => 10, - 'message' => "Your screech must be between {{min}} and {{max}} characters long." - ] - ] - ] + 'min' => 5, + 'max' => 10, + 'message' => 'Your screech must be between {{min}} and {{max}} characters long.', + ], + ], + ], ]); // Act @@ -137,14 +152,14 @@ public function testValidateLengthBetween() $this->assertEquals([ 'rules' => [ 'screech' => [ - 'rangelength' => [5, 10] - ] + 'rangelength' => [5, 10], + ], ], 'messages' => [ 'screech' => [ - 'rangelength' => "Your screech must be between 5 and 10 characters long." - ] - ] + 'rangelength' => 'Your screech must be between 5 and 10 characters long.', + ], + ], ], $result); } @@ -155,11 +170,11 @@ public function testValidateLengthMin() 'screech' => [ 'validators' => [ 'length' => [ - 'min' => 5, - 'message' => "Your screech must be at least {{min}} characters long." - ] - ] - ] + 'min' => 5, + 'message' => 'Your screech must be at least {{min}} characters long.', + ], + ], + ], ]); // Act @@ -169,14 +184,14 @@ public function testValidateLengthMin() $this->assertEquals([ 'rules' => [ 'screech' => [ - 'minlength' => 5 - ] + 'minlength' => 5, + ], ], 'messages' => [ 'screech' => [ - 'minlength' => "Your screech must be at least 5 characters long." - ] - ] + 'minlength' => 'Your screech must be at least 5 characters long.', + ], + ], ], $result); } @@ -187,11 +202,11 @@ public function testValidateLengthMax() 'screech' => [ 'validators' => [ 'length' => [ - 'max' => 10, - 'message' => "Your screech must be no more than {{max}} characters long." - ] - ] - ] + 'max' => 10, + 'message' => 'Your screech must be no more than {{max}} characters long.', + ], + ], + ], ]); // Act @@ -201,14 +216,14 @@ public function testValidateLengthMax() $this->assertEquals([ 'rules' => [ 'screech' => [ - 'maxlength' => 10 - ] + 'maxlength' => 10, + ], ], 'messages' => [ 'screech' => [ - 'maxlength' => "Your screech must be no more than 10 characters long." - ] - ] + 'maxlength' => 'Your screech must be no more than 10 characters long.', + ], + ], ], $result); } @@ -219,11 +234,11 @@ public function testValidateMatches() 'password' => [ 'validators' => [ 'matches' => [ - 'field' => 'passwordc', - 'message' => "The value of this field does not match the value of the '{{field}}' field." - ] - ] - ] + 'field' => 'passwordc', + 'message' => "The value of this field does not match the value of the '{{field}}' field.", + ], + ], + ], ]); // Act @@ -233,14 +248,14 @@ public function testValidateMatches() $this->assertEquals([ 'rules' => [ 'password' => [ - 'matchFormField' => 'passwordc' - ] + 'matchFormField' => 'passwordc', + ], ], 'messages' => [ 'password' => [ - 'matchFormField' => "The value of this field does not match the value of the 'passwordc' field." - ] - ] + 'matchFormField' => "The value of this field does not match the value of the 'passwordc' field.", + ], + ], ], $result); } @@ -251,11 +266,11 @@ public function testValidateMemberOf() 'genus' => [ 'validators' => [ 'member_of' => [ - 'values' => ["Megascops", "Bubo", "Glaucidium", "Tyto", "Athene"], - 'message' => "Sorry, that is not one of the permitted genuses." - ] - ] - ] + 'values' => ['Megascops', 'Bubo', 'Glaucidium', 'Tyto', 'Athene'], + 'message' => 'Sorry, that is not one of the permitted genuses.', + ], + ], + ], ]); // Act @@ -265,14 +280,14 @@ public function testValidateMemberOf() $this->assertEquals([ 'rules' => [ 'genus' => [ - 'memberOf' => ["Megascops", "Bubo", "Glaucidium", "Tyto", "Athene"] - ] + 'memberOf' => ['Megascops', 'Bubo', 'Glaucidium', 'Tyto', 'Athene'], + ], ], 'messages' => [ 'genus' => [ - 'memberOf' => "Sorry, that is not one of the permitted genuses." - ] - ] + 'memberOf' => 'Sorry, that is not one of the permitted genuses.', + ], + ], ], $result); } @@ -283,10 +298,10 @@ public function testValidateNoLeadingWhitespace() 'user_name' => [ 'validators' => [ 'no_leading_whitespace' => [ - 'message' => "'{{self}}' cannot begin with whitespace characters" - ] - ] - ] + 'message' => "'{{self}}' cannot begin with whitespace characters", + ], + ], + ], ]); // Act @@ -296,14 +311,14 @@ public function testValidateNoLeadingWhitespace() $this->assertEquals([ 'rules' => [ 'user_name' => [ - 'noLeadingWhitespace' => true - ] + 'noLeadingWhitespace' => true, + ], ], 'messages' => [ 'user_name' => [ - 'noLeadingWhitespace' => "'user_name' cannot begin with whitespace characters" - ] - ] + 'noLeadingWhitespace' => "'user_name' cannot begin with whitespace characters", + ], + ], ], $result); } @@ -314,10 +329,10 @@ public function testValidateNoTrailingWhitespace() 'user_name' => [ 'validators' => [ 'no_trailing_whitespace' => [ - 'message' => "'{{self}}' cannot end with whitespace characters" - ] - ] - ] + 'message' => "'{{self}}' cannot end with whitespace characters", + ], + ], + ], ]); // Act @@ -327,14 +342,14 @@ public function testValidateNoTrailingWhitespace() $this->assertEquals([ 'rules' => [ 'user_name' => [ - 'noTrailingWhitespace' => true - ] + 'noTrailingWhitespace' => true, + ], ], 'messages' => [ 'user_name' => [ - 'noTrailingWhitespace' => "'user_name' cannot end with whitespace characters" - ] - ] + 'noTrailingWhitespace' => "'user_name' cannot end with whitespace characters", + ], + ], ], $result); } @@ -346,12 +361,12 @@ public function testValidateNotEquals() 'voles' => [ 'validators' => [ 'not_equals' => [ - 'value' => 0, + 'value' => 0, 'caseSensitive' => false, - 'message' => 'Voles must not be equal to {{value}}.' - ] - ] - ] + 'message' => 'Voles must not be equal to {{value}}.', + ], + ], + ], ]); // Act @@ -362,17 +377,17 @@ public function testValidateNotEquals() 'rules' => [ 'voles' => [ 'notEquals' => [ - 'value' => 0, + 'value' => 0, 'caseSensitive' => false, - 'message' => 'Voles must not be equal to {{value}}.' - ] - ] + 'message' => 'Voles must not be equal to {{value}}.', + ], + ], ], 'messages' => [ 'voles' => [ - 'notEquals' => 'Voles must not be equal to 0.' - ] - ] + 'notEquals' => 'Voles must not be equal to 0.', + ], + ], ], $result); } @@ -383,11 +398,11 @@ public function testValidateNotMatches() 'password' => [ 'validators' => [ 'not_matches' => [ - 'field' => 'user_name', - 'message' => "Your password cannot be the same as your username." - ] - ] - ] + 'field' => 'user_name', + 'message' => 'Your password cannot be the same as your username.', + ], + ], + ], ]); // Act @@ -397,14 +412,14 @@ public function testValidateNotMatches() $this->assertEquals([ 'rules' => [ 'password' => [ - 'notMatchFormField' => 'user_name' - ] + 'notMatchFormField' => 'user_name', + ], ], 'messages' => [ 'password' => [ - 'notMatchFormField' => "Your password cannot be the same as your username." - ] - ] + 'notMatchFormField' => 'Your password cannot be the same as your username.', + ], + ], ], $result); } @@ -415,11 +430,11 @@ public function testValidateNotMemberOf() 'genus' => [ 'validators' => [ 'not_member_of' => [ - 'values' => ["Myodes", "Microtus", "Neodon", "Alticola"], - 'message' => "Sorry, it would appear that you are not an owl." - ] - ] - ] + 'values' => ['Myodes', 'Microtus', 'Neodon', 'Alticola'], + 'message' => 'Sorry, it would appear that you are not an owl.', + ], + ], + ], ]); // Act @@ -429,14 +444,14 @@ public function testValidateNotMemberOf() $this->assertEquals([ 'rules' => [ 'genus' => [ - 'notMemberOf' => ["Myodes", "Microtus", "Neodon", "Alticola"] - ] + 'notMemberOf' => ['Myodes', 'Microtus', 'Neodon', 'Alticola'], + ], ], 'messages' => [ 'genus' => [ - 'notMemberOf' => "Sorry, it would appear that you are not an owl." - ] - ] + 'notMemberOf' => 'Sorry, it would appear that you are not an owl.', + ], + ], ], $result); } @@ -447,10 +462,10 @@ public function testValidateNumeric() 'accuracy' => [ 'validators' => [ 'numeric' => [ - 'message' => "Sorry, your strike accuracy must be a number." - ] - ] - ] + 'message' => 'Sorry, your strike accuracy must be a number.', + ], + ], + ], ]); // Act @@ -460,14 +475,14 @@ public function testValidateNumeric() $this->assertEquals([ 'rules' => [ 'accuracy' => [ - 'number' => true - ] + 'number' => true, + ], ], 'messages' => [ 'accuracy' => [ - 'number' => "Sorry, your strike accuracy must be a number." - ] - ] + 'number' => 'Sorry, your strike accuracy must be a number.', + ], + ], ], $result); } @@ -478,12 +493,12 @@ public function testValidateRange() 'voles' => [ 'validators' => [ 'range' => [ - 'min' => 5, - 'max' => 10, - 'message' => "You must catch {{min}} - {{max}} voles." - ] - ] - ] + 'min' => 5, + 'max' => 10, + 'message' => 'You must catch {{min}} - {{max}} voles.', + ], + ], + ], ]); // Act @@ -493,14 +508,14 @@ public function testValidateRange() $this->assertEquals([ 'rules' => [ 'voles' => [ - 'range' => [5, 10] - ] + 'range' => [5, 10], + ], ], 'messages' => [ 'voles' => [ - 'range' => "You must catch 5 - 10 voles." - ] - ] + 'range' => 'You must catch 5 - 10 voles.', + ], + ], ], $result); } @@ -511,11 +526,11 @@ public function testValidateMin() 'voles' => [ 'validators' => [ 'range' => [ - 'min' => 5, - 'message' => "You must catch at least {{min}} voles." - ] - ] - ] + 'min' => 5, + 'message' => 'You must catch at least {{min}} voles.', + ], + ], + ], ]); // Act @@ -525,14 +540,14 @@ public function testValidateMin() $this->assertEquals([ 'rules' => [ 'voles' => [ - 'min' => 5 - ] + 'min' => 5, + ], ], 'messages' => [ 'voles' => [ - 'min' => "You must catch at least 5 voles." - ] - ] + 'min' => 'You must catch at least 5 voles.', + ], + ], ], $result); } @@ -543,11 +558,11 @@ public function testValidateMax() 'voles' => [ 'validators' => [ 'range' => [ - 'max' => 10, - 'message' => "You must catch no more than {{max}} voles." - ] - ] - ] + 'max' => 10, + 'message' => 'You must catch no more than {{max}} voles.', + ], + ], + ], ]); // Act @@ -557,14 +572,14 @@ public function testValidateMax() $this->assertEquals([ 'rules' => [ 'voles' => [ - 'max' => 10 - ] + 'max' => 10, + ], ], 'messages' => [ 'voles' => [ - 'max' => "You must catch no more than 10 voles." - ] - ] + 'max' => 'You must catch no more than 10 voles.', + ], + ], ], $result); } @@ -575,11 +590,11 @@ public function testValidateRegex() 'screech' => [ 'validators' => [ 'regex' => [ - 'regex' => "^who(o*)$", - 'message' => "You did not provide a valid screech." - ] - ] - ] + 'regex' => '^who(o*)$', + 'message' => 'You did not provide a valid screech.', + ], + ], + ], ]); // Act @@ -589,14 +604,14 @@ public function testValidateRegex() $this->assertEquals([ 'rules' => [ 'screech' => [ - 'pattern' => "^who(o*)$" - ] + 'pattern' => '^who(o*)$', + ], ], 'messages' => [ 'screech' => [ - 'pattern' => "You did not provide a valid screech." - ] - ] + 'pattern' => 'You did not provide a valid screech.', + ], + ], ], $result); } @@ -607,10 +622,10 @@ public function testValidateRequired() 'species' => [ 'validators' => [ 'required' => [ - 'message' => "Please tell us your species." - ] - ] - ] + 'message' => 'Please tell us your species.', + ], + ], + ], ]); // Act @@ -620,14 +635,14 @@ public function testValidateRequired() $this->assertEquals([ 'rules' => [ 'species' => [ - 'required' => true - ] + 'required' => true, + ], ], 'messages' => [ 'species' => [ - 'required' => "Please tell us your species." - ] - ] + 'required' => 'Please tell us your species.', + ], + ], ], $result); } @@ -638,10 +653,10 @@ public function testValidateTelephone() 'phone' => [ 'validators' => [ 'telephone' => [ - 'message' => "Whoa there, check your phone number again." - ] - ] - ] + 'message' => 'Whoa there, check your phone number again.', + ], + ], + ], ]); // Act @@ -651,14 +666,14 @@ public function testValidateTelephone() $this->assertEquals([ 'rules' => [ 'phone' => [ - 'phoneUS' => true - ] + 'phoneUS' => true, + ], ], 'messages' => [ 'phone' => [ - 'phoneUS' => "Whoa there, check your phone number again." - ] - ] + 'phoneUS' => 'Whoa there, check your phone number again.', + ], + ], ], $result); } @@ -669,10 +684,10 @@ public function testValidateUri() 'website' => [ 'validators' => [ 'uri' => [ - 'message' => "That's not even a valid URL..." - ] - ] - ] + 'message' => "That's not even a valid URL...", + ], + ], + ], ]); // Act @@ -682,14 +697,14 @@ public function testValidateUri() $this->assertEquals([ 'rules' => [ 'website' => [ - 'url' => true - ] + 'url' => true, + ], ], 'messages' => [ 'website' => [ - 'url' => "That's not even a valid URL..." - ] - ] + 'url' => "That's not even a valid URL...", + ], + ], ], $result); } @@ -700,10 +715,10 @@ public function testValidateUsername() 'user_name' => [ 'validators' => [ 'username' => [ - 'message' => "Sorry buddy, that's not a valid username." - ] - ] - ] + 'message' => "Sorry buddy, that's not a valid username.", + ], + ], + ], ]); // Act @@ -713,18 +728,17 @@ public function testValidateUsername() $this->assertEquals([ 'rules' => [ 'user_name' => [ - 'username' => true - ] + 'username' => true, + ], ], 'messages' => [ 'user_name' => [ - 'username' => "Sorry buddy, that's not a valid username." - ] - ] + 'username' => "Sorry buddy, that's not a valid username.", + ], + ], ], $result); } - public function testDomainRulesClientOnly() { // Arrange @@ -732,11 +746,11 @@ public function testDomainRulesClientOnly() 'plumage' => [ 'validators' => [ 'required' => [ - 'domain' => 'client', - 'message' => "Are you sure you don't want to show us your plumage?" - ] - ] - ] + 'domain' => 'client', + 'message' => "Are you sure you don't want to show us your plumage?", + ], + ], + ], ]); // Act @@ -746,32 +760,31 @@ public function testDomainRulesClientOnly() $this->assertEquals([ 'rules' => [ 'plumage' => [ - 'required' => true - ] + 'required' => true, + ], ], 'messages' => [ 'plumage' => [ - 'required' => "Are you sure you don't want to show us your plumage?" - ] - ] + 'required' => "Are you sure you don't want to show us your plumage?", + ], + ], ], $result); - // Srinvas Nukala : Adding Test with Form array prefix 'coolform1' + // Adding Test with Form array prefix 'coolform1' $result1 = $adapter->rules('json', false, 'coolform1'); $this->assertEquals([ 'rules' => [ 'coolform1[plumage]' => [ - 'required' => true - ] + 'required' => true, + ], ], 'messages' => [ 'coolform1[plumage]' => [ - 'required' => "Are you sure you don't want to show us your plumage?" - ] - ] + 'required' => "Are you sure you don't want to show us your plumage?", + ], + ], ], $result1); - } public function testDomainRulesServerOnly() @@ -781,11 +794,11 @@ public function testDomainRulesServerOnly() 'plumage' => [ 'validators' => [ 'required' => [ - 'domain' => 'server', - 'message' => "Are you sure you don't want to show us your plumage?" - ] - ] - ] + 'domain' => 'server', + 'message' => "Are you sure you don't want to show us your plumage?", + ], + ], + ], ]); // Act @@ -794,431 +807,367 @@ public function testDomainRulesServerOnly() $this->assertEquals([ 'rules' => [ - 'plumage' => [] + 'plumage' => [], ], - 'messages' => [] + 'messages' => [], ], $result); - // Srinvas Nukala : Adding Test with Form array prefix 'coolform1' + // Adding Test with Form array prefix 'coolform1' $result1 = $adapter->rules('json', false, 'coolform1'); $this->assertEquals([ 'rules' => [ - 'coolform1[plumage]' => [] + 'coolform1[plumage]' => [], ], - 'messages' => [] + 'messages' => [], ], $result1); } public function testManyRules() { // Arrange - $schema = new RequestSchemaRepository(array( - 'user_name' => - array( - 'validators' => - array( - 'length' => - array( - 'min' => 1, - 'max' => 50, + $schema = new RequestSchemaRepository([ + 'user_name' => [ + 'validators' => [ + 'length' => [ + 'min' => 1, + 'max' => 50, 'message' => 'ACCOUNT_USER_CHAR_LIMIT', - ), - 'no_leading_whitespace' => - array( + ], + 'no_leading_whitespace' => [ 'message' => "'{{self}}' must not contain leading whitespace.", - ), - 'no_trailing_whitespace' => - array( + ], + 'no_trailing_whitespace' => [ 'message' => "'{{self}}' must not contain trailing whitespace.", - ), - 'required' => - array( + ], + 'required' => [ 'message' => 'ACCOUNT_SPECIFY_USERNAME', - ), - 'username' => - array( + ], + 'username' => [ 'message' => "'{{self}}' must be a valid username.", - ), - ), - ), - 'display_name' => - array( - 'validators' => - array( - 'length' => - array( - 'min' => 1, - 'max' => 50, + ], + ], + ], + 'display_name' => [ + 'validators' => [ + 'length' => [ + 'min' => 1, + 'max' => 50, 'message' => 'ACCOUNT_DISPLAY_CHAR_LIMIT', - ), - 'required' => - array( + ], + 'required' => [ 'message' => 'ACCOUNT_SPECIFY_DISPLAY_NAME', - ), - ), - ), - 'secret' => - array( - 'validators' => - array( - 'length' => - array( - 'min' => 1, - 'max' => 100, + ], + ], + ], + 'secret' => [ + 'validators' => [ + 'length' => [ + 'min' => 1, + 'max' => 100, 'message' => 'Secret must be between {{ min }} and {{ max }} characters long.', - 'domain' => 'client', - ), - 'numeric' => - array(), - 'required' => - array( + 'domain' => 'client', + ], + 'numeric' => [], + 'required' => [ 'message' => 'Secret must be specified.', - 'domain' => 'server', - ), - ), - ), - 'puppies' => - array( - 'validators' => - array( - 'member_of' => - array( - 'values' => - array( + 'domain' => 'server', + ], + ], + ], + 'puppies' => [ + 'validators' => [ + 'member_of' => [ + 'values' => [ 0 => '0', 1 => '1', - ), + ], 'message' => "The value for '{{self}}' must be '0' or '1'.", - ), - ), - 'transformations' => - array( + ], + ], + 'transformations' => [ 0 => 'purify', 1 => 'trim', - ), - ), - 'phone' => - array( - 'validators' => - array( - 'telephone' => - array( + ], + ], + 'phone' => [ + 'validators' => [ + 'telephone' => [ 'message' => "The value for '{{self}}' must be a valid telephone number.", - ), - ), - ), - 'email' => - array( - 'validators' => - array( - 'required' => - array( - 'message' => 'ACCOUNT_SPECIFY_EMAIL' - ), - 'length' => - array( - 'min' => 1, - 'max' => 100, - 'message' => 'ACCOUNT_EMAIL_CHAR_LIMIT' - ), - 'email' => - array( - 'message' => 'ACCOUNT_INVALID_EMAIL' - ), - ) - ), - 'password' => - array( - 'validators' => - array( - 'required' => - array( - 'message' => 'ACCOUNT_SPECIFY_PASSWORD' - ), - 'length' => - array( - 'min' => 8, - 'max' => 50, - 'message' => 'ACCOUNT_PASS_CHAR_LIMIT' - ), - ), - ), - 'passwordc' => - array( - 'validators' => - array( - 'required' => - array( - 'message' => 'ACCOUNT_SPECIFY_PASSWORD' - ), - 'matches' => - array( - 'field' => 'password', - 'message' => 'ACCOUNT_PASS_MISMATCH' - ), - 'length' => - array( - 'min' => 8, - 'max' => 50, - 'message' => 'ACCOUNT_PASS_CHAR_LIMIT' - ) - ) - ) - )); + ], + ], + ], + 'email' => [ + 'validators' => [ + 'required' => [ + 'message' => 'ACCOUNT_SPECIFY_EMAIL', + ], + 'length' => [ + 'min' => 1, + 'max' => 100, + 'message' => 'ACCOUNT_EMAIL_CHAR_LIMIT', + ], + 'email' => [ + 'message' => 'ACCOUNT_INVALID_EMAIL', + ], + ], + ], + 'password' => [ + 'validators' => [ + 'required' => [ + 'message' => 'ACCOUNT_SPECIFY_PASSWORD', + ], + 'length' => [ + 'min' => 8, + 'max' => 50, + 'message' => 'ACCOUNT_PASS_CHAR_LIMIT', + ], + ], + ], + 'passwordc' => [ + 'validators' => [ + 'required' => [ + 'message' => 'ACCOUNT_SPECIFY_PASSWORD', + ], + 'matches' => [ + 'field' => 'password', + 'message' => 'ACCOUNT_PASS_MISMATCH', + ], + 'length' => [ + 'min' => 8, + 'max' => 50, + 'message' => 'ACCOUNT_PASS_CHAR_LIMIT', + ], + ], + ], + ]); // Act $adapter = new JqueryValidationAdapter($schema, $this->translator); $result = $adapter->rules(); - $this->assertEquals(array( - 'rules' => - array( - 'user_name' => - array( - 'rangelength' => - array( + $this->assertEquals([ + 'rules' => [ + 'user_name' => [ + 'rangelength' => [ 0 => 1, - 1 => 50 - ), - 'noLeadingWhitespace' => true, + 1 => 50, + ], + 'noLeadingWhitespace' => true, 'noTrailingWhitespace' => true, - 'required' => true, - 'username' => true - ), - 'display_name' => - array( - 'rangelength' => - array( + 'required' => true, + 'username' => true, + ], + 'display_name' => [ + 'rangelength' => [ 0 => 1, 1 => 50, - ), - 'required' => true - ), - 'secret' => - array( - 'rangelength' => - array( + ], + 'required' => true, + ], + 'secret' => [ + 'rangelength' => [ 0 => 1, - 1 => 100 - ), - 'number' => true - ), - 'puppies' => - array( - 'memberOf' => - array( + 1 => 100, + ], + 'number' => true, + ], + 'puppies' => [ + 'memberOf' => [ 0 => '0', - 1 => '1' - ) - ), - 'phone' => - array( - 'phoneUS' => true - ), - 'email' => - array( - 'required' => true, - 'rangelength' => - array( + 1 => '1', + ], + ], + 'phone' => [ + 'phoneUS' => true, + ], + 'email' => [ + 'required' => true, + 'rangelength' => [ 0 => 1, - 1 => 100 - ), - 'email' => true - ), - 'password' => - array( - 'required' => true, - 'rangelength' => - array( + 1 => 100, + ], + 'email' => true, + ], + 'password' => [ + 'required' => true, + 'rangelength' => [ 0 => 8, - 1 => 50 - ) - ), - 'passwordc' => - array( - 'required' => true, + 1 => 50, + ], + ], + 'passwordc' => [ + 'required' => true, 'matchFormField' => 'password', - 'rangelength' => - array( + 'rangelength' => [ 0 => 8, - 1 => 50 - ) - ) - ), - 'messages' => - array( - 'user_name' => - array( - 'rangelength' => 'ACCOUNT_USER_CHAR_LIMIT', - 'noLeadingWhitespace' => "'user_name' must not contain leading whitespace.", + 1 => 50, + ], + ], + ], + 'messages' => [ + 'user_name' => [ + 'rangelength' => 'ACCOUNT_USER_CHAR_LIMIT', + 'noLeadingWhitespace' => "'user_name' must not contain leading whitespace.", 'noTrailingWhitespace' => "'user_name' must not contain trailing whitespace.", - 'required' => 'ACCOUNT_SPECIFY_USERNAME', - 'username' => "'user_name' must be a valid username.", - ), - 'display_name' => - array( + 'required' => 'ACCOUNT_SPECIFY_USERNAME', + 'username' => "'user_name' must be a valid username.", + ], + 'display_name' => [ 'rangelength' => 'ACCOUNT_DISPLAY_CHAR_LIMIT', - 'required' => 'ACCOUNT_SPECIFY_DISPLAY_NAME', - ), - 'secret' => - array( + 'required' => 'ACCOUNT_SPECIFY_DISPLAY_NAME', + ], + 'secret' => [ 'rangelength' => 'Secret must be between 1 and 100 characters long.', - ), - 'puppies' => - array( + ], + 'puppies' => [ 'memberOf' => "The value for 'puppies' must be '0' or '1'.", - ), - 'phone' => - array( + ], + 'phone' => [ 'phoneUS' => "The value for 'phone' must be a valid telephone number.", - ), - 'email' => - array( - 'required' => 'ACCOUNT_SPECIFY_EMAIL', + ], + 'email' => [ + 'required' => 'ACCOUNT_SPECIFY_EMAIL', 'rangelength' => 'ACCOUNT_EMAIL_CHAR_LIMIT', - 'email' => 'ACCOUNT_INVALID_EMAIL', - ), - 'password' => - array( - 'required' => 'ACCOUNT_SPECIFY_PASSWORD', + 'email' => 'ACCOUNT_INVALID_EMAIL', + ], + 'password' => [ + 'required' => 'ACCOUNT_SPECIFY_PASSWORD', 'rangelength' => 'ACCOUNT_PASS_CHAR_LIMIT', - ), - 'passwordc' => - array( - 'required' => 'ACCOUNT_SPECIFY_PASSWORD', + ], + 'passwordc' => [ + 'required' => 'ACCOUNT_SPECIFY_PASSWORD', 'matchFormField' => 'ACCOUNT_PASS_MISMATCH', - 'rangelength' => 'ACCOUNT_PASS_CHAR_LIMIT' - ) - ), - ), $result); - + 'rangelength' => 'ACCOUNT_PASS_CHAR_LIMIT', + ], + ], + ], $result); - // Srinvas Nukala : Adding Test with Form array prefix 'coolform1' + // Adding Test with Form array prefix 'coolform1' $result1 = $adapter->rules('json', false, 'coolform1'); - $this->assertEquals(array( - 'rules' => - array( - 'coolform1[user_name]' => - array( - 'rangelength' => - array( + $this->assertEquals([ + 'rules' => [ + 'coolform1[user_name]' => [ + 'rangelength' => [ 0 => 1, - 1 => 50 - ), - 'noLeadingWhitespace' => true, + 1 => 50, + ], + 'noLeadingWhitespace' => true, 'noTrailingWhitespace' => true, - 'required' => true, - 'username' => true - ), - 'coolform1[display_name]' => - array( - 'rangelength' => - array( + 'required' => true, + 'username' => true, + ], + 'coolform1[display_name]' => [ + 'rangelength' => [ 0 => 1, 1 => 50, - ), - 'required' => true - ), - 'coolform1[secret]' => - array( - 'rangelength' => - array( + ], + 'required' => true, + ], + 'coolform1[secret]' => [ + 'rangelength' => [ 0 => 1, - 1 => 100 - ), - 'number' => true - ), - 'coolform1[puppies]' => - array( - 'memberOf' => - array( + 1 => 100, + ], + 'number' => true, + ], + 'coolform1[puppies]' => [ + 'memberOf' => [ 0 => '0', - 1 => '1' - ) - ), - 'coolform1[phone]' => - array( - 'phoneUS' => true - ), - 'coolform1[email]' => - array( - 'required' => true, - 'rangelength' => - array( + 1 => '1', + ], + ], + 'coolform1[phone]' => [ + 'phoneUS' => true, + ], + 'coolform1[email]' => [ + 'required' => true, + 'rangelength' => [ 0 => 1, - 1 => 100 - ), - 'email' => true - ), - 'coolform1[password]' => - array( - 'required' => true, - 'rangelength' => - array( + 1 => 100, + ], + 'email' => true, + ], + 'coolform1[password]' => [ + 'required' => true, + 'rangelength' => [ 0 => 8, - 1 => 50 - ) - ), - 'coolform1[passwordc]' => - array( - 'required' => true, + 1 => 50, + ], + ], + 'coolform1[passwordc]' => [ + 'required' => true, 'matchFormField' => 'password', - 'rangelength' => - array( + 'rangelength' => [ 0 => 8, - 1 => 50 - ) - ) - ), - 'messages' => - array( - 'coolform1[user_name]' => - array( - 'rangelength' => 'ACCOUNT_USER_CHAR_LIMIT', - 'noLeadingWhitespace' => "'user_name' must not contain leading whitespace.", + 1 => 50, + ], + ], + ], + 'messages' => [ + 'coolform1[user_name]' => [ + 'rangelength' => 'ACCOUNT_USER_CHAR_LIMIT', + 'noLeadingWhitespace' => "'user_name' must not contain leading whitespace.", 'noTrailingWhitespace' => "'user_name' must not contain trailing whitespace.", - 'required' => 'ACCOUNT_SPECIFY_USERNAME', - 'username' => "'user_name' must be a valid username.", - ), - 'coolform1[display_name]' => - array( + 'required' => 'ACCOUNT_SPECIFY_USERNAME', + 'username' => "'user_name' must be a valid username.", + ], + 'coolform1[display_name]' => [ 'rangelength' => 'ACCOUNT_DISPLAY_CHAR_LIMIT', - 'required' => 'ACCOUNT_SPECIFY_DISPLAY_NAME', - ), - 'coolform1[secret]' => - array( + 'required' => 'ACCOUNT_SPECIFY_DISPLAY_NAME', + ], + 'coolform1[secret]' => [ 'rangelength' => 'Secret must be between 1 and 100 characters long.', - ), - 'coolform1[puppies]' => - array( + ], + 'coolform1[puppies]' => [ 'memberOf' => "The value for 'puppies' must be '0' or '1'.", - ), - 'coolform1[phone]' => - array( + ], + 'coolform1[phone]' => [ 'phoneUS' => "The value for 'phone' must be a valid telephone number.", - ), - 'coolform1[email]' => - array( - 'required' => 'ACCOUNT_SPECIFY_EMAIL', + ], + 'coolform1[email]' => [ + 'required' => 'ACCOUNT_SPECIFY_EMAIL', 'rangelength' => 'ACCOUNT_EMAIL_CHAR_LIMIT', - 'email' => 'ACCOUNT_INVALID_EMAIL', - ), - 'coolform1[password]' => - array( - 'required' => 'ACCOUNT_SPECIFY_PASSWORD', + 'email' => 'ACCOUNT_INVALID_EMAIL', + ], + 'coolform1[password]' => [ + 'required' => 'ACCOUNT_SPECIFY_PASSWORD', 'rangelength' => 'ACCOUNT_PASS_CHAR_LIMIT', - ), - 'coolform1[passwordc]' => - array( - 'required' => 'ACCOUNT_SPECIFY_PASSWORD', + ], + 'coolform1[passwordc]' => [ + 'required' => 'ACCOUNT_SPECIFY_PASSWORD', 'matchFormField' => 'ACCOUNT_PASS_MISMATCH', - 'rangelength' => 'ACCOUNT_PASS_CHAR_LIMIT' - ) - ), - ), $result1); + 'rangelength' => 'ACCOUNT_PASS_CHAR_LIMIT', + ], + ], + ], $result1); + } + + public function testValidateNoRule() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'user_name' => [ + 'validators' => [ + 'foo' => [ + 'message' => "Sorry buddy, that's not a valid username.", + ], + ], + ], + ]); + // Act + $adapter = new JqueryValidationAdapter($schema, $this->translator); + $result = $adapter->rules(); + $this->assertEquals([ + 'rules' => [ + 'user_name' => [], + ], + 'messages' => [ + 'user_name' => [], + ], + ], $result); } } diff --git a/tests/RequestDataTransformerTest.php b/tests/RequestDataTransformerTest.php index 363508e..2bf50a6 100644 --- a/tests/RequestDataTransformerTest.php +++ b/tests/RequestDataTransformerTest.php @@ -1,7 +1,16 @@ basePath = __DIR__ . '/data'; + $this->basePath = __DIR__.'/data'; // Arrange - $loader = new YamlFileLoader($this->basePath . '/register.yaml'); + $loader = new YamlFileLoader($this->basePath.'/register.yaml'); $schema = new RequestSchemaRepository($loader->load()); $this->transformer = new RequestDataTransformer($schema); } /** - * Basic whitelisting + * Basic whitelisting. */ public function testBasic() { // Arrange $rawInput = [ - 'email' => 'david@owlfancy.com', - 'admin' => 1, - 'description' => 'Some stuff to describe' + 'email' => 'david@owlfancy.com', + 'admin' => 1, + 'description' => 'Some stuff to describe', ]; // Arrange $schema = new RequestSchemaRepository(); $schema->mergeItems(null, [ - 'email' => [], - 'description' => null // Replicating an input that has no validation operations + 'email' => [], + 'description' => null, // Replicating an input that has no validation operations ]); $this->transformer = new RequestDataTransformer($schema); - + // Act $result = $this->transformer->transform($rawInput, 'skip'); // Assert $transformedData = [ - 'email' => 'david@owlfancy.com', - 'description' => 'Some stuff to describe' + 'email' => 'david@owlfancy.com', + 'description' => 'Some stuff to describe', ]; $this->assertEquals($transformedData, $result); } + public function testBasicWithOnUnexpectedVarAllow() + { + // Arrange + $rawInput = [ + 'email' => 'david@owlfancy.com', + 'admin' => 1, + 'description' => 'Some stuff to describe', + ]; + + // Arrange + $schema = new RequestSchemaRepository(); + + $schema->mergeItems(null, [ + 'email' => [], + 'description' => null, // Replicating an input that has no validation operations + ]); + $this->transformer = new RequestDataTransformer($schema); + + // Act + $result = $this->transformer->transform($rawInput, 'allow'); + + // Assert + $transformedData = [ + 'email' => 'david@owlfancy.com', + 'admin' => 1, + 'description' => 'Some stuff to describe', + ]; + + $this->assertEquals($transformedData, $result); + } + + /** + * @expectedException \UserFrosting\Support\Exception\BadRequestException + * @expectedExceptionMessage The field 'admin' is not a valid input field. + */ + public function testBasicWithOnUnexpectedVarError() + { + // Arrange + $rawInput = [ + 'email' => 'david@owlfancy.com', + 'admin' => 1, + 'description' => 'Some stuff to describe', + ]; + + // Arrange + $schema = new RequestSchemaRepository(); + + $schema->mergeItems(null, [ + 'email' => [], + 'description' => null, // Replicating an input that has no validation operations + ]); + $this->transformer = new RequestDataTransformer($schema); + + // Act + $result = $this->transformer->transform($rawInput, 'error'); + } + /** - * "Trim" transformer + * "Trim" transformer. */ public function testTrim() { // Act $rawInput = [ - 'display_name' => "THE GREATEST " + 'display_name' => 'THE GREATEST ', ]; - + $result = $this->transformer->transform($rawInput, 'skip'); // Assert $transformedData = [ - 'email' => 'david@owlfancy.com', - 'display_name' => "THE GREATEST" + 'email' => 'david@owlfancy.com', + 'display_name' => 'THE GREATEST', ]; $this->assertEquals($transformedData, $result); } /** - * "Escape" transformer + * "Escape" transformer. */ public function testEscape() { // Act $rawInput = [ - 'display_name' => "My Super-Important Name" + 'display_name' => 'My Super-Important Name', ]; - + $result = $this->transformer->transform($rawInput, 'skip'); // Assert $transformedData = [ - 'email' => 'david@owlfancy.com', - 'display_name' => "<b>My Super-Important Name</b>" + 'email' => 'david@owlfancy.com', + 'display_name' => '<b>My Super-Important Name</b>', ]; $this->assertEquals($transformedData, $result); } /** - * "Purge" transformer + * @depends testEscape + */ + public function testEscapeWithArrayValue() + { + // Act + $rawInput = [ + 'display_name' => ['My Super-Important Name'], + ]; + + $result = $this->transformer->transform($rawInput, 'skip'); + + // Assert + $transformedData = [ + 'email' => 'david@owlfancy.com', + 'display_name' => ['<b>My Super-Important Name</b>'], + ]; + + $this->assertEquals($transformedData, $result); + } + + /** + * "Purge" transformer. */ public function testPurge() { // Act $rawInput = [ - 'user_name' => "My Super-Important Name" + 'user_name' => 'My Super-Important Name', ]; - + $result = $this->transformer->transform($rawInput, 'skip'); // Assert $transformedData = [ - 'email' => 'david@owlfancy.com', - 'user_name' => "My Super-Important Name" + 'email' => 'david@owlfancy.com', + 'user_name' => 'My Super-Important Name', ]; $this->assertEquals($transformedData, $result); } /** - * "Purify" transformer + * @depends testPurge + */ + public function testPurgeWithArrayValue() + { + // Act + $rawInput = [ + 'user_name' => ['My Super-Important Name'], + ]; + + $result = $this->transformer->transform($rawInput, 'skip'); + + // Assert + $transformedData = [ + 'email' => 'david@owlfancy.com', + 'user_name' => ['My Super-Important Name'], + ]; + + $this->assertEquals($transformedData, $result); + } + + /** + * "Purify" transformer. */ public function testPurify() { // Act $rawInput = [ - 'puppies' => "0" + 'puppies' => "0", ]; - + $result = $this->transformer->transform($rawInput, 'skip'); // Assert $transformedData = [ - 'email' => 'david@owlfancy.com', - 'puppies' => "0" + 'email' => 'david@owlfancy.com', + 'puppies' => '0', ]; $this->assertEquals($transformedData, $result); diff --git a/tests/RequestSchemaRepositoryTest.php b/tests/RequestSchemaRepositoryTest.php new file mode 100644 index 0000000..a3c8158 --- /dev/null +++ b/tests/RequestSchemaRepositoryTest.php @@ -0,0 +1,278 @@ +basePath = __DIR__.'/data'; + + $this->contactSchema = [ + 'message' => [ + 'validators' => [ + 'required' => [ + 'message' => 'Please enter a message', + ], + ], + ], + ]; + } + + public function testReadJsonSchema() + { + // Arrange + $loader = new YamlFileLoader($this->basePath.'/contact.json'); + $schema = new RequestSchemaRepository($loader->load()); + + // Act + $result = $schema->all(); + + // Assert + $this->assertArraySubset($this->contactSchema, $result); + } + + public function testReadYamlSchema() + { + // Arrange + $loader = new YamlFileLoader($this->basePath.'/contact.yaml'); + $schema = new RequestSchemaRepository($loader->load()); + + // Act + $result = $schema->all(); + + // Assert + $this->assertArraySubset($this->contactSchema, $result); + } + + public function testSetDefault() + { + // Arrange + $loader = new YamlFileLoader($this->basePath.'/contact.yaml'); + $schema = new RequestSchemaRepository($loader->load()); + + // Act + $schema->setDefault('message', 'I require more voles.'); + $result = $schema->all(); + + // Assert + $contactSchema = [ + 'message' => [ + 'default' => 'I require more voles.', + 'validators' => [ + 'required' => [ + 'message' => 'Please enter a message', + ], + ], + ], + ]; + $this->assertArraySubset($contactSchema, $result); + } + + public function testSetDefaultWithMissingField() + { + // Arrange + $loader = new YamlFileLoader($this->basePath.'/contact.yaml'); + $schema = new RequestSchemaRepository($loader->load()); + + // Act + $schema->setDefault('foo', 'bar'); + $result = $schema->all(); + + // Assert + $contactSchema = [ + 'foo' => [ + 'default' => 'bar', + ], + ]; + $this->assertArraySubset($contactSchema, $result); + } + + public function testAddValidator() + { + // Arrange + $loader = new YamlFileLoader($this->basePath.'/contact.yaml'); + $schema = new RequestSchemaRepository($loader->load()); + + // Act + $schema->addValidator('message', 'length', [ + 'max' => 10000, + 'message' => 'Your message is too long!', + ]); + $result = $schema->all(); + + // Assert + $contactSchema = [ + 'message' => [ + 'validators' => [ + 'required' => [ + 'message' => 'Please enter a message', + ], + 'length' => [ + 'max' => 10000, + 'message' => 'Your message is too long!', + ], + ], + ], + ]; + $this->assertArraySubset($contactSchema, $result); + } + + public function testAddValidatorWithMissingField() + { + // Arrange + $loader = new YamlFileLoader($this->basePath.'/contact.yaml'); + $schema = new RequestSchemaRepository($loader->load()); + + // Act + $schema->addValidator('foo', 'length', [ + 'max' => 10000, + 'message' => 'Your message is too long!', + ]); + $result = $schema->all(); + + // Assert + $contactSchema = [ + 'foo' => [ + 'validators' => [ + 'length' => [ + 'max' => 10000, + 'message' => 'Your message is too long!', + ], + ], + ], + ]; + $this->assertArraySubset($contactSchema, $result); + } + + public function testRemoveValidator() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'message' => [ + 'validators' => [ + 'required' => [ + 'message' => 'Please enter a message', + ], + 'length' => [ + 'max' => 10000, + 'message' => 'Your message is too long!', + ], + ], + ], + ]); + + // Act + $schema->removeValidator('message', 'required'); + // Check that attempting to remove a rule that doesn't exist, will have no effect + $schema->removeValidator('wings', 'required'); + $schema->removeValidator('message', 'telephone'); + + $result = $schema->all(); + + // Assert + $contactSchema = [ + 'message' => [ + 'validators' => [ + 'length' => [ + 'max' => 10000, + 'message' => 'Your message is too long!', + ], + ], + ], + ]; + + $this->assertEquals($contactSchema, $result); + } + + public function testSetTransformation() + { + // Arrange + $loader = new YamlFileLoader($this->basePath.'/contact.yaml'); + $schema = new RequestSchemaRepository($loader->load()); + + // Act + $schema->setTransformations('message', ['purge', 'owlify']); + $result = $schema->all(); + + // Assert + $contactSchema = [ + 'message' => [ + 'validators' => [ + 'required' => [ + 'message' => 'Please enter a message', + ], + ], + 'transformations' => [ + 'purge', + 'owlify', + ], + ], + ]; + $this->assertArraySubset($contactSchema, $result); + } + + public function testSetTransformationNotAnArray() + { + // Arrange + $loader = new YamlFileLoader($this->basePath.'/contact.yaml'); + $schema = new RequestSchemaRepository($loader->load()); + + // Act + $schema->setTransformations('message', 'purge'); + $result = $schema->all(); + + // Assert + $contactSchema = [ + 'message' => [ + 'validators' => [ + 'required' => [ + 'message' => 'Please enter a message', + ], + ], + 'transformations' => [ + 'purge', + ], + ], + ]; + $this->assertArraySubset($contactSchema, $result); + } + + public function testSetTransformationWithMissingField() + { + // Arrange + $loader = new YamlFileLoader($this->basePath.'/contact.yaml'); + $schema = new RequestSchemaRepository($loader->load()); + + // Act + $schema->setTransformations('foo', ['purge', 'owlify']); + $result = $schema->all(); + + // Assert + $contactSchema = [ + 'foo' => [ + 'transformations' => [ + 'purge', + 'owlify', + ], + ], + ]; + $this->assertArraySubset($contactSchema, $result); + } +} diff --git a/tests/RequestSchemaTest.php b/tests/RequestSchemaTest.php index 050a6e0..c323558 100644 --- a/tests/RequestSchemaTest.php +++ b/tests/RequestSchemaTest.php @@ -1,175 +1,46 @@ basePath = __DIR__ . '/data'; + $this->basePath = __DIR__.'/data/contact.json'; $this->contactSchema = [ - "message" => [ - "validators" => [ - "required" => [ - "message" => "Please enter a message" - ] - ] - ] - ]; - } - - public function testReadJsonSchema() - { - // Arrange - $loader = new YamlFileLoader($this->basePath . '/contact.json'); - $schema = new RequestSchemaRepository($loader->load()); - - // Act - $result = $schema->all(); - - // Assert - $this->assertArraySubset($this->contactSchema, $result); - } - - public function testReadYamlSchema() - { - // Arrange - $loader = new YamlFileLoader($this->basePath . '/contact.yaml'); - $schema = new RequestSchemaRepository($loader->load()); - - // Act - $result = $schema->all(); - - // Assert - $this->assertArraySubset($this->contactSchema, $result); - } - - public function testSetDefault() - { - // Arrange - $loader = new YamlFileLoader($this->basePath . '/contact.yaml'); - $schema = new RequestSchemaRepository($loader->load()); - - // Act - $schema->setDefault('message', "I require more voles."); - $result = $schema->all(); - - // Assert - $contactSchema = [ - "message" => [ - "default" => "I require more voles.", - "validators" => [ - "required" => [ - "message" => "Please enter a message" - ] - ] - ] - ]; - $this->assertArraySubset($contactSchema, $result); - } - - public function testAddValidator() - { - // Arrange - $loader = new YamlFileLoader($this->basePath . '/contact.yaml'); - $schema = new RequestSchemaRepository($loader->load()); - - // Act - $schema->addValidator('message', 'length', [ - 'max' => 10000, - 'message' => 'Your message is too long!' - ]); - $result = $schema->all(); - - // Assert - $contactSchema = [ - "message" => [ - "validators" => [ - "required" => [ - "message" => "Please enter a message" + 'message' => [ + 'validators' => [ + 'required' => [ + 'message' => 'Please enter a message', ], - "length" => [ - "max" => 10000, - "message" => "Your message is too long!" - ] - ] - ] + ], + ], ]; - $this->assertArraySubset($contactSchema, $result); } - public function testRemoveValidator() + public function testWithNoPath() { - // Arrange - $schema = new RequestSchemaRepository([ - "message" => [ - "validators" => [ - "required" => [ - "message" => "Please enter a message" - ], - "length" => [ - "max" => 10000, - "message" => "Your message is too long!" - ] - ] - ] - ]); - - // Act - $schema->removeValidator('message', 'required'); - // Check that attempting to remove a rule that doesn't exist, will have no effect - $schema->removeValidator('wings', 'required'); - $schema->removeValidator('message', 'telephone'); - - $result = $schema->all(); - - // Assert - $contactSchema = [ - "message" => [ - "validators" => [ - "length" => [ - "max" => 10000, - "message" => "Your message is too long!" - ] - ] - ] - ]; - - $this->assertEquals($contactSchema, $result); + $requestSchema = new RequestSchema(); + $this->assertSame([], $requestSchema->getSchema()); + $this->assertSame($requestSchema->all(), $requestSchema->getSchema()); } - public function testSetTransformation() + public function testWithPath() { - // Arrange - $loader = new YamlFileLoader($this->basePath . '/contact.yaml'); - $schema = new RequestSchemaRepository($loader->load()); - - // Act - $schema->setTransformations('message', ['purge', 'owlify']); - $result = $schema->all(); - - // Assert - $contactSchema = [ - "message" => [ - "validators" => [ - "required" => [ - "message" => "Please enter a message" - ] - ], - "transformations" => [ - "purge", - "owlify" - ] - ] - ]; - $this->assertArraySubset($contactSchema, $result); + $requestSchema = new RequestSchema($this->basePath); + $this->assertArraySubset($this->contactSchema, $requestSchema->getSchema()); + $this->assertSame($requestSchema->all(), $requestSchema->getSchema()); } } diff --git a/tests/ServerSideValidatorTest.php b/tests/ServerSideValidatorTest.php index f3c0c7e..15e853c 100644 --- a/tests/ServerSideValidatorTest.php +++ b/tests/ServerSideValidatorTest.php @@ -1,21 +1,48 @@ translator = new MessageTranslator(); } + public function testValidateNoValidators() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'email' => [], + ]); + + // Act + $validator = new ServerSideValidator($schema, $this->translator); + + $result = $validator->validate([ + 'email' => 'david@owlfancy.com', + ]); + + // Check passing validation + $this->assertTrue($result); + } + public function testValidateEmail() { // Arrange @@ -23,16 +50,16 @@ public function testValidateEmail() 'email' => [ 'validators' => [ 'email' => [ - ] - ] - ] + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'email' => 'david@owlfancy.com' + 'email' => 'david@owlfancy.com', ]); // Check that the correct Valitron rule was generated @@ -43,7 +70,39 @@ public function testValidateEmail() // Check failing validation $this->assertFalse($validator->validate([ - 'email' => 'screeeech' + 'email' => 'screeeech', + ])); + } + + public function testValidateArray() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'screech' => [ + 'validators' => [ + 'array' => [ + 'message' => 'Array must be an array.', + ], + ], + ], + ]); + + // Act + $validator = new ServerSideValidator($schema, $this->translator); + + $result = $validator->validate([ + 'screech' => ['foo', 'bar'], + ]); + + // Check that the correct Valitron rule was generated + $this->assertTrue($validator->hasRule('array', 'screech')); + + // Check passing validation + $this->assertTrue($result); + + // Check failing validation + $this->assertFalse($validator->validate([ + 'screech' => 'screeeech', ])); } @@ -55,19 +114,19 @@ public function testValidateEquals() 'voles' => [ 'validators' => [ 'equals' => [ - 'value' => 8, + 'value' => 8, 'caseSensitive' => false, - 'message' => 'Voles must be equal to {{value}}.' - ] - ] - ] + 'message' => 'Voles must be equal to {{value}}.', + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'voles' => 8 + 'voles' => 8, ]); // Check that the correct Valitron rule was generated @@ -78,7 +137,7 @@ public function testValidateEquals() // Check failing validation $this->assertFalse($validator->validate([ - 'voles' => 3 + 'voles' => 3, ])); } @@ -89,17 +148,17 @@ public function testValidateInteger() 'voles' => [ 'validators' => [ 'integer' => [ - 'message' => 'Voles must be numeric.' - ] - ] - ] + 'message' => 'Voles must be numeric.', + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'voles' => 8 + 'voles' => 8, ]); // Check that the correct Valitron rule was generated @@ -110,11 +169,11 @@ public function testValidateInteger() // Check failing validations $this->assertFalse($validator->validate([ - 'voles' => 'yes' + 'voles' => 'yes', ])); $this->assertFalse($validator->validate([ - 'voles' => 0.5 + 'voles' => 0.5, ])); } @@ -125,19 +184,19 @@ public function testValidateLengthBetween() 'screech' => [ 'validators' => [ 'length' => [ - 'min' => 5, - 'max' => 10, - 'message' => "Your screech must be between {{min}} and {{max}} characters long." - ] - ] - ] + 'min' => 5, + 'max' => 10, + 'message' => 'Your screech must be between {{min}} and {{max}} characters long.', + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'screech' => 'cawwwwww' + 'screech' => 'cawwwwww', ]); // Check that the correct Valitron rule was generated @@ -148,11 +207,11 @@ public function testValidateLengthBetween() // Check failing validations $this->assertFalse($validator->validate([ - 'screech' => 'caw' + 'screech' => 'caw', ])); $this->assertFalse($validator->validate([ - 'screech' => 'cawwwwwwwwwwwwwwwwwww' + 'screech' => 'cawwwwwwwwwwwwwwwwwww', ])); } @@ -163,18 +222,18 @@ public function testValidateLengthMin() 'screech' => [ 'validators' => [ 'length' => [ - 'min' => 5, - 'message' => "Your screech must be at least {{min}} characters long." - ] - ] - ] + 'min' => 5, + 'message' => 'Your screech must be at least {{min}} characters long.', + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'screech' => 'cawwwwww' + 'screech' => 'cawwwwww', ]); // Check that the correct Valitron rule was generated @@ -185,7 +244,7 @@ public function testValidateLengthMin() // Check failing validations $this->assertFalse($validator->validate([ - 'screech' => 'caw' + 'screech' => 'caw', ])); } @@ -196,18 +255,18 @@ public function testValidateLengthMax() 'screech' => [ 'validators' => [ 'length' => [ - 'max' => 10, - 'message' => "Your screech must be no more than {{max}} characters long." - ] - ] - ] + 'max' => 10, + 'message' => 'Your screech must be no more than {{max}} characters long.', + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'screech' => 'cawwwwww' + 'screech' => 'cawwwwww', ]); // Check that the correct Valitron rule was generated @@ -217,7 +276,7 @@ public function testValidateLengthMax() $this->assertTrue($result); $this->assertFalse($validator->validate([ - 'screech' => 'cawwwwwwwwwwwwwwwwwww' + 'screech' => 'cawwwwwwwwwwwwwwwwwww', ])); } @@ -228,19 +287,19 @@ public function testValidateMatches() 'password' => [ 'validators' => [ 'matches' => [ - 'field' => 'passwordc', - 'message' => "The value of this field does not match the value of the '{{field}}' field." - ] - ] - ] + 'field' => 'passwordc', + 'message' => "The value of this field does not match the value of the '{{field}}' field.", + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'password' => 'secret', - 'passwordc' => 'secret' + 'password' => 'secret', + 'passwordc' => 'secret', ]); // Check that the correct Valitron rule was generated @@ -250,8 +309,8 @@ public function testValidateMatches() $this->assertTrue($result); $this->assertFalse($validator->validate([ - 'password' => 'secret', - 'passwordc' => 'hoothoot' + 'password' => 'secret', + 'passwordc' => 'hoothoot', ])); } @@ -262,18 +321,18 @@ public function testValidateMemberOf() 'genus' => [ 'validators' => [ 'member_of' => [ - 'values' => ["Megascops", "Bubo", "Glaucidium", "Tyto", "Athene"], - 'message' => "Sorry, that is not one of the permitted genuses." - ] - ] - ] + 'values' => ['Megascops', 'Bubo', 'Glaucidium', 'Tyto', 'Athene'], + 'message' => 'Sorry, that is not one of the permitted genuses.', + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'genus' => 'Megascops' + 'genus' => 'Megascops', ]); // Check that the correct Valitron rule was generated @@ -283,7 +342,7 @@ public function testValidateMemberOf() $this->assertTrue($result); $this->assertFalse($validator->validate([ - 'genus' => 'Dolomedes' + 'genus' => 'Dolomedes', ])); } @@ -294,24 +353,24 @@ public function testValidateNoLeadingWhitespace() 'user_name' => [ 'validators' => [ 'no_leading_whitespace' => [ - 'message' => "{{self}} cannot begin with whitespace characters" - ] - ] - ] + 'message' => '{{self}} cannot begin with whitespace characters', + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'user_name' => 'alexw' + 'user_name' => 'alexw', ]); // Check passing validation $this->assertTrue($result); $this->assertFalse($validator->validate([ - 'user_name' => ' alexw' + 'user_name' => ' alexw', ])); } @@ -322,17 +381,17 @@ public function testValidateNoTrailingWhitespace() 'user_name' => [ 'validators' => [ 'no_trailing_whitespace' => [ - 'message' => "{{self}} cannot end with whitespace characters" - ] - ] - ] + 'message' => '{{self}} cannot end with whitespace characters', + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'user_name' => 'alexw' + 'user_name' => 'alexw', ]); // Check passing validation @@ -340,11 +399,11 @@ public function testValidateNoTrailingWhitespace() // Should still allow starting with whitespace $this->assertTrue($validator->validate([ - 'user_name' => ' alexw' + 'user_name' => ' alexw', ])); $this->assertFalse($validator->validate([ - 'user_name' => 'alexw ' + 'user_name' => 'alexw ', ])); } @@ -356,19 +415,19 @@ public function testValidateNotEquals() 'voles' => [ 'validators' => [ 'not_equals' => [ - 'value' => 0, + 'value' => 0, 'caseSensitive' => false, - 'message' => 'Voles must be not be equal to {{value}}.' - ] - ] - ] + 'message' => 'Voles must be not be equal to {{value}}.', + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'voles' => 8 + 'voles' => 8, ]); // Check that the correct Valitron rule was generated @@ -379,7 +438,7 @@ public function testValidateNotEquals() // Check failing validation $this->assertFalse($validator->validate([ - 'voles' => 0 + 'voles' => 0, ])); } @@ -390,19 +449,19 @@ public function testValidateNotMatches() 'password' => [ 'validators' => [ 'not_matches' => [ - 'field' => 'user_name', - 'message' => "Your password cannot be the same as your username." - ] - ] - ] + 'field' => 'user_name', + 'message' => 'Your password cannot be the same as your username.', + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'password' => 'secret', - 'user_name' => 'alexw' + 'password' => 'secret', + 'user_name' => 'alexw', ]); // Check that the correct Valitron rule was generated @@ -412,8 +471,8 @@ public function testValidateNotMatches() $this->assertTrue($result); $this->assertFalse($validator->validate([ - 'password' => 'secret', - 'user_name' => 'secret' + 'password' => 'secret', + 'user_name' => 'secret', ])); } @@ -424,18 +483,18 @@ public function testValidateNotMemberOf() 'genus' => [ 'validators' => [ 'not_member_of' => [ - 'values' => ["Myodes", "Microtus", "Neodon", "Alticola"], - 'message' => "Sorry, it would appear that you are not an owl." - ] - ] - ] + 'values' => ['Myodes', 'Microtus', 'Neodon', 'Alticola'], + 'message' => 'Sorry, it would appear that you are not an owl.', + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'genus' => 'Megascops' + 'genus' => 'Megascops', ]); // Check that the correct Valitron rule was generated @@ -445,7 +504,7 @@ public function testValidateNotMemberOf() $this->assertTrue($result); $this->assertFalse($validator->validate([ - 'genus' => 'Myodes' + 'genus' => 'Myodes', ])); } @@ -456,17 +515,17 @@ public function testValidateNumeric() 'accuracy' => [ 'validators' => [ 'numeric' => [ - 'message' => "Sorry, your strike accuracy must be a number." - ] - ] - ] + 'message' => 'Sorry, your strike accuracy must be a number.', + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'accuracy' => 0.99 + 'accuracy' => 0.99, ]); // Check that the correct Valitron rule was generated @@ -476,19 +535,19 @@ public function testValidateNumeric() $this->assertTrue($result); $this->assertTrue($validator->validate([ - 'accuracy' => '0.99' + 'accuracy' => '0.99', ])); $this->assertTrue($validator->validate([ - 'accuracy' => '' + 'accuracy' => '', ])); $this->assertFalse($validator->validate([ - 'accuracy' => false + 'accuracy' => false, ])); $this->assertFalse($validator->validate([ - 'accuracy' => 'yes' + 'accuracy' => 'yes', ])); } @@ -499,19 +558,19 @@ public function testValidateRange() 'voles' => [ 'validators' => [ 'range' => [ - 'min' => 5, - 'max' => 10, - 'message' => "You must catch {{min}} - {{max}} voles." - ] - ] - ] + 'min' => 5, + 'max' => 10, + 'message' => 'You must catch {{min}} - {{max}} voles.', + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'voles' => 6 + 'voles' => 6, ]); // Check that the correct Valitron rules were generated @@ -522,15 +581,15 @@ public function testValidateRange() $this->assertTrue($result); $this->assertFalse($validator->validate([ - 'voles' => 2 + 'voles' => 2, ])); $this->assertFalse($validator->validate([ - 'voles' => 10000 + 'voles' => 10000, ])); $this->assertFalse($validator->validate([ - 'voles' => 'yes' + 'voles' => 'yes', ])); } @@ -541,18 +600,18 @@ public function testValidateRegex() 'screech' => [ 'validators' => [ 'regex' => [ - 'regex' => "^who(o*)$", - 'message' => "You did not provide a valid screech." - ] - ] - ] + 'regex' => '^who(o*)$', + 'message' => 'You did not provide a valid screech.', + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'screech' => 'whooooooooo' + 'screech' => 'whooooooooo', ]); // Check that the correct Valitron rule was generated @@ -562,11 +621,11 @@ public function testValidateRegex() $this->assertTrue($result); $this->assertFalse($validator->validate([ - 'screech' => 'whoot' + 'screech' => 'whoot', ])); $this->assertFalse($validator->validate([ - 'screech' => 'ribbit' + 'screech' => 'ribbit', ])); } @@ -577,17 +636,17 @@ public function testValidateRequired() 'species' => [ 'validators' => [ 'required' => [ - 'message' => "Please tell us your species." - ] - ] - ] + 'message' => 'Please tell us your species.', + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'species' => 'Athene noctua' + 'species' => 'Athene noctua', ]); // Check that the correct Valitron rule was generated @@ -597,7 +656,7 @@ public function testValidateRequired() $this->assertTrue($result); $this->assertFalse($validator->validate([ - 'species' => '' + 'species' => '', ])); $this->assertFalse($validator->validate([])); @@ -610,17 +669,17 @@ public function testValidateTelephone() 'phone' => [ 'validators' => [ 'telephone' => [ - 'message' => "Whoa there, check your phone number again." - ] - ] - ] + 'message' => 'Whoa there, check your phone number again.', + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'phone' => '1(212)-999-2345' + 'phone' => '1(212)-999-2345', ]); // Check that the correct Valitron rule was generated @@ -630,23 +689,23 @@ public function testValidateTelephone() $this->assertTrue($result); $this->assertTrue($validator->validate([ - 'phone' => '212 999 2344' + 'phone' => '212 999 2344', ])); $this->assertTrue($validator->validate([ - 'phone' => '212-999-0983' + 'phone' => '212-999-0983', ])); $this->assertFalse($validator->validate([ - 'phone' => '111-123-5434' + 'phone' => '111-123-5434', ])); $this->assertFalse($validator->validate([ - 'phone' => '212 123 4567' + 'phone' => '212 123 4567', ])); $this->assertFalse($validator->validate([ - 'phone' => '' + 'phone' => '', ])); } @@ -657,17 +716,17 @@ public function testValidateUri() 'website' => [ 'validators' => [ 'uri' => [ - 'message' => "That's not even a valid URL..." - ] - ] - ] + 'message' => "That's not even a valid URL...", + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'website' => 'http://www.owlfancy.com' + 'website' => 'http://www.owlfancy.com', ]); // Check that the correct Valitron rule was generated @@ -677,28 +736,28 @@ public function testValidateUri() $this->assertTrue($result); $this->assertTrue($validator->validate([ - 'website' => 'http://owlfancy.com' + 'website' => 'http://owlfancy.com', ])); $this->assertTrue($validator->validate([ - 'website' => 'https://learn.userfrosting.com' + 'website' => 'https://learn.userfrosting.com', ])); // Note that we require URLs to begin with http(s):// $this->assertFalse($validator->validate([ - 'website' => 'www.owlfancy.com' + 'website' => 'www.owlfancy.com', ])); $this->assertFalse($validator->validate([ - 'website' => 'owlfancy.com' + 'website' => 'owlfancy.com', ])); $this->assertFalse($validator->validate([ - 'website' => 'owls' + 'website' => 'owls', ])); $this->assertFalse($validator->validate([ - 'website' => '' + 'website' => '', ])); } @@ -709,17 +768,17 @@ public function testValidateUsername() 'user_name' => [ 'validators' => [ 'username' => [ - 'message' => "Sorry buddy, that's not a valid username." - ] - ] - ] + 'message' => "Sorry buddy, that's not a valid username.", + ], + ], + ], ]); // Act $validator = new ServerSideValidator($schema, $this->translator); $result = $validator->validate([ - 'user_name' => 'alex.weissman' + 'user_name' => 'alex.weissman', ]); // Check that the correct Valitron rule was generated @@ -729,28 +788,28 @@ public function testValidateUsername() $this->assertTrue($result); $this->assertTrue($validator->validate([ - 'user_name' => 'alexweissman' + 'user_name' => 'alexweissman', ])); $this->assertTrue($validator->validate([ - 'user_name' => 'alex-weissman-the-wise' + 'user_name' => 'alex-weissman-the-wise', ])); // Note that we require URLs to begin with http(s):// $this->assertFalse($validator->validate([ - 'user_name' => "" + 'user_name' => "", ])); $this->assertFalse($validator->validate([ - 'user_name' => '#owlfacts' + 'user_name' => '#owlfacts', ])); $this->assertFalse($validator->validate([ - 'user_name' => 'Did you ever hear the tragedy of Darth Plagueis the Wise?' + 'user_name' => 'Did you ever hear the tragedy of Darth Plagueis the Wise?', ])); $this->assertFalse($validator->validate([ - 'user_name' => '' + 'user_name' => '', ])); } @@ -761,11 +820,11 @@ public function testDomainRulesClientOnly() 'plumage' => [ 'validators' => [ 'required' => [ - 'domain' => 'client', - 'message' => "Are you sure you don't want to show us your plumage?" - ] - ] - ] + 'domain' => 'client', + 'message' => "Are you sure you don't want to show us your plumage?", + ], + ], + ], ]); // Act @@ -787,11 +846,11 @@ public function testDomainRulesServerOnly() 'plumage' => [ 'validators' => [ 'required' => [ - 'domain' => 'server', - 'message' => "Are you sure you don't want to show us your plumage?" - ] - ] - ] + 'domain' => 'server', + 'message' => "Are you sure you don't want to show us your plumage?", + ], + ], + ], ]); // Act @@ -805,4 +864,32 @@ public function testDomainRulesServerOnly() // Check passing validation $this->assertFalse($result); } + + /** + * @depends testValidateUsername + */ + public function testValidateWithNoValidatorMessage() + { + // Arrange + $schema = new RequestSchemaRepository([ + 'user_name' => [ + 'validators' => [ + 'username' => [], + ], + ], + ]); + + // Act + $validator = new ServerSideValidator($schema, $this->translator); + + $result = $validator->validate([ + 'user_name' => 'alex.weissman', + ]); + + // Check that the correct Valitron rule was generated + $this->assertTrue($validator->hasRule('username', 'user_name')); + + // Check passing validation + $this->assertTrue($result); + } }