-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement convert option to Phalcon\Validation\Validator\Uniqueness. F…
…ixes #12005.
- Loading branch information
Showing
3 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,27 @@ public function testSingleField() | |
}); | ||
} | ||
|
||
/** | ||
* Tests uniqueness validator with single fields and a converted value | ||
* | ||
* @author Bas Stottelaar <[email protected]> | ||
* @since 2016-07-25 | ||
*/ | ||
public function testSingleFieldConvert() | ||
{ | ||
$this->specify('Test uniqueness with single field and a converted value.', function () { | ||
$validation = new Validation(); | ||
$validation->add('type', new Uniqueness([ | ||
'convert' => function (array $values) { | ||
$values['type'] = 'hydraulic'; // mechanical -> hydraulic | ||
return $values; | ||
} | ||
])); | ||
$messages = $validation->validate(null, $this->robot); | ||
expect($messages->count())->equals(0); | ||
}); | ||
} | ||
|
||
/** | ||
* Tests uniqueness validator with single field and a null value | ||
* | ||
|
@@ -107,6 +128,27 @@ public function testMultipleFields() | |
}); | ||
} | ||
|
||
/** | ||
* Tests uniqueness validator with multiple fields and a converted value | ||
* | ||
* @author Bas Stottelaar <[email protected]> | ||
* @since 2016-07-25 | ||
*/ | ||
public function testMultipleFieldsConvert() | ||
{ | ||
$this->specify('Test uniqueness with combination of fields and a converted value.', function () { | ||
$validation = new Validation(); | ||
$validation->add(['name', 'type'], new Uniqueness([ | ||
'convert' => function (array $values) { | ||
$values['type'] = 'hydraulic'; // mechanical -> hydraulic | ||
return $values; | ||
} | ||
])); | ||
$messages = $validation->validate(null, $this->robot); | ||
expect($messages->count())->equals(0); | ||
}); | ||
} | ||
|
||
/** | ||
* Tests uniqueness validator with multiple fields and a null value | ||
* | ||
|
@@ -213,6 +255,31 @@ public function testExceptMultipleFieldMultipleExcept() | |
}); | ||
} | ||
|
||
/** | ||
* Tests value conversion for returning an array. | ||
* | ||
* @author Bas Stottelaar <[email protected]> | ||
* @since 2016-07-25 | ||
*/ | ||
public function testConvertArrayReturnsArray() | ||
{ | ||
$this->specify('Test value conversion to return an array.', function () { | ||
$validation = new Validation(); | ||
$validation->add('type', new Uniqueness([ | ||
'convert' => function (array $values) { | ||
($values); | ||
return null; | ||
} | ||
])); | ||
try { | ||
$validation->validate(null, $this->robot); | ||
verify_that(false); | ||
} catch (\Exception $e) { | ||
verify_that(true); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Initialize data for the tests | ||
*/ | ||
|