From 910e41816e216cb9c2d658b3b29c1cc5e14a6f2c Mon Sep 17 00:00:00 2001 From: Jakob Tolkemit Date: Thu, 13 Aug 2020 09:39:12 +0200 Subject: [PATCH] Updated spec for overlap validator --- composer.json | 3 +- ...elSpecialPriceDateOverlapValidatorSpec.php | 81 ++++++++++++------- 2 files changed, 54 insertions(+), 30 deletions(-) diff --git a/composer.json b/composer.json index a273cc0..4294b47 100644 --- a/composer.json +++ b/composer.json @@ -49,7 +49,8 @@ "psr-4": { "Brille24\\SyliusSpecialPricePlugin\\": "src/", "Tests\\Brille24\\SyliusSpecialPricePlugin\\": "tests/", - "Tests\\Application\\SyliusSpecialPricePlugin\\": "tests/Application/src/" + "Tests\\Application\\SyliusSpecialPricePlugin\\": "tests/Application/src/", + "spec\\Brille24\\SyliusSpecialPricePlugin\\": "spec/" } }, "autoload-dev": { diff --git a/spec/Validator/ProductVariantChannelSpecialPriceDateOverlapValidatorSpec.php b/spec/Validator/ProductVariantChannelSpecialPriceDateOverlapValidatorSpec.php index d2dd843..29deea1 100644 --- a/spec/Validator/ProductVariantChannelSpecialPriceDateOverlapValidatorSpec.php +++ b/spec/Validator/ProductVariantChannelSpecialPriceDateOverlapValidatorSpec.php @@ -7,6 +7,7 @@ use Brille24\SyliusSpecialPricePlugin\Entity\ChannelSpecialPricing; use Brille24\SyliusSpecialPricePlugin\Validator\ProductVariantChannelSpecialPriceDateOverlapConstraint; use Brille24\SyliusSpecialPricePlugin\Validator\ProductVariantChannelSpecialPriceDateOverlapValidator; +use DateTime; use Doctrine\Common\Collections\ArrayCollection; use PhpSpec\ObjectBehavior; use Prophecy\Argument; @@ -17,7 +18,7 @@ class ProductVariantChannelSpecialPriceDateOverlapValidatorSpec extends ObjectBehavior { - function let( + public function let( ExecutionContextInterface $context, ConstraintViolationBuilderInterface $violationBuilder ): void { @@ -27,63 +28,85 @@ function let( $this->initialize($context); } - function it_is_initializable() + public function it_is_initializable() { $this->shouldHaveType(ProductVariantChannelSpecialPriceDateOverlapValidator::class); } - function it_is_a_validator() + public function it_is_a_validator() { $this->shouldBeAnInstanceOf(ConstraintValidator::class); } - function it_does_not_add_violation_if_dates_dont_overlap( + public function it_does_not_add_violation_if_dates_dont_overlap( ProductVariant $productVariant, ProductVariantChannelSpecialPriceDateOverlapConstraint $constraint, ExecutionContextInterface $context, ConstraintViolationBuilderInterface $violationBuilder ): void { - $firstPrice = new ChannelSpecialPricing(); - $firstPrice->setStartsAt(new \DateTime('2 days ago')); - $firstPrice->setEndsAt(new \DateTime('yesterday')); - $firstPrice->setChannelCode('FIRST'); - - $secondPrice = new ChannelSpecialPricing(); - $secondPrice->setStartsAt(new \DateTime('now')); - $secondPrice->setEndsAt(new \DateTime('tomorrow')); - $secondPrice->setChannelCode('FIRST'); - - $productVariant->getChannelSpecialPricings()->willReturn(new ArrayCollection([$firstPrice, $secondPrice])); + foreach ($this->notOverlappingDatesProvider() as $dates) { + $productVariant->getChannelSpecialPricings()->willReturn(new ArrayCollection($this->getChannelPrices($dates))); - $context->buildViolation(Argument::any())->shouldNotBeCalled(); - $violationBuilder->atPath(Argument::any())->shouldNotBeCalled(); - $violationBuilder->addViolation()->shouldNotBeCalled(); + $context->buildViolation(Argument::any())->shouldNotBeCalled(); + $violationBuilder->atPath(Argument::any())->shouldNotBeCalled(); + $violationBuilder->addViolation()->shouldNotBeCalled(); - $this->validate($productVariant, $constraint); + $this->validate($productVariant, $constraint); + } } - function it_adds_violation_if_dates_overlap( + public function it_adds_violation_if_dates_overlap( ProductVariant $productVariant, ProductVariantChannelSpecialPriceDateOverlapConstraint $constraint, ExecutionContextInterface $context, ConstraintViolationBuilderInterface $violationBuilder ): void { + foreach ($this->overlappingDatesProvider() as $dates) { + $productVariant->getChannelSpecialPricings()->willReturn(new ArrayCollection($this->getChannelPrices($dates))); + + $context->buildViolation(Argument::any())->shouldBeCalled(); + $violationBuilder->atPath(Argument::any())->shouldBeCalled(); + $violationBuilder->addViolation()->shouldBeCalled(); + + $this->validate($productVariant, $constraint); + } + } + + private function getChannelPrices(array $dates): array + { + [$firstStart, $firstEnd, $secondStart, $secondEnd] = $dates; + $firstPrice = new ChannelSpecialPricing(); - $firstPrice->setStartsAt(new \DateTime('2 days ago')); - $firstPrice->setEndsAt(new \DateTime('tomorrow')); + $firstPrice->setStartsAt($firstStart); + $firstPrice->setEndsAt($firstEnd); $firstPrice->setChannelCode('FIRST'); $secondPrice = new ChannelSpecialPricing(); - $secondPrice->setStartsAt(new \DateTime('now')); - $secondPrice->setEndsAt(new \DateTime('tomorrow')); + $secondPrice->setStartsAt($secondStart); + $secondPrice->setEndsAt($secondEnd); $secondPrice->setChannelCode('FIRST'); - $productVariant->getChannelSpecialPricings()->willReturn(new ArrayCollection([$firstPrice, $secondPrice])); + return [$firstPrice, $secondPrice]; + } - $context->buildViolation(Argument::any())->shouldBeCalled(); - $violationBuilder->atPath(Argument::any())->shouldBeCalled(); - $violationBuilder->addViolation()->shouldBeCalled(); + private function overlappingDatesProvider(): array + { + return [ + [null, null, null, null], + [null, null, null, new \DateTime('now')], + [null, null, new \DateTime('now'), null], + [new \DateTime('now'), new \DateTime('tomorrow'), new \DateTime('yesterday'), new \DateTime('tomorrow')], + [new \DateTime('now'), new \DateTime('tomorrow'), new \DateTime('yesterday'), null], + [new \DateTime('now'), new \DateTime('tomorrow'), null, new \DateTime('tomorrow')], + [new \DateTime('now'), null, null, new \DateTime('tomorrow')], + ]; + } - $this->validate($productVariant, $constraint); + private function notOverlappingDatesProvider(): array + { + return [ + [new \DateTime('tomorrow'), null, null, new \DateTime('now')], + [new \DateTime('yesterday'), new \DateTime('now'), new \DateTime('+1 minute'), new \DateTime('tomorrow')], + ]; } }