Skip to content

Commit

Permalink
bug #13769 [Core][Shipping] Fix estimated shipping costs (coldic3, lc…
Browse files Browse the repository at this point in the history
…hrusciel)

This PR was merged into the 1.10 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | 1.10
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | fixes Sylius/Sylius#12554, Sylius/Sylius#13123, related to Sylius/Sylius#13126
| License         | MIT



Commits
-------

2863527a9dc012b92dceb8fe6f067db59c5f4879 [Core][Shipping][Behat] Add estimated shipping cost scenarios
caa04093d1ec25bce846395f0472dc5cbd1dcfab [Core][Shipping] Change order processing priorities
d6dbbcdd785236c85a00416ecca5f19b9b68066a [Core][Shipping] Update UPGRADE-1.10.md
9d9f8d9b18573d361672b77dc2a93b08728be85b [Core][Shipping][Behat] Update estimated shipping cost scenarios
04ab77c3ad75d6f2879e0486d167f91e02b2a52b [Core][Configuration] Add possibilty to change priorities based on configuration
225557e9c7f2eb01934c88503ce997049a34859b [Core][Configuration] Rename flag that changes priorities in order processing
12ba23573f24c125209a69885953a207398ff4c1 [Core][Configuration] Fix SyliusCoreConfigurationTest
3333d077aca47ce0f31333c01ce6fdd9b3caa833 [Core][Configuration] Refactor test cases
9fcae9e31fa9804576aa98bc6e2de2d2497ed5e1 [Core][Configuration] Rename flag that changes priorities in order processing
aac69ca011fd0dd92397f460fecdc5995634afdd [Core][Shipping] Update UPGRADE-1.10.md
d790944fe36a02d356a001c9bef4e16ae6da372a [Core][Configuration] Rename flag that changes priorities in order processing
  • Loading branch information
GSadee authored Mar 18, 2022
2 parents ce21304 + 08f07cc commit 72549ce
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 24 deletions.
3 changes: 2 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->children()
->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end()
->booleanNode('prepend_doctrine_migrations')->defaultTrue()->end()
->booleanNode('process_shipments_before_recalculating_prices')->defaultFalse()->end()
->end()
;

Expand Down Expand Up @@ -87,7 +88,7 @@ private function addResourcesSection(ArrayNodeDefinition $node): void
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(AvatarImage::class)->cannotBeEmpty()->end()
->scalarNode('interface')->defaultValue(AvatarImageInterface::class)->cannotBeEmpty()->end()
->scalarNode('interface')->defaultValue(AvatarImageInterface::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(AvatarImageRepository::class)->cannotBeEmpty()->end()
->scalarNode('factory')->defaultValue(Factory::class)->end()
->end()
Expand Down
28 changes: 28 additions & 0 deletions DependencyInjection/SyliusCoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sylius\Bundle\ResourceBundle\DependencyInjection\Extension\AbstractResourceExtension;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

Expand Down Expand Up @@ -57,6 +58,13 @@ public function load(array $configs, ContainerBuilder $container): void
if ('test' === $env || 'test_cached' === $env) {
$loader->load('test_services.xml');
}

if ($config['process_shipments_before_recalculating_prices']) {
$this->switchOrderProcessorsPriorities(
$container->getDefinition('sylius.order_processing.order_shipment_processor'),
$container->getDefinition('sylius.order_processing.order_prices_recalculator')
);
}
}

public function prepend(ContainerBuilder $container): void
Expand Down Expand Up @@ -135,4 +143,24 @@ private function prependJmsSerializerIfAdminApiBundleIsNotPresent(ContainerBuild
],
]);
}

private function switchOrderProcessorsPriorities(
Definition $firstServiceDefinition,
Definition $secondServiceDefinition
) {
$firstServicePriority = $firstServiceDefinition->getTag('sylius.order_processor')[0]['priority'];
$secondServicePriority = $secondServiceDefinition->getTag('sylius.order_processor')[0]['priority'];

$firstServiceDefinition->clearTag('sylius.order_processor');
$secondServiceDefinition->clearTag('sylius.order_processor');

$firstServiceDefinition->addTag(
'sylius.order_processor',
['priority' => $secondServicePriority]
);
$secondServiceDefinition->addTag(
'sylius.order_processor',
['priority' => $firstServicePriority]
);
}
}
10 changes: 5 additions & 5 deletions Resources/config/services/order_processing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
<tag name="sylius.order_processor" priority="60"/>
</service>

<service id="sylius.order_processing.order_prices_recalculator" class="Sylius\Component\Core\OrderProcessing\OrderPricesRecalculator">
<argument type="service" id="sylius.calculator.product_variant_price" />
<tag name="sylius.order_processor" priority="50"/>
</service>

<service id="sylius.order_processing.order_shipment_processor" class="Sylius\Component\Core\OrderProcessing\OrderShipmentProcessor">
<argument type="service" id="sylius.shipping_method_resolver.default" />
<argument type="service" id="sylius.factory.shipment" />
<argument type="service" id="sylius.shipping_methods_resolver" />
<tag name="sylius.order_processor" priority="50"/>
</service>

