|
| 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\AdobeStockAsset\Test\Integration\Model; |
| 9 | + |
| 10 | +use Magento\AdobeStockClient\Model\Client; |
| 11 | +use Magento\Framework\Api\AttributeValue; |
| 12 | +use Magento\Framework\Api\Search\Document; |
| 13 | +use Magento\Framework\Api\Search\SearchCriteriaInterface; |
| 14 | +use Magento\Framework\Api\Search\SearchResult; |
| 15 | +use Magento\Framework\Api\Search\SearchResultInterface; |
| 16 | + |
| 17 | +class ClientMock extends Client |
| 18 | +{ |
| 19 | + private const ID = 'id'; |
| 20 | + private const CUSTOM_ATTRIBUTES = 'custom_attributes'; |
| 21 | + |
| 22 | + /** |
| 23 | + * Search for assets |
| 24 | + * |
| 25 | + * @param SearchCriteriaInterface $searchCriteria |
| 26 | + * @return SearchResultInterface |
| 27 | + */ |
| 28 | + public function search(SearchCriteriaInterface $searchCriteria): SearchResultInterface |
| 29 | + { |
| 30 | + $items = []; |
| 31 | + foreach ($this->getStockFiles() as $file) { |
| 32 | + $items[] = $this->getStockFileDocument($file); |
| 33 | + } |
| 34 | + |
| 35 | + $searchResult = new SearchResult(); |
| 36 | + $searchResult->setSearchCriteria($searchCriteria); |
| 37 | + $searchResult->setItems($items); |
| 38 | + $searchResult->setTotalCount(3); |
| 39 | + |
| 40 | + return $searchResult; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Get array of stock files data. |
| 45 | + * |
| 46 | + * @return array |
| 47 | + */ |
| 48 | + private function getStockFiles(): array |
| 49 | + { |
| 50 | + $stockFilesData = [ |
| 51 | + [ |
| 52 | + 'id' => 1, |
| 53 | + 'custom_attributes' => [ |
| 54 | + 'id_field_name' => 'id', |
| 55 | + 'id' => 1, |
| 56 | + 'thumbnail_240_url' => 'https://test.url/1', |
| 57 | + 'width' => 110, |
| 58 | + 'height' => 210, |
| 59 | + 'comp_url' => 'https://test.url/1', |
| 60 | + 'category' => [ |
| 61 | + 'id' => 1, |
| 62 | + 'name' => 'Test', |
| 63 | + 'link' => null |
| 64 | + ], |
| 65 | + 'category_id' => 1 |
| 66 | + ] |
| 67 | + ], |
| 68 | + [ |
| 69 | + 'id' => 2, |
| 70 | + 'custom_attributes' => [ |
| 71 | + 'id_field_name' => 'id', |
| 72 | + 'id' => 2, |
| 73 | + 'thumbnail_240_url' => 'https://test.url/2', |
| 74 | + 'width' => 120, |
| 75 | + 'height' => 220, |
| 76 | + 'comp_url' => 'https://test.url/2', |
| 77 | + 'category' => [ |
| 78 | + 'id' => 1, |
| 79 | + 'name' => 'Test', |
| 80 | + 'link' => null |
| 81 | + ], |
| 82 | + 'category_id' => 1 |
| 83 | + ] |
| 84 | + ], |
| 85 | + [ |
| 86 | + 'id' => 3, |
| 87 | + 'custom_attributes' => [ |
| 88 | + 'id_field_name' => 'id', |
| 89 | + 'id' => 3, |
| 90 | + 'thumbnail_240_url' => 'https://test.url/3', |
| 91 | + 'width' => 130, |
| 92 | + 'height' => 230, |
| 93 | + 'comp_url' => 'https://test.url/3', |
| 94 | + 'category' => [ |
| 95 | + 'id' => 1, |
| 96 | + 'name' => 'Test', |
| 97 | + 'link' => null |
| 98 | + ], |
| 99 | + 'category_id' => 1 |
| 100 | + ], |
| 101 | + ] |
| 102 | + ]; |
| 103 | + |
| 104 | + return $stockFilesData; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @param array $stockFiles |
| 109 | + * @return Document |
| 110 | + */ |
| 111 | + private function getStockFileDocument(array $stockFiles): Document |
| 112 | + { |
| 113 | + $item = new Document(); |
| 114 | + $item->setId($stockFiles[self::ID]); |
| 115 | + |
| 116 | + $attributes = []; |
| 117 | + foreach ($stockFiles[self::CUSTOM_ATTRIBUTES] as $attributeCode => $value) { |
| 118 | + $attribute = new AttributeValue(); |
| 119 | + $attribute->setAttributeCode($attributeCode); |
| 120 | + $attribute->setValue($value); |
| 121 | + $attributes[$attributeCode] = $attribute; |
| 122 | + } |
| 123 | + |
| 124 | + $item->setCustomAttributes($attributes); |
| 125 | + |
| 126 | + return $item; |
| 127 | + } |
| 128 | +} |
0 commit comments