Skip to content

Commit 4c666e4

Browse files
authored
Merge pull request #235 from magento-l3/ACP2E-782-2.4-develop
ACP2E-782: Introduce an alternative way to define fixtures using PHP8 Attributes
2 parents e05d62a + fd7fb5c commit 4c666e4

File tree

7 files changed

+131
-34
lines changed

7 files changed

+131
-34
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\InventoryApi\Test\Fixture;
9+
10+
use Magento\Framework\DataObject;
11+
use Magento\InventoryApi\Api\Data\SourceItemInterface;
12+
use Magento\InventoryApi\Api\SourceItemsDeleteInterface;
13+
use Magento\InventoryApi\Api\SourceItemsSaveInterface;
14+
use Magento\TestFramework\Fixture\Api\ServiceFactory;
15+
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
16+
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
17+
18+
class SourceItem implements RevertibleDataFixtureInterface
19+
{
20+
private const DEFAULT_DATA = [
21+
'sku' => 'sku%uniqid%',
22+
'source_code' => 'source%uniqid%',
23+
'quantity' => 100,
24+
'status' => SourceItemInterface::STATUS_IN_STOCK,
25+
];
26+
27+
/**
28+
* @var ServiceFactory
29+
*/
30+
private ServiceFactory $serviceFactory;
31+
32+
/**
33+
* @var ProcessorInterface
34+
*/
35+
private ProcessorInterface $dataProcessor;
36+
37+
/**
38+
* @param ServiceFactory $serviceFactory
39+
* @param ProcessorInterface $dataProcessor
40+
*/
41+
public function __construct(
42+
ServiceFactory $serviceFactory,
43+
ProcessorInterface $dataProcessor
44+
) {
45+
$this->serviceFactory = $serviceFactory;
46+
$this->dataProcessor = $dataProcessor;
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
* @param array $data Parameters. Same format as SourceItem::DEFAULT_DATA.
52+
*/
53+
public function apply(array $data = []): ?DataObject
54+
{
55+
$service = $this->serviceFactory->create(SourceItemsSaveInterface::class, 'execute');
56+
57+
return $service->execute(['sourceItems' => [$this->prepareData($data)]]);
58+
}
59+
60+
/**
61+
* @inheritdoc
62+
*/
63+
public function revert(DataObject $data): void
64+
{
65+
$service = $this->serviceFactory->create(SourceItemsDeleteInterface::class, 'execute');
66+
$service->execute(['sourceItems' => [$this->prepareData($data->getData())]]);
67+
}
68+
69+
/**
70+
* Prepare source item data
71+
*
72+
* @param array $data
73+
* @return array
74+
*/
75+
private function prepareData(array $data): array
76+
{
77+
$data = array_merge(self::DEFAULT_DATA, $data);
78+
79+
return $this->dataProcessor->process($this, $data);
80+
}
81+
}

InventoryCatalog/Test/Integration/AddNewSourceToProductTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77

88
namespace Magento\InventoryCatalog\Test\Integration;
99

10+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
1011
use Magento\Framework\Api\DataObjectHelper;
1112
use Magento\InventoryApi\Api\Data\SourceItemInterface;
1213
use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;
1314
use Magento\InventoryApi\Api\SourceItemsSaveInterface;
15+
use Magento\TestFramework\Fixture\DataFixture;
1416
use Magento\TestFramework\Fixture\DataFixtureStorage;
1517
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
18+
use Magento\TestFramework\Fixture\DbIsolation;
1619
use Magento\TestFramework\Helper\Bootstrap;
1720
use PHPUnit\Framework\TestCase;
1821

@@ -36,9 +39,11 @@ protected function setUp(): void
3639

3740
/**
3841
* @magentoDataFixture Magento_InventoryApi::Test/_files/stock_with_source_link.php
39-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Product with:{"sku":"simple", "type_id":"simple"} as:p1
40-
* @magentoDbIsolation disabled
4142
*/
43+
#[
44+
DbIsolation(false),
45+
DataFixture(ProductFixture::class, ['sku' => 'simple'], 'p1'),
46+
]
4247
public function testAddNewSourceToProduct()
4348
{
4449
/** @var DataObjectHelper $dataObjectHelper */

InventoryCatalog/Test/Integration/Bulk/SourceAssignTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77

88
namespace Magento\InventoryCatalog\Test\Integration\Bulk;
99

10+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
1011
use Magento\Framework\Api\SearchCriteriaBuilder;
1112
use Magento\InventoryApi\Api\Data\SourceItemInterface;
1213
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
1314
use Magento\InventoryCatalogApi\Api\BulkSourceAssignInterface;
15+
use Magento\TestFramework\Fixture\DataFixture;
16+
use Magento\TestFramework\Fixture\DbIsolation;
1417
use Magento\TestFramework\Helper\Bootstrap;
1518
use PHPUnit\Framework\TestCase;
1619

@@ -138,10 +141,12 @@ public function testBulkSourceAssignmentOnMixedProducts()
138141

139142
/**
140143
* @magentoDataFixture Magento_InventoryApi::Test/_files/sources.php
141-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Product with:{"sku": "01234"} as:product1
142-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Product with:{"sku": "1234"} as:product2
143-
* @magentoDbIsolation enabled
144144
*/
145+
#[
146+
DbIsolation(true),
147+
DataFixture(ProductFixture::class, ['sku' => '01234'], 'product1'),
148+
DataFixture(ProductFixture::class, ['sku' => '1234'], 'product2'),
149+
]
145150
public function testBulkSourceAssignmentOfProductsWithNumericSku(): void
146151
{
147152
$skus = ['01234', '1234'];

InventoryCatalog/Test/Integration/Bulk/SourceUnassignTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77

88
namespace Magento\InventoryCatalog\Test\Integration\Bulk;
99

10+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
1011
use Magento\Framework\Api\SearchCriteriaBuilder;
1112
use Magento\InventoryApi\Api\Data\SourceItemInterface;
1213
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
14+
use Magento\InventoryApi\Test\Fixture\SourceItem as SourceItemFixture;
1315
use Magento\InventoryCatalogApi\Api\BulkSourceUnassignInterface;
16+
use Magento\TestFramework\Fixture\DataFixture;
17+
use Magento\TestFramework\Fixture\DbIsolation;
1418
use Magento\TestFramework\Helper\Bootstrap;
1519
use PHPUnit\Framework\TestCase;
1620

@@ -100,12 +104,14 @@ public function testBulkSourceUnassignment()
100104

101105
/**
102106
* @magentoDataFixture Magento_InventoryApi::Test/_files/sources.php
103-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Product with:{"sku": "01234"} as:product1
104-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Product with:{"sku": "1234"} as:product2
105-
* @magentoDataFixture Magento\CatalogInventory\Test\Fixture\SourceItem with:{"sku": "01234", "source_code": "eu-1"}
106-
* @magentoDataFixture Magento\CatalogInventory\Test\Fixture\SourceItem with:{"sku": "1234", "source_code": "eu-1"}
107-
* @magentoDbIsolation enabled
108107
*/
108+
#[
109+
DbIsolation(true),
110+
DataFixture(ProductFixture::class, ['sku' => '01234'], 'product1'),
111+
DataFixture(ProductFixture::class, ['sku' => '1234'], 'product2'),
112+
DataFixture(SourceItemFixture::class, ['sku' => '01234', 'source_code' => 'eu-1']),
113+
DataFixture(SourceItemFixture::class, ['sku' => '1234', 'source_code' => 'eu-1']),
114+
]
109115
public function testBulkSourceUnAssignmentOfProductsWithNumericSku(): void
110116
{
111117
$skus = ['01234', '1234'];

InventorySalesApi/Test/Api/GetProductSalableQuantityTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
class GetProductSalableQuantityTest extends WebapiAbstract
1717
{
18-
const API_PATH = '/V1/inventory/get-product-salable-quantity';
19-
const SERVICE_NAME = 'inventorySalesApiGetProductSalableQtyV1';
18+
private const API_PATH = '/V1/inventory/get-product-salable-quantity';
19+
private const SERVICE_NAME = 'inventorySalesApiGetProductSalableQtyV1';
2020

2121
/**
2222
* Verify get product salable quantity will return correct quantity for given product and stock.
@@ -25,12 +25,12 @@ class GetProductSalableQuantityTest extends WebapiAbstract
2525
* @param int $stockId
2626
* @param float $expectedResult
2727
*
28-
* @magentoDataFixture Magento_InventoryApi::Test/_files/products.php
29-
* @magentoDataFixture Magento_InventoryApi::Test/_files/sources.php
30-
* @magentoDataFixture Magento_InventoryApi::Test/_files/stocks.php
31-
* @magentoDataFixture Magento_InventoryApi::Test/_files/source_items.php
32-
* @magentoDataFixture Magento_InventoryApi::Test/_files/stock_source_links.php
33-
* @magentoDataFixture Magento_InventoryIndexer::Test/_files/reindex_inventory.php
28+
* @magentoApiDataFixture Magento_InventoryApi::Test/_files/products.php
29+
* @magentoApiDataFixture Magento_InventoryApi::Test/_files/sources.php
30+
* @magentoApiDataFixture Magento_InventoryApi::Test/_files/stocks.php
31+
* @magentoApiDataFixture Magento_InventoryApi::Test/_files/source_items.php
32+
* @magentoApiDataFixture Magento_InventoryApi::Test/_files/stock_source_links.php
33+
* @magentoApiDataFixture Magento_InventoryIndexer::Test/_files/reindex_inventory.php
3434
* @dataProvider getSalableQuantityDataProvider
3535
*
3636
* @magentoDbIsolation disabled

InventorySalesApi/Test/Api/IsProductSalableForRequestedQtyTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
class IsProductSalableForRequestedQtyTest extends WebapiAbstract
1414
{
15-
const API_PATH = '/V1/inventory/is-product-salable-for-requested-qty';
16-
const SERVICE_NAME = 'inventorySalesApiIsProductSalableForRequestedQtyV1';
15+
private const API_PATH = '/V1/inventory/is-product-salable-for-requested-qty';
16+
private const SERVICE_NAME = 'inventorySalesApiIsProductSalableForRequestedQtyV1';
1717

1818
/**
1919
* @return array
@@ -33,12 +33,12 @@ public function executeDataProvider(): array
3333
* @param float $requestedQty
3434
* @param bool $expectedResult
3535
*
36-
* @magentoDataFixture Magento_InventoryApi::Test/_files/products.php
37-
* @magentoDataFixture Magento_InventoryApi::Test/_files/sources.php
38-
* @magentoDataFixture Magento_InventoryApi::Test/_files/stocks.php
39-
* @magentoDataFixture Magento_InventoryApi::Test/_files/source_items.php
40-
* @magentoDataFixture Magento_InventoryApi::Test/_files/stock_source_links.php
41-
* @magentoDataFixture Magento_InventoryIndexer::Test/_files/reindex_inventory.php
36+
* @magentoApiDataFixture Magento_InventoryApi::Test/_files/products.php
37+
* @magentoApiDataFixture Magento_InventoryApi::Test/_files/sources.php
38+
* @magentoApiDataFixture Magento_InventoryApi::Test/_files/stocks.php
39+
* @magentoApiDataFixture Magento_InventoryApi::Test/_files/source_items.php
40+
* @magentoApiDataFixture Magento_InventoryApi::Test/_files/stock_source_links.php
41+
* @magentoApiDataFixture Magento_InventoryIndexer::Test/_files/reindex_inventory.php
4242
* @magentoConfigFixture default_store cataloginventory/item_options/manage_stock 0
4343
* @dataProvider executeDataProvider
4444
*

InventorySalesApi/Test/Api/IsProductSalableTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
class IsProductSalableTest extends WebapiAbstract
1717
{
18-
const API_PATH = '/V1/inventory/is-product-salable';
19-
const SERVICE_NAME = 'inventorySalesApiIsProductSalableV1';
18+
private const API_PATH = '/V1/inventory/is-product-salable';
19+
private const SERVICE_NAME = 'inventorySalesApiIsProductSalableV1';
2020

2121
/**
2222
* @return array
@@ -35,12 +35,12 @@ public function executeDataProvider(): array
3535
* @param int $stockId
3636
* @param bool $expectedResult
3737
*
38-
* @magentoDataFixture Magento_InventoryApi::Test/_files/products.php
39-
* @magentoDataFixture Magento_InventoryApi::Test/_files/sources.php
40-
* @magentoDataFixture Magento_InventoryApi::Test/_files/stocks.php
41-
* @magentoDataFixture Magento_InventoryApi::Test/_files/source_items.php
42-
* @magentoDataFixture Magento_InventoryApi::Test/_files/stock_source_links.php
43-
* @magentoDataFixture Magento_InventoryIndexer::Test/_files/reindex_inventory.php
38+
* @magentoApiDataFixture Magento_InventoryApi::Test/_files/products.php
39+
* @magentoApiDataFixture Magento_InventoryApi::Test/_files/sources.php
40+
* @magentoApiDataFixture Magento_InventoryApi::Test/_files/stocks.php
41+
* @magentoApiDataFixture Magento_InventoryApi::Test/_files/source_items.php
42+
* @magentoApiDataFixture Magento_InventoryApi::Test/_files/stock_source_links.php
43+
* @magentoApiDataFixture Magento_InventoryIndexer::Test/_files/reindex_inventory.php
4444
* @magentoConfigFixture default_store cataloginventory/item_options/manage_stock 0
4545
* @dataProvider executeDataProvider
4646
*

0 commit comments

Comments
 (0)