|
4 | 4 | * Copyright © Magento, Inc. All rights reserved.
|
5 | 5 | * See COPYING.txt for license details.
|
6 | 6 | */
|
| 7 | +declare(strict_types=1); |
| 8 | + |
7 | 9 | namespace Magento\Catalog\Api;
|
8 | 10 |
|
| 11 | +use Magento\Catalog\Model\ProductLink\Link; |
| 12 | +use Magento\Framework\ObjectManagerInterface; |
| 13 | +use Magento\Framework\Webapi\Rest\Request; |
9 | 14 | use Magento\TestFramework\Helper\Bootstrap;
|
10 | 15 | use Magento\TestFramework\TestCase\WebapiAbstract;
|
11 | 16 |
|
| 17 | +/** |
| 18 | + * Class checks product relations functionality |
| 19 | + * |
| 20 | + * @see \Magento\Catalog\Api\ProductLinkRepositoryInterface |
| 21 | + */ |
12 | 22 | class ProductLinkRepositoryInterfaceTest extends WebapiAbstract
|
13 | 23 | {
|
| 24 | + /** |
| 25 | + * @var string |
| 26 | + */ |
14 | 27 | const SERVICE_NAME = 'catalogProductLinkRepositoryV1';
|
| 28 | + |
| 29 | + /** |
| 30 | + * @var string |
| 31 | + */ |
15 | 32 | const SERVICE_VERSION = 'V1';
|
| 33 | + |
| 34 | + /** |
| 35 | + * @var string |
| 36 | + */ |
16 | 37 | const RESOURCE_PATH = '/V1/products/';
|
17 | 38 |
|
18 | 39 | /**
|
19 |
| - * @var \Magento\Framework\ObjectManagerInterface |
| 40 | + * @var ObjectManagerInterface |
20 | 41 | */
|
21 |
| - protected $objectManager; |
| 42 | + private $objectManager; |
22 | 43 |
|
| 44 | + /** |
| 45 | + * @var ProductLinkManagementInterface |
| 46 | + */ |
| 47 | + private $linkManagement; |
| 48 | + |
| 49 | + /** |
| 50 | + * @inheritdoc |
| 51 | + */ |
23 | 52 | protected function setUp(): void
|
24 | 53 | {
|
| 54 | + parent::setUp(); |
| 55 | + |
25 | 56 | $this->objectManager = Bootstrap::getObjectManager();
|
| 57 | + $this->linkManagement = $this->objectManager->get(ProductLinkManagementInterface::class); |
26 | 58 | }
|
27 | 59 |
|
28 | 60 | /**
|
29 | 61 | * @magentoApiDataFixture Magento/Catalog/_files/products_related_multiple.php
|
30 |
| - * @magentoAppIsolation enabled |
| 62 | + * |
| 63 | + * @return void |
31 | 64 | */
|
32 |
| - public function testDelete() |
| 65 | + public function testDelete(): void |
33 | 66 | {
|
34 | 67 | $productSku = 'simple_with_cross';
|
35 |
| - $linkedSku = 'simple'; |
36 | 68 | $linkType = 'related';
|
37 |
| - $this->_webApiCall( |
38 |
| - [ |
39 |
| - 'rest' => [ |
40 |
| - 'resourcePath' => self::RESOURCE_PATH . $productSku . '/links/' . $linkType . '/' . $linkedSku, |
41 |
| - 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE, |
42 |
| - ], |
43 |
| - 'soap' => [ |
44 |
| - 'service' => self::SERVICE_NAME, |
45 |
| - 'serviceVersion' => self::SERVICE_VERSION, |
46 |
| - 'operation' => self::SERVICE_NAME . 'DeleteById', |
47 |
| - ], |
48 |
| - ], |
49 |
| - [ |
50 |
| - 'sku' => $productSku, |
51 |
| - 'type' => $linkType, |
52 |
| - 'linkedProductSku' => $linkedSku |
53 |
| - ] |
54 |
| - ); |
55 |
| - /** @var \Magento\Catalog\Model\ProductLink\Management $linkManagement */ |
56 |
| - $linkManagement = $this->objectManager->create(\Magento\Catalog\Api\ProductLinkManagementInterface::class); |
57 |
| - $linkedProducts = $linkManagement->getLinkedItemsByType($productSku, $linkType); |
| 69 | + $this->deleteApiCall($productSku, $linkType, 'simple'); |
| 70 | + $linkedProducts = $this->linkManagement->getLinkedItemsByType($productSku, $linkType); |
58 | 71 | $this->assertCount(1, $linkedProducts);
|
59 |
| - /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $product */ |
60 | 72 | $product = current($linkedProducts);
|
61 |
| - $this->assertEquals($product->getLinkedProductSku(), 'simple_with_cross_two'); |
| 73 | + $this->assertEquals('simple_with_cross_two', $product->getLinkedProductSku()); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php |
| 78 | + * |
| 79 | + * @return void |
| 80 | + */ |
| 81 | + public function testDeleteNotExistedProductLink(): void |
| 82 | + { |
| 83 | + $this->expectException(\Exception::class); |
| 84 | + $this->expectExceptionMessage((string)__("Product %1 doesn't have linked %2 as %3")); |
| 85 | + $this->deleteApiCall('simple', 'related', 'not_exists_product'); |
62 | 86 | }
|
63 | 87 |
|
64 | 88 | /**
|
65 | 89 | * @magentoApiDataFixture Magento/Catalog/_files/products_related.php
|
| 90 | + * |
| 91 | + * @return void |
66 | 92 | */
|
67 |
| - public function testSave() |
| 93 | + public function testSave(): void |
68 | 94 | {
|
69 | 95 | $productSku = 'simple_with_cross';
|
70 | 96 | $linkType = 'related';
|
| 97 | + $data = [ |
| 98 | + 'entity' => [ |
| 99 | + Link::KEY_SKU => 'simple_with_cross', |
| 100 | + Link::KEY_LINK_TYPE => 'related', |
| 101 | + Link::KEY_LINKED_PRODUCT_SKU => 'simple', |
| 102 | + Link::KEY_LINKED_PRODUCT_TYPE => 'simple', |
| 103 | + Link::KEY_POSITION => 1000, |
| 104 | + ], |
| 105 | + ]; |
| 106 | + $this->saveApiCall($productSku, $data); |
| 107 | + $actual = $this->linkManagement->getLinkedItemsByType($productSku, $linkType); |
| 108 | + $this->assertCount(1, $actual, 'Invalid actual linked products count'); |
| 109 | + $this->assertEquals(1000, $actual[0]->getPosition(), 'Product position is not updated'); |
| 110 | + } |
71 | 111 |
|
72 |
| - $serviceInfo = [ |
| 112 | + /** |
| 113 | + * Get service info for api call |
| 114 | + * |
| 115 | + * @param string $resourcePath |
| 116 | + * @param string $httpMethod |
| 117 | + * @param string $operation |
| 118 | + * @return array |
| 119 | + */ |
| 120 | + private function getServiceInfo(string $resourcePath, string $httpMethod, string $operation): array |
| 121 | + { |
| 122 | + return [ |
73 | 123 | 'rest' => [
|
74 |
| - 'resourcePath' => self::RESOURCE_PATH . $productSku . '/links', |
75 |
| - 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, |
| 124 | + 'resourcePath' => self::RESOURCE_PATH . $resourcePath, |
| 125 | + 'httpMethod' => $httpMethod, |
76 | 126 | ],
|
77 | 127 | 'soap' => [
|
78 | 128 | 'service' => self::SERVICE_NAME,
|
79 | 129 | 'serviceVersion' => self::SERVICE_VERSION,
|
80 |
| - 'operation' => self::SERVICE_NAME . 'Save', |
| 130 | + 'operation' => self::SERVICE_NAME . $operation, |
81 | 131 | ],
|
82 | 132 | ];
|
| 133 | + } |
83 | 134 |
|
84 |
| - $this->_webApiCall( |
| 135 | + /** |
| 136 | + * Make api call to delete product link |
| 137 | + * |
| 138 | + * @param string $productSku |
| 139 | + * @param string $linkType |
| 140 | + * @param string $linkedSku |
| 141 | + * @return array|int|string|float|bool |
| 142 | + */ |
| 143 | + private function deleteApiCall(string $productSku, string $linkType, string $linkedSku) |
| 144 | + { |
| 145 | + $serviceInfo = $this->getServiceInfo( |
| 146 | + $productSku . '/links/' . $linkType . '/' . $linkedSku, |
| 147 | + Request::HTTP_METHOD_DELETE, |
| 148 | + 'DeleteById' |
| 149 | + ); |
| 150 | + |
| 151 | + return $this->_webApiCall( |
85 | 152 | $serviceInfo,
|
86 | 153 | [
|
87 |
| - 'entity' => [ |
88 |
| - 'sku' => 'simple_with_cross', |
89 |
| - 'link_type' => 'related', |
90 |
| - 'linked_product_sku' => 'simple', |
91 |
| - 'linked_product_type' => 'simple', |
92 |
| - 'position' => 1000, |
93 |
| - ] |
| 154 | + 'sku' => $productSku, |
| 155 | + 'type' => $linkType, |
| 156 | + 'linkedProductSku' => $linkedSku, |
94 | 157 | ]
|
95 | 158 | );
|
| 159 | + } |
96 | 160 |
|
97 |
| - /** @var \Magento\Catalog\Model\ProductLink\Management $linkManagement */ |
98 |
| - $linkManagement = $this->objectManager->get(\Magento\Catalog\Api\ProductLinkManagementInterface::class); |
99 |
| - $actual = $linkManagement->getLinkedItemsByType($productSku, $linkType); |
100 |
| - $this->assertCount(1, $actual, 'Invalid actual linked products count'); |
101 |
| - $this->assertEquals(1000, $actual[0]->getPosition(), 'Product position is not updated'); |
| 161 | + /** |
| 162 | + * Make api call to save product link |
| 163 | + * |
| 164 | + * @param string $productSku |
| 165 | + * @param array $data |
| 166 | + * @return array|bool|float|int|string |
| 167 | + */ |
| 168 | + private function saveApiCall(string $productSku, array $data) |
| 169 | + { |
| 170 | + $serviceInfo = $this->getServiceInfo( |
| 171 | + $productSku . '/links', |
| 172 | + Request::HTTP_METHOD_PUT, |
| 173 | + 'Save' |
| 174 | + ); |
| 175 | + |
| 176 | + return $this->_webApiCall($serviceInfo, $data); |
102 | 177 | }
|
103 | 178 | }
|
0 commit comments