SearchTest.php 3.2 KB

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