Skip to content

Commit

Permalink
feat(orders): implement automatic order status updates
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Sep 20, 2023
1 parent 68d04f2 commit 9af8725
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 15 deletions.
24 changes: 21 additions & 3 deletions src/Pdk/Order/Service/PsOrderStatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,30 @@

use Context;
use MyParcelNL\Pdk\App\Order\Contract\OrderStatusServiceInterface;
use MyParcelNL\PrestaShop\Contract\PsOrderServiceInterface;
use OrderState;

class PsOrderStatusService implements OrderStatusServiceInterface
final class PsOrderStatusService implements OrderStatusServiceInterface
{
/**
* @var \MyParcelNL\PrestaShop\Contract\PsOrderServiceInterface
*/
private $psOrderService;

/**
* @param \MyParcelNL\PrestaShop\Contract\PsOrderServiceInterface $psOrderService
*/
public function __construct(PsOrderServiceInterface $psOrderService)
{
$this->psOrderService = $psOrderService;
}

/**
* @return array
*/
public function all(): array
{
$orderStates = OrderState::getOrderStates((int) Context::getContext()->language->id);
$orderStates = OrderState::getOrderStates(Context::getContext()->language->id);

$array = [];

Expand All @@ -34,6 +48,10 @@ public function all(): array
*/
public function updateStatus(array $orderIds, string $status): void
{
// TODO: Implement updateStatus() method.
foreach ($orderIds as $orderId) {
$psOrder = $this->psOrderService->get($orderId);

$psOrder->setCurrentState((int) $status);
}
}
}
29 changes: 29 additions & 0 deletions tests/Mock/MockPsOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,36 @@
namespace MyParcelNL\PrestaShop\Tests\Mock;

use ObjectModel;
use OrderState;

/**
* @see \OrderCore
*/
abstract class MockPsOrder extends ObjectModel
{
public function getCurrentOrderState(): ?OrderState
{
$state = $this->getAttribute('current_state');

if ($state) {
return new OrderState($state);
}

return null;
}

/**
* @param int $state
* @param int|null $idEmployee
*
* @return bool
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function setCurrentState(int $state, int $idEmployee = null): bool
{
$this->setAttribute('current_state', $state);

return $this->update();
}
}
59 changes: 59 additions & 0 deletions tests/Unit/Pdk/Order/Service/PsOrderStatusServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection,StaticClosureCanBeUsedInspection */

declare(strict_types=1);

namespace MyParcelNL\PrestaShop\Pdk\Order\Service;

use MyParcelNL\Pdk\App\Order\Contract\OrderStatusServiceInterface;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\PrestaShop\Tests\Uses\UsesMockPsPdkInstance;
use Order as PsOrder;
use function MyParcelNL\Pdk\Tests\usesShared;
use function MyParcelNL\PrestaShop\psFactory;

usesShared(new UsesMockPsPdkInstance());

it('retrieves all order statuses', function () {
/** @var OrderStatusServiceInterface $service */
$service = Pdk::get(OrderStatusServiceInterface::class);

/** @see UsesMockPsPdkInstance::createOrderStates() */
expect($service->all())->toBe([
1 => 'Awaiting check payment',
2 => 'Payment accepted',
3 => 'Processing in progress',
4 => 'Shipped',
5 => 'Delivered',
6 => 'Canceled',
7 => 'Refunded',
8 => 'Payment error',
9 => 'On backorder (paid)',
10 => 'Awaiting bank wire payment',
11 => 'Remote payment accepted',
12 => 'On backorder (not paid)',
13 => 'Awaiting Cash On Delivery validation',
]);
});

it('updates order status', function () {
psFactory(PsOrder::class)
->withId(14)
->store();
psFactory(PsOrder::class)
->withId(16)
->store();

/** @var OrderStatusServiceInterface $service */
$service = Pdk::get(OrderStatusServiceInterface::class);

$service->updateStatus([14, 16], '4');

$order14 = new PsOrder(14);
$order16 = new PsOrder(16);

expect($order14->current_state)
->toBe(4)
->and($order16->current_state)
->toBe(4);
});
92 changes: 85 additions & 7 deletions tests/Uses/UsesMockPsPdkInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,32 @@
use MyParcelNL\Pdk\Base\FileSystemInterface;
use MyParcelNL\Pdk\Facade\Config;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\Pdk\Tests\Factory\Collection\FactoryCollection;
use MyParcelNL\Pdk\Tests\Factory\SharedFactoryState;
use MyParcelNL\Pdk\Tests\Uses\UsesEachMockPdkInstance;
use MyParcelNL\PrestaShop\Tests\Bootstrap\MockPsPdkBootstrapper;
use MyParcelNL\PrestaShop\Tests\Mock\MockPsModule;
use OrderState;
use OrderStateFactory;
use Zone;
use function MyParcelNL\PrestaShop\psFactory;

class UsesMockPsPdkInstance extends UsesEachMockPdkInstance
{
/**
* @return void
* @throws \MyParcelNL\Pdk\Tests\Factory\Exception\InvalidFactoryException
*/
public function createZones(): void
{
(new FactoryCollection([
psFactory(Zone::class)
->withName('Europe'),
psFactory(Zone::class)
->withName('North America'),
]))->store();
}

/**
* @return void
* @throws \MyParcelNL\Pdk\Tests\Factory\Exception\InvalidFactoryException
Expand All @@ -27,13 +44,8 @@ protected function addDefaultData(): void
{
psFactory(Configuration::class)->make();

psFactory(Zone::class)
->withName('Europe')
->store();

psFactory(Zone::class)
->withName('North America')
->store();
$this->createZones();
$this->createOrderStates();

/** @var FileSystemInterface $fileSystem */
$fileSystem = Pdk::get(FileSystemInterface::class);
Expand Down Expand Up @@ -70,4 +82,70 @@ protected function setup(): void

$this->addDefaultData();
}

/**
* @return void
* @throws \MyParcelNL\Pdk\Tests\Factory\Exception\InvalidFactoryException
*/
private function createOrderStates(): void
{
$collection = new FactoryCollection([
psFactory(OrderState::class)
->withId(1)
->withName('Awaiting check payment'),
psFactory(OrderState::class)
->withId(2)
->withName('Payment accepted')
->withPaid(1),
psFactory(OrderState::class)
->withId(3)
->withName('Processing in progress')
->withDelivery(1)
->withPaid(1),
psFactory(OrderState::class)
->withId(4)
->withName('Shipped')
->withDelivery(1)
->withPaid(1)
->withShipped(1),
psFactory(OrderState::class)
->withId(5)
->withName('Delivered')
->withPaid(1)
->withDelivery(1)
->withShipped(1),
psFactory(OrderState::class)
->withId(6)
->withName('Canceled'),
psFactory(OrderState::class)
->withId(7)
->withName('Refunded'),
psFactory(OrderState::class)
->withId(8)
->withName('Payment error'),
psFactory(OrderState::class)
->withId(9)
->withName('On backorder (paid)')
->withPaid(1),
psFactory(OrderState::class)
->withId(10)
->withName('Awaiting bank wire payment'),
psFactory(OrderState::class)
->withId(11)
->withName('Remote payment accepted')
->withPaid(1),
psFactory(OrderState::class)
->withId(12)
->withName('On backorder (not paid)'),
psFactory(OrderState::class)
->withId(13)
->withName('Awaiting Cash On Delivery validation'),
]);

$collection
->map(function (OrderStateFactory $factory) {
return $factory->withUnremovable(1);
})
->store();
}
}
51 changes: 51 additions & 0 deletions tests/factories/OrderStateFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

use MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface;
use MyParcelNL\PrestaShop\Tests\Factory\AbstractPsObjectModelFactory;

/**
* @method $this withColor(string $color)
* @method $this withDeleted(int $deleted)
* @method $this withDelivery(int $delivery)
* @method $this withHidden(int $hidden)
* @method $this withIdLang(int $idLang)
* @method $this withIdOrderState(int $idOrderState)
* @method $this withInvoice(int $invoice)
* @method $this withLang(int|Lang|LangFactory $lang)
* @method $this withLogable(int $logable)
* @method $this withModuleName(string $moduleName)
* @method $this withName(string $name)
* @method $this withOrderState(int|OrderState|OrderStateFactory $orderState)
* @method $this withPaid(int $paid)
* @method $this withPdfDelivery(int $pdfDelivery)
* @method $this withPdfInvoice(int $pdfInvoice)
* @method $this withSendEmail(int $sendEmail)
* @method $this withShipped(int $shipped)
* @method $this withTemplate(string $template)
* @method $this withUnremovable(int $unremovable)
*/
final class OrderStateFactory extends AbstractPsObjectModelFactory
{
protected function createDefault(): FactoryInterface
{
return $this
->withColor('#34209E')
->withDeleted(0)
->withDelivery(0)
->withHidden(0)
->withLogable(0)
->withPaid(0)
->withPdfDelivery(0)
->withPdfInvoice(0)
->withShipped(0)
->withUnremovable(0)
->withLang(1);
}

protected function getObjectModelClass(): string
{
return OrderState::class;
}
}
5 changes: 4 additions & 1 deletion tests/mock_class_map.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ abstract class OrderCore extends MockPsOrder { }
final class Order extends OrderCore { }

/** @see \OrderStateCore */
abstract class OrderStateCore extends ObjectModel { }
abstract class OrderStateCore extends ObjectModel
{
protected $hasCustomIdKey = true;
}

final class OrderState extends OrderStateCore { }

Expand Down
1 change: 0 additions & 1 deletion tests/mock_namespaced_class_map.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ public function hydrate(array $keyValueData): void;

public function save(): void;
}

4 changes: 1 addition & 3 deletions tests/mock_namespaced_class_map_after.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

use Exception;

class InvalidModuleException extends Exception
{
}
class InvalidModuleException extends Exception { }

namespace Symfony\Bundle\FrameworkBundle\Controller;

Expand Down

0 comments on commit 9af8725

Please sign in to comment.