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 7c2b118 commit c7466a0
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 67 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
22 changes: 11 additions & 11 deletions spec/EventSubscriber/TableRateDeleteSubscriberSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,46 @@

class TableRateDeleteSubscriberSpec extends ObjectBehavior
{
function let(ShippingMethodRepositoryInterface $shippingMethodRepository)
public function let(ShippingMethodRepositoryInterface $shippingMethodRepository): void
{
$this->beConstructedWith($shippingMethodRepository);
}

function it_is_initializable()
public function it_is_initializable(): void
{
$this->shouldHaveType(TableRateDeleteSubscriber::class);
}

function it_should_implement_event_subscriber_interface()
public function it_should_implement_event_subscriber_interface(): void
{
$this->shouldImplement(EventSubscriberInterface::class);
}

function it_should_subscribe_to_table_rate_pre_delete_event()
public function it_should_subscribe_to_table_rate_pre_delete_event(): void
{
self::getSubscribedEvents()->shouldReturn(
['webgriffe.shipping_table_rate.pre_delete' => 'onTableRatePreDelete']
);
}

function it_should_stop_event_if_table_rate_is_already_in_use(
public function it_should_stop_event_if_table_rate_is_already_in_use(
ResourceControllerEvent $event,
ShippingMethodRepositoryInterface $shippingMethodRepository
) {
): void {
$tableRate = new ShippingTableRate();
$tableRate->setCode('TABLE_RATE');
$anotherTableRate = new ShippingTableRate();
$anotherTableRate->setCode('ANOTHER_RATE');
$shippingMethod1 = new ShippingMethod();
$shippingMethod1->setCode('METHOD1');
$shippingMethod1->setConfiguration(['WEB-US' => ['table_rate' => $tableRate]]);
$shippingMethod1->setConfiguration(['WEB-US' => ['table_rate' => 'TABLE_RATE']]);
$shippingMethod2 = new ShippingMethod();
$shippingMethod2->setCode('METHOD2');
$shippingMethod2->setConfiguration(['WEB-US' => ['table_rate' => $anotherTableRate]]);
$shippingMethod2->setConfiguration(['WEB-US' => ['table_rate' => 'ANOTHER_RATE']]);
$shippingMethod3 = new ShippingMethod();
$shippingMethod3->setCode('METHOD3');
$shippingMethod3->setConfiguration(
['WEB-US' => ['table_rate' => $anotherTableRate], 'WEB-EU' => ['table_rate' => $tableRate]]
['WEB-US' => ['table_rate' => 'ANOTHER_RATE'], 'WEB-EU' => ['table_rate' => 'TABLE_RATE']]
);
$shippingMethodRepository
->findBy(['calculator' => TableRateShippingCalculator::TYPE])
Expand All @@ -76,11 +76,11 @@ function it_should_stop_event_if_table_rate_is_already_in_use(
$this->onTableRatePreDelete($event);
}

function it_should_not_stop_event_if_table_rate_is_not_in_use(
public function it_should_not_stop_event_if_table_rate_is_not_in_use(
ShippingTableRate $shippingTableRate,
ResourceControllerEvent $event,
ShippingMethodRepositoryInterface $shippingMethodRepository
) {
): void {
$event->getSubject()->willReturn($shippingTableRate);
$shippingMethodRepository
->findBy(['calculator' => TableRateShippingCalculator::TYPE])
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
6 changes: 3 additions & 3 deletions src/EventSubscriber/TableRateDeleteSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function onTableRatePreDelete(ResourceControllerEvent $event): void
/** @var ShippingMethod $shippingMethod */
foreach ($shippingMethods as $shippingMethod) {
foreach ($shippingMethod->getConfiguration() as $channelConfiguration) {
/** @var ShippingTableRate|null $channelTableRate */
$channelTableRate = $channelConfiguration['table_rate'] ?? null;
if ($channelTableRate !== null && $channelTableRate->getCode() === $shippingTableRate->getCode()) {
/** @var string|null $channelTableRateCode */
$channelTableRateCode = $channelConfiguration['table_rate'] ?? null;
if ($channelTableRateCode !== null && $channelTableRateCode === $shippingTableRate->getCode()) {
$foundMethods[] = $shippingMethod->getCode();
}
}
Expand Down
68 changes: 64 additions & 4 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 @@ -38,8 +47,8 @@ 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 +63,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()];
}
}
Loading

0 comments on commit c7466a0

Please sign in to comment.