-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-6372: magento/adobe-stock-integration#584: DataExtractor unit …
…tests #25809
- Loading branch information
Showing
1 changed file
with
139 additions
and
0 deletions.
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
app/code/Magento/MediaGallery/Test/Unit/Model/DataExtractorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\MediaGallery\Test\Unit\Model; | ||
|
||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use Magento\MediaGallery\Model\Asset; | ||
use Magento\MediaGallery\Model\Keyword; | ||
use Magento\MediaGalleryApi\Api\Data\AssetInterface; | ||
use Magento\MediaGalleryApi\Api\Data\KeywordInterface; | ||
use Magento\MediaGallery\Model\DataExtractor; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DataExtractorTest extends TestCase | ||
{ | ||
/** | ||
* @var DataExtractor|MockObject | ||
*/ | ||
private $dataExtractor; | ||
|
||
/** | ||
* Initialize basic test class mocks | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->dataExtractor = new DataExtractor(); | ||
} | ||
|
||
/** | ||
* Test extract object data by interface | ||
* | ||
* @dataProvider assetProvider | ||
* | ||
* @param string $class | ||
* @param string|null $interfaceClass | ||
* @param array $expectedData | ||
* | ||
* @throws \ReflectionException | ||
*/ | ||
public function testExtractData(string $class, $interfaceClass, array $expectedData): void | ||
{ | ||
$data = []; | ||
foreach ($expectedData as $expectedDataKey => $expectedDataItem) { | ||
$data[$expectedDataKey] = $expectedDataItem['value']; | ||
} | ||
$model = (new ObjectManager($this))->getObject( | ||
$class, | ||
[ | ||
'data' => $data, | ||
] | ||
); | ||
$receivedData = $this->dataExtractor->extract($model, $interfaceClass); | ||
$this->checkValues($expectedData, $receivedData, $model); | ||
} | ||
|
||
/** | ||
* @param array $expectedData | ||
* @param array $data | ||
* @param object $model | ||
*/ | ||
protected function checkValues(array $expectedData, array $data, $model) | ||
{ | ||
foreach ($expectedData as $expectedDataKey => $expectedDataItem) { | ||
$this->assertEquals($data[$expectedDataKey] ?? null, $model->{$expectedDataItem['method']}()); | ||
$this->assertEquals($data[$expectedDataKey] ?? null, $expectedDataItem['value']); | ||
} | ||
$this->assertEquals(array_keys($expectedData), array_keys($expectedData)); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function assetProvider() | ||
{ | ||
return [ | ||
'Asset conversion with interface' => [ | ||
Asset::class, | ||
AssetInterface::class, | ||
[ | ||
'id' => [ | ||
'value' => 2, | ||
'method' => 'getId', | ||
], | ||
'path' => [ | ||
'value' => 'path', | ||
'method' => 'getPath', | ||
], | ||
'title' => [ | ||
'value' => 'title', | ||
'method' => 'getTitle', | ||
], | ||
'source' => [ | ||
'value' => 'source', | ||
'method' => 'getSource', | ||
], | ||
'content_type' => [ | ||
'value' => 'content_type', | ||
'method' => 'getContentType', | ||
], | ||
'width' => [ | ||
'value' => 3, | ||
'method' => 'getWidth', | ||
], | ||
'height' => [ | ||
'value' => 4, | ||
'method' => 'getHeight', | ||
], | ||
'created_at' => [ | ||
'value' => '2019-11-28 10:40:09', | ||
'method' => 'getCreatedAt', | ||
], | ||
'updated_at' => [ | ||
'value' => '2019-11-28 10:41:08', | ||
'method' => 'getUpdatedAt', | ||
], | ||
], | ||
], | ||
'Keyword conversion without interface' => [ | ||
Keyword::class, | ||
null, | ||
[ | ||
'id' => [ | ||
'value' => 2, | ||
'method' => 'getId', | ||
], | ||
'keyword' => [ | ||
'value' => 'keyword', | ||
'method' => 'getKeyword', | ||
], | ||
], | ||
] | ||
]; | ||
} | ||
} |