Skip to content

Commit

Permalink
Fix compatibility with Sylius 1.13
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Jul 29, 2024
1 parent 9b14c48 commit d4d551c
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 54 deletions.
11 changes: 0 additions & 11 deletions .github/actions/roave-bc-check/Dockerfile

This file was deleted.

5 changes: 0 additions & 5 deletions .github/actions/roave-bc-check/action.yml

This file was deleted.

27 changes: 21 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
matrix:
php: [ "8.1", "8.2", "8.3" ]
symfony: [ "5.4.*", "^6.4" ]
sylius: ["1.12.16", "1.13.1"]
sylius: ["1.13.3"]
node: ["18.x"]
mysql: ["8.0"]

Expand Down Expand Up @@ -192,9 +192,24 @@ jobs:
roave_bc_check:
name: Roave BC Check
runs-on: ubuntu-latest
env:
PHP_VERSION: 8.2
steps:
- uses: actions/checkout@master
- name: fetch tags
run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- name: Roave BC Check
uses: ./.github/actions/roave-bc-check
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ env.PHP_VERSION }}"
extensions: intl
tools: flex,symfony
coverage: none

- name: Install roave/backward-compatibility-check.
run: composer require --dev roave/backward-compatibility-check --no-plugins

- name: Run roave/backward-compatibility-check.
run: vendor/bin/roave-backward-compatibility-check --format=github-actions
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"license": "MIT",
"require": {
"php": "^8.1",
"sylius/sylius": "^1.12",
"sylius/sylius": "^1.13.3",
"symfony/webpack-encore-bundle": "^1.15"
},
"require-dev": {
Expand Down
16 changes: 8 additions & 8 deletions features/managing_table_rates.feature
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Feature: Managing table rates
@ui
Scenario: Updating a table rate
Given the store has a shipping table rate "East Coast Rates" for currency "USD"
And this shipping table rate has a rate "$5" for shipments up to 1000 kg
And this shipping table rate has a rate "$5.00" for shipments up to 1000 kg
And I want to modify the "East Coast Rates" table rate
When I change its name to "Edited Rates"
And I save my changes
Expand All @@ -52,8 +52,8 @@ Feature: Managing table rates
Scenario: Adding weight rates into a table rate
Given the store has a shipping table rate "East Coast Rates" for currency "USD"
And I want to modify the "East Coast Rates" table rate
When I add a new rate of "$5" for shipments up to 5 kg
And I add a new rate of "$10" for shipments up to 20 kg
When I add a new rate of "$5.00" for shipments up to 5 kg
And I add a new rate of "$10.00" for shipments up to 20 kg
And I save my changes
Then I should be notified that it has been successfully edited
And this shipping table rate should have 2 rates
Expand All @@ -63,21 +63,21 @@ Feature: Managing table rates
When I try to add a new shipping table
And I specify its code as "VALIDATE_ME"
And I specify its currency as "USD"
And I add a new rate of "$10" for shipments up to 20 kg
And I add a new rate of "$10.00" for shipments up to 20 kg
But I do not specify its name
And I try to add it
Then I should be notified that name is required
When I try to add a new shipping table
And I specify its name as "Validate Me"
And I specify its currency as "USD"
And I add a new rate of "$10" for shipments up to 20 kg
And I add a new rate of "$10.00" for shipments up to 20 kg
But I do not specify its code
And I try to add it
Then I should be notified that code is required
When I try to add a new shipping table
And I specify its code as "VALIDATE_ME"
And I specify its name as "Validate Me"
And I add a new rate of "$10" for shipments up to 20 kg
And I add a new rate of "$10.00" for shipments up to 20 kg
But I do not specify its currency
And I try to add it
Then I should be notified that currency is required
Expand All @@ -92,7 +92,7 @@ Feature: Managing table rates
@ui
Scenario: Trying to change table rate code (even if its field is disabled)
Given the store has a shipping table rate "East Coast Rates" for currency "USD"
And this shipping table rate has a rate "$5" for shipments up to 1000 kg
And this shipping table rate has a rate "$5.00" for shipments up to 1000 kg
And I want to modify the "East Coast Rates" table rate
Then the code field should be disabled
When I change its code to "ANOTHER_CODE"
Expand All @@ -103,7 +103,7 @@ Feature: Managing table rates
Scenario: Trying to change table rate currency (even if its field is disabled)
Given the store also operates on another channel named "Europe" in "EUR" currency
And the store has a shipping table rate "East Coast Rates" for currency "USD"
And this shipping table rate has a rate "$5" for shipments up to 1000 kg
And this shipping table rate has a rate "$5.00" for shipments up to 1000 kg
And I want to modify the "East Coast Rates" table rate
Then the currency field should be disabled
When I change its currency to "EUR"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Feature: Seeing correct shipping fees for table rate based shipping methods
Given the store operates on a single channel in "United States"
And the store has a product "Bottle of water" which weights 1 kg
And the store has a shipping table rate "Weight-based" for currency "USD"
And it has a rate "$5" for shipments up to 5 kg
And it has a rate "$10" for shipments up to 20 kg
And it has a rate "$5.00" for shipments up to 5 kg
And it has a rate "$10.00" for shipments up to 20 kg
And the store has "Basic" shipping method using "Weight-based" table rate for "United States" channel
And I am a logged in customer

Expand Down
10 changes: 5 additions & 5 deletions spec/Resolver/TableRateResolverSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@

class TableRateResolverSpec extends ObjectBehavior
{
function let(RepositoryInterface $tableRateRepository): void
public function let(RepositoryInterface $tableRateRepository): void
{
$this->beConstructedWith($tableRateRepository);
}

function it_should_implement_table_rate_resolver_interface(): void
public function it_should_implement_table_rate_resolver_interface(): void
{
$this->shouldImplement(TableRateResolverInterface::class);
}

function it_should_resolve_table_rate_from_shipment(
public function it_should_resolve_table_rate_from_shipment(
ShipmentInterface $shipment,
OrderInterface $order,
ChannelInterface $channel,
Expand All @@ -38,12 +38,12 @@ function it_should_resolve_table_rate_from_shipment(
$channel->getCode()->willReturn('CHANNEL_CODE');
$tableRate->getCode()->willReturn('TABLE_RATE_CODE');
$tableRateRepository->findOneBy(['code' => 'TABLE_RATE_CODE'])->willReturn($tableRate);
$calculatorConfig = ['CHANNEL_CODE' => ['table_rate' => $tableRate]];
$calculatorConfig = ['CHANNEL_CODE' => ['table_rate' => 'TABLE_RATE_CODE']];

$this->resolve($shipment, $calculatorConfig)->shouldReturn($tableRate);
}

function it_throws_a_missing_channel_configuration_exception_if_the_order_channel_is_not_configured(
public function it_throws_a_missing_channel_configuration_exception_if_the_order_channel_is_not_configured(
ShipmentInterface $shipment,
OrderInterface $order,
ChannelInterface $channel,
Expand Down
69 changes: 64 additions & 5 deletions src/Form/Type/Shipping/Calculator/TableRateConfigurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
use Webgriffe\SyliusTableRateShippingPlugin\Entity\ShippingTableRate;

final class TableRateConfigurationType extends AbstractType
final class TableRateConfigurationType extends AbstractType implements DataMapperInterface
{
public function __construct(
private RepositoryInterface $tableRateRepository,
) {
}

public const TABLE_RATE_FIELD_NAME = 'table_rate';

public function buildForm(FormBuilderInterface $builder, array $options): void
Expand All @@ -26,7 +35,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
EntityType::class,
[
'label' => $messagesNamespace . 'table_rate.label',
'placeholder' => '' . $messagesNamespace . 'table_rate.placeholder',
'placeholder' => $messagesNamespace . 'table_rate.placeholder',
'class' => ShippingTableRate::class,
'query_builder' => function (EntityRepository $entityRepository) use ($currency): QueryBuilder {
return $entityRepository
Expand All @@ -36,10 +45,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
;
},
'choice_label' => 'name',
'choice_value' => 'code',
'constraints' => [new NotBlank(['groups' => ['sylius']])],
]
);
],
)->setDataMapper($this);
}

public function configureOptions(OptionsResolver $resolver): void
Expand All @@ -54,4 +62,55 @@ public function getBlockPrefix(): string
{
return 'webgriffe_sylius_table_rate_shipping_plugin_calculator_table_rate';
}

/**
* @psalm-suppress MoreSpecificImplementedParamType
* @psalm-suppress PossiblyInvalidArgument
* @psalm-suppress UnnecessaryVarAnnotation
*/
public function mapDataToForms(mixed $viewData, \Traversable $forms): void
{
// there is no data yet, so nothing to prepopulate
if (null === $viewData) {
return;
}

// invalid data type
if (!is_array($viewData)) {
throw new UnexpectedTypeException($viewData, 'array');
}

/** @var FormInterface[] $forms */
$forms = iterator_to_array($forms);

if (!array_key_exists(self::TABLE_RATE_FIELD_NAME, $viewData)) {
return;
}

// initialize form field values
$forms['table_rate']->setData($this->tableRateRepository->findOneBy(['code' => $viewData[self::TABLE_RATE_FIELD_NAME]]));
}

/**
* @psalm-suppress MoreSpecificImplementedParamType
* @psalm-suppress PossiblyInvalidArgument
* @psalm-suppress UnnecessaryVarAnnotation
*/
public function mapFormsToData(\Traversable $forms, mixed &$viewData): void
{
/** @var FormInterface[] $forms */
$forms = iterator_to_array($forms);

// as data is passed by reference, overriding it will change it in
// the form object as well
// beware of type inconsistency, see caution below
$tableRateSelected = $forms[self::TABLE_RATE_FIELD_NAME]->getData();
if (!$tableRateSelected instanceof ShippingTableRate) {
$viewData = [];

return;
}

$viewData = [self::TABLE_RATE_FIELD_NAME => $tableRateSelected->getCode()];
}
}
16 changes: 8 additions & 8 deletions src/Resolver/TableRateResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function resolve(ShipmentInterface $shipment, array $calculatorConfig): S
$channel = $order->getChannel();
if (null === $channel) {
throw new \RuntimeException(
'Cannot resolve a table rate, there\'s no channel for this shipment\'s order.'
'Cannot resolve a table rate, there\'s no channel for this shipment\'s order.',
);
}
$channelCode = $channel->getCode();
Expand All @@ -40,23 +40,23 @@ public function resolve(ShipmentInterface $shipment, array $calculatorConfig): S
throw new MissingChannelConfigurationException(
sprintf(
'This shipment has no configuration for channel "%s".',
$channel->getName()
)
$channel->getName(),
),
);
}

