|
| 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 | + 'Asset conversion with interface' => [ |
| 82 | + Asset::class, |
| 83 | + AssetInterface::class, |
| 84 | + [ |
| 85 | + 'id' => [ |
| 86 | + 'value' => 2, |
| 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' => 3, |
| 107 | + 'method' => 'getWidth', |
| 108 | + ], |
| 109 | + 'height' => [ |
| 110 | + 'value' => 4, |
| 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 | + 'Keyword conversion without interface' => [ |
| 124 | + Keyword::class, |
| 125 | + null, |
| 126 | + [ |
| 127 | + 'id' => [ |
| 128 | + 'value' => 2, |
| 129 | + 'method' => 'getId', |
| 130 | + ], |
| 131 | + 'keyword' => [ |
| 132 | + 'value' => 'keyword', |
| 133 | + 'method' => 'getKeyword', |
| 134 | + ], |
| 135 | + ], |
| 136 | + ] |
| 137 | + ]; |
| 138 | + } |
| 139 | +} |
0 commit comments