fix after RV

This commit is contained in:
tuka217 2016-10-10 10:04:06 +02:00
parent 641651b75e
commit 8c0597d5be
5 changed files with 27 additions and 30 deletions

View file

@ -28,9 +28,9 @@ Feature: Taxon image unique code validation within a taxon
@ui @javascript
Scenario: Trying to add images with the same code
When I want to modify the "T-Shirts" taxon
And I attach the "mugs.jpg" image with a code "banner"
When I want to modify the "Mugs" taxon
And I attach the "t-shirts.jpg" image with a code "banner"
And I attach the "mugs.jpg" image with a code "banner"
And I try to save my changes
Then I should be notified that the 1st image should have an unique code
And I should be notified that the 2nd image should have an unique code

View file

@ -412,7 +412,7 @@ final class ManagingTaxonsContext implements Context
preg_match_all('!\d+!', $imageNumber, $matches);
Assert::same(
$this->updatePage->getValidationMessageForImageAtPlace((int) $matches[0]),
$this->updatePage->getValidationMessageForImageAtPlace(((int) $matches[0][0]) - 1),
'Image code must be unique within this taxon.'
);
}

View file

@ -33,8 +33,5 @@
<argument>%currency%</argument>
<tag name="validator.constraint_validator" alias="sylius_cannot_disable_currency" />
</service>
<service id="sylius.validator.unique.image_code" class="Sylius\Bundle\CoreBundle\Validator\Constraints\ImageUniqueCodeValidator">
<tag name="validator.constraint_validator" alias="image_unique_code" />
</service>
</services>
</container>

View file

@ -26,17 +26,19 @@ class ImageUniqueCodeValidator extends ConstraintValidator
*/
public function validate($images, Constraint $constraint)
{
$imagesCodes = [];
/** @var ImageInterface[] $images */
foreach ($images as $key => $image) {
$filteredImages = $images->filter(function(ImageInterface $imageFromObject) use ($image) {
return $imageFromObject->getOwner() === $image->getOwner()
&& $imageFromObject->getCode() === $image->getCode()
&& $imageFromObject !== $image
;
});
if (!array_key_exists($image->getCode(), $imagesCodes)) {
$imagesCodes[$image->getCode()] = $key;
continue;
}
if(0 !== count($filteredImages)) {
$this->context->addViolationAt(sprintf('[%d].code', $key), $constraint->message);
$this->context->addViolationAt(sprintf('[%d].code', $key), $constraint->message);
if (false !== $imagesCodes[$image->getCode()]) {
$this->context->addViolationAt(sprintf('[%d].code', $imagesCodes[$image->getCode()]), $constraint->message);
$imagesCodes[$image->getCode()] = false;
}
}
}

View file

@ -40,35 +40,33 @@ final class ImageUniqueCodeValidatorSpec extends ObjectBehavior
function it_adds_violation_if_there_two_images_with_the_same_owner_which_have_same_codes(
ImageInterface $firstImage,
ImageInterface $secondImage,
ImageAwareInterface $owner
ImageInterface $secondImage
) {
$firstImage->getOwner()->willReturn($owner);
$secondImage->getOwner()->willReturn($owner);
$firstImage->getCode()->willReturn('car');
$secondImage->getCode()->willReturn('car');
$this->bulidViolation('[0].code', Argument::type('string'))->shouldBeCalled();
$this->bulidViolation('[1].code', Argument::type('string'))->shouldBeCalled();
$this->addViolationAt('[0].code', Argument::type('string'))->shouldBeCalled();
$this->addViolationAt('[1].code', Argument::type('string'))->shouldBeCalled();
$this->validate(new ArrayCollection($firstImage->getWrappedObject(), $secondImage->getWrappedObject()), new ImageUniqueCode());
$this->validate(
new ArrayCollection($firstImage->getWrappedObject(), $secondImage->getWrappedObject()),
new ImageUniqueCode()
);
}
function it_does_not_add_violation_if_there_is_no_duplication_of_a_code(
ImageInterface $firstImage,
ImageInterface $secondImage,
ImageAwareInterface $owner
ImageInterface $secondImage
) {
$firstImage->getOwner()->willReturn($owner);
$secondImage->getOwner()->willReturn($owner);
$firstImage->getCode()->willReturn('car');
$secondImage->getCode()->willReturn('wipers');
$this->bulidViolation('[0].code', Argument::type('string'))->shouldNotBeCalled();
$this->bulidViolation('[1].code', Argument::type('string'))->shouldNotBeCalled();
$this->addViolationAt('[0].code', Argument::type('string'))->shouldNotBeCalled();
$this->addViolationAt('[1].code', Argument::type('string'))->shouldNotBeCalled();
$this->validate(new ArrayCollection($firstImage->getWrappedObject(), $secondImage->getWrappedObject()), new ImageUniqueCode());
$this->validate(
new ArrayCollection($firstImage->getWrappedObject(), $secondImage->getWrappedObject()),
new ImageUniqueCode()
);
}
}