From b61d1970fcc916ee1648fe5500da850e4f8eec6e Mon Sep 17 00:00:00 2001 From: Edie Lemoine Date: Thu, 14 Sep 2023 14:32:22 +0200 Subject: [PATCH] fix: improve entities and carrier logic --- config/pdk.php | 7 + src/Carrier/Service/CarrierBuilder.php | 89 +++--- src/Contract/PsCarrierServiceInterface.php | 14 +- src/Controller/PdkController.php | 14 +- src/Database/AbstractDatabaseMigration.php | 18 +- ...teCarrierMappingTableDatabaseMigration.php | 26 +- ...tDeliveryOptionsTableDatabaseMigration.php | 24 +- .../CreateOrderDataTableDatabaseMigration.php | 24 +- ...ateOrderShipmentTableDatabaseMigration.php | 27 +- ...eProductSettingsTableDatabaseMigration.php | 24 +- .../Sql/Contract/SqlBuilderInterface.php | 15 ++ src/Database/Sql/CreateTableSqlBuilder.php | 98 +++++++ src/Database/Sql/DropTableSqlBuilder.php | 13 + src/Database/Sql/SqlBuilder.php | 39 +++ src/Entity/AbstractEntity.php | 39 --- src/Entity/Concern/HasJsonData.php | 18 +- src/Entity/Concern/HasTimestamps.php | 63 +++++ src/Entity/Contract/EntityInterface.php | 5 - src/Entity/MyparcelnlCarrierMapping.php | 46 +++- src/Entity/MyparcelnlCartDeliveryOptions.php | 34 ++- src/Entity/MyparcelnlOrderData.php | 32 ++- src/Entity/MyparcelnlOrderShipment.php | 51 +++- src/Entity/MyparcelnlProductSettings.php | 34 ++- src/Facade/EntityManager.php | 24 +- src/Hooks/HasPdkCheckoutHooks.php | 2 +- src/Hooks/HasPsCarrierHooks.php | 2 +- .../Repository/PsPdkAccountRepository.php | 11 - .../Backend/Account/PsUpdateAccountAction.php | 27 ++ src/Pdk/Base/PsPdkBootstrapper.php | 8 +- .../Installer/Migration/Migration2_0_0.php | 8 +- .../Order/Repository/PsPdkOrderRepository.php | 10 +- .../Repository/PdkProductRepository.php | 4 +- src/Repository/AbstractPsObjectRepository.php | 9 +- src/Service/PsCarrierService.php | 141 +++++----- src/Service/PsEntityManagerService.php | 39 +++ src/Service/PsOrderService.php | 6 +- tests/Bootstrap/PsTestBootstrapper.php | 10 + tests/Factory/AbstractPsEntityFactory.php | 14 +- tests/Factory/AbstractPsModelFactory.php | 22 +- .../Factory/AbstractPsObjectModelFactory.php | 18 ++ tests/Mock/MockPsCarrier.php | 6 + tests/Mock/MockPsEntityManager.php | 5 + tests/Mock/MockPsEntityRepository.php | 4 +- tests/Pest.php | 13 +- .../Sql/CreateTableSqlBuilderTest.php | 23 ++ .../Database/Sql/DropTableSqlBuilderTest.php | 13 + tests/Unit/Hooks/HasPsCarrierHooksTest.php | 4 +- tests/Unit/Service/PsCarrierServiceTest.php | 34 ++- tests/Uses/UsesMockPlugin.php | 1 + tests/Uses/UsesMockPsPdkInstance.php | 37 ++- ...creates_carriers_on_account_update__1.json | 255 +++++++++++++++++- ...does_not_create_duplicate_carriers__1.json | 215 ++------------- .../MyparcelnlCarrierMappingFactory.php | 5 +- .../MyparcelnlCartDeliveryOptionsFactory.php | 14 +- .../Entity/MyparcelnlOrderDataFactory.php | 15 +- .../Entity/MyparcelnlOrderShipmentFactory.php | 17 +- .../MyparcelnlProductSettingsFactory.php | 15 +- tests/mock_class_map.php | 5 + 58 files changed, 1137 insertions(+), 653 deletions(-) create mode 100644 src/Database/Sql/Contract/SqlBuilderInterface.php create mode 100644 src/Database/Sql/CreateTableSqlBuilder.php create mode 100644 src/Database/Sql/DropTableSqlBuilder.php create mode 100644 src/Database/Sql/SqlBuilder.php create mode 100644 src/Entity/Concern/HasTimestamps.php create mode 100644 src/Pdk/Action/Backend/Account/PsUpdateAccountAction.php create mode 100644 src/Service/PsEntityManagerService.php create mode 100644 tests/Bootstrap/PsTestBootstrapper.php create mode 100644 tests/Unit/Database/Sql/CreateTableSqlBuilderTest.php create mode 100644 tests/Unit/Database/Sql/DropTableSqlBuilderTest.php diff --git a/config/pdk.php b/config/pdk.php index 8ffe4cc1..314c8c98 100644 --- a/config/pdk.php +++ b/config/pdk.php @@ -4,6 +4,7 @@ use MyParcelNL\Pdk\Api\Contract\ClientAdapterInterface; use MyParcelNL\Pdk\App\Account\Contract\PdkAccountRepositoryInterface; +use MyParcelNL\Pdk\App\Action\Backend\Account\UpdateAccountAction; use MyParcelNL\Pdk\App\Api\Contract\BackendEndpointServiceInterface; use MyParcelNL\Pdk\App\Api\Contract\FrontendEndpointServiceInterface; use MyParcelNL\Pdk\App\Cart\Contract\PdkCartRepositoryInterface; @@ -29,6 +30,7 @@ use MyParcelNL\PrestaShop\Contract\PsCarrierServiceInterface; use MyParcelNL\PrestaShop\Contract\PsOrderServiceInterface; use MyParcelNL\PrestaShop\Pdk\Account\Repository\PsPdkAccountRepository; +use MyParcelNL\PrestaShop\Pdk\Action\Backend\Account\PsUpdateAccountAction; use MyParcelNL\PrestaShop\Pdk\Api\Adapter\Guzzle7ClientAdapter; use MyParcelNL\PrestaShop\Pdk\Api\Service\PsBackendEndpointService; use MyParcelNL\PrestaShop\Pdk\Api\Service\PsFrontendEndpointService; @@ -95,6 +97,11 @@ PdkWebhookServiceInterface::class => get(PsWebhookService::class), PdkWebhooksRepositoryInterface::class => get(PsWebhooksRepository::class), + /** + * Actions + */ + UpdateAccountAction::class => get(PsUpdateAccountAction::class), + /** * Miscellaneous */ diff --git a/src/Carrier/Service/CarrierBuilder.php b/src/Carrier/Service/CarrierBuilder.php index 6268ed53..97d29121 100644 --- a/src/Carrier/Service/CarrierBuilder.php +++ b/src/Carrier/Service/CarrierBuilder.php @@ -6,14 +6,12 @@ use Carrier as PsCarrier; use Context; -use Db; use Group; use Language as PsLanguage; use MyParcelNL\Pdk\Base\Support\Arr; use MyParcelNL\Pdk\Carrier\Model\Carrier; use MyParcelNL\Pdk\Facade\Language; use MyParcelNL\Pdk\Facade\Pdk; -use MyParcelNL\PrestaShop\Database\Table; use MyParcelNL\PrestaShop\Repository\PsCarrierMappingRepository; use ObjectModel; use RangePrice; @@ -53,22 +51,22 @@ public function __construct(Carrier $myParcelCarrier) } /** - * @return void + * @return \Carrier * @throws \Doctrine\ORM\ORMException * @throws \PrestaShopDatabaseException * @throws \PrestaShopException */ - public function create(): void + public function create(): PsCarrier { - $this->psCarrier = $this->createCarrier(); + $this->createCarrier(); $this->addGroups(); $this->addRanges(); $this->addZones(); - $this->psCarrier->update(); - $this->addCarrierMapping(); + + return $this->psCarrier; } /** @@ -76,14 +74,12 @@ public function create(): void */ private function addCarrierMapping(): void { - $this->carrierMappingRepository->updateOrCreate( - [ - 'myparcelCarrier' => $this->myParcelCarrier->externalIdentifier, - ], - [ - 'idCarrier' => (int) $this->psCarrier->id, - ] - ); + $values = [ + 'carrierId' => (int) $this->psCarrier->id, + 'myparcelCarrier' => $this->myParcelCarrier->externalIdentifier, + ]; + + $this->carrierMappingRepository->updateOrCreate($values, $values); } /** @@ -107,7 +103,11 @@ private function addRanges(): void { /** @var RangeWeight|RangePrice $objectClass */ foreach (self::RANGE_CLASSES as $objectClass) { - $this->deleteExistingRanges($objectClass); + $hasExistingRanges = $objectClass::getRanges($this->psCarrier->id); + + if ($hasExistingRanges) { + continue; + } $instance = new $objectClass(); @@ -124,13 +124,13 @@ private function addRanges(): void */ private function addZones(): void { - $existingZones = Arr::pluck($this->psCarrier->getZones(), 'id_zone'); + $existingZones = $this->psCarrier->getZones(); - foreach (Zone::getZones() as $zone) { - if (in_array($zone['id_zone'], $existingZones, true)) { - continue; - } + if ($existingZones) { + return; + } + foreach (Zone::getZones() as $zone) { if ($this->psCarrier->addZone($zone['id_zone'])) { continue; } @@ -140,11 +140,11 @@ private function addZones(): void } /** - * @return PsCarrier + * @return void * @throws \PrestaShopDatabaseException * @throws \PrestaShopException */ - private function createCarrier(): PsCarrier + private function createCarrier(): void { /** @var \MyParcelNL $module */ $module = Pdk::get('moduleInstance'); @@ -162,15 +162,13 @@ private function createCarrier(): PsCarrier $psCarrier->shipping_external = true; $psCarrier->shipping_method = 2; - // TODO: add logo - foreach (PsLanguage::getLanguages() as $lang) { $psCarrier->delay[$lang['id_lang']] = Language::translate('carrier_delivery_time', $lang['iso_code']); } $this->updateOrAdd($psCarrier, (bool) $psCarrier->id); - return $psCarrier; + $this->psCarrier = $psCarrier; } /** @@ -180,27 +178,7 @@ private function createCarrierIdReference(): int { $carrierId = str_pad((string) $this->myParcelCarrier->id, 3, '0'); - return (int) ($carrierId . $this->myParcelCarrier->externalIdentifier); - } - - /** - * @param class-string $objectClass - * - * @return void - */ - private function deleteExistingRanges(string $objectClass): void - { - $existing = $objectClass::getRanges($this->psCarrier->id); - - if (! $existing) { - return; - } - - $definition = $objectClass::getDefinition($objectClass); - $table = Table::withPrefix($definition['table']); - - Db::getInstance() - ->execute("DELETE FROM `$table` WHERE `id_carrier` = {$this->psCarrier->id}"); + return (int) ($carrierId . $this->myParcelCarrier->subscriptionId); } /** @@ -211,18 +189,13 @@ private function getExistingPsCarrier(): ?PsCarrier $mapping = $this->carrierMappingRepository ->findOneBy(['myparcelCarrier' => $this->myParcelCarrier->externalIdentifier]); - return $mapping ? new PsCarrier($mapping->idCarrier) : null; - } + if ($mapping) { + return new PsCarrier($mapping->getCarrierId()); + } - /** - * @param int $id - * @param string $class - * - * @return \ObjectModel - */ - private function getOrCreateModel(int $id, string $class): ObjectModel - { - return new $class($id) ?? new $class(); + $existingCarrier = PsCarrier::getCarrierByReference($this->createCarrierIdReference()); + + return $existingCarrier ?: null; } /** diff --git a/src/Contract/PsCarrierServiceInterface.php b/src/Contract/PsCarrierServiceInterface.php index d9ab0c5d..622c25df 100644 --- a/src/Contract/PsCarrierServiceInterface.php +++ b/src/Contract/PsCarrierServiceInterface.php @@ -5,6 +5,7 @@ namespace MyParcelNL\PrestaShop\Contract; use Carrier as PsCarrier; +use MyParcelNL\Pdk\Base\Support\Collection; use MyParcelNL\Pdk\Carrier\Collection\CarrierCollection; use MyParcelNL\Pdk\Carrier\Model\Carrier; @@ -13,21 +14,14 @@ interface PsCarrierServiceInterface /** * @param \MyParcelNL\Pdk\Carrier\Collection\CarrierCollection $carriers * - * @return void - */ - public function createOrUpdateCarriers(CarrierCollection $carriers): void; - - /** - * @return void + * @return \MyParcelNL\Pdk\Base\Support\Collection */ - public function deleteCarriers(): void; + public function createOrUpdateCarriers(CarrierCollection $carriers): Collection; /** - * @param $carriers - * * @return void */ - public function deleteUnusedCarriers($carriers): void; + public function disableCarriers(): void; /** * @param int|PsCarrier $input diff --git a/src/Controller/PdkController.php b/src/Controller/PdkController.php index e8eb91d2..dd204b14 100644 --- a/src/Controller/PdkController.php +++ b/src/Controller/PdkController.php @@ -35,7 +35,7 @@ public function index(): Response return new Response($e->getMessage(), 400); } - $this->flushEntityManager(); + EntityManager::flush(); return $response; } @@ -52,16 +52,4 @@ protected function createNormalizedRequest(): Request return $request; } - - /** - * @return void - */ - private function flushEntityManager(): void - { - if (! EntityManager::isOpen()) { - return; - } - - EntityManager::flush(); - } } diff --git a/src/Database/AbstractDatabaseMigration.php b/src/Database/AbstractDatabaseMigration.php index c33ad667..f8729e3c 100755 --- a/src/Database/AbstractDatabaseMigration.php +++ b/src/Database/AbstractDatabaseMigration.php @@ -7,6 +7,7 @@ use Db; use MyParcelNL\Pdk\App\Installer\Contract\MigrationInterface; use MyParcelNL\Pdk\Facade\Logger; +use MyParcelNL\PrestaShop\Database\Sql\Contract\SqlBuilderInterface; abstract class AbstractDatabaseMigration implements MigrationInterface { @@ -16,22 +17,19 @@ public function getVersion(): string } /** - * @param string $sql + * @param string|\MyParcelNL\PrestaShop\Database\Sql\Contract\SqlBuilderInterface $sql * * @return void */ - protected function execute(string $sql): void + protected function execute($sql): void { - $replacedSql = strtr($sql, [ - '{ENGINE}' => _MYSQL_ENGINE_, - ]); - - $trimmedSql = str_replace("\n", ' ', $replacedSql); - $trimmedSql = trim(preg_replace('/\s+/m', ' ', $trimmedSql)); + if ($sql instanceof SqlBuilderInterface) { + $sql = $sql->build(); + } Db::getInstance(_PS_USE_SQL_SLAVE_) - ->execute($trimmedSql); + ->execute($sql); - Logger::debug('Query executed', ['class' => static::class, 'sql' => $trimmedSql]); + Logger::debug('Query executed', ['class' => static::class, 'sql' => $sql]); } } diff --git a/src/Database/CreateCarrierMappingTableDatabaseMigration.php b/src/Database/CreateCarrierMappingTableDatabaseMigration.php index fffee3ae..4d58995a 100644 --- a/src/Database/CreateCarrierMappingTableDatabaseMigration.php +++ b/src/Database/CreateCarrierMappingTableDatabaseMigration.php @@ -4,29 +4,23 @@ namespace MyParcelNL\PrestaShop\Database; +use MyParcelNL\PrestaShop\Database\Sql\CreateTableSqlBuilder; +use MyParcelNL\PrestaShop\Database\Sql\DropTableSqlBuilder; +use MyParcelNL\PrestaShop\Entity\MyparcelnlCarrierMapping; + final class CreateCarrierMappingTableDatabaseMigration extends AbstractDatabaseMigration { public function down(): void { - $table = $this->getTable(); - $this->execute("DROP TABLE IF EXISTS `$table`"); + $this->execute(new DropTableSqlBuilder($this->getTable())); } public function up(): void { - $table = $this->getTable(); - $sql = <<getTable())) + ->id('carrier_id') + ->column('myparcel_carrier', 'VARCHAR(32)') + ->primary(['carrier_id', 'myparcel_carrier']); $this->execute($sql); } @@ -36,6 +30,6 @@ public function up(): void */ private function getTable(): string { - return Table::withPrefix(Table::TABLE_CARRIER_MAPPING); + return MyparcelnlCarrierMapping::getTable(); } } diff --git a/src/Database/CreateCartDeliveryOptionsTableDatabaseMigration.php b/src/Database/CreateCartDeliveryOptionsTableDatabaseMigration.php index 624cdfa6..5409e21b 100755 --- a/src/Database/CreateCartDeliveryOptionsTableDatabaseMigration.php +++ b/src/Database/CreateCartDeliveryOptionsTableDatabaseMigration.php @@ -4,6 +4,8 @@ namespace MyParcelNL\PrestaShop\Database; +use MyParcelNL\PrestaShop\Database\Sql\CreateTableSqlBuilder; +use MyParcelNL\PrestaShop\Database\Sql\DropTableSqlBuilder; use MyParcelNL\PrestaShop\Entity\MyparcelnlCartDeliveryOptions; /** @@ -13,24 +15,16 @@ final class CreateCartDeliveryOptionsTableDatabaseMigration extends AbstractData { public function down(): void { - $table = $this->getTable(); - $this->execute("DROP TABLE IF EXISTS `$table`"); + $this->execute(new DropTableSqlBuilder($this->getTable())); } public function up(): void { - $table = $this->getTable(); - $sql = <<getTable())) + ->id('cart_id') + ->column('data') + ->timestamps() + ->primary(['cart_id']); $this->execute($sql); } @@ -40,6 +34,6 @@ public function up(): void */ private function getTable(): string { - return Table::withPrefix(MyparcelnlCartDeliveryOptions::getTable()); + return MyparcelnlCartDeliveryOptions::getTable(); } } diff --git a/src/Database/CreateOrderDataTableDatabaseMigration.php b/src/Database/CreateOrderDataTableDatabaseMigration.php index 21126c17..bd1978f2 100755 --- a/src/Database/CreateOrderDataTableDatabaseMigration.php +++ b/src/Database/CreateOrderDataTableDatabaseMigration.php @@ -4,6 +4,8 @@ namespace MyParcelNL\PrestaShop\Database; +use MyParcelNL\PrestaShop\Database\Sql\CreateTableSqlBuilder; +use MyParcelNL\PrestaShop\Database\Sql\DropTableSqlBuilder; use MyParcelNL\PrestaShop\Entity\MyparcelnlOrderData; /** @@ -13,24 +15,16 @@ final class CreateOrderDataTableDatabaseMigration extends AbstractDatabaseMigrat { public function down(): void { - $table = $this->getTable(); - $this->execute("DROP TABLE IF EXISTS `$table`"); + $this->execute(new DropTableSqlBuilder($this->getTable())); } public function up(): void { - $table = $this->getTable(); - $sql = <<getTable())) + ->id('order_id') + ->column('data') + ->timestamps() + ->primary(['order_id']); $this->execute($sql); } @@ -40,6 +34,6 @@ public function up(): void */ private function getTable(): string { - return Table::withPrefix(MyparcelnlOrderData::getTable()); + return MyparcelnlOrderData::getTable(); } } diff --git a/src/Database/CreateOrderShipmentTableDatabaseMigration.php b/src/Database/CreateOrderShipmentTableDatabaseMigration.php index 58339c61..c5a6a896 100755 --- a/src/Database/CreateOrderShipmentTableDatabaseMigration.php +++ b/src/Database/CreateOrderShipmentTableDatabaseMigration.php @@ -4,6 +4,8 @@ namespace MyParcelNL\PrestaShop\Database; +use MyParcelNL\PrestaShop\Database\Sql\CreateTableSqlBuilder; +use MyParcelNL\PrestaShop\Database\Sql\DropTableSqlBuilder; use MyParcelNL\PrestaShop\Entity\MyparcelnlOrderShipment; /** @@ -13,26 +15,17 @@ final class CreateOrderShipmentTableDatabaseMigration extends AbstractDatabaseMi { public function down(): void { - $table = $this->getTable(); - $this->execute("DROP TABLE IF EXISTS `$table`"); + $this->execute(new DropTableSqlBuilder($this->getTable())); } public function up(): void { - $table = $this->getTable(); - $sql = <<getTable())) + ->id('order_id') + ->id('order_shipment') + ->column('data', 'TEXT NOT NULL') + ->timestamps() + ->primary(['order_id', 'order_shipment']); $this->execute($sql); } @@ -42,6 +35,6 @@ public function up(): void */ private function getTable(): string { - return Table::withPrefix(MyparcelnlOrderShipment::getTable()); + return MyparcelnlOrderShipment::getTable(); } } diff --git a/src/Database/CreateProductSettingsTableDatabaseMigration.php b/src/Database/CreateProductSettingsTableDatabaseMigration.php index 8f62601f..7bfd96e5 100755 --- a/src/Database/CreateProductSettingsTableDatabaseMigration.php +++ b/src/Database/CreateProductSettingsTableDatabaseMigration.php @@ -4,6 +4,8 @@ namespace MyParcelNL\PrestaShop\Database; +use MyParcelNL\PrestaShop\Database\Sql\CreateTableSqlBuilder; +use MyParcelNL\PrestaShop\Database\Sql\DropTableSqlBuilder; use MyParcelNL\PrestaShop\Entity\MyparcelnlProductSettings; /** @@ -13,24 +15,16 @@ final class CreateProductSettingsTableDatabaseMigration extends AbstractDatabase { public function down(): void { - $table = $this->getTable(); - $this->execute("DROP TABLE IF EXISTS `$table`"); + $this->execute(new DropTableSqlBuilder($this->getTable())); } public function up(): void { - $table = $this->getTable(); - $sql = <<getTable())) + ->id('product_id') + ->column('data') + ->primary(['product_id']) + ->timestamps(); $this->execute($sql); } @@ -40,6 +34,6 @@ public function up(): void */ private function getTable(): string { - return Table::withPrefix(MyparcelnlProductSettings::getTable()); + return MyparcelnlProductSettings::getTable(); } } diff --git a/src/Database/Sql/Contract/SqlBuilderInterface.php b/src/Database/Sql/Contract/SqlBuilderInterface.php new file mode 100644 index 00000000..024e5626 --- /dev/null +++ b/src/Database/Sql/Contract/SqlBuilderInterface.php @@ -0,0 +1,15 @@ +columns); + + $string = implode(', ', $rows); + + if (! empty($this->primary)) { + $string .= sprintf(', PRIMARY KEY (%s)', implode(', ', $this->primary)); + } + + return sprintf( + 'CREATE TABLE if NOT EXISTS `%s` (%s) ENGINE=%s DEFAULT CHARSET=utf8;', + $this->getTable(), + $string, + $this->getEngine() + ); + } + + /** + * @param string $name + * @param string $type + * @param bool $nullable + * + * @return $this + */ + public function column(string $name, string $type = 'TEXT', bool $nullable = false): self + { + $this->columns[] = [ + 'name' => $name, + 'type' => $type, + 'options' => [ + $nullable ? self::NULL : self::NOT_NULL, + ], + ]; + + return $this; + } + + /** + * @param string $name + * + * @return $this + */ + public function id(string $name): self + { + $this->columns[] = [ + 'name' => $name, + 'type' => 'INT(10) unsigned', + 'options' => [self::NOT_NULL], + ]; + + return $this; + } + + /** + * @param array $keys + * + * @return $this + */ + public function primary(array $keys): self + { + $this->primary = $keys; + + return $this; + } + + public function timestamps(): self + { + return $this + ->column('date_add', 'DATETIME') + ->column('date_upd', 'DATETIME'); + } +} diff --git a/src/Database/Sql/DropTableSqlBuilder.php b/src/Database/Sql/DropTableSqlBuilder.php new file mode 100644 index 00000000..3d39cd7e --- /dev/null +++ b/src/Database/Sql/DropTableSqlBuilder.php @@ -0,0 +1,13 @@ +getTable()); + } +} diff --git a/src/Database/Sql/SqlBuilder.php b/src/Database/Sql/SqlBuilder.php new file mode 100644 index 00000000..dd6d0d4d --- /dev/null +++ b/src/Database/Sql/SqlBuilder.php @@ -0,0 +1,39 @@ +table = $table; + } + + /** + * @return mixed + */ + protected function getEngine() + { + return _MYSQL_ENGINE_; + } + + /** + * @return string + */ + protected function getTable(): string + { + return _DB_PREFIX_ . $this->table; + } +} diff --git a/src/Entity/AbstractEntity.php b/src/Entity/AbstractEntity.php index fc04f104..17a9f892 100644 --- a/src/Entity/AbstractEntity.php +++ b/src/Entity/AbstractEntity.php @@ -8,44 +8,5 @@ abstract class AbstractEntity implements EntityInterface { - /** - * @var \DateTime - * @Doctrine\ORM\Mapping\Column(type="datetime", nullable=false, options={"default" = "CURRENT_TIMESTAMP"}) - * @Doctrine\ORM\Mapping\GeneratedValue(strategy="AUTO") - */ - public $created; - - /** - * @var int - * @Doctrine\ORM\Mapping\Id - * @Doctrine\ORM\Mapping\Column(type="integer") - * @Doctrine\ORM\Mapping\GeneratedValue(strategy="AUTO") - */ - public $id; - - /** - * @var \DateTime - * @Doctrine\ORM\Mapping\Column(type="datetime", nullable=false, options={"default" = "CURRENT_TIMESTAMP", - * "onUpdate" = "CURRENT_TIMESTAMP"}) - * @Doctrine\ORM\Mapping\GeneratedValue(strategy="AUTO") - */ - public $updated; - public function __construct() {} - - /** - * @return int - */ - public function getId(): int - { - return $this->id; - } - - /** - * @return array - */ - public function toArray(): array - { - return get_object_vars($this); - } } diff --git a/src/Entity/Concern/HasJsonData.php b/src/Entity/Concern/HasJsonData.php index 22bc66d7..8c47ef49 100644 --- a/src/Entity/Concern/HasJsonData.php +++ b/src/Entity/Concern/HasJsonData.php @@ -4,13 +4,15 @@ namespace MyParcelNL\PrestaShop\Entity\Concern; +use Doctrine\ORM\Mapping as ORM; + trait HasJsonData { /** * @var string - * @Doctrine\ORM\Mapping\Column(type="text", nullable=false) + * @ORM\Column(name="data", type="text", nullable=false) */ - public $data; + private $data; /** * @return array @@ -19,4 +21,16 @@ public function getData(): array { return json_decode($this->data, true); } + + /** + * @param string $data + * + * @return $this + */ + public function setData(string $data): self + { + $this->data = $data; + + return $this; + } } diff --git a/src/Entity/Concern/HasTimestamps.php b/src/Entity/Concern/HasTimestamps.php new file mode 100644 index 00000000..5e745388 --- /dev/null +++ b/src/Entity/Concern/HasTimestamps.php @@ -0,0 +1,63 @@ +dateAdd; + } + + public function getDateUpd(): DateTime + { + return $this->dateUpd; + } + + public function setDateAdd(DateTime $dateAdd): self + { + $this->dateAdd = $dateAdd; + + return $this; + } + + public function setDateUpd(DateTime $dateUpd): self + { + $this->dateUpd = $dateUpd; + + return $this; + } + + /** + * @ORM\PrePersist + * @ORM\PreUpdate + */ + public function updatedTimestamps(): void + { + $this->setDateUpd(new DateTime()); + + if (! $this->getDateAdd()) { + $this->setDateAdd(new DateTime()); + } + } +} diff --git a/src/Entity/Contract/EntityInterface.php b/src/Entity/Contract/EntityInterface.php index e437680e..df6775d1 100644 --- a/src/Entity/Contract/EntityInterface.php +++ b/src/Entity/Contract/EntityInterface.php @@ -14,9 +14,4 @@ public function __construct(); * @return string */ public static function getTable(): string; - - /** - * @return int - */ - public function getId(): int; } diff --git a/src/Entity/MyparcelnlCarrierMapping.php b/src/Entity/MyparcelnlCarrierMapping.php index 9ed0837b..b5b0bf93 100755 --- a/src/Entity/MyparcelnlCarrierMapping.php +++ b/src/Entity/MyparcelnlCarrierMapping.php @@ -4,29 +4,63 @@ namespace MyParcelNL\PrestaShop\Entity; +use Doctrine\ORM\Mapping as ORM; use MyParcelNL\PrestaShop\Database\Table; /** - * @Doctrine\ORM\Mapping\Table() - * @Doctrine\ORM\Mapping\Entity() + * @ORM\Table() + * @ORM\Entity() * @see \MyParcelNL\PrestaShop\Database\CreateCarrierMappingTableDatabaseMigration */ final class MyparcelnlCarrierMapping extends AbstractEntity { /** * @var int - * @Doctrine\ORM\Mapping\Column(type="integer", nullable=false, unique=true) + * @ORM\Id + * @ORM\Column(name="carrier_id", type="integer", nullable=false, unique=true) */ - public $idCarrier; + private $carrierId; /** * @var string - * @Doctrine\ORM\Mapping\Column(type="string", nullable=false, unique=true) + * @ORM\Column(name="myparcel_carrier", type="string", nullable=false, unique=true) */ - public $myparcelCarrier; + private $myparcelCarrier; public static function getTable(): string { return Table::TABLE_CARRIER_MAPPING; } + + public function getCarrierId(): int + { + return $this->carrierId; + } + + public function getMyparcelCarrier(): string + { + return $this->myparcelCarrier; + } + + public function setCarrierId(int $carrierId): MyparcelnlCarrierMapping + { + $this->carrierId = $carrierId; + + return $this; + } + + public function setMyparcelCarrier(string $myparcelCarrier): MyparcelnlCarrierMapping + { + $this->myparcelCarrier = $myparcelCarrier; + + return $this; + } + + public function toArray(): array + { + return [ + 'carrierId' => $this->getCarrierId(), + 'myparcelCarrier' => $this->getMyparcelCarrier(), + ]; + } } diff --git a/src/Entity/MyparcelnlCartDeliveryOptions.php b/src/Entity/MyparcelnlCartDeliveryOptions.php index 69df78ba..a86a0aeb 100755 --- a/src/Entity/MyparcelnlCartDeliveryOptions.php +++ b/src/Entity/MyparcelnlCartDeliveryOptions.php @@ -4,26 +4,52 @@ namespace MyParcelNL\PrestaShop\Entity; +use Doctrine\ORM\Mapping as ORM; use MyParcelNL\PrestaShop\Database\Table; use MyParcelNL\PrestaShop\Entity\Concern\HasJsonData; +use MyParcelNL\PrestaShop\Entity\Concern\HasTimestamps; /** - * @Doctrine\ORM\Mapping\Table() - * @Doctrine\ORM\Mapping\Entity() + * @ORM\Table + * @ORM\Entity * @see \MyParcelNL\PrestaShop\Database\CreateOrderDataTableDatabaseMigration */ final class MyparcelnlCartDeliveryOptions extends AbstractEntity { use HasJsonData; + use HasTimestamps; /** * @var int - * @Doctrine\ORM\Mapping\Column(type="integer", nullable=false, unique=true) + * @ORM\Id + * @ORM\Column(name="cart_id", type="integer") */ - public $idCart; + private $cartId; public static function getTable(): string { return Table::TABLE_CART_DELIVERY_OPTIONS; } + + public function getCartId(): int + { + return $this->cartId; + } + + public function setCartId(int $cartId): MyparcelnlCartDeliveryOptions + { + $this->cartId = $cartId; + + return $this; + } + + public function toArray(): array + { + return [ + 'cartId' => $this->getCartId(), + 'data' => $this->getData(), + 'dateAdd' => $this->getDateAdd(), + 'dateUpd' => $this->getDateUpd(), + ]; + } } diff --git a/src/Entity/MyparcelnlOrderData.php b/src/Entity/MyparcelnlOrderData.php index c003100a..53f806c0 100755 --- a/src/Entity/MyparcelnlOrderData.php +++ b/src/Entity/MyparcelnlOrderData.php @@ -4,12 +4,13 @@ namespace MyParcelNL\PrestaShop\Entity; +use Doctrine\ORM\Mapping as ORM; use MyParcelNL\PrestaShop\Database\Table; use MyParcelNL\PrestaShop\Entity\Concern\HasJsonData; /** - * @Doctrine\ORM\Mapping\Table() - * @Doctrine\ORM\Mapping\Entity() + * @ORM\Table + * @ORM\Entity * @see \MyParcelNL\PrestaShop\Database\CreateOrderDataTableDatabaseMigration */ final class MyparcelnlOrderData extends AbstractEntity @@ -17,13 +18,34 @@ final class MyparcelnlOrderData extends AbstractEntity use HasJsonData; /** - * @var string - * @Doctrine\ORM\Mapping\Column(type="string", nullable=false, unique=true) + * @var int + * @ORM\Id + * @ORM\Column(name="order_id", type="integer") */ - public $idOrder; + private $orderId; public static function getTable(): string { return Table::TABLE_ORDER_DATA; } + + public function getOrderId(): int + { + return $this->orderId; + } + + public function setOrderId(int $orderId): MyparcelnlOrderData + { + $this->orderId = $orderId; + + return $this; + } + + public function toArray(): array + { + return [ + 'orderId' => $this->getOrderId(), + 'data' => $this->getData(), + ]; + } } diff --git a/src/Entity/MyparcelnlOrderShipment.php b/src/Entity/MyparcelnlOrderShipment.php index 47bd292e..7184d6c9 100755 --- a/src/Entity/MyparcelnlOrderShipment.php +++ b/src/Entity/MyparcelnlOrderShipment.php @@ -4,32 +4,71 @@ namespace MyParcelNL\PrestaShop\Entity; +use Doctrine\ORM\Mapping as ORM; use MyParcelNL\PrestaShop\Database\Table; use MyParcelNL\PrestaShop\Entity\Concern\HasJsonData; +use MyParcelNL\PrestaShop\Entity\Concern\HasTimestamps; /** - * @Doctrine\ORM\Mapping\Table() - * @Doctrine\ORM\Mapping\Entity() + * @ORM\Table + * @ORM\Entity * @see \MyParcelNL\PrestaShop\Database\CreateOrderShipmentTableDatabaseMigration */ final class MyparcelnlOrderShipment extends AbstractEntity { use HasJsonData; + use HasTimestamps; /** * @var string - * @Doctrine\ORM\Mapping\Column(type="string", nullable=false) + * @ORM\Column(name="order_id", type="integer") */ - public $idOrder; + private $orderId; /** * @var int - * @Doctrine\ORM\Mapping\Column(type="integer", nullable=false, unique=true) + * @ORM\Id + * @ORM\Column(name="shipment_id", type="integer") */ - public $idShipment; + private $shipmentId; public static function getTable(): string { return Table::TABLE_ORDER_SHIPMENT; } + + public function getOrderId(): string + { + return $this->orderId; + } + + public function getShipmentId(): int + { + return $this->shipmentId; + } + + public function setOrderId(string $orderId): self + { + $this->orderId = $orderId; + + return $this; + } + + public function setShipmentId(int $shipmentId): self + { + $this->shipmentId = $shipmentId; + + return $this; + } + + public function toArray(): array + { + return [ + 'orderId' => $this->getOrderId(), + 'shipmentId' => $this->getShipmentId(), + 'data' => $this->getData(), + 'dateAdd' => $this->getDateAdd(), + 'dateUpd' => $this->getDateUpd(), + ]; + } } diff --git a/src/Entity/MyparcelnlProductSettings.php b/src/Entity/MyparcelnlProductSettings.php index d8f76f61..ebeb4832 100755 --- a/src/Entity/MyparcelnlProductSettings.php +++ b/src/Entity/MyparcelnlProductSettings.php @@ -4,26 +4,52 @@ namespace MyParcelNL\PrestaShop\Entity; +use Doctrine\ORM\Mapping as ORM; use MyParcelNL\PrestaShop\Database\Table; use MyParcelNL\PrestaShop\Entity\Concern\HasJsonData; +use MyParcelNL\PrestaShop\Entity\Concern\HasTimestamps; /** - * @Doctrine\ORM\Mapping\Table() - * @Doctrine\ORM\Mapping\Entity() + * @ORM\Table + * @ORM\Entity * @see \MyParcelNL\PrestaShop\Database\CreateProductSettingsTableDatabaseMigration */ final class MyparcelnlProductSettings extends AbstractEntity { use HasJsonData; + use HasTimestamps; /** * @var int - * @Doctrine\ORM\Mapping\Column(type="integer", nullable=false, unique=true) + * @ORM\Id + * @ORM\Column(name="product_id", type="integer") */ - public $idProduct; + private $productId; public static function getTable(): string { return Table::TABLE_PRODUCT_SETTINGS; } + + public function getProductId(): int + { + return $this->productId; + } + + public function setProductId(int $productId): MyparcelnlProductSettings + { + $this->productId = $productId; + + return $this; + } + + public function toArray(): array + { + return [ + 'productId' => $this->getProductId(), + 'data' => $this->getData(), + 'dateAdd' => $this->getDateAdd(), + 'dateUpd' => $this->getDateUpd(), + ]; + } } diff --git a/src/Facade/EntityManager.php b/src/Facade/EntityManager.php index 6b11a8a6..37956cd3 100644 --- a/src/Facade/EntityManager.php +++ b/src/Facade/EntityManager.php @@ -5,34 +5,16 @@ namespace MyParcelNL\PrestaShop\Facade; use MyParcelNL\Pdk\Base\Facade; -use MyParcelNL\Pdk\Facade\Logger; +use MyParcelNL\PrestaShop\Service\PsEntityManagerService; /** - * @see \Doctrine\ORM\EntityManager + * @see PsEntityManagerService * @method static void flush() - * @method static bool isOpen() */ final class EntityManager extends Facade { - /** - * @param $method - * @param $args - * - * @return mixed - * @throws \DI\DependencyException - * @throws \DI\NotFoundException - * @throws \MyParcelNL\Pdk\Base\Exception\InvalidFacadeException - * @todo remove this - */ - public static function __callStatic($method, $args) - { - Logger::debug(sprintf('Calling EntityManager::%s()', $method)); - - return parent::__callStatic($method, $args); // TODO: Change the autogenerated stub - } - protected static function getFacadeAccessor(): string { - return 'ps.entityManager'; + return PsEntityManagerService::class; } } diff --git a/src/Hooks/HasPdkCheckoutHooks.php b/src/Hooks/HasPdkCheckoutHooks.php index be39d2f5..ec4466e1 100644 --- a/src/Hooks/HasPdkCheckoutHooks.php +++ b/src/Hooks/HasPdkCheckoutHooks.php @@ -70,7 +70,7 @@ private function saveOrderData(int $cartId, PdkOrder $order): void $cartDeliveryOptionsRepository->updateOrCreate( [ - 'idCart' => $cartId, + 'cartId' => $cartId, ], [ 'data' => json_encode($order->toStorableArray()), diff --git a/src/Hooks/HasPsCarrierHooks.php b/src/Hooks/HasPsCarrierHooks.php index 9afefcf1..554b1bf5 100644 --- a/src/Hooks/HasPsCarrierHooks.php +++ b/src/Hooks/HasPsCarrierHooks.php @@ -56,7 +56,7 @@ public function updateCarrierId(int $oldId, int $newId): void /** @var PsCarrierMappingRepository $carrierMappingRepository */ $carrierMappingRepository = Pdk::get(PsCarrierMappingRepository::class); - $carrierMappingRepository->update(['idCarrier' => $newId], ['idCarrier' => $oldId]); + $carrierMappingRepository->update(['carrierId' => $newId], ['carrierId' => $oldId]); EntityManager::flush(); } diff --git a/src/Pdk/Account/Repository/PsPdkAccountRepository.php b/src/Pdk/Account/Repository/PsPdkAccountRepository.php index 46d7a64c..8af06346 100644 --- a/src/Pdk/Account/Repository/PsPdkAccountRepository.php +++ b/src/Pdk/Account/Repository/PsPdkAccountRepository.php @@ -7,13 +7,10 @@ use MyParcelNL\Pdk\Account\Model\Account; use MyParcelNL\Pdk\Account\Repository\AccountRepository; use MyParcelNL\Pdk\App\Account\Repository\AbstractPdkAccountRepository; -use MyParcelNL\Pdk\Facade\Logger; use MyParcelNL\Pdk\Facade\Pdk; use MyParcelNL\Pdk\Settings\Contract\SettingsRepositoryInterface; use MyParcelNL\Pdk\Storage\Contract\StorageInterface; use MyParcelNL\PrestaShop\Configuration\Contract\ConfigurationServiceInterface; -use MyParcelNL\PrestaShop\Contract\PsCarrierServiceInterface; -use Throwable; final class PsPdkAccountRepository extends AbstractPdkAccountRepository { @@ -58,14 +55,6 @@ public function store(?Account $account): ?Account $this->configurationService->set($key, $account->toStorableArray()); - try { - /** @var PsCarrierServiceInterface $carrierService */ - $carrierService = Pdk::get(PsCarrierServiceInterface::class); - $carrierService->updateCarriers(); - } catch (Throwable $e) { - Logger::error($e->getMessage()); - } - return $this->save(self::STORAGE_KEY_ACCOUNT, $account); } diff --git a/src/Pdk/Action/Backend/Account/PsUpdateAccountAction.php b/src/Pdk/Action/Backend/Account/PsUpdateAccountAction.php new file mode 100644 index 00000000..07e3f9ba --- /dev/null +++ b/src/Pdk/Action/Backend/Account/PsUpdateAccountAction.php @@ -0,0 +1,27 @@ +updateCarriers(); + } +} diff --git a/src/Pdk/Base/PsPdkBootstrapper.php b/src/Pdk/Base/PsPdkBootstrapper.php index 43f4d704..d38f12a0 100644 --- a/src/Pdk/Base/PsPdkBootstrapper.php +++ b/src/Pdk/Base/PsPdkBootstrapper.php @@ -43,7 +43,7 @@ protected function getAdditionalConfig( string $url ): array { return array_replace( - $this->getConfig($version, $name), + $this->getConfig($version, $name, $path), $this->resolvePrestaShopServices() ); } @@ -51,10 +51,11 @@ protected function getAdditionalConfig( /** * @param string $version * @param string $name + * @param string $path * * @return array */ - protected function getConfig(string $version, string $name): array + protected function getConfig(string $version, string $name, string $path): array { return [ 'userAgent' => value([ @@ -77,7 +78,8 @@ protected function getConfig(string $version, string $name): array 'sidebarParentClass' => value('AdminParentShipping'), - 'logDirectory' => value(sprintf('%s/var/logs/%s', _PS_ROOT_DIR_, $name)), + 'logDirectory' => value(sprintf('%s/var/logs/%s', _PS_ROOT_DIR_, $name)), + 'carrierLogosDirectory' => value(sprintf('%sprivate/carrier-logos/', $path)), /** * The symfony routes that are used by the pdk. Must match the routes in config/routes.yml. diff --git a/src/Pdk/Installer/Migration/Migration2_0_0.php b/src/Pdk/Installer/Migration/Migration2_0_0.php index e4015550..89fcaae8 100644 --- a/src/Pdk/Installer/Migration/Migration2_0_0.php +++ b/src/Pdk/Installer/Migration/Migration2_0_0.php @@ -668,7 +668,7 @@ private function migrateDeliveryOptions(): void $this->orderDataRepository->updateOrCreate( [ - 'idOrder' => (string) $orderId, + 'orderId' => (string) $orderId, ], [ 'data' => json_encode(['deliveryOptions' => $deliveryOptions]), @@ -702,10 +702,10 @@ private function migrateOrderShipments(): void $this->orderShipmentRepository->updateOrCreate( [ - 'idShipment' => (int) $oldOrderLabel['id_label'], + 'shipmentId' => (int) $oldOrderLabel['id_label'], ], [ - 'idOrder' => (string) $oldOrderLabel['id_order'], + 'orderId' => (string) $oldOrderLabel['id_order'], 'data' => json_encode($shipment), ] ); @@ -739,7 +739,7 @@ private function migrateProductSettings(): void foreach ($productsWithSettings as $productId => $productSettings) { $this->productSettingsRepository->updateOrCreate( [ - 'idProduct' => (int) $productId, + 'productId' => (int) $productId, ], [ 'data' => json_encode([ diff --git a/src/Pdk/Order/Repository/PsPdkOrderRepository.php b/src/Pdk/Order/Repository/PsPdkOrderRepository.php index 9328a5e6..edc80bee 100644 --- a/src/Pdk/Order/Repository/PsPdkOrderRepository.php +++ b/src/Pdk/Order/Repository/PsPdkOrderRepository.php @@ -148,8 +148,8 @@ public function updateMany(PdkOrderCollection $collection): PdkOrderCollection $order->shipments->each(function (Shipment $shipment) use ($order) { $this->psOrderShipmentRepository->updateOrCreate( [ - 'idOrder' => $order->externalIdentifier, - 'idShipment' => $shipment->id, + 'orderId' => $order->externalIdentifier, + 'shipmentId' => $shipment->id, ], [ 'data' => json_encode($shipment->toStorableArray()), @@ -218,11 +218,11 @@ protected function getKeyPrefix(): string protected function getShipments(Order $order): Collection { return $this->psOrderShipmentRepository - ->where('idOrder', $order->id) + ->where('orderId', $order->id) ->map(static function (MyparcelnlOrderShipment $shipment) { return array_replace($shipment->getData(), [ - 'id' => $shipment->idShipment, - 'orderId' => $shipment->idOrder, + 'id' => $shipment->getShipmentId(), + 'orderId' => $shipment->getOrderId(), ]); }); } diff --git a/src/Pdk/Product/Repository/PdkProductRepository.php b/src/Pdk/Product/Repository/PdkProductRepository.php index bee165ef..1b998de4 100644 --- a/src/Pdk/Product/Repository/PdkProductRepository.php +++ b/src/Pdk/Product/Repository/PdkProductRepository.php @@ -97,7 +97,7 @@ public function getProductSettings($identifier): ProductSettings { return $this->retrieve("product_settings_$identifier", function () use ($identifier) { /** @var \MyParcelNL\PrestaShop\Entity\MyparcelnlProductSettings $psProductSettings */ - $psProductSettings = $this->psProductRepository->findOneBy(['idProduct' => $identifier]); + $psProductSettings = $this->psProductRepository->findOneBy(['productId' => $identifier]); $data = $psProductSettings ? $psProductSettings->toArray() : []; $parameters = json_decode($data['data'] ?? '', true); @@ -127,7 +127,7 @@ public function update(PdkProduct $product): void { $this->productSettingsRepository->updateOrCreate( [ - 'idProduct' => (int) $product->externalIdentifier, + 'productId' => (int) $product->externalIdentifier, ], [ 'data' => json_encode($product->settings->toArray()), diff --git a/src/Repository/AbstractPsObjectRepository.php b/src/Repository/AbstractPsObjectRepository.php index 580a9662..824e0391 100644 --- a/src/Repository/AbstractPsObjectRepository.php +++ b/src/Repository/AbstractPsObjectRepository.php @@ -4,7 +4,6 @@ namespace MyParcelNL\PrestaShop\Repository; -use DateTime; use Doctrine\ORM\EntityNotFoundException; use MyParcelNL\Pdk\Base\Support\Collection; use MyParcelNL\Pdk\Base\Support\Utils; @@ -144,14 +143,12 @@ public function updateOrCreate(array $where, array $values) } if (! $entity) { - $entity = $this->createEntity(); - $entity->created = new DateTime(); + $entity = $this->createEntity(); } - $entity->updated = new DateTime(); - foreach (array_replace($where, $values) as $key => $value) { - $entity->{$key} = $value; + $setter = sprintf('set%s', Str::studly($key)); + $entity->{$setter}($value); } $this->entityManager->persist($entity); diff --git a/src/Service/PsCarrierService.php b/src/Service/PsCarrierService.php index c8fc11dc..50e011d8 100644 --- a/src/Service/PsCarrierService.php +++ b/src/Service/PsCarrierService.php @@ -5,13 +5,16 @@ namespace MyParcelNL\PrestaShop\Service; use Carrier as PsCarrier; +use Context; +use MyParcelNL\Pdk\Base\Support\Collection; use MyParcelNL\Pdk\Carrier\Collection\CarrierCollection; use MyParcelNL\Pdk\Carrier\Model\Carrier; use MyParcelNL\Pdk\Facade\AccountSettings; use MyParcelNL\Pdk\Facade\Logger; +use MyParcelNL\Pdk\Facade\Pdk; use MyParcelNL\PrestaShop\Carrier\Service\CarrierBuilder; use MyParcelNL\PrestaShop\Contract\PsCarrierServiceInterface; -use MyParcelNL\PrestaShop\Entity\MyparcelnlCarrierMapping; +use MyParcelNL\PrestaShop\Facade\EntityManager; use MyParcelNL\PrestaShop\Facade\MyParcelModule; use MyParcelNL\PrestaShop\Repository\PsCarrierMappingRepository; @@ -33,89 +36,43 @@ public function __construct(PsCarrierMappingRepository $carrierMappingRepository /** * @param \MyParcelNL\Pdk\Carrier\Collection\CarrierCollection $carriers * - * @return void - * @throws \Doctrine\ORM\ORMException - * @throws \PrestaShopDatabaseException - * @throws \PrestaShopException - */ - public function createOrUpdateCarriers(CarrierCollection $carriers): void - { - $carriers->each(static function (Carrier $carrier): void { - $builder = new CarrierBuilder($carrier); - - $builder->create(); - }); - - Logger::debug( - 'Created carriers', - [ - 'carriers' => $carriers - ->pluck('externalIdentifier') - ->toArray(), - ] - ); - } - - /** - * @return void - * @throws \Doctrine\ORM\Exception\ORMException - * @throws \Doctrine\ORM\ORMException - * @throws \Doctrine\ORM\OptimisticLockException + * @return \MyParcelNL\Pdk\Base\Support\Collection */ - public function deleteCarriers(): void + public function createOrUpdateCarriers(CarrierCollection $carriers): Collection { - $mappings = $this->carrierMappingRepository->all(); - - $mappings->each(function (MyparcelnlCarrierMapping $carrier): void { - $psCarrier = new PsCarrier($carrier->idCarrier); + return (new Collection($carriers))->map( + static function (Carrier $carrier): PsCarrier { + $builder = new CarrierBuilder($carrier); - if (! $psCarrier->delete()) { - Logger::error('Failed to delete carrier', [ - 'id' => $carrier->idCarrier, - 'myParcelCarrier' => $carrier->myparcelCarrier, - ]); + Logger::info('Created carrier ' . $carrier->externalIdentifier); - return; + return $builder->create(); } - - $this->carrierMappingRepository->delete($carrier); - - Logger::debug('Deleted carrier', [ - 'id' => $carrier->idCarrier, - 'myParcelCarrier' => $carrier->myparcelCarrier, - ]); - }); + ); } /** - * @param $carriers - * * @return void + * @throws \PrestaShopDatabaseException + * @throws \PrestaShopException */ - public function deleteUnusedCarriers($carriers): void + public function disableCarriers(): void { - // delete other carriers - $mappings = $this->carrierMappingRepository->all(); + $psCarriers = new Collection($this->getPsCarriers()); - $mappings - ->filter(static function (MyparcelnlCarrierMapping $entity) use ($carriers): bool { - return ! $carriers->containsStrict('externalIdentifier', $entity->myparcelCarrier); - }) - ->each(static function (MyparcelnlCarrierMapping $mapping): void { - $psCarrier = new PsCarrier($mapping->idCarrier); + $psCarriers->where('external_module_name', Pdk::getAppInfo()->name) + ->each(function (array $carrier): void { + $psCarrier = new PsCarrier($carrier['id_carrier']); - $context = [ - 'id' => $mapping->idCarrier, - 'myParcelCarrier' => $mapping->myparcelCarrier, - ]; + $psCarrier->active = false; - if (! $psCarrier->delete()) { - Logger::error('Failed to delete carrier', $context); + if (! $psCarrier->softDelete()) { + Logger::error("Failed to soft delete carrier {$carrier['id_carrier']}"); return; } - Logger::debug('Deleted carrier', $context); + Logger::debug("Soft deleted carrier {$carrier['id_carrier']}"); }); } @@ -163,9 +120,9 @@ public function getMyParcelCarrier($input): ?Carrier public function getMyParcelCarrierIdentifier($input): ?string { $psCarrierId = $this->getId($input); - $match = $this->carrierMappingRepository->firstWhere('idCarrier', $psCarrierId); + $match = $this->carrierMappingRepository->firstWhere('carrierId', $psCarrierId); - return $match->myparcelCarrier ?? null; + return $match ? $match->getMyparcelCarrier() : null; } /** @@ -187,10 +144,54 @@ public function updateCarriers(): void { $carriers = AccountSettings::getCarriers(); - $this->createOrUpdateCarriers($carriers); - $this->deleteUnusedCarriers($carriers); + $createdCarriers = $this->createOrUpdateCarriers($carriers); + $this->deleteUnusedCarriers($createdCarriers); // Refresh the hooks MyParcelModule::registerHooks(); + EntityManager::flush(); + } + + /** + * @param \MyParcelNL\Pdk\Base\Support\Collection $createdCarriers + * + * @return void + */ + protected function deleteUnusedCarriers(Collection $createdCarriers): void + { + $psCarriers = new Collection($this->getPsCarriers()); + $moduleName = Pdk::getAppInfo()->name; + + $psCarriers + ->filter(function (array $carrier) use ($moduleName, $createdCarriers): bool { + return $carrier['external_module_name'] === $moduleName + && ! $createdCarriers->contains('id', $carrier['id_carrier']); + }) + ->each(static function (array $carrier): void { + $psCarrier = new PsCarrier($carrier['id_carrier']); + + if (! $psCarrier->delete()) { + Logger::error("Failed to delete carrier {$carrier['id_carrier']}"); + + return; + } + + Logger::debug("Deleted carrier {$carrier['id_carrier']}"); + }); + } + + /** + * @return array + */ + private function getPsCarriers(): array + { + return PsCarrier::getCarriers( + Context::getContext()->language->id, + false, + false, + null, + null, + PsCarrier::CARRIERS_MODULE + ); } } diff --git a/src/Service/PsEntityManagerService.php b/src/Service/PsEntityManagerService.php new file mode 100644 index 00000000..bda64874 --- /dev/null +++ b/src/Service/PsEntityManagerService.php @@ -0,0 +1,39 @@ +entityManager = Pdk::get('ps.entityManager'); + } + + public function flush(): void + { + if (! $this->entityManager->isOpen()) { + Logger::info('Entity manager is closed'); + + return; + } + + try { + $this->entityManager->flush(); + } catch (Throwable $e) { + Logger::error('Failed to flush entity manager', ['exception' => $e]); + + throw $e; + } + } +} diff --git a/src/Service/PsOrderService.php b/src/Service/PsOrderService.php index 5647e3f9..c6499a76 100644 --- a/src/Service/PsOrderService.php +++ b/src/Service/PsOrderService.php @@ -69,7 +69,7 @@ public function get($input): Order */ public function getOrderData($orderOrId): array { - $fromOrder = $this->psOrderDataRepository->findOneBy(['idOrder' => (int) $this->getOrderId($orderOrId)]); + $fromOrder = $this->psOrderDataRepository->findOneBy(['orderId' => (int) $this->getOrderId($orderOrId)]); if (! $fromOrder) { return $this->getFromCart($this->get($orderOrId)); @@ -88,7 +88,7 @@ public function getOrderData($orderOrId): array public function updateData($orderOrId, array $orderData): void { $this->psOrderDataRepository->updateOrCreate( - ['idOrder' => (int) $this->getOrderId($orderOrId)], + ['orderId' => (int) $this->getOrderId($orderOrId)], ['data' => json_encode($orderData)] ); } @@ -103,7 +103,7 @@ public function updateData($orderOrId, array $orderData): void */ private function getFromCart(Order $order): array { - $fromCart = $this->psCartDeliveryOptionsRepository->findOneBy(['idCart' => $order->id_cart]); + $fromCart = $this->psCartDeliveryOptionsRepository->findOneBy(['cartId' => $order->id_cart]); if ($fromCart) { Logger::debug("[Order $order->id] Delivery options found in cart, saving to order"); diff --git a/tests/Bootstrap/PsTestBootstrapper.php b/tests/Bootstrap/PsTestBootstrapper.php new file mode 100644 index 00000000..a31c7ddc --- /dev/null +++ b/tests/Bootstrap/PsTestBootstrapper.php @@ -0,0 +1,10 @@ +withCreated('2020-01-01 08:00:00') - ->withUpdated('2020-01-02 12:00:00'); - } - /** * @param class-string $class * @param array $attributes @@ -40,7 +29,8 @@ protected function createObject(string $class, array $attributes) $instance = new $class(); foreach ($attributes as $key => $value) { - $instance->{Str::camel($key)} = $value; + $setter = sprintf('set%s', Str::studly($key)); + $instance->{$setter}($value); } return $instance; diff --git a/tests/Factory/AbstractPsModelFactory.php b/tests/Factory/AbstractPsModelFactory.php index 9bf7d64b..2e33d57e 100644 --- a/tests/Factory/AbstractPsModelFactory.php +++ b/tests/Factory/AbstractPsModelFactory.php @@ -4,7 +4,7 @@ namespace MyParcelNL\PrestaShop\Tests\Factory; -use Props\BadMethodCallException; +use BadMethodCallException; /** * @template T @@ -37,7 +37,6 @@ public function make() /** * @return T - * @throws \Exception */ public function store() { @@ -81,32 +80,13 @@ protected function getNextId(string $key = null): int return $this->state->getNextId($key); } - /** - * @return array - */ - protected function resolveAttributes(): array - { - return array_replace(parent::resolveAttributes(), ['id' => $this->getId()]); - } - /** * @param T $model * * @return void - * @throws \Exception */ protected function save($model): void { throw new BadMethodCallException('Not implemented'); } - - /** - * @param int $id - * - * @return self - */ - protected function withId(int $id): self - { - return $this->with(['id' => $id]); - } } diff --git a/tests/Factory/AbstractPsObjectModelFactory.php b/tests/Factory/AbstractPsObjectModelFactory.php index 1b04550c..265a4b70 100644 --- a/tests/Factory/AbstractPsObjectModelFactory.php +++ b/tests/Factory/AbstractPsObjectModelFactory.php @@ -17,6 +17,16 @@ */ abstract class AbstractPsObjectModelFactory extends AbstractPsModelFactory implements PsObjectModelFactoryInterface { + /** + * @param int $id + * + * @return self + */ + public function withId(int $id): self + { + return $this->with(['id' => $id]); + } + /** * @return \MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface */ @@ -57,6 +67,14 @@ protected function getClass(): string */ abstract protected function getObjectModelClass(): string; + /** + * @return array + */ + protected function resolveAttributes(): array + { + return array_replace(parent::resolveAttributes(), ['id' => $this->getId()]); + } + /** * @param T $model * diff --git a/tests/Mock/MockPsCarrier.php b/tests/Mock/MockPsCarrier.php index 77d6190b..5db325e0 100644 --- a/tests/Mock/MockPsCarrier.php +++ b/tests/Mock/MockPsCarrier.php @@ -10,6 +10,12 @@ abstract class MockPsCarrier extends ObjectModel { + public const PS_CARRIERS_ONLY = 1; + public const CARRIERS_MODULE = 2; + public const CARRIERS_MODULE_NEED_RANGE = 3; + public const PS_CARRIERS_AND_CARRIER_MODULES_NEED_RANGE = 4; + public const ALL_CARRIERS = 5; + public $attributes = [ 'zones' => [], ]; diff --git a/tests/Mock/MockPsEntityManager.php b/tests/Mock/MockPsEntityManager.php index e2230244..bef4f650 100644 --- a/tests/Mock/MockPsEntityManager.php +++ b/tests/Mock/MockPsEntityManager.php @@ -32,6 +32,11 @@ public function getRepository(string $entityName): MockPsEntityRepository return self::$repositories[$entityName]; } + public function isOpen(): bool + { + return true; + } + public function persist($entity): void { MockPsEntities::addOrUpdate($entity); diff --git a/tests/Mock/MockPsEntityRepository.php b/tests/Mock/MockPsEntityRepository.php index 018a56ae..82157875 100644 --- a/tests/Mock/MockPsEntityRepository.php +++ b/tests/Mock/MockPsEntityRepository.php @@ -6,6 +6,7 @@ use MyParcelNL\Pdk\Base\Support\Collection; use MyParcelNL\PrestaShop\Entity\Contract\EntityInterface; +use MyParcelNL\Sdk\src\Support\Str; /** * @template T of EntityInterface @@ -66,7 +67,8 @@ public function findBy(array $criteria, array $orderBy = null, $limit = null, $o ->filter( static function (object $object) use ($criteria) { foreach ($criteria as $key => $value) { - if ($object->{$key} === $value) { + $getter = sprintf('get%s', Str::studly($key)); + if ($object->{$getter}() === $value) { continue; } diff --git a/tests/Pest.php b/tests/Pest.php index 5f14e206..243400d5 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -19,10 +19,15 @@ require __DIR__ . '/mock_namespaced_class_map.php'; require __DIR__ . '/mock_class_map.php'; -const _DB_PREFIX_ = 'ps_'; -const _PS_MODE_DEV_ = false; -const _PS_ROOT_DIR_ = __DIR__ . '/../'; -const _PS_VERSION_ = '8.0.0'; +const _PS_ROOT_DIR_ = '/var/www/html/'; +const _PS_CORE_DIR_ = _PS_ROOT_DIR_; +const _PS_MODULE_DIR_ = _PS_CORE_DIR_ . 'modules/'; +const _PS_SHIP_IMG_DIR_ = _PS_CORE_DIR_ . 's/'; + +const _DB_PREFIX_ = 'ps_'; +const _MYSQL_ENGINE_ = 'InnoDB'; +const _PS_MODE_DEV_ = false; +const _PS_VERSION_ = '8.0.0'; usesShared(new ClearContainerCache())->in(__DIR__); diff --git a/tests/Unit/Database/Sql/CreateTableSqlBuilderTest.php b/tests/Unit/Database/Sql/CreateTableSqlBuilderTest.php new file mode 100644 index 00000000..7fa1d908 --- /dev/null +++ b/tests/Unit/Database/Sql/CreateTableSqlBuilderTest.php @@ -0,0 +1,23 @@ +id('id'); + $builder->column('name', 'VARCHAR(255)'); + $builder->column('nullable', 'VARCHAR(255)', true); + $builder->primary(['id']); + + $expected = <<build())->toBe($expected); +}); diff --git a/tests/Unit/Database/Sql/DropTableSqlBuilderTest.php b/tests/Unit/Database/Sql/DropTableSqlBuilderTest.php new file mode 100644 index 00000000..325d6553 --- /dev/null +++ b/tests/Unit/Database/Sql/DropTableSqlBuilderTest.php @@ -0,0 +1,13 @@ +build())->toBe($expected); +}); diff --git a/tests/Unit/Hooks/HasPsCarrierHooksTest.php b/tests/Unit/Hooks/HasPsCarrierHooksTest.php index 8295eab4..948a085e 100644 --- a/tests/Unit/Hooks/HasPsCarrierHooksTest.php +++ b/tests/Unit/Hooks/HasPsCarrierHooksTest.php @@ -32,7 +32,7 @@ class WithHasPsCarrierHooks ->store(); psFactory(MyparcelnlCarrierMapping::class) - ->withIdCarrier(14) + ->withCarrierId(14) ->withMyparcelCarrier(Carrier::CARRIER_DPD_NAME) ->store(); @@ -50,5 +50,5 @@ class WithHasPsCarrierHooks throw new Exception('No match found'); } - expect($found->idCarrier)->toBe(15); + expect($found->getCarrierId())->toBe(15); }); diff --git a/tests/Unit/Service/PsCarrierServiceTest.php b/tests/Unit/Service/PsCarrierServiceTest.php index 8200de47..b14afec2 100644 --- a/tests/Unit/Service/PsCarrierServiceTest.php +++ b/tests/Unit/Service/PsCarrierServiceTest.php @@ -7,14 +7,21 @@ use Carrier as PsCarrier; use MyParcelNL\Pdk\Account\Model\Shop; -use MyParcelNL\Pdk\Base\Support\Arr; +use MyParcelNL\Pdk\App\Api\Backend\PdkBackendActions; use MyParcelNL\Pdk\Base\Support\Collection; use MyParcelNL\Pdk\Carrier\Collection\CarrierCollection; use MyParcelNL\Pdk\Carrier\Model\Carrier; +use MyParcelNL\Pdk\Facade\Actions; use MyParcelNL\Pdk\Facade\Pdk; +use MyParcelNL\Pdk\Tests\Api\Response\ExampleAclResponse; +use MyParcelNL\Pdk\Tests\Api\Response\ExampleGetAccountsResponse; +use MyParcelNL\Pdk\Tests\Api\Response\ExampleGetCarrierConfigurationResponse; +use MyParcelNL\Pdk\Tests\Api\Response\ExampleGetCarrierOptionsResponse; +use MyParcelNL\Pdk\Tests\Bootstrap\MockApi; use MyParcelNL\Pdk\Tests\Bootstrap\TestBootstrapper; use MyParcelNL\PrestaShop\Contract\PsCarrierServiceInterface; use MyParcelNL\PrestaShop\Repository\PsCarrierMappingRepository; +use MyParcelNL\PrestaShop\Tests\Bootstrap\PsTestBootstrapper; use MyParcelNL\PrestaShop\Tests\Uses\UsesMockPsPdkInstance; use Psr\Log\LoggerInterface; use RangePrice; @@ -25,17 +32,6 @@ usesShared(new UsesMockPsPdkInstance()); -function createCarrierMappingsArray(Collection $carrierMappings): array -{ - expect($carrierMappings->all())->each->toHaveKeys(['created', 'updated']); - - return (new Collection($carrierMappings->toArray())) - ->map(function (array $item) { - return Arr::except($item, ['created', 'updated']); - }) - ->toArrayWithoutNull(); -} - function doSnapshotTest(Collection $carrierMappings, Collection $psCarriers): void { /** @var \MyParcelNL\Pdk\Tests\Bootstrap\MockLogger $logger */ @@ -60,13 +56,23 @@ function doSnapshotTest(Collection $carrierMappings, Collection $psCarriers): vo ]); }) ->toArrayWithoutNull(), - 'carrierMappings' => createCarrierMappingsArray($carrierMappings), + 'carrierMappings' => $carrierMappings->toArray(), ]) ); } it('creates carriers on account update', function () { TestBootstrapper::hasAccount(); + PsTestBootstrapper::hasCarrierImages(); + + MockApi::enqueue( + new ExampleGetAccountsResponse(), + new ExampleGetCarrierConfigurationResponse(), + new ExampleGetCarrierOptionsResponse(), + new ExampleAclResponse() + ); + + Actions::execute(PdkBackendActions::UPDATE_ACCOUNT); /** @var PsCarrierMappingRepository $repository */ $repository = Pdk::get(PsCarrierMappingRepository::class); @@ -89,6 +95,8 @@ function doSnapshotTest(Collection $carrierMappings, Collection $psCarriers): vo ) ->store(); + PsTestBootstrapper::hasCarrierImages(); + /** @var PsCarrierServiceInterface $service */ $service = Pdk::get(PsCarrierServiceInterface::class); $service->updateCarriers(); diff --git a/tests/Uses/UsesMockPlugin.php b/tests/Uses/UsesMockPlugin.php index fe0d3277..53bf9730 100644 --- a/tests/Uses/UsesMockPlugin.php +++ b/tests/Uses/UsesMockPlugin.php @@ -12,6 +12,7 @@ final class UsesMockPlugin extends UsesMockPsPdkInstance { /** * @return void + * @throws \MyParcelNL\Pdk\Tests\Factory\Exception\InvalidFactoryException */ protected function setup(): void { diff --git a/tests/Uses/UsesMockPsPdkInstance.php b/tests/Uses/UsesMockPsPdkInstance.php index fce4d478..fad6eb7d 100644 --- a/tests/Uses/UsesMockPsPdkInstance.php +++ b/tests/Uses/UsesMockPsPdkInstance.php @@ -6,8 +6,9 @@ use CarrierModule; use Configuration; -use Exception; use MyParcelNL\Pdk\Base\Facade; +use MyParcelNL\Pdk\Base\FileSystemInterface; +use MyParcelNL\Pdk\Facade\Config; use MyParcelNL\Pdk\Facade\Pdk; use MyParcelNL\Pdk\Tests\Factory\SharedFactoryState; use MyParcelNL\Pdk\Tests\Uses\UsesEachMockPdkInstance; @@ -20,21 +21,31 @@ class UsesMockPsPdkInstance extends UsesEachMockPdkInstance { /** * @return void + * @throws \MyParcelNL\Pdk\Tests\Factory\Exception\InvalidFactoryException */ protected function addDefaultData(): void { - try { - psFactory(Configuration::class)->make(); - - psFactory(Zone::class) - ->withName('Europe') - ->store(); - - psFactory(Zone::class) - ->withName('North America') - ->store(); - } catch (Exception $e) { - echo $e->getMessage(); + psFactory(Configuration::class)->make(); + + psFactory(Zone::class) + ->withName('Europe') + ->store(); + + psFactory(Zone::class) + ->withName('North America') + ->store(); + + /** @var FileSystemInterface $fileSystem */ + $fileSystem = Pdk::get(FileSystemInterface::class); + + $fileSystem->mkdir(_PS_SHIP_IMG_DIR_, true); + $fileSystem->mkdir(Pdk::get('carrierLogosDirectory'), true); + + /** @var FileSystemInterface $fileSystem */ + $fileSystem = Pdk::get(FileSystemInterface::class); + + foreach (Config::get('carriers') as $carrier) { + $fileSystem->put(sprintf('%s%s.png', Pdk::get('carrierLogosDirectory'), $carrier['name']), '[IMAGE]'); } } diff --git a/tests/__snapshots__/PsCarrierServiceTest__it_creates_carriers_on_account_update__1.json b/tests/__snapshots__/PsCarrierServiceTest__it_creates_carriers_on_account_update__1.json index 8d2cd9a0..59a9250e 100644 --- a/tests/__snapshots__/PsCarrierServiceTest__it_creates_carriers_on_account_update__1.json +++ b/tests/__snapshots__/PsCarrierServiceTest__it_creates_carriers_on_account_update__1.json @@ -45,13 +45,262 @@ "id": 4 } ] + }, + { + "zones": [ + { + "active": 1, + "name": "Europe", + "id": 1, + "id_zone": 1 + }, + { + "active": 1, + "name": "North America", + "id": 2, + "id_zone": 2 + } + ], + "name": "PostNL", + "active": 1, + "id_reference": 1008123, + "deleted": 0, + "is_module": true, + "need_range": 1, + "range_behavior": 1, + "shipping_external": true, + "shipping_method": 2, + "id": 7, + "rangeWeights": [ + { + "id_lang": null, + "id_shop": null, + "id_carrier": 7, + "delimiter1": "0", + "delimiter2": "10000", + "id": 9 + } + ], + "rangePrices": [ + { + "id_lang": null, + "id_shop": null, + "id_carrier": 7, + "delimiter1": "0", + "delimiter2": "10000", + "id": 8 + } + ] + }, + { + "zones": [ + { + "active": 1, + "name": "Europe", + "id": 1, + "id_zone": 1 + }, + { + "active": 1, + "name": "North America", + "id": 2, + "id_zone": 2 + } + ], + "name": "DHL For You", + "active": 1, + "id_reference": 900, + "deleted": 0, + "is_module": true, + "need_range": 1, + "range_behavior": 1, + "shipping_external": true, + "shipping_method": 2, + "id": 11, + "rangeWeights": [ + { + "id_lang": null, + "id_shop": null, + "id_carrier": 11, + "delimiter1": "0", + "delimiter2": "10000", + "id": 13 + } + ], + "rangePrices": [ + { + "id_lang": null, + "id_shop": null, + "id_carrier": 11, + "delimiter1": "0", + "delimiter2": "10000", + "id": 12 + } + ] + }, + { + "zones": [ + { + "active": 1, + "name": "Europe", + "id": 1, + "id_zone": 1 + }, + { + "active": 1, + "name": "North America", + "id": 2, + "id_zone": 2 + } + ], + "name": "DHL For You", + "active": 1, + "id_reference": 900677, + "deleted": 0, + "is_module": true, + "need_range": 1, + "range_behavior": 1, + "shipping_external": true, + "shipping_method": 2, + "id": 15, + "rangeWeights": [ + { + "id_lang": null, + "id_shop": null, + "id_carrier": 15, + "delimiter1": "0", + "delimiter2": "10000", + "id": 17 + } + ], + "rangePrices": [ + { + "id_lang": null, + "id_shop": null, + "id_carrier": 15, + "delimiter1": "0", + "delimiter2": "10000", + "id": 16 + } + ] + }, + { + "zones": [ + { + "active": 1, + "name": "Europe", + "id": 1, + "id_zone": 1 + }, + { + "active": 1, + "name": "North America", + "id": 2, + "id_zone": 2 + } + ], + "name": "DHL Parcel Connect", + "active": 1, + "id_reference": 100, + "deleted": 0, + "is_module": true, + "need_range": 1, + "range_behavior": 1, + "shipping_external": true, + "shipping_method": 2, + "id": 19, + "rangeWeights": [ + { + "id_lang": null, + "id_shop": null, + "id_carrier": 19, + "delimiter1": "0", + "delimiter2": "10000", + "id": 21 + } + ], + "rangePrices": [ + { + "id_lang": null, + "id_shop": null, + "id_carrier": 19, + "delimiter1": "0", + "delimiter2": "10000", + "id": 20 + } + ] + }, + { + "zones": [ + { + "active": 1, + "name": "Europe", + "id": 1, + "id_zone": 1 + }, + { + "active": 1, + "name": "North America", + "id": 2, + "id_zone": 2 + } + ], + "name": "DHL Europlus", + "active": 1, + "id_reference": 110, + "deleted": 0, + "is_module": true, + "need_range": 1, + "range_behavior": 1, + "shipping_external": true, + "shipping_method": 2, + "id": 23, + "rangeWeights": [ + { + "id_lang": null, + "id_shop": null, + "id_carrier": 23, + "delimiter1": "0", + "delimiter2": "10000", + "id": 25 + } + ], + "rangePrices": [ + { + "id_lang": null, + "id_shop": null, + "id_carrier": 23, + "delimiter1": "0", + "delimiter2": "10000", + "id": 24 + } + ] } ], "carrierMappings": [ { - "idCarrier": 3, - "myparcelCarrier": "postnl", - "id": 6 + "carrierId": 3, + "myparcelCarrier": "postnl" + }, + { + "carrierId": 7, + "myparcelCarrier": "postnl:8123" + }, + { + "carrierId": 11, + "myparcelCarrier": "dhlforyou" + }, + { + "carrierId": 15, + "myparcelCarrier": "dhlforyou:677" + }, + { + "carrierId": 19, + "myparcelCarrier": "dhlparcelconnect" + }, + { + "carrierId": 23, + "myparcelCarrier": "dhleuroplus" } ] } diff --git a/tests/__snapshots__/PsCarrierServiceTest__it_does_not_create_duplicate_carriers__1.json b/tests/__snapshots__/PsCarrierServiceTest__it_does_not_create_duplicate_carriers__1.json index 6c307215..a504354d 100644 --- a/tests/__snapshots__/PsCarrierServiceTest__it_does_not_create_duplicate_carriers__1.json +++ b/tests/__snapshots__/PsCarrierServiceTest__it_does_not_create_duplicate_carriers__1.json @@ -34,46 +34,6 @@ "delimiter1": "0", "delimiter2": "10000", "id": 5 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 3, - "delimiter1": "0", - "delimiter2": "10000", - "id": 8 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 3, - "delimiter1": "0", - "delimiter2": "10000", - "id": 10 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 3, - "delimiter1": "0", - "delimiter2": "10000", - "id": 20 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 3, - "delimiter1": "0", - "delimiter2": "10000", - "id": 26 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 3, - "delimiter1": "0", - "delimiter2": "10000", - "id": 32 } ], "rangePrices": [ @@ -84,46 +44,6 @@ "delimiter1": "0", "delimiter2": "10000", "id": 4 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 3, - "delimiter1": "0", - "delimiter2": "10000", - "id": 7 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 3, - "delimiter1": "0", - "delimiter2": "10000", - "id": 9 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 3, - "delimiter1": "0", - "delimiter2": "10000", - "id": 19 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 3, - "delimiter1": "0", - "delimiter2": "10000", - "id": 25 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 3, - "delimiter1": "0", - "delimiter2": "10000", - "id": 31 } ] }, @@ -151,74 +71,26 @@ "range_behavior": 1, "shipping_external": true, "shipping_method": 2, - "id": 11, - "id_carrier": 11, + "id": 7, + "id_carrier": 7, "rangeWeights": [ { "id_lang": null, "id_shop": null, - "id_carrier": 11, - "delimiter1": "0", - "delimiter2": "10000", - "id": 13 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 11, + "id_carrier": 7, "delimiter1": "0", "delimiter2": "10000", - "id": 22 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 11, - "delimiter1": "0", - "delimiter2": "10000", - "id": 28 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 11, - "delimiter1": "0", - "delimiter2": "10000", - "id": 34 + "id": 9 } ], "rangePrices": [ { "id_lang": null, "id_shop": null, - "id_carrier": 11, + "id_carrier": 7, "delimiter1": "0", "delimiter2": "10000", - "id": 12 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 11, - "delimiter1": "0", - "delimiter2": "10000", - "id": 21 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 11, - "delimiter1": "0", - "delimiter2": "10000", - "id": 27 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 11, - "delimiter1": "0", - "delimiter2": "10000", - "id": 33 + "id": 8 } ] }, @@ -246,93 +118,42 @@ "range_behavior": 1, "shipping_external": true, "shipping_method": 2, - "id": 15, - "id_carrier": 15, + "id": 11, + "id_carrier": 11, "rangeWeights": [ { "id_lang": null, "id_shop": null, - "id_carrier": 15, - "delimiter1": "0", - "delimiter2": "10000", - "id": 17 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 15, - "delimiter1": "0", - "delimiter2": "10000", - "id": 24 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 15, - "delimiter1": "0", - "delimiter2": "10000", - "id": 30 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 15, + "id_carrier": 11, "delimiter1": "0", "delimiter2": "10000", - "id": 36 + "id": 13 } ], "rangePrices": [ { "id_lang": null, "id_shop": null, - "id_carrier": 15, - "delimiter1": "0", - "delimiter2": "10000", - "id": 16 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 15, - "delimiter1": "0", - "delimiter2": "10000", - "id": 23 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 15, - "delimiter1": "0", - "delimiter2": "10000", - "id": 29 - }, - { - "id_lang": null, - "id_shop": null, - "id_carrier": 15, + "id_carrier": 11, "delimiter1": "0", "delimiter2": "10000", - "id": 35 + "id": 12 } ] } ], "carrierMappings": [ { - "idCarrier": 3, - "myparcelCarrier": "postnl", - "id": 6 + "carrierId": 3, + "myparcelCarrier": "postnl" }, { - "idCarrier": 11, - "myparcelCarrier": "dhlforyou", - "id": 14 + "carrierId": 7, + "myparcelCarrier": "dhlforyou" }, { - "idCarrier": 15, - "myparcelCarrier": "dhleuroplus", - "id": 18 + "carrierId": 11, + "myparcelCarrier": "dhleuroplus" } ] } diff --git a/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlCarrierMappingFactory.php b/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlCarrierMappingFactory.php index 20fdbc16..d96f9078 100644 --- a/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlCarrierMappingFactory.php +++ b/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlCarrierMappingFactory.php @@ -7,11 +7,8 @@ use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsEntityFactory; /** - * @method self withId(int $id) - * @method self withIdCarrier(int $idCarrier) + * @method self withCarrierId(int $carrierId) * @method self withMyparcelCarrier(string $myparcelCarrier) - * @method self withCreated(string $created) - * @method self withUpdated(string $updated) */ final class MyparcelnlCarrierMappingFactory extends AbstractPsEntityFactory { diff --git a/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlCartDeliveryOptionsFactory.php b/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlCartDeliveryOptionsFactory.php index 2c470565..3b3a1f52 100644 --- a/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlCartDeliveryOptionsFactory.php +++ b/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlCartDeliveryOptionsFactory.php @@ -4,17 +4,25 @@ namespace MyParcelNL\PrestaShop\Entity; +use MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface; use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsEntityFactory; /** * @method self withId(int $id) - * @method self withIdCart(int $idCart) + * @method self withCartId(int $cartId) * @method self withData(string $data) - * @method self withCreated(string $created) - * @method self withUpdated(string $updated) + * @method self withDateAdd(string $dateAdd) + * @method self withDateUpd(string $dateUpd) */ final class MyparcelnlCartDeliveryOptionsFactory extends AbstractPsEntityFactory { + protected function createDefault(): FactoryInterface + { + return $this + ->withDateAdd('2020-01-01 08:00:00') + ->withDateUpd('2020-01-02 12:00:00'); + } + protected function getEntityClass(): string { return MyparcelnlCartDeliveryOptions::class; diff --git a/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlOrderDataFactory.php b/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlOrderDataFactory.php index 8838e920..62266536 100644 --- a/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlOrderDataFactory.php +++ b/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlOrderDataFactory.php @@ -4,17 +4,24 @@ namespace MyParcelNL\PrestaShop\Entity; +use MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface; use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsEntityFactory; /** - * @method self withId(int $id) - * @method self withIdOrder(int $idOrder) + * @method self withOrderId(int $orderId) * @method self withData(string $data) - * @method self withCreated(string $created) - * @method self withUpdated(string $updated) + * @method self withDateAdd(string $dateAdd) + * @method self withDateUpd(string $dateUpd) */ final class MyparcelnlOrderDataFactory extends AbstractPsEntityFactory { + protected function createDefault(): FactoryInterface + { + return $this + ->withDateAdd('2020-01-01 08:00:00') + ->withDateUpd('2020-01-02 12:00:00'); + } + protected function getEntityClass(): string { return MyparcelnlOrderData::class; diff --git a/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlOrderShipmentFactory.php b/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlOrderShipmentFactory.php index 5d66b940..90c3a1e7 100644 --- a/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlOrderShipmentFactory.php +++ b/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlOrderShipmentFactory.php @@ -4,18 +4,25 @@ namespace MyParcelNL\PrestaShop\Entity; +use MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface; use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsEntityFactory; /** - * @method self withId(int $id) - * @method self withIdOrder(int $idOrder) - * @method self withIdShipment(int $idShipment) + * @method self withOrderId(int $orderId) + * @method self withShipmentId(int $shipmentId) * @method self withData(string $data) - * @method self withCreated(string $created) - * @method self withUpdated(string $updated) + * @method self withDateAdd(string $dateAdd) + * @method self withDateUpd(string $dateUpd) */ final class MyparcelnlOrderShipmentFactory extends AbstractPsEntityFactory { + protected function createDefault(): FactoryInterface + { + return $this + ->withDateAdd('2020-01-01 08:00:00') + ->withDateUpd('2020-01-02 12:00:00'); + } + protected function getEntityClass(): string { return MyparcelnlOrderShipment::class; diff --git a/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlProductSettingsFactory.php b/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlProductSettingsFactory.php index 9ebce155..439f4d23 100644 --- a/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlProductSettingsFactory.php +++ b/tests/factories/MyParcelNL/PrestaShop/Entity/MyparcelnlProductSettingsFactory.php @@ -4,17 +4,24 @@ namespace MyParcelNL\PrestaShop\Entity; +use MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface; use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsEntityFactory; /** - * @method self withId(int $id) - * @method self withIdProduct(int $idProduct) + * @method self withProductId(int $productId) * @method self withData(string $data) - * @method self withCreated(string $created) - * @method self withUpdated(string $updated) + * @method self withDateAdd(string $dateAdd) + * @method self withDateUpd(string $dateUpd) */ final class MyparcelnlProductSettingsFactory extends AbstractPsEntityFactory { + protected function createDefault(): FactoryInterface + { + return $this + ->withDateAdd('2020-01-01 08:00:00') + ->withDateUpd('2020-01-02 12:00:00'); + } + protected function getEntityClass(): string { return MyparcelnlProductSettings::class; diff --git a/tests/mock_class_map.php b/tests/mock_class_map.php index 0530a935..62f202e5 100644 --- a/tests/mock_class_map.php +++ b/tests/mock_class_map.php @@ -154,6 +154,11 @@ abstract class OrderCore extends MockPsOrder { } final class Order extends OrderCore { } +/** @see \OrderStateCore */ +abstract class OrderStateCore extends MockPsObjectModel { } + +final class OrderState extends OrderStateCore { } + /** @see \RangePriceCore */ abstract class RangePriceCore extends MockPsRangeObjectModel { }