Skip to content

Commit

Permalink
ENGCOM-6372: magento/adobe-stock-integration#584: DataExtractor unit …
Browse files Browse the repository at this point in the history
…tests #25809
  • Loading branch information
VladimirZaets authored Dec 2, 2019
2 parents f1d0880 + b13a823 commit 64143a1
Showing 1 changed file with 139 additions and 0 deletions.
139 changes: 139 additions & 0 deletions app/code/Magento/MediaGallery/Test/Unit/Model/DataExtractorTest.php
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',
],
],
]
];
}
}

0 comments on commit 64143a1

Please sign in to comment.