From 8c0597d5be0b9c3c5b81b49e8ba20db7498d2849 Mon Sep 17 00:00:00 2001 From: tuka217 Date: Mon, 10 Oct 2016 10:04:06 +0200 Subject: [PATCH] fix after RV --- ...nique_code_validation_within_taxon.feature | 4 +-- .../Ui/Admin/ManagingTaxonsContext.php | 2 +- .../Resources/config/services/validators.xml | 3 -- .../Constraints/ImageUniqueCodeValidator.php | 18 ++++++----- .../ImageUniqueCodeValidatorSpec.php | 30 +++++++++---------- 5 files changed, 27 insertions(+), 30 deletions(-) diff --git a/features/taxonomy/managing_taxons/taxon_image_unique_code_validation_within_taxon.feature b/features/taxonomy/managing_taxons/taxon_image_unique_code_validation_within_taxon.feature index 26f3934163..1bbbb58718 100644 --- a/features/taxonomy/managing_taxons/taxon_image_unique_code_validation_within_taxon.feature +++ b/features/taxonomy/managing_taxons/taxon_image_unique_code_validation_within_taxon.feature @@ -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 diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php index 57e67430c2..356c7842bf 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php @@ -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.' ); } diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/services/validators.xml b/src/Sylius/Bundle/CoreBundle/Resources/config/services/validators.xml index 1587b1d192..c5fa8ea014 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/services/validators.xml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/services/validators.xml @@ -33,8 +33,5 @@ %currency% - - - diff --git a/src/Sylius/Bundle/CoreBundle/Validator/Constraints/ImageUniqueCodeValidator.php b/src/Sylius/Bundle/CoreBundle/Validator/Constraints/ImageUniqueCodeValidator.php index 9c678fe6df..a937300e82 100644 --- a/src/Sylius/Bundle/CoreBundle/Validator/Constraints/ImageUniqueCodeValidator.php +++ b/src/Sylius/Bundle/CoreBundle/Validator/Constraints/ImageUniqueCodeValidator.php @@ -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; } } } diff --git a/src/Sylius/Bundle/CoreBundle/spec/Validator/Constraints/ImageUniqueCodeValidatorSpec.php b/src/Sylius/Bundle/CoreBundle/spec/Validator/Constraints/ImageUniqueCodeValidatorSpec.php index dfa2fb75c7..c391992441 100644 --- a/src/Sylius/Bundle/CoreBundle/spec/Validator/Constraints/ImageUniqueCodeValidatorSpec.php +++ b/src/Sylius/Bundle/CoreBundle/spec/Validator/Constraints/ImageUniqueCodeValidatorSpec.php @@ -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() + ); } }