77
88namespace Magento \AdobeStockAsset \Test \Integration \Model ;
99
10- use AdobeStock \Api \Models \StockFile ;
11- use AdobeStock \Api \Response \SearchFiles as SearchFilesResponse ;
1210use Magento \AdobeStockClient \Model \Client ;
13- use Magento \AdobeStockClient \Model \StockFileToDocument ;
11+ use Magento \Framework \Api \AttributeValue ;
12+ use Magento \Framework \Api \Search \Document ;
1413use Magento \Framework \Api \Search \SearchCriteriaInterface ;
15- use Magento \Framework \Api \Search \SearchResultFactory ;
14+ use Magento \Framework \Api \Search \SearchResult ;
1615use Magento \Framework \Api \Search \SearchResultInterface ;
17- use Magento \Framework \Exception \IntegrationException ;
18- use Psr \Log \LoggerInterface ;
1916
2017class ClientMock extends Client
2118{
22- /**
23- * @var SearchResultFactory
24- */
25- private $ searchResultFactory ;
26-
27- /**
28- * @var StockFileToDocument
29- */
30- private $ stockFileToDocument ;
31-
32- /**
33- * @var LoggerInterface
34- */
35- private $ logger ;
36-
37- /**
38- * @param SearchResultFactory $searchResultFactory
39- * @param LoggerInterface $logger
40- * @param StockFileToDocument $stockFileToDocument
41- */
42- public function __construct (
43- SearchResultFactory $ searchResultFactory ,
44- LoggerInterface $ logger ,
45- StockFileToDocument $ stockFileToDocument
46- ) {
47- $ this ->searchResultFactory = $ searchResultFactory ;
48- $ this ->logger = $ logger ;
49- $ this ->stockFileToDocument = $ stockFileToDocument ;
50- }
19+ private const ID = 'id ' ;
20+ private const CUSTOM_ATTRIBUTES = 'custom_attributes ' ;
5121
5222 /**
5323 * Search for assets
@@ -58,97 +28,101 @@ public function __construct(
5828 public function search (SearchCriteriaInterface $ searchCriteria ): SearchResultInterface
5929 {
6030 $ items = [];
61- $ totalCount = 0 ;
62-
63- try {
64- $ response = $ this ->getNextResponse ();
65- /** @var StockFile $file */
66- foreach ($ response ->getFiles () as $ file ) {
67- $ items [] = $ this ->stockFileToDocument ->convert ($ file );
68- }
69- $ totalCount = $ response ->getNbResults ();
70- } catch (IntegrationException $ exception ) {
71- $ this ->logger ->critical ($ exception ->getMessage ());
31+ foreach ($ this ->getStockFiles () as $ file ) {
32+ $ items [] = $ this ->getStockFileDocument ($ file );
7233 }
7334
74- $ searchResult = $ this -> searchResultFactory -> create ();
35+ $ searchResult = new SearchResult ();
7536 $ searchResult ->setSearchCriteria ($ searchCriteria );
7637 $ searchResult ->setItems ($ items );
77- $ searchResult ->setTotalCount ($ totalCount );
38+ $ searchResult ->setTotalCount (3 );
7839
7940 return $ searchResult ;
8041 }
8142
8243 /**
83- * Get the next search files response page
84- *
85- * @return SearchFilesResponse
86- */
87- private function getNextResponse (): SearchFilesResponse
88- {
89- $ response = new SearchFilesResponse ();
90- $ response ->setFiles ($ this ->getStockFiles ());
91- $ response ->setNbResults (3 );
92- return $ response ;
93- }
94-
95- /**
96- * Result files.
44+ * Get array of stock files data.
9745 *
98- * @return StockFile[]
46+ * @return array
9947 */
10048 private function getStockFiles (): array
10149 {
10250 $ stockFilesData = [
10351 [
10452 'id ' => 1 ,
105- 'comp_url ' => 'https://test.url/1 ' ,
106- 'thumbnail_240_url ' => 'https://test.url/1 ' ,
107- 'width ' => 110 ,
108- 'height ' => 210 ,
109- 'some_bool_param ' => false ,
110- 'some_nullable_param ' => null ,
111- 'category ' => [
53+ 'custom_attributes ' => [
54+ 'id_field_name ' => 'id ' ,
11255 'id ' => 1 ,
113- 'N
114- name ' => 'Test '
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
11566 ]
11667 ],
11768 [
11869 'id ' => 2 ,
119- 'comp_url ' => 'https://test.url/2 ' ,
120- 'thumbnail_240_url ' => 'https://test.url/2 ' ,
121- 'width ' => 120 ,
122- 'height ' => 220 ,
123- 'some_bool_params ' => false ,
124- 'some_nullable_param ' => 1 ,
125- 'category ' => [
126- 'id ' => 1 ,
127- 'N
128- name ' => 'Test '
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
12983 ]
13084 ],
13185 [
13286 'id ' => 3 ,
133- 'comp_url ' => 'https://test.url/3 ' ,
134- 'thumbnail_240_url ' => 'https://test.url/3 ' ,
135- 'width ' => 130 ,
136- 'height ' => 230 ,
137- 'some_bool_params ' => true ,
138- 'some_nullable_param ' => 2 ,
139- 'category ' => [
140- 'id ' => 1 ,
141- 'N
142- name ' => 'Test '
143- ]
144- ],
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+ ]
145102 ];
146103
147- $ stockFiles = [];
148- foreach ($ stockFilesData as $ stockFileData ) {
149- $ stockFiles [] = new StockFile ($ stockFileData );
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 ;
150122 }
151123
152- return $ stockFiles ;
124+ $ item ->setCustomAttributes ($ attributes );
125+
126+ return $ item ;
153127 }
154128}
0 commit comments