From 6e242905b13830a5ce42e482062cb3fd0d35ff80 Mon Sep 17 00:00:00 2001 From: SirDomin Date: Mon, 21 Jun 2021 15:15:29 +0200 Subject: [PATCH] revert to decorating factory --- spec/Factory/ShopBillingDataFactorySpec.php | 29 ++++++++++++++------- src/Factory/ShopBillingDataFactory.php | 18 ++++++++++--- src/Resources/config/app/config.yml | 1 - src/Resources/config/services/factories.xml | 9 +++++++ tests/Application/var/.gitignore | 0 5 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 tests/Application/var/.gitignore diff --git a/spec/Factory/ShopBillingDataFactorySpec.php b/spec/Factory/ShopBillingDataFactorySpec.php index dc0986be4..5f7965ab3 100644 --- a/spec/Factory/ShopBillingDataFactorySpec.php +++ b/spec/Factory/ShopBillingDataFactorySpec.php @@ -14,25 +14,36 @@ namespace spec\Sylius\RefundPlugin\Factory; use PhpSpec\ObjectBehavior; -use Sylius\Component\Resource\Exception\UnsupportedMethodException; -use Sylius\RefundPlugin\Entity\ShopBillingData; +use Sylius\Component\Resource\Factory\FactoryInterface; +use Sylius\RefundPlugin\Entity\ShopBillingDataInterface; use Sylius\RefundPlugin\Factory\ShopBillingDataFactoryInterface; -class ShopBillingDataFactorySpec extends ObjectBehavior +final class ShopBillingDataFactorySpec extends ObjectBehavior { + public function let(FactoryInterface $shopBillingDataFactory): void + { + $this->beConstructedWith($shopBillingDataFactory); + } + public function it_implements_shop_billing_data_factory_interface(): void { $this->shouldImplement(ShopBillingDataFactoryInterface::class); } - public function it_doesnt_support_create_new_method(): void - { - $this->shouldThrow(UnsupportedMethodException::class)->during('createNew'); + public function it_creates_new_shop_billing_data( + FactoryInterface $shopBillingDataFactory, + ShopBillingDataInterface $shopBillingData + ): void { + $shopBillingDataFactory->createNew()->willReturn($shopBillingData); + + $this->createNew()->shouldReturn($shopBillingData); } - public function it_creates_new_shop_billing_data_with_data(): void - { - $shopBillingData = new ShopBillingData(); + public function it_creates_new_shop_billing_data_with_data( + ShopBillingDataInterface $shopBillingData, + FactoryInterface $shopBillingDataFactory + ): void { + $shopBillingDataFactory->createNew()->willReturn($shopBillingData); $shopBillingData->setCompany('Needful Things'); $shopBillingData->setTaxId('000222'); diff --git a/src/Factory/ShopBillingDataFactory.php b/src/Factory/ShopBillingDataFactory.php index 3e40113b0..9cee2db8b 100644 --- a/src/Factory/ShopBillingDataFactory.php +++ b/src/Factory/ShopBillingDataFactory.php @@ -13,15 +13,25 @@ namespace Sylius\RefundPlugin\Factory; -use Sylius\Component\Resource\Exception\UnsupportedMethodException; -use Sylius\RefundPlugin\Entity\ShopBillingData; +use Sylius\Component\Resource\Factory\FactoryInterface; use Sylius\RefundPlugin\Entity\ShopBillingDataInterface; final class ShopBillingDataFactory implements ShopBillingDataFactoryInterface { + /** @var FactoryInterface */ + private $shopBillingDataFactory; + + public function __construct(FactoryInterface $shopBillingDataFactory) + { + $this->shopBillingDataFactory = $shopBillingDataFactory; + } + public function createNew(): ShopBillingDataInterface { - throw new UnsupportedMethodException('This object is not default constructable.'); + /** @var ShopBillingDataInterface $shopBillingData */ + $shopBillingData = $this->shopBillingDataFactory->createNew(); + + return $shopBillingData; } public function createWithData( @@ -32,7 +42,7 @@ public function createWithData( ?string $city, ?string $postcode ): ShopBillingDataInterface { - $shopBillingData = new ShopBillingData(); + $shopBillingData = $this->createNew(); $shopBillingData->setCompany($company); $shopBillingData->setTaxId($taxId); diff --git a/src/Resources/config/app/config.yml b/src/Resources/config/app/config.yml index 3705ace1d..4875cddd1 100644 --- a/src/Resources/config/app/config.yml +++ b/src/Resources/config/app/config.yml @@ -25,7 +25,6 @@ sylius_resource: sylius_refund.shop_billing_data: classes: model: Sylius\RefundPlugin\Entity\ShopBillingData - factory: Sylius\RefundPlugin\Factory\ShopBillingDataFactory sylius_mailer: emails: diff --git a/src/Resources/config/services/factories.xml b/src/Resources/config/services/factories.xml index 82875c7d5..f004fae0e 100644 --- a/src/Resources/config/services/factories.xml +++ b/src/Resources/config/services/factories.xml @@ -31,6 +31,15 @@ + + + +