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 3a415d7
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .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
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
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
65 changes: 60 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,51 @@ public function getBlockPrefix(): string
{
return 'webgriffe_sylius_table_rate_shipping_plugin_calculator_table_rate';
}

/**
* @param \Traversable<mixed, FormInterface> $forms
*/
public function mapDataToForms(mixed $viewData, \Traversable $forms): void

Check failure on line 69 in src/Form/Type/Shipping/Calculator/TableRateConfigurationType.php

View workflow job for this annotation

GitHub Actions / Sylius 1.13.3, PHP 8.1, Symfony 5.4.*, MySQL 8.0

Parameter #2 $forms (Traversable<mixed, Symfony\Component\Form\FormInterface>) of method Webgriffe\SyliusTableRateShippingPlugin\Form\Type\Shipping\Calculator\TableRateConfigurationType::mapDataToForms() should be contravariant with parameter $forms (iterable<Symfony\Component\Form\FormInterface>&Traversable) of method Symfony\Component\Form\DataMapperInterface::mapDataToForms()

Check failure on line 69 in src/Form/Type/Shipping/Calculator/TableRateConfigurationType.php

View workflow job for this annotation

GitHub Actions / Sylius 1.13.3, PHP 8.2, Symfony 5.4.*, MySQL 8.0

Parameter #2 $forms (Traversable<mixed, Symfony\Component\Form\FormInterface>) of method Webgriffe\SyliusTableRateShippingPlugin\Form\Type\Shipping\Calculator\TableRateConfigurationType::mapDataToForms() should be contravariant with parameter $forms (iterable<Symfony\Component\Form\FormInterface>&Traversable) of method Symfony\Component\Form\DataMapperInterface::mapDataToForms()

Check failure on line 69 in src/Form/Type/Shipping/Calculator/TableRateConfigurationType.php

View workflow job for this annotation

GitHub Actions / Sylius 1.13.3, PHP 8.2, Symfony 5.4.*, MySQL 8.0

Parameter #2 $forms (Traversable<mixed, Symfony\Component\Form\FormInterface>) of method Webgriffe\SyliusTableRateShippingPlugin\Form\Type\Shipping\Calculator\TableRateConfigurationType::mapDataToForms() should be contravariant with parameter $forms (iterable<Symfony\Component\Form\FormInterface>&Traversable) of method Symfony\Component\Form\DataMapperInterface::mapDataToForms()

Check failure on line 69 in src/Form/Type/Shipping/Calculator/TableRateConfigurationType.php

View workflow job for this annotation

GitHub Actions / Sylius 1.13.3, PHP 8.3, Symfony 5.4.*, MySQL 8.0

Parameter #2 $forms (Traversable<mixed, Symfony\Component\Form\FormInterface>) of method Webgriffe\SyliusTableRateShippingPlugin\Form\Type\Shipping\Calculator\TableRateConfigurationType::mapDataToForms() should be contravariant with parameter $forms (iterable<Symfony\Component\Form\FormInterface>&Traversable) of method Symfony\Component\Form\DataMapperInterface::mapDataToForms()

Check failure on line 69 in src/Form/Type/Shipping/Calculator/TableRateConfigurationType.php

View workflow job for this annotation

GitHub Actions / Sylius 1.13.3, PHP 8.1, Symfony 5.4.*, MySQL 8.0

Parameter #2 $forms (Traversable<mixed, Symfony\Component\Form\FormInterface>) of method Webgriffe\SyliusTableRateShippingPlugin\Form\Type\Shipping\Calculator\TableRateConfigurationType::mapDataToForms() should be contravariant with parameter $forms (iterable<Symfony\Component\Form\FormInterface>&Traversable) of method Symfony\Component\Form\DataMapperInterface::mapDataToForms()

Check failure on line 69 in src/Form/Type/Shipping/Calculator/TableRateConfigurationType.php

View workflow job for this annotation

GitHub Actions / Sylius 1.13.3, PHP 8.3, Symfony 5.4.*, MySQL 8.0

Parameter #2 $forms (Traversable<mixed, Symfony\Component\Form\FormInterface>) of method Webgriffe\SyliusTableRateShippingPlugin\Form\Type\Shipping\Calculator\TableRateConfigurationType::mapDataToForms() should be contravariant with parameter $forms (iterable<Symfony\Component\Form\FormInterface>&Traversable) of method Symfony\Component\Form\DataMapperInterface::mapDataToForms()
{
// 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]]));
}

