From 37933b6891eaa4834a7e1a3408f0fa232b9f63a6 Mon Sep 17 00:00:00 2001 From: Edie Lemoine Date: Wed, 9 Oct 2024 13:54:08 +0200 Subject: [PATCH] fix(orders): export correct recipient name Needs #276 --- .../PsObjectModelServiceInterface.php | 64 ++++++++++------- src/Pdk/Base/Adapter/PsAddressAdapter.php | 40 +++++------ src/Service/PsObjectModelService.php | 7 +- tests/Factory/AbstractPsModelFactory.php | 28 ++++---- .../Factory/AbstractPsObjectModelFactory.php | 56 ++++++++++----- tests/Factory/Concern/WithActive.php | 10 +++ .../{Contract => Concern}/WithCarrier.php | 2 +- .../{Contract => Concern}/WithCart.php | 2 +- .../{Contract => Concern}/WithCountry.php | 2 +- .../{Contract => Concern}/WithCurrency.php | 2 +- .../{Contract => Concern}/WithCustomer.php | 2 +- .../{Contract => Concern}/WithGender.php | 2 +- .../{Contract => Concern}/WithGroup.php | 2 +- tests/Factory/Concern/WithGuest.php | 14 ++++ .../{Contract => Concern}/WithLang.php | 2 +- .../WithManufacturer.php | 4 +- .../{Contract => Concern}/WithOrderState.php | 2 +- .../{Contract => Concern}/WithRisk.php | 2 +- .../{Contract => Concern}/WithShop.php | 2 +- .../{Contract => Concern}/WithShopGroup.php | 2 +- tests/Factory/Concern/WithSoftDeletes.php | 10 +++ .../{Contract => Concern}/WithState.php | 2 +- .../{Contract => Concern}/WithSupplier.php | 2 +- .../{Contract => Concern}/WithTimestamps.php | 2 +- .../{Contract => Concern}/WithWarehouse.php | 2 +- .../{Contract => Concern}/WithZone.php | 2 +- tests/Factory/Contract/WithSoftDeletes.php | 10 --- .../Pdk/Base/Adapter/PsAddressAdapterTest.php | 69 +++++++++++++++++++ tests/Uses/UsesMockPsPdkInstance.php | 31 +++++++++ tests/factories/AddressFactory.php | 16 +++-- tests/factories/CarrierFactory.php | 13 ++-- tests/factories/CartFactory.php | 45 +++++++++--- tests/factories/CountryFactory.php | 2 + tests/factories/CurrencyFactory.php | 2 + tests/factories/CustomerFactory.php | 34 ++++++--- tests/factories/GenderFactory.php | 2 + tests/factories/GroupFactory.php | 6 +- tests/factories/GuestFactory.php | 17 +++++ tests/factories/LangFactory.php | 2 + tests/factories/ManufacturerFactory.php | 2 + tests/factories/OrderFactory.php | 41 +++++++---- tests/factories/OrderStateFactory.php | 8 ++- tests/factories/ProductFactory.php | 2 + tests/factories/RiskFactory.php | 2 + tests/factories/ShopFactory.php | 2 + tests/factories/ShopGroupFactory.php | 2 + tests/factories/StateFactory.php | 11 +-- tests/factories/SupplierFactory.php | 8 ++- tests/factories/TabFactory.php | 22 ++++-- tests/factories/ZoneFactory.php | 12 ++-- 50 files changed, 453 insertions(+), 175 deletions(-) create mode 100644 tests/Factory/Concern/WithActive.php rename tests/Factory/{Contract => Concern}/WithCarrier.php (81%) rename tests/Factory/{Contract => Concern}/WithCart.php (79%) rename tests/Factory/{Contract => Concern}/WithCountry.php (81%) rename tests/Factory/{Contract => Concern}/WithCurrency.php (81%) rename tests/Factory/{Contract => Concern}/WithCustomer.php (81%) rename tests/Factory/{Contract => Concern}/WithGender.php (80%) rename tests/Factory/{Contract => Concern}/WithGroup.php (79%) create mode 100644 tests/Factory/Concern/WithGuest.php rename tests/Factory/{Contract => Concern}/WithLang.php (79%) rename tests/Factory/{Contract => Concern}/WithManufacturer.php (76%) rename tests/Factory/{Contract => Concern}/WithOrderState.php (82%) rename tests/Factory/{Contract => Concern}/WithRisk.php (79%) rename tests/Factory/{Contract => Concern}/WithShop.php (79%) rename tests/Factory/{Contract => Concern}/WithShopGroup.php (82%) create mode 100644 tests/Factory/Concern/WithSoftDeletes.php rename tests/Factory/{Contract => Concern}/WithState.php (77%) rename tests/Factory/{Contract => Concern}/WithSupplier.php (81%) rename tests/Factory/{Contract => Concern}/WithTimestamps.php (74%) rename tests/Factory/{Contract => Concern}/WithWarehouse.php (82%) rename tests/Factory/{Contract => Concern}/WithZone.php (79%) delete mode 100644 tests/Factory/Contract/WithSoftDeletes.php create mode 100644 tests/Unit/Pdk/Base/Adapter/PsAddressAdapterTest.php create mode 100644 tests/factories/GuestFactory.php diff --git a/src/Contract/PsObjectModelServiceInterface.php b/src/Contract/PsObjectModelServiceInterface.php index baa9e7f5..3489812b 100644 --- a/src/Contract/PsObjectModelServiceInterface.php +++ b/src/Contract/PsObjectModelServiceInterface.php @@ -18,28 +18,31 @@ interface PsObjectModelServiceInterface public function add(ObjectModel $model): bool; /** - * @template T of ObjectModel - * @param T|class-string $class - * @param null|int|T $input + * @template Model of \ObjectModel + * @template Instance of Model + * @param class-string $class + * @param int|Instance $input * - * @return T + * @return Model */ public function create(string $class, $input = null): ObjectModel; /** - * @template T of ObjectModel - * @param T|class-string $class - * @param int|T $input - * @param bool $soft + * @template Model of \ObjectModel + * @template Instance of Model + * @param class-string $class + * @param int|Instance $input + * @param bool $soft * * @return bool */ public function delete(string $class, $input, bool $soft = false): bool; /** - * @template T of ObjectModel - * @param T|class-string $class - * @param (int|T)[]|\MyParcelNL\Pdk\Base\Support\Collection|\PrestaShopCollection $input + * @template Model of \ObjectModel + * @template Instance of Model + * @param class-string $class + * @param (int|Instance)[]|\MyParcelNL\Pdk\Base\Support\Collection|\PrestaShopCollection $input * @param bool $soft * * @return bool @@ -47,32 +50,45 @@ public function delete(string $class, $input, bool $soft = false): bool; public function deleteMany(string $class, $input, bool $soft = false): bool; /** - * @template T of ObjectModel - * @param T|class-string $class - * @param int|T $input + * @template Model of \ObjectModel + * @template Instance of Model + * @param class-string $class + * @param int|Instance $input * * @return bool */ public function exists(string $class, $input): bool; /** - * @template T of ObjectModel - * @param T|class-string $class - * @param int|T $input + * @template Model of \ObjectModel + * @template Instance of Model + * @param class-string $class + * @param int|Instance $input * - * @return null|T + * @return null|Model */ public function get(string $class, $input): ?ObjectModel; /** - * @template T of ObjectModel - * @param string $class - * @param int|T $input + * @template Model of \ObjectModel + * @template Instance of Model + * @param class-string $class + * @param int|Instance $input * * @return null|int */ public function getId(string $class, $input): ?int; + /** + * @template Model of \ObjectModel + * @template Instance of Model + * @param class-string $class + * @param int|Instance $input + * + * @return Model + */ + public function getWithFallback(string $class, $input): ObjectModel; + /** * @param \ObjectModel $model * @@ -83,11 +99,11 @@ public function getId(string $class, $input): ?int; public function update(ObjectModel $model): bool; /** - * @template T of ObjectModel - * @param \T $model + * @template Model of \ObjectModel + * @param Model $model * @param null|bool $existing * - * @return \T + * @return Model */ public function updateOrAdd(ObjectModel $model, ?bool $existing = null): ObjectModel; } diff --git a/src/Pdk/Base/Adapter/PsAddressAdapter.php b/src/Pdk/Base/Adapter/PsAddressAdapter.php index 804cc934..a83016b7 100644 --- a/src/Pdk/Base/Adapter/PsAddressAdapter.php +++ b/src/Pdk/Base/Adapter/PsAddressAdapter.php @@ -8,6 +8,7 @@ use Country; use Customer; use MyParcelNL\Pdk\Facade\Platform; +use MyParcelNL\PrestaShop\Contract\PsObjectModelServiceInterface; use Order; use State; @@ -16,20 +17,26 @@ final class PsAddressAdapter public const ADDRESS_TYPE_SHIPPING = 'shipping'; public const ADDRESS_TYPE_BILLING = 'billing'; + /** + * @var \MyParcelNL\PrestaShop\Contract\PsObjectModelServiceInterface + */ + private PsObjectModelServiceInterface $psObjectModelService; + + public function __construct(PsObjectModelServiceInterface $psObjectModelService) + { + $this->psObjectModelService = $psObjectModelService; + } + /** * @param \Address|int|null $address * * @return array - * @throws \PrestaShopDatabaseException - * @throws \PrestaShopException */ public function fromAddress($address): array { - if (! $address instanceof Address) { - $address = new Address((int) $address); - } + $model = $this->psObjectModelService->getWithFallback(Address::class, $address); - return $this->createFromAddress($address); + return $this->createFromAddress($model); } /** @@ -37,35 +44,28 @@ public function fromAddress($address): array * @param string $addressType * * @return array - * @throws \PrestaShopDatabaseException - * @throws \PrestaShopException */ public function fromOrder($order, string $addressType = self::ADDRESS_TYPE_SHIPPING): array { - if (! $order instanceof Order) { - $order = new Order((int) $order); - } - - $addressId = $addressType === self::ADDRESS_TYPE_SHIPPING + $orderModel = $this->psObjectModelService->getWithFallback(Order::class, $order); + $addressId = $addressType === self::ADDRESS_TYPE_SHIPPING ? $order->id_address_delivery : $order->id_address_invoice; - $address = new Address($addressId); - $customer = new Customer($order->id_customer); + $address = $this->psObjectModelService->getWithFallback(Address::class, $addressId); + $customer = $this->psObjectModelService->getWithFallback(Customer::class, $orderModel->id_customer); - return $this->createFromCustomer($customer) + $this->createFromAddress($address); + return array_replace($this->createFromCustomer($customer), $this->createFromAddress($address)); } /** * @param \Address $address * * @return array - * @throws \PrestaShopDatabaseException - * @throws \PrestaShopException */ private function createFromAddress(Address $address): array { - $country = new Country($address->id_country); + $country = $this->psObjectModelService->getWithFallback(Country::class, $address->id_country); return [ 'cc' => $country->iso_code, @@ -77,7 +77,7 @@ private function createFromAddress(Address $address): array 'phone' => $address->phone, 'region' => $country->iso_code === Platform::get('localCountry') ? null - : (new State($address->id_state))->name, + : $this->psObjectModelService->getWithFallback(State::class, $address->id_state)->name, ]; } diff --git a/src/Service/PsObjectModelService.php b/src/Service/PsObjectModelService.php index 226eb649..9c809b7a 100644 --- a/src/Service/PsObjectModelService.php +++ b/src/Service/PsObjectModelService.php @@ -41,8 +41,8 @@ public function create(string $class, $input = null): ObjectModel } /** - * @throws \PrestaShopException * @throws \PrestaShopDatabaseException + * @throws \PrestaShopException */ public function delete(string $class, $input, bool $soft = false): bool { @@ -122,6 +122,11 @@ public function getId(string $class, $input): ?int return $input ? (int) $input : null; } + public function getWithFallback(string $class, $input): ObjectModel + { + return $this->get($class, $input) ?? $this->create($class); + } + /** * @param \ObjectModel $model * diff --git a/tests/Factory/AbstractPsModelFactory.php b/tests/Factory/AbstractPsModelFactory.php index 2e33d57e..4d927a81 100644 --- a/tests/Factory/AbstractPsModelFactory.php +++ b/tests/Factory/AbstractPsModelFactory.php @@ -14,7 +14,20 @@ abstract class AbstractPsModelFactory extends AbstractPsFactory /** * @var array */ - private $cache = []; + private array $cache = []; + + /** + * @param string $class + * @param array $attributes + * + * @return T + */ + abstract protected function createObject(string $class, array $attributes); + + /** + * @return class-string + */ + abstract protected function getClass(): string; /** * @return T @@ -47,19 +60,6 @@ public function store() return $model; } - /** - * @param string $class - * @param array $attributes - * - * @return T - */ - abstract protected function createObject(string $class, array $attributes); - - /** - * @return class-string - */ - abstract protected function getClass(): string; - /** * @return int */ diff --git a/tests/Factory/AbstractPsObjectModelFactory.php b/tests/Factory/AbstractPsObjectModelFactory.php index 840813c0..4ef743a6 100644 --- a/tests/Factory/AbstractPsObjectModelFactory.php +++ b/tests/Factory/AbstractPsObjectModelFactory.php @@ -5,10 +5,11 @@ namespace MyParcelNL\PrestaShop\Tests\Factory; use MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithActive; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithSoftDeletes; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithTimestamps; use MyParcelNL\PrestaShop\Tests\Factory\Contract\PsFactoryInterface; use MyParcelNL\PrestaShop\Tests\Factory\Contract\PsObjectModelFactoryInterface; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithSoftDeletes; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithTimestamps; use MyParcelNL\PrestaShop\Tests\Mock\MockPsObjectModel; use MyParcelNL\PrestaShop\Tests\Mock\MockPsObjectModels; use MyParcelNL\Sdk\src\Support\Str; @@ -26,12 +27,12 @@ abstract class AbstractPsObjectModelFactory extends AbstractPsModelFactory imple /** * @var PsObjectModelFactoryInterface[] */ - private $additionalModelsToStore = []; + private array $additionalModelsToStore = []; /** * @var null|int */ - private $id; + private ?int $id; /** * @param null|int $id @@ -73,7 +74,7 @@ public function store(): ObjectModel protected function addAttribute(string $attribute, $value, array $attributes = []): AbstractPsFactory { if (Str::startsWith($attribute, 'id_')) { - return $this->withModel(Str::after($attribute, 'id_'), $value, $attributes); + return $this->withModel($attribute, $value, $attributes); } if ($value instanceof PsObjectModelFactoryInterface || $value instanceof MockPsObjectModel) { @@ -101,9 +102,23 @@ protected function createDefault(): FactoryInterface $factory->withDeleted(false); } + if ($factory instanceof WithActive) { + $factory->withActive(true); + } + return $factory; } + /** + * @param string $key + * + * @return string + */ + protected function createIdKey(string $key): string + { + return sprintf('id_%s', Str::snake($key)); + } + /** * @param string $class * @param array $attributes @@ -155,24 +170,25 @@ protected function save($model): void * @param string $key * @param int|ObjectModel|PsObjectModelFactoryInterface $input * @param array $attributes + * @param null|string $keyOverride * * @return $this * @throws \MyParcelNL\Pdk\Tests\Factory\Exception\InvalidFactoryException */ - protected function withModel(string $key, $input, array $attributes = []): self + protected function withModel(string $key, $input, array $attributes = [], ?string $keyOverride = null): self { if (is_int($input)) { $class = Str::after($key, 'id_'); $existingModel = MockPsObjectModels::get($class, $input); if ($existingModel) { - return $this->withModel($class, $existingModel, $attributes); + return $this->withModel($class, $existingModel, $attributes, $keyOverride); } /** @var PsObjectModelFactoryInterface $factory */ $factory = psFactory(Str::studly($class), $input); - return $this->withModel($class, $factory, $attributes); + return $this->withModel($class, $factory, $attributes, $keyOverride); } if ($input instanceof PsFactoryInterface) { @@ -182,25 +198,31 @@ protected function withModel(string $key, $input, array $attributes = []): self ->with($attributes) ->make(); - return $this->withModel($key, $model); + return $this->withModel($key, $model, [], $keyOverride); } - $idKey = sprintf('id_%s', Str::snake($key)); + $idKey = $this->createIdKey($keyOverride ?? $key); return $this->with([$idKey => $input->id]); } /** - * @param string $key - * @param int|ObjectModel|PsObjectModelFactoryInterface $input - * @param array $attributes - * @param string $foreignKey + * @template Model of ObjectModel + * @template Instance of Model + * @param class-string $class + * @param string $key + * @param int|Instance|PsObjectModelFactoryInterface $input + * @param array $attributes * * @return $this * @throws \MyParcelNL\Pdk\Tests\Factory\Exception\InvalidFactoryException */ - protected function withRelation(string $key, $input, array $attributes, string $foreignKey): self - { - return $this->withModel($key, $input, array_replace($attributes, [$foreignKey => $this->getId()])); + protected function withRelation( + string $class, + string $key, + $input, + array $attributes + ): self { + return $this->withModel($class, $input, $attributes, $key); } } diff --git a/tests/Factory/Concern/WithActive.php b/tests/Factory/Concern/WithActive.php new file mode 100644 index 00000000..e01e9382 --- /dev/null +++ b/tests/Factory/Concern/WithActive.php @@ -0,0 +1,10 @@ +store(); + $order = psFactory(Order::class) + ->withAddressDelivery($addressFactory) + ->withCustomer($address->id_customer) + ->store(); + + /** @var \MyParcelNL\PrestaShop\Pdk\Base\Adapter\PsAddressAdapter $adapter */ + $adapter = Pdk::get(PsAddressAdapter::class); + + $resultFromAddress = $adapter->fromAddress($address); + $resultFromOrder = $adapter->fromOrder($order); + + expect([ + 'from address' => Utils::filterNull($resultFromAddress), + 'from order' => Utils::filterNull($resultFromOrder), + ])->toEqual([ + 'from address' => $expectation, + 'from order' => $expectation, + ]); +})->with([ + 'address' => [ + 'addressFactory' => function () { + $customer = psFactory(Customer::class) + ->withFirstname('John') + ->withLastname('Doe') + ->withEmail('test@test.com'); + + $country = psFactory(Country::class) + ->withIsoCode('NL'); + + return psFactory(Address::class) + ->withCustomer($customer) + ->withCountry($country) + ->withFirstname('John') + ->withLastname('Doe') + ->withAddress1('Keizersgracht 1') + ->withAddress2('A') + ->withCity('Amsterdam') + ->withPhone('0612345678') + ->withPostcode('1015CC'); + }, + 'expectation' => [ + 'cc' => 'NL', + 'city' => 'Amsterdam', + 'address1' => 'Keizersgracht 1', + 'address2' => 'A', + 'postalCode' => '1015CC', + 'person' => 'John Doe', + 'phone' => '0612345678', + ], + ], + +]); + diff --git a/tests/Uses/UsesMockPsPdkInstance.php b/tests/Uses/UsesMockPsPdkInstance.php index f855e0f7..e5aebb5a 100644 --- a/tests/Uses/UsesMockPsPdkInstance.php +++ b/tests/Uses/UsesMockPsPdkInstance.php @@ -4,8 +4,15 @@ namespace MyParcelNL\PrestaShop\Tests\Uses; +use Address; use CarrierModule; use Configuration; +use Country; +use Customer; +use Gender; +use Group; +use Lang; +use Manufacturer; use MyParcelNL\Pdk\Base\Facade; use MyParcelNL\Pdk\Base\FileSystemInterface; use MyParcelNL\Pdk\Facade\Config; @@ -17,6 +24,12 @@ use MyParcelNL\PrestaShop\Tests\Mock\MockPsModule; use OrderState; use OrderStateFactory; +use Risk; +use Shop; +use ShopGroup; +use State; +use Supplier; +use Warehouse; use Zone; use function MyParcelNL\PrestaShop\psFactory; @@ -44,6 +57,24 @@ protected function addDefaultData(): void { psFactory(Configuration::class)->make(); + (new FactoryCollection([ + psFactory(Country::class) + ->withIsoCode('NL'), + psFactory(Address::class), + psFactory(Address::class)->withAddress2('(Billing)'), + psFactory(Customer::class), + psFactory(Gender::class), + psFactory(Group::class), + psFactory(Lang::class), + psFactory(Manufacturer::class), + psFactory(Risk::class), + psFactory(Shop::class), + psFactory(ShopGroup::class), + psFactory(State::class), + psFactory(Supplier::class), + psFactory(Warehouse::class), + ]))->store(); + $this->createZones(); $this->createOrderStates(); diff --git a/tests/factories/AddressFactory.php b/tests/factories/AddressFactory.php index 51757d12..46f4f07f 100644 --- a/tests/factories/AddressFactory.php +++ b/tests/factories/AddressFactory.php @@ -4,12 +4,12 @@ use MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface; use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithCountry; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithCustomer; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithManufacturer; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithState; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithSupplier; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithWarehouse; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithCountry; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithCustomer; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithManufacturer; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithState; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithSupplier; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithWarehouse; /** * @see \AddressCore @@ -26,6 +26,8 @@ * @method $this withPhoneMobile(string $phoneMobile) * @method $this withPostcode(string $postcode) * @method $this withVatNumber(string $vatNumber) + * @extends AbstractPsObjectModelFactory
+ * @see \AddressCore */ final class AddressFactory extends AbstractPsObjectModelFactory implements WithCountry, WithCustomer, WithManufacturer, WithState, WithSupplier, WithWarehouse @@ -41,7 +43,7 @@ protected function createDefault(): FactoryInterface ->withAddress1('Antareslaan 31') ->withFirstname('Meredith') ->withLastname('Mailbox') - ->withIdCountry(1) + ->withCountry(Country::getByIso('NL')) ->withIdCustomer(1) ->withIdManufacturer(1) ->withIdState(1) diff --git a/tests/factories/CarrierFactory.php b/tests/factories/CarrierFactory.php index 28853cb4..0e8241db 100644 --- a/tests/factories/CarrierFactory.php +++ b/tests/factories/CarrierFactory.php @@ -4,15 +4,15 @@ use MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface; use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithCarrier; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithSoftDeletes; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithTimestamps; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithActive; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithCarrier; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithSoftDeletes; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithTimestamps; /** * @see \CarrierCore * @method $this withName(string $name) * @method $this withUrl(string $url) - * @method $this withActive(int $active) * @method $this withShippingHandling(int $shippingHandling) * @method $this withRangeBehavior(int $rangeBehavior) * @method $this withIsModule(int $isModule) @@ -27,8 +27,11 @@ * @method $this withMaxDepth(int $maxDepth) * @method $this withMaxWeight(int $maxWeight) * @method $this withGrade(int $grade) + * @extends AbstractPsObjectModelFactory + * @see \CarrierCore */ -final class CarrierFactory extends AbstractPsObjectModelFactory implements WithTimestamps, WithSoftDeletes, WithCarrier +final class CarrierFactory extends AbstractPsObjectModelFactory implements WithTimestamps, WithSoftDeletes, WithCarrier, + WithActive { /** * Defined manually to avoid this being treated as the id of a class. diff --git a/tests/factories/CartFactory.php b/tests/factories/CartFactory.php index 55e9ccc2..8ecde151 100644 --- a/tests/factories/CartFactory.php +++ b/tests/factories/CartFactory.php @@ -3,17 +3,18 @@ declare(strict_types=1); use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithCarrier; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithCurrency; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithCustomer; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithLang; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithShopGroup; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithTimestamps; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithCarrier; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithCurrency; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithCustomer; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithGuest; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithLang; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithShopGroup; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithTimestamps; +use PrestaShop\PrestaShop\Adapter\AddressFactory; /** * @method $this withIdAddressDelivery(int $idAddressDelivery) * @method $this withIdAddressInvoice(int $idAddressInvoice) - * @method $this withIdGuest(int $idGuest) * @method $this withRecyclable(bool $recyclable) * @method $this withGift(bool $gift) * @method $this withGiftMessage(string $giftMessage) @@ -24,11 +25,37 @@ * @method $this withTextFields(array $textFields) * @method $this withDeliveryOption(string $deliveryOption) * @method $this withAllowSeperatedPackage(bool $allowSeperatedPackage) - * + * @extends AbstractPsObjectModelFactory + * @see \CartCore */ final class CartFactory extends AbstractPsObjectModelFactory implements WithShopGroup, WithLang, WithCurrency, - WithCustomer, WithCarrier, WithTimestamps + WithCustomer, WithCarrier, WithTimestamps, + WithGuest { + /** + * @param int|Address|AddressFactory $input + * @param array $attributes + * + * @return $this + * @throws \MyParcelNL\Pdk\Tests\Factory\Exception\InvalidFactoryException, + */ + public function withAddressDelivery($input, array $attributes = []): self + { + return $this->withRelation(Address::class, 'address_delivery', $input, $attributes); + } + + /** + * @param int|Address|AddressFactory $input + * @param array $attributes + * + * @return $this + * @throws \MyParcelNL\Pdk\Tests\Factory\Exception\InvalidFactoryException + */ + public function withAddressInvoice($input, array $attributes = []): self + { + return $this->withRelation(Address::class, 'address_invoice', $input, $attributes); + } + protected function getObjectModelClass(): string { return Cart::class; diff --git a/tests/factories/CountryFactory.php b/tests/factories/CountryFactory.php index f9fbc280..ca3fa3d6 100644 --- a/tests/factories/CountryFactory.php +++ b/tests/factories/CountryFactory.php @@ -5,6 +5,8 @@ use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; /** + * @method $this withIsoCode(string $isoCode) + * @extends AbstractPsObjectModelFactory * @see \CountryCore */ final class CountryFactory extends AbstractPsObjectModelFactory diff --git a/tests/factories/CurrencyFactory.php b/tests/factories/CurrencyFactory.php index 9427f1ba..755fe1a1 100644 --- a/tests/factories/CurrencyFactory.php +++ b/tests/factories/CurrencyFactory.php @@ -5,6 +5,8 @@ use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; /** + * @extends AbstractPsObjectModelFactory + * @see \CurrencyCore */ final class CurrencyFactory extends AbstractPsObjectModelFactory { diff --git a/tests/factories/CustomerFactory.php b/tests/factories/CustomerFactory.php index 226e316b..b615cd52 100644 --- a/tests/factories/CustomerFactory.php +++ b/tests/factories/CustomerFactory.php @@ -4,21 +4,19 @@ use MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface; use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithGender; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithLang; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithRisk; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithShop; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithShopGroup; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithSoftDeletes; -use function MyParcelNL\PrestaShop\psFactory; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithActive; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithGender; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithLang; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithRisk; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithShop; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithShopGroup; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithSoftDeletes; /** * @see \CustomerCore - * @method $this withActive(bool $active) * @method $this withApe(string $ape) * @method $this withBirthday(string $birthday) * @method $this withCompany(string $company) - * @method $this withDefaultGroup(int|Group|GroupFactory $defaultGroup) * @method $this withEmail(string $email) * @method $this withFirstname(string $firstname) * @method $this withIdDefaultGroup(int $idDefaultGroup) @@ -37,10 +35,24 @@ * @method $this withShowPublicPrices(int $showPublicPrices) * @method $this withSiret(string $siret) * @method $this withWebsite(string $website) + * @extends AbstractPsObjectModelFactory + * @see \CustomerCore */ final class CustomerFactory extends AbstractPsObjectModelFactory implements WithShop, WithShopGroup, WithLang, WithRisk, - WithGender, WithSoftDeletes + WithGender, WithSoftDeletes, WithActive { + /** + * @param int|Group|\GroupFactory $input + * @param array $attributes + * + * @return $this + * @throws \MyParcelNL\Pdk\Tests\Factory\Exception\InvalidFactoryException + */ + public function withDefaultGroup($input, array $attributes = []): self + { + return $this->withRelation(Group::class, 'default_group', $input, $attributes); + } + /** * @return \MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface * @throws \MyParcelNL\Pdk\Tests\Factory\Exception\InvalidFactoryException @@ -50,7 +62,7 @@ protected function createDefault(): FactoryInterface return parent::createDefault() ->withFirstname('Felicia') ->withLastname('Parcel') - ->withDefaultGroup(psFactory(Group::class, 1)) + ->withDefaultGroup(1) ->withIdGender(1) ->withIdLang(1) ->withIdRisk(1) diff --git a/tests/factories/GenderFactory.php b/tests/factories/GenderFactory.php index 403e0bbe..7150ab1d 100644 --- a/tests/factories/GenderFactory.php +++ b/tests/factories/GenderFactory.php @@ -5,6 +5,8 @@ use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; /** + * @see \GenderCore + * @extends AbstractPsObjectModelFactory * @see \GenderCore */ final class GenderFactory extends AbstractPsObjectModelFactory diff --git a/tests/factories/GroupFactory.php b/tests/factories/GroupFactory.php index fc719c1a..e5bc025b 100644 --- a/tests/factories/GroupFactory.php +++ b/tests/factories/GroupFactory.php @@ -3,13 +3,15 @@ declare(strict_types=1); use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithGroup; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithTimestamps; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithGroup; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithTimestamps; /** * @method $this withReduction(float $reduction) * @method $this withPriceDisplayMethod(int $priceDisplayMethod) * @method $this withShowPrices(int $showPrices) + * @extends AbstractPsObjectModelFactory + * @see \GroupCore */ final class GroupFactory extends AbstractPsObjectModelFactory implements WithTimestamps, WithGroup { diff --git a/tests/factories/GuestFactory.php b/tests/factories/GuestFactory.php new file mode 100644 index 00000000..a0a28957 --- /dev/null +++ b/tests/factories/GuestFactory.php @@ -0,0 +1,17 @@ + + * @see \GuestCore + */ +final class GuestFactory extends AbstractPsObjectModelFactory +{ + protected function getObjectModelClass(): string + { + return Guest::class; + } +} diff --git a/tests/factories/LangFactory.php b/tests/factories/LangFactory.php index ca890001..453726b8 100644 --- a/tests/factories/LangFactory.php +++ b/tests/factories/LangFactory.php @@ -5,6 +5,8 @@ use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; /** + * @extends AbstractPsObjectModelFactory + * @see \LangCore */ final class LangFactory extends AbstractPsObjectModelFactory { diff --git a/tests/factories/ManufacturerFactory.php b/tests/factories/ManufacturerFactory.php index 94902e9b..1183b3ad 100644 --- a/tests/factories/ManufacturerFactory.php +++ b/tests/factories/ManufacturerFactory.php @@ -5,6 +5,8 @@ use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; /** + * @see \ManufacturerCore + * @extends AbstractPsObjectModelFactory * @see \ManufacturerCore */ final class ManufacturerFactory extends AbstractPsObjectModelFactory diff --git a/tests/factories/OrderFactory.php b/tests/factories/OrderFactory.php index cd48509c..3d90bf60 100644 --- a/tests/factories/OrderFactory.php +++ b/tests/factories/OrderFactory.php @@ -4,21 +4,19 @@ use MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface; use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithCarrier; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithCart; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithCurrency; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithCustomer; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithLang; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithShop; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithShopGroup; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithTimestamps; -use function MyParcelNL\PrestaShop\psFactory; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithCarrier; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithCart; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithCurrency; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithCustomer; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithLang; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithShop; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithShopGroup; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithTimestamps; /** * @see \OrderCore * @method $this add() * @method $this withConversionRate(float $conversionRate) - * @method $this withCurrentState(int $currentState) * @method $this withDeliveryDate(string $deliveryDate) * @method $this withDeliveryNumber(int $deliveryNumber) * @method $this withGift(bool $gift) @@ -52,6 +50,8 @@ * @method $this withTotalWrappingTaxExcl(float $totalWrappingTaxExcl) * @method $this withTotalWrappingTaxIncl(float $totalWrappingTaxIncl) * @method $this withValid(bool $valid) + * @extends AbstractPsObjectModelFactory + * @see \OrderCore */ final class OrderFactory extends AbstractPsObjectModelFactory implements WithShop, WithShopGroup, WithLang, WithCustomer, WithCurrency, WithCart, @@ -66,7 +66,7 @@ final class OrderFactory extends AbstractPsObjectModelFactory implements WithSho */ public function withAddressDelivery($input, array $attributes = []): self { - return $this->withRelation('address_delivery', $input, $attributes, 'id_customer'); + return $this->withRelation(Address::class, 'address_delivery', $input, $attributes); } /** @@ -78,7 +78,19 @@ public function withAddressDelivery($input, array $attributes = []): self */ public function withAddressInvoice($input, array $attributes = []): self { - return $this->withRelation('address_invoice', $input, $attributes, 'id_customer'); + return $this->withRelation(Address::class, 'address_invoice', $input, $attributes); + } + + /** + * @param int|\OrderState|\OrderStateFactory $input + * @param array $attributes + * + * @return $this + * @throws \MyParcelNL\Pdk\Tests\Factory\Exception\InvalidFactoryException + */ + public function withCurrentState($input, array $attributes = []): self + { + return $this->withRelation(OrderState::class, 'current_state', $input, $attributes); } /** @@ -88,8 +100,9 @@ public function withAddressInvoice($input, array $attributes = []): self protected function createDefault(): FactoryInterface { return parent::createDefault() - ->withAddressDelivery(psFactory(Address::class, 1)) - ->withAddressInvoice(psFactory(Address::class, 2)) + ->withAddressDelivery(1) + ->withAddressInvoice(2) + ->withCurrentState(1) ->withIdCarrier(1) ->withIdCart(1) ->withIdCurrency(1) diff --git a/tests/factories/OrderStateFactory.php b/tests/factories/OrderStateFactory.php index 90b27217..79a3fa34 100644 --- a/tests/factories/OrderStateFactory.php +++ b/tests/factories/OrderStateFactory.php @@ -4,9 +4,9 @@ use MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface; use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithLang; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithOrderState; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithSoftDeletes; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithLang; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithOrderState; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithSoftDeletes; /** * @method $this withColor(string $color) @@ -23,6 +23,8 @@ * @method $this withShipped(int $shipped) * @method $this withTemplate(string $template) * @method $this withUnremovable(int $unremovable) + * @extends AbstractPsObjectModelFactory + * @see \OrderStateCore */ final class OrderStateFactory extends AbstractPsObjectModelFactory implements WithSoftDeletes, WithLang, WithOrderState { diff --git a/tests/factories/ProductFactory.php b/tests/factories/ProductFactory.php index 838d18f8..66b5bd5c 100644 --- a/tests/factories/ProductFactory.php +++ b/tests/factories/ProductFactory.php @@ -13,6 +13,8 @@ * @method $this withName(array $names) * @method $this withPrice(int $price) * @method $this withWeight(float|string $weight) + * @extends AbstractPsObjectModelFactory + * @see \ProductCore */ final class ProductFactory extends AbstractPsObjectModelFactory { diff --git a/tests/factories/RiskFactory.php b/tests/factories/RiskFactory.php index b2466309..e1b605f7 100644 --- a/tests/factories/RiskFactory.php +++ b/tests/factories/RiskFactory.php @@ -5,6 +5,8 @@ use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; /** + * @see \RiskCore + * @extends AbstractPsObjectModelFactory * @see \RiskCore */ final class RiskFactory extends AbstractPsObjectModelFactory diff --git a/tests/factories/ShopFactory.php b/tests/factories/ShopFactory.php index fbc02714..be8359a6 100644 --- a/tests/factories/ShopFactory.php +++ b/tests/factories/ShopFactory.php @@ -3,6 +3,8 @@ use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; /** + * @extends AbstractPsObjectModelFactory + * @see \ShopCore */ final class ShopFactory extends AbstractPsObjectModelFactory { diff --git a/tests/factories/ShopGroupFactory.php b/tests/factories/ShopGroupFactory.php index 89c1a1d5..ce427f77 100644 --- a/tests/factories/ShopGroupFactory.php +++ b/tests/factories/ShopGroupFactory.php @@ -5,6 +5,8 @@ use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; /** + * @extends AbstractPsObjectModelFactory + * @see \ShopGroupCore */ final class ShopGroupFactory extends AbstractPsObjectModelFactory { diff --git a/tests/factories/StateFactory.php b/tests/factories/StateFactory.php index ec23696c..9b909c1c 100644 --- a/tests/factories/StateFactory.php +++ b/tests/factories/StateFactory.php @@ -4,16 +4,18 @@ use MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface; use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithCountry; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithZone; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithActive; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithCountry; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithZone; /** * @see \StateCore * @method $this withIsoCode(string $isoCode) * @method $this withName(string $name) - * @method $this withActive(bool $active) + * @extends AbstractPsObjectModelFactory + * @see \StateCore */ -final class StateFactory extends AbstractPsObjectModelFactory implements WithCountry, WithZone +final class StateFactory extends AbstractPsObjectModelFactory implements WithCountry, WithZone, WithActive { /** * @return \MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface @@ -21,7 +23,6 @@ final class StateFactory extends AbstractPsObjectModelFactory implements WithCou protected function createDefault(): FactoryInterface { return parent::createDefault() - ->withActive(true) ->withIdCountry(1) ->withIdZone(1); } diff --git a/tests/factories/SupplierFactory.php b/tests/factories/SupplierFactory.php index a41704d4..9c572e63 100644 --- a/tests/factories/SupplierFactory.php +++ b/tests/factories/SupplierFactory.php @@ -3,19 +3,21 @@ declare(strict_types=1); use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithSupplier; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithActive; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithSupplier; /** * @see \SupplierCore - * @method $this withActive(bool $active) * @method $this withDescription(string|array $description) * @method $this withLinkRewrite(string $linkRewrite) * @method $this withMetaDescription(string|array $metaDescription) * @method $this withMetaKeywords(string|array $metaKeywords) * @method $this withMetaTitle(string|array $metaTitle) * @method $this withName(string $name) + * @extends AbstractPsObjectModelFactory + * @see \SupplierCore */ -final class SupplierFactory extends AbstractPsObjectModelFactory implements WithSupplier +final class SupplierFactory extends AbstractPsObjectModelFactory implements WithSupplier, WithActive { protected function getObjectModelClass(): string { diff --git a/tests/factories/TabFactory.php b/tests/factories/TabFactory.php index aa36ad69..ce261959 100644 --- a/tests/factories/TabFactory.php +++ b/tests/factories/TabFactory.php @@ -3,19 +3,33 @@ declare(strict_types=1); use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithLang; -use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithShop; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithActive; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithLang; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithShop; /** * @method $this withIdParent(int $idParent) - * @method $this withActive(bool $active) * @method $this withClassName(string $className) * @method $this withModule(string $module) * @method $this withName(array $name) * @method $this withRouteName(string $routeName) + * @extends AbstractPsObjectModelFactory + * @see \TabCore */ -final class TabFactory extends AbstractPsObjectModelFactory implements WithLang, WithShop +final class TabFactory extends AbstractPsObjectModelFactory implements WithLang, WithShop, WithActive { + /** + * @param int|Tab|TabFactory $input + * @param array $attributes + * + * @return $this + * @throws \MyParcelNL\Pdk\Tests\Factory\Exception\InvalidFactoryException + */ + public function withParent($input, array $attributes = []): self + { + return $this->withRelation(Tab::class, 'parent', $input, $attributes); + } + protected function getObjectModelClass(): string { return Tab::class; diff --git a/tests/factories/ZoneFactory.php b/tests/factories/ZoneFactory.php index 3e212e00..66f562d5 100644 --- a/tests/factories/ZoneFactory.php +++ b/tests/factories/ZoneFactory.php @@ -2,20 +2,16 @@ declare(strict_types=1); -use MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface; use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory; +use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithActive; /** * @method $this withName(string $name) - * @method $this withActive(int $active) + * @extends AbstractPsObjectModelFactory + * @see \ZoneCore */ -final class ZoneFactory extends AbstractPsObjectModelFactory +final class ZoneFactory extends AbstractPsObjectModelFactory implements WithActive { - protected function createDefault(): FactoryInterface - { - return $this->withActive(1); - } - protected function getObjectModelClass(): string { return Zone::class;