Skip to content

Commit c2051df

Browse files
committed
API-functional test for Search
1 parent d40a9ec commit c2051df

File tree

1 file changed

+111
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)