Skip to content

Commit

Permalink
[ECP-8778] Create new state for adyen_authorized status (#2375)
Browse files Browse the repository at this point in the history
* [ECP-8778] Create new state

* [ECP-8778] Update mock class

* [ECP-8778] Update mock class
  • Loading branch information
candemiralp authored Nov 29, 2023
1 parent 4fa6926 commit 74d15b4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
5 changes: 4 additions & 1 deletion Model/Config/Source/PreAuthorized.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
*/
class PreAuthorized extends \Magento\Sales\Model\Config\Source\Order\Status
{
const STATE_ADYEN_AUTHORIZED = 'adyen_authorized';

/**
* @var string[]
*/
protected $_stateStatuses = [
Order::STATE_NEW
Order::STATE_NEW,
self::STATE_ADYEN_AUTHORIZED
];
}
52 changes: 28 additions & 24 deletions Setup/Patch/Data/CreateStatusAuthorized.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@
namespace Adyen\Payment\Setup\Patch\Data;

use Adyen\Payment\Helper\DataPatch;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use Magento\Framework\App\Config\ReinitableConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\StatusFactory;
use Magento\Sales\Model\ResourceModel\Order\StatusFactory as StatusResourceFactory;
use Magento\Sales\Model\ResourceModel\Order\Status as StatusResource;

class CreateStatusAuthorized implements DataPatchInterface, PatchVersionInterface
{
private ModuleDataSetupInterface $moduleDataSetup;
private WriterInterface $configWriter;
private ReinitableConfigInterface $reinitableConfig;
private DataPatch $dataPatchHelper;
private StatusFactory $statusFactory;
private StatusResourceFactory $statusResourceFactory;

const ADYEN_AUTHORIZED_STATUS = 'adyen_authorized';
const ADYEN_AUTHORIZED_STATUS_LABEL = 'Authorized';
Expand All @@ -33,43 +39,41 @@ public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
WriterInterface $configWriter,
ReinitableConfigInterface $reinitableConfig,
DataPatch $dataPatchHelper
DataPatch $dataPatchHelper,
StatusFactory $statusFactory,
StatusResourceFactory $statusResourceFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->configWriter = $configWriter;
$this->reinitableConfig = $reinitableConfig;
$this->dataPatchHelper = $dataPatchHelper;
$this->statusFactory = $statusFactory;
$this->statusResourceFactory = $statusResourceFactory;
}

public function apply()
{
$setup = $this->moduleDataSetup;

$orderStateAssignmentTable = $this->moduleDataSetup->getTable('sales_order_status_state');
$orderStatusTable = $this->moduleDataSetup->getTable('sales_order_status');

$status = [
[
'status' => self::ADYEN_AUTHORIZED_STATUS,
'label' => self::ADYEN_AUTHORIZED_STATUS_LABEL
]
];

$this->moduleDataSetup->getConnection()->insertOnDuplicate($orderStatusTable, $status);

$stateAssignment = [
[
'status' => self::ADYEN_AUTHORIZED_STATUS,
'state' => Order::STATE_NEW,
'is_default' => 0,
'visible_on_front' => 1
]
];
/** @var StatusResource $statusResource */
$statusResource = $this->statusResourceFactory->create();

$status = $this->statusFactory->create();
$status->setData([
'status' => self::ADYEN_AUTHORIZED_STATUS,
'label' => self::ADYEN_AUTHORIZED_STATUS_LABEL,
]);

try {
$statusResource->save($status);
} catch (AlreadyExistsException $exception) {
return;
}

$this->moduleDataSetup->getConnection()->insertOnDuplicate($orderStateAssignmentTable, $stateAssignment);
$status->assignState(self::ADYEN_AUTHORIZED_STATUS, true, true);

$setup = $this->moduleDataSetup;
$path = 'payment/adyen_abstract/payment_pre_authorized';

// Processing status was assigned mistakenly. It shouldn't be used on payment_pre_authorized.
$config = $this->dataPatchHelper->findConfig($setup, $path, Order::STATE_PROCESSING);
if (isset($config)) {
$this->configWriter->save(
Expand Down
13 changes: 12 additions & 1 deletion Test/Unit/Setup/Patch/Data/CreateStatusAuthorizedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Sales\Model\Order\Status;
use Magento\Sales\Model\Order\StatusFactory;
use Magento\Sales\Model\ResourceModel\Order\Status as StatusResource;
use Magento\Sales\Model\ResourceModel\Order\StatusFactory as StatusResourceFactory;

class CreateStatusAuthorizedTest extends AbstractAdyenTestCase
{
Expand Down Expand Up @@ -66,12 +70,19 @@ public function getCreateStatusAuthorized(): CreateStatusAuthorized
$dataPatchHelperMock = $this->createConfiguredMock(DataPatch::class, [
'findConfig' => null
]);
$statusFactoryMock = $this->createGeneratedMock(StatusFactory::class, ['create']);
$statusFactoryMock->method('create')->willReturn($this->createMock(Status::class));
$statusResourceFactoryMock = $this->createGeneratedMock(StatusResourceFactory::class, ['create']);
$statusResourceFactoryMock->method('create')
->willReturn($this->createMock(StatusResource::class));

return new CreateStatusAuthorized(
$moduleDataSetupMock,
$configWriteMock,
$reinitableConfigMock,
$dataPatchHelperMock
$dataPatchHelperMock,
$statusFactoryMock,
$statusResourceFactoryMock
);
}
}

0 comments on commit 74d15b4

Please sign in to comment.