Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Add validator's priority retrieval into Factory #180

Merged
merged 4 commits into from
Aug 28, 2019
Merged

Add validator's priority retrieval into Factory #180

merged 4 commits into from
Aug 28, 2019

Conversation

mtagliab
Copy link
Contributor

I noticed that it was impossible to set validator's priority using the "array notation", even if this feature was already there as it already exists for filters (see line 384 of Factory.php).

To add the priority, it will be enough to add the priority key in the array:

$inputFilter[] = [
    'name' => 'text_elt',
    'required' => true,
    'filters' => [
        ['name' => 'StripTags'],
        ['name' => 'StringTrim']
    ],
    'validators' => [
        [
            'name' => 'StringLength',
            'options' => [
                'max' => 256
            ],
            'priority' => 2
        ]
    ]
];

FactoryTest fix

I also noticed that the FactoryTest class had a minor issue, which was that validators where not tested inside the testFactoryWillCreateInputWithSuggestedValidators method.
I fixed the loop and added a final assert to verify that all the validators have been tested.

@michalbundyra michalbundyra added this to the 2.10.1 milestone Aug 23, 2019
@froschdesign
Copy link
Member

@webimpress
I think an unit test is missing here. Like this one:

public function testFactoryWillNotGetPrioritySetting()
{
//Reminder: Priority at which to enqueue filter; defaults to 1000 (higher executes earlier)
$factory = $this->createDefaultFactory();
$input = $factory->createInput([
'name' => 'foo',
'filters' => [
[
'name' => 'StringTrim',
'priority' => Filter\FilterChain::DEFAULT_PRIORITY - 1, // 999
],
[
'name' => 'StringToUpper',
'priority' => Filter\FilterChain::DEFAULT_PRIORITY + 1, //1001
],
[
'name' => 'StringToLower', // default priority 1000
],
],
]);
// We should have 3 filters
$this->assertEquals(3, $input->getFilterChain()->count());
// Filters should pop in the following order:
// string_to_upper (1001), string_to_lower (1000), string_trim (999)
$index = 0;
foreach ($input->getFilterChain()->getFilters() as $filter) {
switch ($index) {
case 0:
$this->assertInstanceOf(Filter\StringToUpper::class, $filter);
break;
case 1:
$this->assertInstanceOf(Filter\StringToLower::class, $filter);
break;
case 2:
$this->assertInstanceOf(Filter\StringTrim::class, $filter);
break;
}
$index++;
}
}

The foreach on the chain won't return its validators. The method $chain->getValidators() return an array with extra data, so the instance need to be extracted.
Asserts haven't been changed, but an additional assert assure that the foreach has been executed.
@michalbundyra michalbundyra changed the base branch from develop to master August 23, 2019 14:28
Test is a bit tricky, because when we get validators from ValidatorChain
using getValidators method we are getting them as an array, not in order
by priority. When validators are called (using isValid method) these
are using the order they were added to the chain.
@michalbundyra
Copy link
Member

@froschdesign test added. Would you mind to have a look on it, please?

It's a bit tricky as I mentioned in the commit comment - we can't use getValidators on ValidatorChain, as the result is not sorted by priority. When validators are called (in isValid) these are ordered, and this is how I check it.

@froschdesign
Copy link
Member

@webimpress

When validators are called (in isValid) these are ordered, and this is how I check it.

👍

if (isset($validator['priority'])) {
$priority = $validator['priority'];
}
$priority = isset($validator['priority']) ? $validator['priority'] : ValidatorChain::DEFAULT_PRIORITY;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even if this is the most logic way to do it, I used the "ugly" format to maintain coherence with the upper statement (breakChainOnFailure).
If you want to use the ternary operator, it would be nicer to re-format the previous check too, so there aren't different coding styles in the same file.

Copy link
Member

Choose a reason for hiding this comment

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

@mtagliab
I've changed it to match the logic from populateFilters:

$priority = isset($filter['priority']) ? $filter['priority'] : FilterChain::DEFAULT_PRIORITY;

As you can see the coding style is mixed in the file...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, you are right, I didn't see that.

michalbundyra added a commit that referenced this pull request Aug 28, 2019
Add validator's priority retrieval into Factory
michalbundyra added a commit that referenced this pull request Aug 28, 2019
@michalbundyra michalbundyra merged commit cc1effe into zendframework:master Aug 28, 2019
michalbundyra added a commit that referenced this pull request Aug 28, 2019
@michalbundyra
Copy link
Member

Thanks, @mtagliab!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants