Skip to content

Commit 1e38cca

Browse files
committed
ACP2E-782: Introduce an alternative way to define fixtures using PHP8 Attributes
1 parent f737760 commit 1e38cca

File tree

6 files changed

+80
-105
lines changed

6 files changed

+80
-105
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/Product/ProductCategoriesTest.php

Lines changed: 73 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,41 @@
88

99
namespace Magento\GraphQl\Catalog\Product;
1010

11-
use Magento\Catalog\Api\Data\ProductInterface;
12-
use Magento\Catalog\Api\ProductRepositoryInterface;
13-
use Magento\Store\Api\WebsiteRepositoryInterface;
14-
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\Catalog\Test\Fixture\Category as CategoryFixture;
12+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
13+
use Magento\Store\Test\Fixture\Group as GroupFixture;
14+
use Magento\Store\Test\Fixture\Store as StoreFixture;
15+
use Magento\Store\Test\Fixture\Website as WebsiteFixture;
16+
use Magento\TestFramework\Fixture\AppArea;
17+
use Magento\TestFramework\Fixture\DataFixture;
18+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
1519
use Magento\TestFramework\TestCase\GraphQlAbstract;
1620

1721
/**
1822
* Test for product categories
1923
*/
2024
class ProductCategoriesTest extends GraphQlAbstract
2125
{
22-
/**
23-
* phpcs:disable Generic.Files.LineLength.TooLong
24-
* @magentoDataFixture Magento/Catalog/_files/category.php
25-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Category with:{"name":"Second Root Category","parent_id":"1","position":"2"} as:c1
26-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Category with:{"name":"Second Root Subcategory","parent_id":"$c1.id$","level":"2"} as:c2
27-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Category with:{"name":"Second Root Subsubcategory","parent_id":"$c2.id$","level":"2"} as:c3
28-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Product with:{"name":"Simple Product In Stock","sku":"in-stock-product","category_ids":["2","333","$c1.id$","$c2.id$","$c3.id$"]}
29-
* @magentoDataFixture Magento\Store\Test\Fixture\Website with:{"code":"test","name":"Test Website","default_group_id":"1"} as:w2
30-
* @magentoDataFixture Magento\Store\Test\Fixture\Group with:{"code":"test_store_group_1","name":"Test Store Group","website_id":"$w2.id$","root_category_id":"$c1.id$"} as:s2
31-
* @magentoDataFixture Magento\Store\Test\Fixture\Store with:{"code":"test_store_1","name":"Test Store","website_id":"$w2.id$","store_group_id":"$s2.id$"}
32-
* @magentoDbIsolation disabled
33-
* @magentoAppArea adminhtml
34-
* phpcs:enable Generic.Files.LineLength.TooLong
35-
*/
26+
#[
27+
AppArea('adminhtml'),
28+
DataFixture(CategoryFixture::class, ['name' => 'Category 1', 'parent_id' => '2'], 'c11'),
29+
DataFixture(CategoryFixture::class, ['name' => 'Category 2', 'parent_id' => '1'], 'c2'),
30+
DataFixture(CategoryFixture::class, ['name' => 'Category 2.1', 'parent_id' => '$c2.id$'], 'c21'),
31+
DataFixture(CategoryFixture::class, ['name' => 'Category 2.1.1', 'parent_id' => '$c21.id$'], 'c211'),
32+
DataFixture(WebsiteFixture::class, as: 'w2'),
33+
DataFixture(GroupFixture::class, ['website_id' => '$w2.id$', 'root_category_id' => '$c2.id$'], 's2'),
34+
DataFixture(StoreFixture::class, ['store_group_id' => '$s2.id$']),
35+
DataFixture(
36+
ProductFixture::class,
37+
[
38+
'sku' => 'in-stock-product',
39+
'category_ids' => ['2', '$c11.id$', '$c2.id$', '$c21.id$', '$c211.id$'],
40+
'website_ids' => ['1', '$w2.id$']
41+
],
42+
),
43+
]
3644
public function testProductCategoriesInDefaultStore(): void
3745
{
38-
$objectManager = Bootstrap::getObjectManager();
39-
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
40-
$defaultWebsiteId = $websiteRepository->get('base')->getId();
41-
$secondWebsiteId = $websiteRepository->get('test')->getId();
42-
43-
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
44-
/** @var $product ProductInterface */
45-
$product = $productRepository->get('in-stock-product');
46-
$product
47-
->setUrlKey('in-stock-product')
48-
->setWebsiteIds([$defaultWebsiteId, $secondWebsiteId]);
49-
$productRepository->save($product);
50-
5146
$response = $this->graphQlQuery(
5247
$this->getQuery('in-stock-product'),
5348
[],
@@ -64,78 +59,77 @@ public function testProductCategoriesInDefaultStore(): void
6459
self::assertNull($categories[0]['breadcrumbs']);
6560
}
6661

67-
/**
68-
* phpcs:disable Generic.Files.LineLength.TooLong
69-
* @magentoDataFixture Magento/Catalog/_files/category.php
70-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Category with:{"name":"Second Root Category","parent_id":"1","position":"2"} as:c1
71-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Category with:{"name":"Second Root Subcategory","parent_id":"$c1.id$","level":"2"} as:c2
72-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Category with:{"name":"Second Root Subsubcategory","parent_id":"$c2.id$","level":"2"} as:c3
73-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Product with:{"name":"Simple Product In Stock","sku":"in-stock-product","category_ids":["2","333","$c1.id$","$c2.id$","$c3.id$"]}
74-
* @magentoDataFixture Magento\Store\Test\Fixture\Website with:{"code":"test","name":"Test Website","default_group_id":"1"} as:w2
75-
* @magentoDataFixture Magento\Store\Test\Fixture\Group with:{"code":"test_store_group_1","name":"Test Store Group","website_id":"$w2.id$","root_category_id":"$c1.id$"} as:s2
76-
* @magentoDataFixture Magento\Store\Test\Fixture\Store with:{"code":"test_store_1","name":"Test Store","website_id":"$w2.id$","store_group_id":"$s2.id$"}
77-
* @magentoDbIsolation disabled
78-
* @magentoAppArea adminhtml
79-
* phpcs:enable Generic.Files.LineLength.TooLong
80-
*/
62+
#[
63+
AppArea('adminhtml'),
64+
DataFixture(CategoryFixture::class, ['name' => 'Category 1', 'parent_id' => '2'], 'c11'),
65+
DataFixture(CategoryFixture::class, ['name' => 'Category 2', 'parent_id' => '1'], 'c2'),
66+
DataFixture(CategoryFixture::class, ['name' => 'Category 2.1', 'parent_id' => '$c2.id$'], 'c21'),
67+
DataFixture(CategoryFixture::class, ['name' => 'Category 2.1.1', 'parent_id' => '$c21.id$'], 'c211'),
68+
DataFixture(WebsiteFixture::class, as: 'w2'),
69+
DataFixture(GroupFixture::class, ['website_id' => '$w2.id$', 'root_category_id' => '$c2.id$'], 's2'),
70+
DataFixture(StoreFixture::class, ['store_group_id' => '$s2.id$'], as: 'store2'),
71+
DataFixture(
72+
ProductFixture::class,
73+
[
74+
'sku' => 'in-stock-product',
75+
'category_ids' => ['2', '$c11.id$', '$c2.id$', '$c21.id$', '$c211.id$'],
76+
'website_ids' => ['1', '$w2.id$']
77+
],
78+
),
79+
]
8180
public function testProductCategoriesInNonDefaultStore(): void
8281
{
83-
$objectManager = Bootstrap::getObjectManager();
84-
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
85-
$defaultWebsiteId = $websiteRepository->get('base')->getId();
86-
$secondWebsiteId = $websiteRepository->get('test')->getId();
87-
88-
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
89-
/** @var $product ProductInterface */
90-
$product = $productRepository->get('in-stock-product');
91-
$product
92-
->setUrlKey('in-stock-product')
93-
->setWebsiteIds([$defaultWebsiteId, $secondWebsiteId]);
94-
$productRepository->save($product);
82+
$fixtures = DataFixtureStorageManager::getStorage();
83+
$storeCode = $fixtures->get('store2')->getCode();
9584

9685
$response = $this->graphQlQuery(
9786
$this->getQuery('in-stock-product'),
9887
[],
9988
'',
100-
['Store' => 'test_store_1']
89+
['Store' => $storeCode]
10190
);
10291

10392
$product = current($response['products']['items']);
10493
$categories = $product['categories'];
10594

10695
self::assertCount(2, $categories);
107-
self::assertEquals('Second Root Subcategory', $categories[0]['name']);
108-
self::assertEquals('second-root-subcategory', $categories[0]['url_path']);
96+
self::assertEquals('Category 2.1', $categories[0]['name']);
97+
self::assertEquals('category-2-1', $categories[0]['url_path']);
10998
self::assertNull($categories[0]['breadcrumbs']);
110-
self::assertEquals('Second Root Subsubcategory', $categories[1]['name']);
111-
self::assertEquals('second-root-subcategory/second-root-subsubcategory', $categories[1]['url_path']);
99+
self::assertEquals('Category 2.1.1', $categories[1]['name']);
100+
self::assertEquals('category-2-1/category-2-1-1', $categories[1]['url_path']);
112101
self::assertCount(1, $categories[1]['breadcrumbs']);
113-
self::assertEquals('Second Root Subcategory', $categories[1]['breadcrumbs'][0]['category_name']);
102+
self::assertEquals('Category 2.1', $categories[1]['breadcrumbs'][0]['category_name']);
114103
self::assertEquals(2, $categories[1]['breadcrumbs'][0]['category_level']);
115104
}
116105

117-
/**
118-
* phpcs:disable Generic.Files.LineLength.TooLong
119-
* @magentoDataFixture Magento/Catalog/_files/category.php
120-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Category with:{"name":"Second Root Category","parent_id":"1","position":"2"} as:c1
121-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Category with:{"name":"Second Root Subcategory","parent_id":"$c1.id$","level":"2"} as:c2
122-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Category with:{"name":"Second Root Subsubcategory","parent_id":"$c2.id$","level":"2"} as:c3
123-
* @magentoDataFixture Magento\Catalog\Test\Fixture\Product with:{"name":"Simple Product In Stock","sku":"in-stock-product","category_ids":["2","333","$c1.id$","$c2.id$","$c3.id$"]}
124-
* @magentoDataFixture Magento\Store\Test\Fixture\Website with:{"code":"test","name":"Test Website","default_group_id":"1"} as:w2
125-
* @magentoDataFixture Magento\Store\Test\Fixture\Group with:{"code":"test_store_group_1","name":"Test Store Group","website_id":"$w2.id$","root_category_id":"$c1.id$"} as:s2
126-
* @magentoDataFixture Magento\Store\Test\Fixture\Store with:{"code":"test_store_1","name":"Test Store","website_id":"$w2.id$","store_group_id":"$s2.id$"}
127-
* @magentoApiDataFixture Magento/Store/_files/second_store.php
128-
* @magentoDbIsolation disabled
129-
* @magentoAppArea adminhtml
130-
* phpcs:enable Generic.Files.LineLength.TooLong
131-
*/
106+
#[
107+
AppArea('adminhtml'),
108+
DataFixture(CategoryFixture::class, ['name' => 'Category 1', 'parent_id' => '2'], 'c11'),
109+
DataFixture(CategoryFixture::class, ['name' => 'Category 2', 'parent_id' => '1'], 'c2'),
110+
DataFixture(CategoryFixture::class, ['name' => 'Category 2.1', 'parent_id' => '$c2.id$'], 'c21'),
111+
DataFixture(CategoryFixture::class, ['name' => 'Category 2.1.1', 'parent_id' => '$c21.id$'], 'c211'),
112+
DataFixture(WebsiteFixture::class, as: 'w2'),
113+
DataFixture(GroupFixture::class, ['website_id' => '$w2.id$', 'root_category_id' => '$c2.id$'], 's2'),
114+
DataFixture(StoreFixture::class, ['store_group_id' => '$s2.id$'], as: 'store2'),
115+
DataFixture(
116+
ProductFixture::class,
117+
[
118+
'sku' => 'in-stock-product',
119+
'category_ids' => ['2', '$c11.id$', '$c2.id$', '$c21.id$', '$c211.id$']
120+
],
121+
),
122+
]
132123
public function testProductCategoriesInNotRelevantStore(): void
133124
{
125+
$fixtures = DataFixtureStorageManager::getStorage();
126+
$storeCode = $fixtures->get('store2')->getCode();
127+
134128
$response = $this->graphQlQuery(
135129
$this->getQuery('in-stock-product'),
136130
[],
137131
'',
138-
['Store' => 'fixture_second_store']
132+
['Store' => $storeCode]
139133
);
140134

141135
self::assertEmpty($response['products']['items']);

dev/tests/integration/framework/Magento/TestFramework/Annotation/Cache.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function startTest(TestCase $test)
3838
$this->getFixturesFromCacheAnnotation($test, ParserInterface::SCOPE_METHOD)
3939
);
4040
if (!$statusList) {
41-
array_merge(
41+
$statusList = array_merge(
4242
$this->getFixturesFromCacheAttribute($test, ParserInterface::SCOPE_CLASS),
4343
$this->getFixturesFromCacheAnnotation($test, ParserInterface::SCOPE_CLASS)
4444
);
@@ -58,7 +58,6 @@ public function startTest(TestCase $test)
5858
}
5959
$this->setValues($values, $test);
6060
}
61-
6261
}
6362

