Skip to content

Commit

Permalink
Merge pull request #7 from BitBagCommerce/fixes
Browse files Browse the repository at this point in the history
OP-121 OP-123 OP-124
  • Loading branch information
leszczuu authored Jan 14, 2021
2 parents 4007b28 + e1e476c commit d039d4e
Show file tree
Hide file tree
Showing 31 changed files with 236 additions and 472 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ before_script:
script:
- composer validate

- vendor/bin/phpstan analyse -c phpstan.neon -l max src/
- vendor/bin/psalm

- vendor/bin/phpunit
- vendor/bin/phpspec run
- vendor/bin/behat --strict -vvv --no-interaction || vendor/bin/behat --strict -vvv --no-interaction --rerun
Expand Down
4 changes: 2 additions & 2 deletions phpspec.yml.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
suites:
main:
namespace: Acme\SyliusExamplePlugin
psr4_prefix: Acme\SyliusExamplePlugin
namespace: BitBag\SyliusCatalogPlugin
psr4_prefix: BitBag\SyliusCatalogPlugin
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
colors="true"
bootstrap="tests/Application/config/bootstrap.php">
<testsuites>
<testsuite name="AcmeSyliusExamplePlugin Test Suite">
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
Expand Down
71 changes: 71 additions & 0 deletions spec/Resolver/CompleteCatalogsForProductResolverSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace spec\BitBag\SyliusCatalogPlugin\Resolver;

use BitBag\SyliusCatalogPlugin\Entity\CatalogInterface;
use BitBag\SyliusCatalogPlugin\Resolver\CatalogsForProductResolverInterface;
use BitBag\SyliusCatalogPlugin\Resolver\CompleteCatalogsForProductResolver;
use BitBag\SyliusCatalogPlugin\Resolver\ProductsInsideCatalogResolverInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\ProductInterface;

class CompleteCatalogsForProductResolverSpec extends ObjectBehavior
{
function let(
CatalogsForProductResolverInterface $catalogsForProductResolver,
ProductsInsideCatalogResolverInterface $catalogResolver
): void {
$this->beConstructedWith($catalogsForProductResolver, $catalogResolver);
}

function it_resolves_products_inside_each_catalog(
ProductInterface $product,
CatalogInterface $catalog1,
CatalogInterface $catalog2,
ProductInterface $productInsideCatalog1,
ProductInterface $productInsideCatalog2,
CatalogsForProductResolverInterface $catalogsForProductResolver,
ProductsInsideCatalogResolverInterface $catalogResolver
): void {
$date = new \DateTimeImmutable();
$catalogsForProductResolver->resolveProductCatalogs($product, $date)->willReturn([$catalog1, $catalog2]);
$catalogResolver->findMatchingProducts($catalog1)->willReturn([$productInsideCatalog1]);
$catalogResolver->findMatchingProducts($catalog2)->willReturn([$productInsideCatalog2]);

$this->resolveProductCatalogs($product, $date)->shouldIterateAs(
[
[
'catalog' => $catalog1,
'products' => [$productInsideCatalog1],
],
[
'catalog' => $catalog2,
'products' => [$productInsideCatalog2],
],
]
);
}

function it_filters_out_empty_catalogs(
ProductInterface $product,
CatalogInterface $catalogNotEmpty,
CatalogInterface $catalogEmpty,
ProductInterface $productInsideCatalog,
CatalogsForProductResolverInterface $catalogsForProductResolver,
ProductsInsideCatalogResolverInterface $catalogResolver
): void {
$date = new \DateTimeImmutable();
$catalogsForProductResolver->resolveProductCatalogs($product, $date)->willReturn([$catalogEmpty, $catalogNotEmpty]);
$catalogResolver->findMatchingProducts($catalogEmpty)->willReturn([]);
$catalogResolver->findMatchingProducts($catalogNotEmpty)->willReturn([$productInsideCatalog]);

$this->resolveProductCatalogs($product, $date)->shouldIterateAs(
[
[
'catalog' => $catalogNotEmpty,
'products' => [$productInsideCatalog],
],
]
);
}
}
7 changes: 3 additions & 4 deletions src/Controller/Shop/ShowCatalogAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

namespace BitBag\SyliusCatalogPlugin\Controller\Shop;

use BitBag\SyliusCatalogPlugin\Resolver\ProductResolverInterface;
use Doctrine\ORM\Tools\Pagination\Paginator;
use BitBag\SyliusCatalogPlugin\Resolver\ProductsInsideCatalogResolverInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -31,7 +30,7 @@ final class ShowCatalogAction