<service id="sylius.order_processing.order_prices_recalculator" class="Sylius\Component\Core\OrderProcessing\OrderPricesRecalculator">
<argument type="service" id="sylius.calculator.product_variant_price" />
<tag name="sylius.order_processor" priority="40"/>
</service>

Expand Down
62 changes: 62 additions & 0 deletions Tests/DependencyInjection/SyliusCoreConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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\Tests\DependencyInjection;

use Matthias\SymfonyConfigTest\Partial\PartialProcessor;
use Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait;
use PHPUnit\Framework\TestCase;
use Sylius\Bundle\CoreBundle\DependencyInjection\Configuration;
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;

final class SyliusCoreConfigurationTest extends TestCase
{
use ConfigurationTestCaseTrait;

/** @test */
public function it_does_not_bring_back_previous_priorities_for_order_processing_by_default(): void
{
$this->assertProcessedConfigurationEquals(
[[]],
['process_shipments_before_recalculating_prices' => false],
'process_shipments_before_recalculating_prices',
);
}

/** @test */
public function it_allows_to_define_that_previous_priorities_should_be_brought_back_for_order_processing(): void
{
$this->assertProcessedConfigurationEquals(
[['process_shipments_before_recalculating_prices' => true]],
['process_shipments_before_recalculating_prices' => true],
'process_shipments_before_recalculating_prices',
);
}

/** @test */
public function it_does_not_allow_to_define_previous_priorities_with_values_other_then_bool(): void
{
$this->expectException(InvalidTypeException::class);

(new PartialProcessor())->processConfiguration(
$this->getConfiguration(),
'process_shipments_before_recalculating_prices',
[['process_shipments_before_recalculating_prices' => 'yolo']]
);
}

protected function getConfiguration(): Configuration
{
return new Configuration();
}
}
53 changes: 35 additions & 18 deletions Tests/DependencyInjection/SyliusCoreExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,71 @@

use Doctrine\Bundle\MigrationsBundle\DependencyInjection\DoctrineMigrationsExtension;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\DefinitionHasTagConstraint;
use Sylius\Bundle\CoreBundle\DependencyInjection\SyliusCoreExtension;
use SyliusLabs\DoctrineMigrationsExtraBundle\DependencyInjection\SyliusLabsDoctrineMigrationsExtraExtension;

final class SyliusCoreExtensionTest extends AbstractExtensionTestCase
{
/**
* @test
*/
/** @test */
public function it_brings_back_previous_order_processing_priorities(): void
{
$this->container->setParameter('kernel.environment', 'dev');

$this->load(['process_shipments_before_recalculating_prices' => true]);

$this->assertThat(
$this->container->findDefinition('sylius.order_processing.order_prices_recalculator'),
new DefinitionHasTagConstraint('sylius.order_processor', ['priority' => 40])
);

$this->assertThat(
$this->container->findDefinition('sylius.order_processing.order_prices_recalculator'),
$this->logicalNot(new DefinitionHasTagConstraint('sylius.order_processor', ['priority' => 50]))
);

$this->assertThat(
$this->container->findDefinition('sylius.order_processing.order_shipment_processor'),
new DefinitionHasTagConstraint('sylius.order_processor', ['priority' => 50])
);

$this->assertThat(
$this->container->findDefinition('sylius.order_processing.order_shipment_processor'),
$this->logicalNot(new DefinitionHasTagConstraint('sylius.order_processor', ['priority' => 40]))
);
}

/** @test */
public function it_autoconfigures_prepending_doctrine_migrations_with_proper_migrations_path_for_test_env(): void
{
$this->testPrependingDoctrineMigrations('test');
}

/**
* @test
*/
/** @test */
public function it_autoconfigures_prepending_doctrine_migrations_with_proper_migrations_path_for_test_cached_env(): void
{
$this->testPrependingDoctrineMigrations('test_cached');
}

/**
* @test
*/
/** @test */
public function it_autoconfigures_prepending_doctrine_migrations_with_proper_migrations_path_for_dev_env(): void
{
$this->testPrependingDoctrineMigrations('dev');
}

/**
* @test
*/
/** @test */
public function it_does_not_autoconfigure_prepending_doctrine_migrations_if_it_is_disabled_for_test_env(): void
{
$this->testNotPrependingDoctrineMigrations('test');
}

/**
* @test
*/
/** @test */
public function it_does_not_autoconfigure_prepending_doctrine_migrations_if_it_is_disabled_for_test_cached_env(): void
{
$this->testNotPrependingDoctrineMigrations('test_cached');
}

/**
* @test
*/
/** @test */
public function it_does_not_autoconfigure_prepending_doctrine_migrations_if_it_is_disabled_for_dev_env(): void
{
$this->testNotPrependingDoctrineMigrations('dev');
Expand Down

0 comments on commit 72549ce

Please sign in to comment.