/**
* @param \Traversable<mixed, FormInterface> $forms
*/
public function mapFormsToData(\Traversable $forms, mixed &$viewData): void

Check failure on line 95 in src/Form/Type/Shipping/Calculator/TableRateConfigurationType.php

View workflow job for this annotation

GitHub Actions / Sylius 1.13.3, PHP 8.1, Symfony 5.4.*, MySQL 8.0

Parameter #1 $forms (Traversable<mixed, Symfony\Component\Form\FormInterface>) of method Webgriffe\SyliusTableRateShippingPlugin\Form\Type\Shipping\Calculator\TableRateConfigurationType::mapFormsToData() should be contravariant with parameter $forms (iterable<Symfony\Component\Form\FormInterface>&Traversable) of method Symfony\Component\Form\DataMapperInterface::mapFormsToData()

Check failure on line 95 in src/Form/Type/Shipping/Calculator/TableRateConfigurationType.php

View workflow job for this annotation

GitHub Actions / Sylius 1.13.3, PHP 8.2, Symfony 5.4.*, MySQL 8.0

Parameter #1 $forms (Traversable<mixed, Symfony\Component\Form\FormInterface>) of method Webgriffe\SyliusTableRateShippingPlugin\Form\Type\Shipping\Calculator\TableRateConfigurationType::mapFormsToData() should be contravariant with parameter $forms (iterable<Symfony\Component\Form\FormInterface>&Traversable) of method Symfony\Component\Form\DataMapperInterface::mapFormsToData()

Check failure on line 95 in src/Form/Type/Shipping/Calculator/TableRateConfigurationType.php

View workflow job for this annotation

GitHub Actions / Sylius 1.13.3, PHP 8.2, Symfony 5.4.*, MySQL 8.0

Parameter #1 $forms (Traversable<mixed, Symfony\Component\Form\FormInterface>) of method Webgriffe\SyliusTableRateShippingPlugin\Form\Type\Shipping\Calculator\TableRateConfigurationType::mapFormsToData() should be contravariant with parameter $forms (iterable<Symfony\Component\Form\FormInterface>&Traversable) of method Symfony\Component\Form\DataMapperInterface::mapFormsToData()

Check failure on line 95 in src/Form/Type/Shipping/Calculator/TableRateConfigurationType.php

View workflow job for this annotation

GitHub Actions / Sylius 1.13.3, PHP 8.3, Symfony 5.4.*, MySQL 8.0

Parameter #1 $forms (Traversable<mixed, Symfony\Component\Form\FormInterface>) of method Webgriffe\SyliusTableRateShippingPlugin\Form\Type\Shipping\Calculator\TableRateConfigurationType::mapFormsToData() should be contravariant with parameter $forms (iterable<Symfony\Component\Form\FormInterface>&Traversable) of method Symfony\Component\Form\DataMapperInterface::mapFormsToData()

Check failure on line 95 in src/Form/Type/Shipping/Calculator/TableRateConfigurationType.php

View workflow job for this annotation

GitHub Actions / Sylius 1.13.3, PHP 8.1, Symfony 5.4.*, MySQL 8.0

Parameter #1 $forms (Traversable<mixed, Symfony\Component\Form\FormInterface>) of method Webgriffe\SyliusTableRateShippingPlugin\Form\Type\Shipping\Calculator\TableRateConfigurationType::mapFormsToData() should be contravariant with parameter $forms (iterable<Symfony\Component\Form\FormInterface>&Traversable) of method Symfony\Component\Form\DataMapperInterface::mapFormsToData()

Check failure on line 95 in src/Form/Type/Shipping/Calculator/TableRateConfigurationType.php

View workflow job for this annotation

GitHub Actions / Sylius 1.13.3, PHP 8.3, Symfony 5.4.*, MySQL 8.0

Parameter #1 $forms (Traversable<mixed, Symfony\Component\Form\FormInterface>) of method Webgriffe\SyliusTableRateShippingPlugin\Form\Type\Shipping\Calculator\TableRateConfigurationType::mapFormsToData() should be contravariant with parameter $forms (iterable<Symfony\Component\Form\FormInterface>&Traversable) of method Symfony\Component\Form\DataMapperInterface::mapFormsToData()
{
/** @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

0 comments on commit 3a415d7

Please sign in to comment.