13
13
use Magento \AdobeStockAssetApi \Api \CreatorRepositoryInterface ;
14
14
use Magento \AdobeStockAssetApi \Api \Data \AssetInterface ;
15
15
use Magento \AdobeStockAssetApi \Api \Data \AssetInterfaceFactory ;
16
+ use Magento \AdobeStockAssetApi \Api \Data \CategoryInterfaceFactory ;
16
17
use Magento \AdobeStockAssetApi \Api \SaveAssetInterface ;
17
18
use Magento \MediaGalleryApi \Api \GetAssetsByPathsInterface ;
18
19
use Magento \MediaGalleryApi \Api \SaveAssetsInterface ;
@@ -35,6 +36,29 @@ class SaveAssetTest extends TestCase
35
36
*/
36
37
private $ saveAsset ;
37
38
39
+ /**
40
+ * @return array
41
+ */
42
+ public function getAssetData (): array
43
+ {
44
+ return [
45
+ 'asset_save ' => [
46
+ 'data ' => [
47
+ 'media_gallery_path ' => ['some/path.jpg ' ],
48
+ 'category_id ' => 42 ,
49
+ 'creator_id ' => 42 ,
50
+ ]
51
+ ],
52
+ 'without_category ' => [
53
+ 'data ' => [
54
+ 'media_gallery_path ' => ['some/path.jpg ' ],
55
+ 'category_id ' => null ,
56
+ 'creator_id ' => 42 ,
57
+ ]
58
+ ]
59
+ ];
60
+ }
61
+
38
62
/**
39
63
* @inheritdoc
40
64
*/
@@ -46,13 +70,16 @@ protected function setUp(): void
46
70
47
71
/**
48
72
* Test save an Adobe Stock asset.
73
+ * @param array $caseData
74
+ *
75
+ * @dataProvider getAssetData
49
76
* @magentoDataFixture ../../../../app/code/Magento/AdobeStockAsset/Test/_files/media_asset.php
50
77
* @magentoDataFixture ../../../../app/code/Magento/AdobeStockAsset/Test/_files/category.php
51
78
* @magentoDataFixture ../../../../app/code/Magento/AdobeStockAsset/Test/_files/creator.php
52
79
*/
53
- public function testExecute (): void
80
+ public function testExecute (array $ caseData ): void
54
81
{
55
- $ asset = $ this ->prepareAsset ();
82
+ $ asset = $ this ->prepareAsset ($ caseData );
56
83
$ this ->saveAsset ->execute ($ asset );
57
84
$ expectedAsset = $ this ->assetRepository ->getById ($ asset ->getId ());
58
85
@@ -66,35 +93,24 @@ public function testExecute(): void
66
93
67
94
/**
68
95
* Prepare an Adobe Stock asset test object.
96
+ *
97
+ * @param array $caseData
98
+ * @return AssetInterface
99
+ * @throws \Magento\Framework\Exception\LocalizedException
69
100
*/
70
- public function prepareAsset (): AssetInterface
101
+ public function prepareAsset (array $ caseData ): AssetInterface
71
102
{
72
- /** @var GetAssetsByPathsInterface $mediaGetByPath */
73
- $ mediaGetByPath = Bootstrap::getObjectManager ()->get (GetAssetsByPathsInterface::class);
74
- $ mediaAssetId = $ mediaGetByPath ->execute (['some/path.jpg ' ])[0 ]->getId ();
75
-
76
- /** @var CategoryRepositoryInterface $categoryRepository */
77
- $ categoryRepository = Bootstrap::getObjectManager ()->get (CategoryRepositoryInterface::class);
78
- $ category = $ categoryRepository ->getById (42 );
79
-
80
- /** @var CreatorRepositoryInterface $creatorRepository */
81
- $ creatorRepository = Bootstrap::getObjectManager ()->get (CreatorRepositoryInterface::class);
82
- $ creator = $ creatorRepository ->getById (42 );
83
-
103
+ $ assetData ['data ' ] = [
104
+ 'id ' => 1 ,
105
+ 'is_licensed ' => 1 ,
106
+ ];
107
+ $ assetData ['data ' ]['media_gallery_id ' ] = $ this ->getMediaAssetId ($ caseData ['media_gallery_path ' ]);
108
+ $ assetData ['data ' ]['category ' ] = $ this ->getCategory ($ caseData ['category_id ' ]);
109
+ $ assetData ['data ' ]['creator ' ] = $ this ->getCreator ($ caseData ['creator_id ' ]);
84
110
/** @var AssetInterfaceFactory $assetFactory */
85
111
$ assetFactory = Bootstrap::getObjectManager ()->get (AssetInterfaceFactory::class);
86
112
/** @var AssetInterface $asset */
87
- $ asset = $ assetFactory ->create (
88
- [
89
- 'data ' => [
90
- 'id ' => 1 ,
91
- 'is_licensed ' => 1 ,
92
- 'media_gallery_id ' => $ mediaAssetId ,
93
- 'category ' => $ category ,
94
- 'creator ' => $ creator
95
- ]
96
- ]
97
- );
113
+ $ asset = $ assetFactory ->create ($ assetData );
98
114
99
115
return $ asset ;
100
116
}
@@ -109,4 +125,47 @@ private function cleanUpEntries(AssetInterface $asset): void
109
125
{
110
126
$ this ->assetRepository ->deleteById ($ asset ->getId ());
111
127
}
128
+
129
+ /**
130
+ * @param $paths
131
+ * @return int|null
132
+ * @throws \Magento\Framework\Exception\LocalizedException
133
+ */
134
+ protected function getMediaAssetId ($ paths ): int
135
+ {
136
+ /** @var GetAssetsByPathsInterface $mediaGetByPath */
137
+ $ mediaGetByPath = Bootstrap::getObjectManager ()->get (GetAssetsByPathsInterface::class);
138
+ $ mediaAssetId = $ mediaGetByPath ->execute ($ paths )[0 ]->getId ();
139
+
140
+ return $ mediaAssetId ;
141
+ }
142
+
143
+ /**
144
+ * @param int|null $categoryId
145
+ * @return \Magento\AdobeStockAssetApi\Api\Data\CategoryInterface|null
146
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
147
+ */
148
+ protected function getCategory (?int $ categoryId ): ?\Magento \AdobeStockAssetApi \Api \Data \CategoryInterface
149
+ {
150
+ /** @var CategoryRepositoryInterface $categoryRepository */
151
+ $ categoryRepository = Bootstrap::getObjectManager ()->get (CategoryRepositoryInterface::class);
152
+ /** @var CategoryInterfaceFactory $categoryRepository */
153
+ $ categoryFactory = Bootstrap::getObjectManager ()->get (CategoryInterfaceFactory::class);
154
+
155
+ return $ categoryId !== null ? $ categoryRepository ->getById ($ categoryId ) : $ categoryFactory ->create ();
156
+ }
157
+
158
+ /**
159
+ * @param int $creatorId
160
+ * @return \Magento\AdobeStockAssetApi\Api\Data\CreatorInterface
161
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
162
+ */
163
+ protected function getCreator (int $ creatorId ): \Magento \AdobeStockAssetApi \Api \Data \CreatorInterface
164
+ {
165
+ /** @var CreatorRepositoryInterface $creatorRepository */
166
+ $ creatorRepository = Bootstrap::getObjectManager ()->get (CreatorRepositoryInterface::class);
167
+ $ creator = $ creatorRepository ->getById ($ creatorId );
168
+
169
+ return $ creator ;
170
+ }
112
171
}
0 commit comments