public function __construct(
RepositoryInterface $catalogRepository,
ProductResolverInterface $productResolver,
ProductsInsideCatalogResolverInterface $productResolver,
EngineInterface $templatingEngine
) {
$this->templatingEngine = $templatingEngine;
Expand All @@ -48,7 +47,7 @@ public function __invoke(Request $request): Response

return $this->templatingEngine->renderResponse($template, [
'catalog' => $catalog,
'products' => new Paginator(),
'products' => $products,
]);
}
}
2 changes: 0 additions & 2 deletions src/Form/Type/AbstractConfigurableCatalogElementType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function __construct(string $dataClass, array $validationGroups = [], For
public function buildForm(FormBuilderInterface $builder, array $options): void
{
parent::buildForm($builder, $options);

$builder
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
$type = $this->getRegistryIdentifier($event->getForm(), $event->getData());
Expand All @@ -55,7 +54,6 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
})
->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event): void {
$data = $event->getData();

if (!isset($data['type'])) {
return;
}
Expand Down
3 changes: 0 additions & 3 deletions src/Form/Type/CatalogType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Sylius\Bundle\ResourceBundle\Form\EventSubscriber\AddCodeFormSubscriber;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Bundle\ResourceBundle\Form\Type\ResourceTranslationsType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
Expand All @@ -25,8 +24,6 @@ final class CatalogType extends AbstractResourceType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{


$builder
->addEventSubscriber(new AddCodeFormSubscriber(TextType::class, [
'label' => 'bitbag_sylius_catalog_plugin.ui.code',
Expand Down
6 changes: 5 additions & 1 deletion src/Form/Type/ChannelBasedRulePricing.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@
namespace BitBag\SyliusCatalogPlugin\Form\Type;

use Sylius\Bundle\CoreBundle\Form\Type\ChannelCollectionType;
use Sylius\Bundle\MoneyBundle\Form\Type\MoneyType;
use Sylius\Bundle\PromotionBundle\Form\Type\Rule\ItemTotalConfigurationType;
use Sylius\Component\Core\Model\ChannelInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Positive;
use Symfony\Component\Validator\Constraints\Type;

class ChannelBasedRulePricing extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'entry_type' => ItemTotalConfigurationType::class,
'entry_type' => ChannelPriceType::class,
'entry_options' => function (ChannelInterface $channel) {
return [
'label' => $channel->getCode(),
Expand Down
52 changes: 52 additions & 0 deletions src/Form/Type/ChannelPriceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on [email protected].
*/

namespace BitBag\SyliusCatalogPlugin\Form\Type;

use Sylius\Bundle\MoneyBundle\Form\Type\MoneyType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Positive;
use Symfony\Component\Validator\Constraints\PositiveOrZero;
use Symfony\Component\Validator\Constraints\Type;

final class ChannelPriceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('amount', MoneyType::class, [
'label' => 'sylius.form.promotion_rule.item_total_configuration.amount',
'constraints' => [
new NotBlank(['groups' => ['sylius']]),
new Type(['type' => 'numeric', 'groups' => ['sylius']]),
new PositiveOrZero(['groups' => ['sylius']])
],
'currency' => $options['currency'],
])
;
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setRequired('currency')
->setAllowedTypes('currency', 'string')
;
}

public function getBlockPrefix(): string
{
return 'sylius_promotion_rule_item_total_configuration';
}
}
2 changes: 2 additions & 0 deletions src/Form/Type/PriceConfigurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Positive;
use Symfony\Component\Validator\Constraints\Valid;

final class PriceConfigurationType extends AbstractType
Expand Down Expand Up @@ -54,6 +55,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'label' => 'bitbag_sylius_catalog_plugin.ui.form.catalog.add_catalog_configuration',
'constraints' => [
new Valid(['groups' => ['sylius']]),
new Positive(['groups' => ['sylius']]),
],
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Type/ProductAssociationRuleChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public function getParent(): string

public function getBlockPrefix(): string
{
return 'bitbag_sylius_catalog_plugin_product_association_rule_choice';
return 'bitbag_sylius_catalog_plugin_catalog_product_rule_choice';
}
}
1 change: 1 addition & 0 deletions src/QueryBuilder/ProductQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function findMatchingProductsQuery(string $connectingRules, Collection $r
case RuleInterface::OR:
foreach ($subQueries as $subQuery) {
$query->addShould($subQuery);
$query->setMinimumShouldMatch(1);
}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use Sylius\Component\Registry\ServiceRegistry;
use Sylius\Component\Resource\Repository\RepositoryInterface;

final class ProductCatalogResolver implements ProductCatalogResolverInterface
final class CatalogsForProductResolver implements CatalogsForProductResolverInterface
{
/** @var RepositoryInterface */
private $catalogRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use BitBag\SyliusCatalogPlugin\Entity\CatalogInterface;
use Sylius\Component\Core\Model\ProductInterface;

interface ProductCatalogResolverInterface
interface CatalogsForProductResolverInterface
{
/**
* @return CatalogInterface[]
Expand Down
52 changes: 52 additions & 0 deletions src/Resolver/CompleteCatalogsForProductResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on [email protected].
*/

declare(strict_types=1);

namespace BitBag\SyliusCatalogPlugin\Resolver;

use BitBag\SyliusCatalogPlugin\Entity\CatalogInterface;
use Sylius\Component\Core\Model\ProductInterface;

final class CompleteCatalogsForProductResolver implements CatalogsForProductResolverInterface
{
/** @var CatalogsForProductResolverInterface */
private $wrappedResolver;

/** @var ProductsInsideCatalogResolverInterface */
private $catalogResolver;

public function __construct(CatalogsForProductResolverInterface $wrappedResolver, ProductsInsideCatalogResolverInterface $catalogResolver)
{
$this->wrappedResolver = $wrappedResolver;
$this->catalogResolver = $catalogResolver;
}

public function resolveProductCatalogs(ProductInterface $product, \DateTimeImmutable $dataTime): array
{
return
array_values(
array_filter(
array_map(
function (CatalogInterface $catalog) {
return [
'catalog' => $catalog,
'products' => $this->catalogResolver->findMatchingProducts($catalog),
];
},
$this->wrappedResolver->resolveProductCatalogs($product, $dataTime)
),
function (array $catalogData) {
return 0 < count($catalogData['products']);
}
)
)
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
use BitBag\SyliusCatalogPlugin\Entity\CatalogInterface;
use BitBag\SyliusCatalogPlugin\QueryBuilder\ProductQueryBuilderInterface;
use BitBag\SyliusCatalogPlugin\Repository\CatalogRepositoryInterface;
use BitBag\SyliusCatalogPlugin\Resolver\ProductCatalogResolverInterface;
use BitBag\SyliusCatalogPlugin\Resolver\CatalogsForProductResolverInterface;
use Elastica\Query\BoolQuery;
use Elastica\Query\Term;
use FOS\ElasticaBundle\Finder\PaginatedFinderInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;

final class ProductCatalogResolver implements ProductCatalogResolverInterface
final class CatalogsForProductResolver implements CatalogsForProductResolverInterface
{
/** @var RepositoryInterface */
private $catalogRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
use BitBag\SyliusCatalogPlugin\Entity\CatalogInterface;
use BitBag\SyliusCatalogPlugin\QueryBuilder\ProductQueryBuilderInterface;
use BitBag\SyliusCatalogPlugin\Resolver\ProductResolverInterface;
use BitBag\SyliusCatalogPlugin\Resolver\ProductsInsideCatalogResolverInterface;
use Elastica\Query\BoolQuery;
use FOS\ElasticaBundle\Finder\PaginatedFinderInterface;

class ProductResolver implements ProductResolverInterface
class ProductsInsideCatalogResolver implements ProductsInsideCatalogResolverInterface
{
/** @var ProductQueryBuilderInterface */
private $productQueryBuilder;
Expand All @@ -31,7 +32,7 @@ public function __construct(ProductQueryBuilderInterface $productQueryBuilder, P
$this->productFinder = $paginatedFinder;
}

public function findMatchingProducts(CatalogInterface $catalog)
public function findMatchingProducts(CatalogInterface $catalog): array
{
$query = new BoolQuery();
if ($catalog->getRules()->count()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Sylius\Bundle\ProductBundle\Doctrine\ORM\ProductRepository;
use Sylius\Component\Registry\ServiceRegistry;

class ProductResolver implements ProductResolverInterface
class ProductsInsideCatalogResolver implements ProductsInsideCatalogResolverInterface
{
/** @var ProductRepository */
private $productRepository;
Expand All @@ -31,7 +31,7 @@ public function __construct(ProductRepository $productRepository, ServiceRegistr
$this->productRepository = $productRepository;
}

public function findMatchingProducts(CatalogInterface $catalog)
public function findMatchingProducts(CatalogInterface $catalog): array
{
$connectingRules = $catalog->getConnectingRules();

Expand Down
Loading

0 comments on commit d039d4e

Please sign in to comment.