diff --git a/dev/tests/api-functional/testsuite/Magento/Search/Api/SearchTest.php b/dev/tests/api-functional/testsuite/Magento/Search/Api/SearchTest.php new file mode 100644 index 0000000000000..f6167a06c6436 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/Search/Api/SearchTest.php @@ -0,0 +1,110 @@ +create(ProductRepositoryInterface::class); + $this->product = $productRepository->get($productSku); + } + + /** + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + */ + public function testExistingProductSearch() + { + $productName = $this->product->getName(); + + $searchCriteria = $this->buildSearchCriteria($productName); + $serviceInfo = $this->buildServiceInfo($searchCriteria); + + $response = $this->_webApiCall($serviceInfo, $searchCriteria); + + self::assertArrayHasKey('search_criteria', $response); + self::assertArrayHasKey('items', $response); + self::assertGreaterThan(0, count($response['items'])); + self::assertGreaterThan(0, $response['items'][0]['id']); + } + + /** + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + */ + public function testNonExistentProductSearch() + { + $searchCriteria = $this->buildSearchCriteria('nonExistentProduct'); + $serviceInfo = $this->buildServiceInfo($searchCriteria); + + $response = $this->_webApiCall($serviceInfo, $searchCriteria); + + self::assertArrayHasKey('search_criteria', $response); + self::assertArrayHasKey('items', $response); + self::assertEquals(0, count($response['items'])); + } + + /** + * @param string $productName + * @return array + */ + private function buildSearchCriteria(string $productName): array + { + return [ + 'searchCriteria' => [ + 'request_name' => 'quick_search_container', + 'filter_groups' => [ + [ + 'filters' => [ + [ + 'field' => 'search_term', + 'value' => $productName, + ] + ] + ] + ] + ] + ]; + } + + /** + * @param array $searchCriteria + * @return array + */ + private function buildServiceInfo(array $searchCriteria): array + { + return [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . '?' . http_build_query($searchCriteria), + 'httpMethod' => Request::HTTP_METHOD_GET + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'Search' + ] + ]; + } +}