6463
/**

dev/tests/integration/framework/Magento/TestFramework/Annotation/DataFixtureBeforeTransaction.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ protected function getParsers(): array
5757
{
5858
$parsers = [];
5959
$parsers[] = Bootstrap::getObjectManager()->create(
60-
\Magento\TestFramework\Fixture\Parser\DataFixtureBeforeTransaction::class
60+
\Magento\TestFramework\Fixture\Parser\DataFixture::class,
61+
[
62+
'attributeClass' => \Magento\TestFramework\Fixture\DataFixtureBeforeTransaction::class
63+
]
6164
);
6265
return array_merge(
6366
parent::getParsers(),

dev/tests/integration/framework/Magento/TestFramework/Fixture/Cache.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use Attribute;
1111

12-
1312
#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
1413
class Cache
1514
{

dev/tests/integration/framework/Magento/TestFramework/Fixture/Parser/DataFixtureBeforeTransaction.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/ProductTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public function testGetAttributeRawValue()
7676
* @throws StateException
7777
*/
7878
#[
79-
DataFixture(AttributeFixture::class, ['attribute_code' => 'prod_attr']),
80-
DataFixture(ProductFixture::class, ['sku' => 'simple']),
79+
AttributeFixture(['attribute_code' => 'prod_attr']),
80+
ProductFixture(['sku' => 'simple']),
8181
]
8282
public function testGetAttributeRawValueGetDefault()
8383
{

0 commit comments

Comments
 (0)