Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductLinkManagementInterfaceTest.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Catalog\Api;
8 
11 
16 {
17  const SERVICE_NAME = 'catalogProductLinkManagementV1';
18  const SERVICE_VERSION = 'V1';
19  const RESOURCE_PATH = '/V1/products/';
20 
24  protected $objectManager;
25 
26  protected function setUp()
27  {
28  $this->objectManager = Bootstrap::getObjectManager();
29  }
30 
35  {
36  $productSku = 'simple_with_cross';
37  $linkType = 'crosssell';
38 
39  $this->assertLinkedProducts($productSku, $linkType);
40  }
41 
45  public function testGetLinkedProductsRelated()
46  {
47  $productSku = 'simple_with_cross';
48  $linkType = 'related';
49 
50  $this->assertLinkedProducts($productSku, $linkType);
51  }
52 
56  public function testGetLinkedProductsUpSell()
57  {
58  $productSku = 'simple_with_upsell';
59  $linkType = 'upsell';
60 
61  $this->assertLinkedProducts($productSku, $linkType);
62  }
63 
68  protected function assertLinkedProducts($productSku, $linkType)
69  {
70  $serviceInfo = [
71  'rest' => [
72  'resourcePath' => self::RESOURCE_PATH . $productSku . '/links/' . $linkType,
74  ],
75  'soap' => [
76  'service' => self::SERVICE_NAME,
77  'serviceVersion' => self::SERVICE_VERSION,
78  'operation' => self::SERVICE_NAME . 'GetLinkedItemsByType',
79  ],
80  ];
81 
82  $actual = $this->_webApiCall($serviceInfo, ['sku' => $productSku, 'type' => $linkType]);
83 
84  $this->assertEquals('simple', $actual[0]['linked_product_type']);
85  $this->assertEquals('simple', $actual[0]['linked_product_sku']);
86  $this->assertEquals(1, $actual[0]['position']);
87  }
88 
93  public function testAssign()
94  {
95  $linkType = 'related';
96  $productSku = 'simple';
97  $linkData = [
98  'linked_product_type' => 'virtual',
99  'linked_product_sku' => 'virtual-product',
100  'position' => 100,
101  'sku' => 'simple',
102  'link_type' => 'related',
103  ];
104 
105  $serviceInfo = [
106  'rest' => [
107  'resourcePath' => self::RESOURCE_PATH . $productSku . '/links',
109  ],
110  'soap' => [
111  'service' => self::SERVICE_NAME,
112  'serviceVersion' => self::SERVICE_VERSION,
113  'operation' => self::SERVICE_NAME . 'SetProductLinks',
114  ],
115  ];
116 
117  $arguments = [
118  'sku' => $productSku,
119  'items' => [$linkData],
120  'type' => $linkType,
121  ];
122 
123  $this->_webApiCall($serviceInfo, $arguments);
124  $actual = $this->getLinkedProducts($productSku, 'related');
125  array_walk($actual, function (&$item) {
126  $item = $item->__toArray();
127  });
128  $this->assertEquals([$linkData], $actual);
129  }
130 
138  protected function getLinkedProducts($sku, $linkType)
139  {
141  $linkManagement = $this->objectManager->get(\Magento\Catalog\Api\ProductLinkManagementInterface::class);
142  $linkedProducts = $linkManagement->getLinkedItemsByType($sku, $linkType);
143 
144  return $linkedProducts;
145  }
146 }
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$arguments