Skip to content

Commit 665c525

Browse files
committed
magento/adobe-stock-integration#584: DataExtractor unit tests
1 parent c7ca62e commit 665c525

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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\MediaGallery\Test\Unit\Model;
9+
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\MediaGallery\Model\Asset;
12+
use Magento\MediaGallery\Model\Keyword;
13+
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
14+
use Magento\MediaGalleryApi\Api\Data\KeywordInterface;
15+
use Magento\MediaGallery\Model\DataExtractor;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class DataExtractorTest extends TestCase
20+
{
21+
/**
22+
* @var DataExtractor|MockObject
23+
*/
24+
private $dataExtractor;
25+
26+
/**
27+
* Initialize basic test class mocks
28+
*/
29+
protected function setUp(): void
30+
{
31+
$this->dataExtractor = new DataExtractor();
32+
}
33+
34+
/**
35+
* Test extract object data by interface
36+
*
37+
* @dataProvider assetProvider
38+
*
39+
* @param string $class
40+
* @param string|null $interfaceClass
41+
* @param array $expectedData
42+
*
43+
* @throws \ReflectionException
44+
*/
45+
public function testExtractData(string $class, $interfaceClass, array $expectedData): void
46+
{
47+
$data = [];
48+
foreach ($expectedData as $expectedDataKey => $expectedDataItem) {
49+
$data[$expectedDataKey] = $expectedDataItem['value'];
50+
}
51+
$model = (new ObjectManager($this))->getObject(
52+
$class,
53+
[
54+
'data' => $data,
55+
]
56+
);
57+
$receivedData = $this->dataExtractor->extract($model, $interfaceClass);
58+
$this->checkValues($expectedData, $receivedData, $model);
59+
}
60+
61+
/**
62+
* @param array $expectedData
63+
* @param array $data
64+
* @param object $model
65+
*/
66+
protected function checkValues(array $expectedData, array $data, $model)
67+
{
68+
foreach ($expectedData as $expectedDataKey => $expectedDataItem) {
69+
$this->assertEquals($data[$expectedDataKey] ?? null, $model->{$expectedDataItem['method']}());
70+
$this->assertEquals($data[$expectedDataKey] ?? null, $expectedDataItem['value']);
71+
}
72+
$this->assertEquals(array_keys($expectedData), array_keys($expectedData));
73+
}
74+
75+
/**
76+
* @return array
77+
*/
78+
public function assetProvider()
79+
{
80+
return [
81+
'Test case asset 1' => [
82+
Asset::class,
83+
null,
84+
[
85+
'id' => [
86+
'value' => 1,
87+
'method' => 'getId',
88+
],
89+
'path' => [
90+
'value' => 'path',
91+
'method' => 'getPath',
92+
],
93+
'title' => [
94+
'value' => 'title',
95+
'method' => 'getTitle',
96+
],
97+
'source' => [
98+
'value' => 'source',
99+
'method' => 'getSource',
100+
],
101+
'content_type' => [
102+
'value' => 'content_type',
103+
'method' => 'getContentType',
104+
],
105+
'width' => [
106+
'value' => 1,
107+
'method' => 'getWidth',
108+
],
109+
'height' => [
110+
'value' => 2,
111+
'method' => 'getHeight',
112+
],
113+
'created_at' => [
114+
'value' => '2019-11-28 10:40:09',
115+
'method' => 'getCreatedAt',
116+
],
117+
'updated_at' => [
118+
'value' => '2019-11-28 10:41:08',
119+
'method' => 'getUpdatedAt',
120+
],
121+
],
122+
],
123+
'Test case asset 2' => [
124+
Asset::class,
125+
AssetInterface::class,
126+
[
127+
'id' => [
128+
'value' => 2,
129+
'method' => 'getId',
130+
],
131+
'path' => [
132+
'value' => 'path',
133+
'method' => 'getPath',
134+
],
135+
'title' => [
136+
'value' => 'title',
137+
'method' => 'getTitle',
138+
],
139+
'source' => [
140+
'value' => 'source',
141+
'method' => 'getSource',
142+
],
143+
'content_type' => [
144+
'value' => 'content_type',
145+
'method' => 'getContentType',
146+
],
147+
'width' => [
148+
'value' => 3,
149+
'method' => 'getWidth',
150+
],
151+
'height' => [
152+
'value' => 4,
153+
'method' => 'getHeight',
154+
],
155+
'created_at' => [
156+
'value' => '2019-11-28 10:40:09',
157+
'method' => 'getCreatedAt',
158+
],
159+
'updated_at' => [
160+
'value' => '2019-11-28 10:41:08',
161+
'method' => 'getUpdatedAt',
162+
],
163+
],
164+
],
165+
'Test case keyword 1' => [
166+
Keyword::class,
167+
null,
168+
[
169+
'id' => [
170+
'value' => 2,
171+
'method' => 'getId',
172+
],
173+
'keyword' => [
174+
'value' => 'keyword',
175+
'method' => 'getKeyword',
176+
],
177+
],
178+
],
179+
'Test case keyword 2' => [
180+
Keyword::class,
181+
KeywordInterface::class,
182+
[
183+
'id' => [
184+
'value' => 3,
185+
'method' => 'getId',
186+
],
187+
'keyword' => [
188+
'value' => 'keyword2',
189+
'method' => 'getKeyword',
190+
],
191+
],
192+
],
193+
];
194+
}
195+
}

0 commit comments

Comments
 (0)