throw new MissingChannelConfigurationException(
sprintf(
'Shipping method "%s" has no configuration for channel "%s".',
$shippingMethod->getName(),
$channel->getName()
)
$channel->getName(),
),
);
}

/** @var ShippingTableRate $tableRate */
$tableRate = $calculatorConfig[$channelCode][TableRateConfigurationType::TABLE_RATE_FIELD_NAME];
$tableRate = $this->tableRateRepository->findOneBy(['code' => $tableRate->getCode()]);
/** @var string $tableRateCode */
$tableRateCode = $calculatorConfig[$channelCode][TableRateConfigurationType::TABLE_RATE_FIELD_NAME];
$tableRate = $this->tableRateRepository->findOneBy(['code' => $tableRateCode]);
Assert::isInstanceOf($tableRate, ShippingTableRate::class);

return $tableRate;
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ services:
- { name: form.type }

Webgriffe\SyliusTableRateShippingPlugin\Form\Type\Shipping\Calculator\TableRateConfigurationType:
arguments:
- '@webgriffe.repository.shipping_table_rate'
tags:
- { name: form.type }

Expand Down
2 changes: 1 addition & 1 deletion tests/Application/config/routes/sylius_shop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ sylius_shop_default_locale:
path: /
methods: [GET]
defaults:
_controller: sylius.controller.shop.locale_switch:switchAction
_controller: sylius.controller.shop.locale_switch::switchAction
2 changes: 1 addition & 1 deletion tests/Behat/Context/Setup/ShippingTableRateContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function theStoreHasShippingMethodUsingTableRateForChannel(
'zone' => $this->getShippingZone(),
'calculator' => [
'type' => TableRateShippingCalculator::TYPE,
'configuration' => [$channel->getCode() => ['table_rate' => $shippingTableRate]],
'configuration' => [$channel->getCode() => [TableRateShippingCalculator::TYPE => $shippingTableRate->getCode()]],
],
'channels' => [$channel],
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public function iShouldBeAbleToChooseOnlyTheTableRateForTheChannel(
/** @var NodeElement[] $options */
$options = $this->updatePage->getTableRateOptions($channel->getCode());
Assert::count($options, 1);
Assert::eq($options[0]->getAttribute('value'), $shippingTableRate->getCode());
Assert::eq($options[0]->getAttribute('value'), $shippingTableRate->getId());
}
}

0 comments on commit d4d551c

Please sign in to comment.