-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor #13174 [CatalogPromotion] Refactor scope validators to be mo…
…re extendable (GSadee) This PR was merged into the 1.11-dev branch. Discussion ---------- | Q | A | --------------- | ----- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | | Related tickets | based on #13173 | License | MIT <!-- - Bug fixes must be submitted against the 1.9 or 1.10 branch (the lowest possible) - Features and deprecations must be submitted against the master branch - Make sure that the correct base branch is set To be sure you are not breaking any Backward Compatibilities, check the documentation: https://docs.sylius.com/en/latest/book/organization/backward-compatibility-promise.html --> Commits ------- c797d49 [CatalogPromotion] Refactor scope validators to be more extendable 9efc29a [CatalogPromotion] Refactor main scope validator to dehardcode types b6efb9a [CatalogPromotion] Refactor scope validators to do an actual validation
- Loading branch information
Showing
8 changed files
with
335 additions
and
159 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
50 changes: 50 additions & 0 deletions
50
src/Sylius/Bundle/CoreBundle/Validator/CatalogPromotionScope/ForTaxonsScopeValidator.php
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\CoreBundle\Validator\CatalogPromotionScope; | ||
|
||
use Sylius\Bundle\CoreBundle\Validator\Constraints\CatalogPromotionScope; | ||
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class ForTaxonsScopeValidator implements ScopeValidatorInterface | ||
{ | ||
private TaxonRepositoryInterface $taxonRepository; | ||
|
||
public function __construct(TaxonRepositoryInterface $taxonRepository) | ||
{ | ||
$this->taxonRepository = $taxonRepository; | ||
} | ||
|
||
public function validate(array $configuration, Constraint $constraint, ExecutionContextInterface $context): void | ||
{ | ||
/** @var CatalogPromotionScope $constraint */ | ||
Assert::isInstanceOf($constraint, CatalogPromotionScope::class); | ||
|
||
if (!isset($configuration['taxons']) || empty($configuration['taxons'])) { | ||
$context->buildViolation($constraint->taxonsNotEmpty)->atPath('configuration.taxons')->addViolation(); | ||
|
||
return; | ||
} | ||
|
||
foreach ($configuration['taxons'] as $taxonCode) { | ||
if (null === $this->taxonRepository->findOneBy(['code' => $taxonCode])) { | ||
$context->buildViolation($constraint->invalidTaxons)->atPath('configuration.taxons')->addViolation(); | ||
|
||
return; | ||
} | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/Sylius/Bundle/CoreBundle/Validator/CatalogPromotionScope/ForVariantsScopeValidator.php
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\CoreBundle\Validator\CatalogPromotionScope; | ||
|
||
use Sylius\Bundle\CoreBundle\Validator\Constraints\CatalogPromotionScope; | ||
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class ForVariantsScopeValidator implements ScopeValidatorInterface | ||
{ | ||
private ProductVariantRepositoryInterface $variantRepository; | ||
|
||
public function __construct(ProductVariantRepositoryInterface $variantRepository) | ||
{ | ||
$this->variantRepository = $variantRepository; | ||
} | ||
|
||
public function validate(array $configuration, Constraint $constraint, ExecutionContextInterface $context): void | ||
{ | ||
/** @var CatalogPromotionScope $constraint */ | ||
Assert::isInstanceOf($constraint, CatalogPromotionScope::class); | ||
|
||
if (!array_key_exists('variants', $configuration) || empty($configuration['variants'])) { | ||
$context->buildViolation($constraint->variantsNotEmpty)->atPath('configuration.variants')->addViolation(); | ||
|
||
return; | ||
} | ||
|
||
foreach ($configuration['variants'] as $variantCode) { | ||
if (null === $this->variantRepository->findOneBy(['code' => $variantCode])) { | ||
$context->buildViolation($constraint->invalidVariants)->atPath('configuration.variants')->addViolation(); | ||
|
||
return; | ||
} | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Sylius/Bundle/CoreBundle/Validator/CatalogPromotionScope/ScopeValidatorInterface.php
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\CoreBundle\Validator\CatalogPromotionScope; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
|
||
interface ScopeValidatorInterface | ||
{ | ||
public function validate(array $configuration, Constraint $constraint, ExecutionContextInterface $context): void; | ||
} |
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
73 changes: 73 additions & 0 deletions
73
...us/Bundle/CoreBundle/spec/Validator/CatalogPromotionScope/ForTaxonsScopeValidatorSpec.php
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Bundle\CoreBundle\Validator\CatalogPromotionScope; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Bundle\CoreBundle\Validator\CatalogPromotionScope\ScopeValidatorInterface; | ||
use Sylius\Bundle\CoreBundle\Validator\Constraints\CatalogPromotionScope; | ||
use Sylius\Component\Core\Model\TaxonInterface; | ||
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface; | ||
|
||
final class ForTaxonsScopeValidatorSpec extends ObjectBehavior | ||
{ | ||
function let(TaxonRepositoryInterface $taxonRepository): void | ||
{ | ||
$this->beConstructedWith($taxonRepository); | ||
} | ||
|
||
function it_is_a_scope_validator(): void | ||
{ | ||
$this->shouldHaveType(ScopeValidatorInterface::class); | ||
} | ||
|
||
function it_adds_violation_if_catalog_promotion_scope_does_not_have_taxons_key_configured( | ||
ExecutionContextInterface $executionContext, | ||
ConstraintViolationBuilderInterface $constraintViolationBuilder | ||
): void { | ||
$executionContext->buildViolation('sylius.catalog_promotion_scope.for_taxons.not_empty')->willReturn($constraintViolationBuilder); | ||
$constraintViolationBuilder->atPath('configuration.taxons')->willReturn($constraintViolationBuilder); | ||
$constraintViolationBuilder->addViolation()->shouldBeCalled(); | ||
|
||
$this->validate([], new CatalogPromotionScope(), $executionContext); | ||
} | ||
|
||
function it_adds_violation_if_catalog_promotion_scope_has_not_existing_taxons_configured( | ||
TaxonRepositoryInterface $taxonRepository, | ||
ExecutionContextInterface $executionContext, | ||
ConstraintViolationBuilderInterface $constraintViolationBuilder | ||
): void { | ||
$taxonRepository->findOneBy(['code' => 'not_existing_taxon'])->willReturn(null); | ||
|
||
$executionContext->buildViolation('sylius.catalog_promotion_scope.for_taxons.invalid_taxons')->willReturn($constraintViolationBuilder); | ||
$constraintViolationBuilder->atPath('configuration.taxons')->willReturn($constraintViolationBuilder); | ||
$constraintViolationBuilder->addViolation()->shouldBeCalled(); | ||
|
||
$this->validate(['taxons' => ['not_existing_taxon']], new CatalogPromotionScope(), $executionContext); | ||
} | ||
|
||
function it_does_nothing_if_catalog_promotion_scope_is_valid( | ||
TaxonRepositoryInterface $taxonRepository, | ||
ExecutionContextInterface $executionContext, | ||
TaxonInterface $taxon | ||
): void { | ||
$taxonRepository->findOneBy(['code' => 'taxon'])->willReturn($taxon); | ||
|
||
$executionContext->buildViolation('sylius.catalog_promotion_scope.for_taxons.not_empty')->shouldNotBeCalled(); | ||
$executionContext->buildViolation('sylius.catalog_promotion_scope.for_taxons.invalid_taxons')->shouldNotBeCalled(); | ||
|
||
$this->validate(['taxons' => ['taxon']], new CatalogPromotionScope(), $executionContext); | ||
} | ||
} |
Oops, something went wrong.