|
| 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\Search\Api; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Framework\Webapi\Rest\Request; |
| 13 | +use Magento\TestFramework\Helper\Bootstrap; |
| 14 | +use Magento\TestFramework\TestCase\WebapiAbstract; |
| 15 | + |
| 16 | +class SearchTest extends WebapiAbstract |
| 17 | +{ |
| 18 | + const SERVICE_VERSION = 'V1'; |
| 19 | + const SERVICE_NAME = 'searchV1'; |
| 20 | + const RESOURCE_PATH = '/V1/search/'; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var ProductInterface |
| 24 | + */ |
| 25 | + private $product; |
| 26 | + |
| 27 | + protected function setUp() |
| 28 | + { |
| 29 | + $productSku = 'simple'; |
| 30 | + |
| 31 | + $objectManager = Bootstrap::getObjectManager(); |
| 32 | + $productRepository = $objectManager->create(ProductRepositoryInterface::class); |
| 33 | + $this->product = $productRepository->get($productSku); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php |
| 38 | + */ |
| 39 | + public function testExistingProductSearch() |
| 40 | + { |
| 41 | + $productName = $this->product->getName(); |
| 42 | + |
| 43 | + $searchCriteria = $this->buildSearchCriteria($productName); |
| 44 | + $serviceInfo = $this->buildServiceInfo($searchCriteria); |
| 45 | + |
| 46 | + $response = $this->_webApiCall($serviceInfo, $searchCriteria); |
| 47 | + |
| 48 | + self::assertArrayHasKey('search_criteria', $response); |
| 49 | + self::assertArrayHasKey('items', $response); |
| 50 | + self::assertGreaterThan(0, count($response['items'])); |
| 51 | + self::assertGreaterThan(0, $response['items'][0]['id']); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php |
| 56 | + */ |
| 57 | + public function testNonExistentProductSearch() |
| 58 | + { |
| 59 | + $searchCriteria = $this->buildSearchCriteria('nonExistentProduct'); |
| 60 | + $serviceInfo = $this->buildServiceInfo($searchCriteria); |
| 61 | + |
| 62 | + $response = $this->_webApiCall($serviceInfo, $searchCriteria); |
| 63 | + |
| 64 | + self::assertArrayHasKey('search_criteria', $response); |
| 65 | + self::assertArrayHasKey('items', $response); |
| 66 | + self::assertEquals(0, count($response['items'])); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param string $productName |
| 71 | + * @return array |
| 72 | + */ |
| 73 | + private function buildSearchCriteria(string $productName): array |
| 74 | + { |
| 75 | + return [ |
| 76 | + 'searchCriteria' => [ |
| 77 | + 'request_name' => 'quick_search_container', |
| 78 | + 'filter_groups' => [ |
| 79 | + [ |
| 80 | + 'filters' => [ |
| 81 | + [ |
| 82 | + 'field' => 'search_term', |
| 83 | + 'value' => $productName, |
| 84 | + ] |
| 85 | + ] |
| 86 | + ] |
| 87 | + ] |
| 88 | + ] |
| 89 | + ]; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @param array $searchCriteria |
| 94 | + * @return array |
| 95 | + */ |
| 96 | + private function buildServiceInfo(array $searchCriteria): array |
| 97 | + { |
| 98 | + return [ |
| 99 | + 'rest' => [ |
| 100 | + 'resourcePath' => self::RESOURCE_PATH . '?' . http_build_query($searchCriteria), |
| 101 | + 'httpMethod' => Request::HTTP_METHOD_GET |
| 102 | + ], |
| 103 | + 'soap' => [ |
| 104 | + 'service' => self::SERVICE_NAME, |
| 105 | + 'serviceVersion' => self::SERVICE_VERSION, |
| 106 | + 'operation' => self::SERVICE_NAME . 'Search' |
| 107 | + ] |
| 108 | + ]; |
| 109 | + } |
| 110 | +} |
0 commit comments