Skip to content

Commit a6880a0

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-25660-Optional-Store-Api-Tests' into develop
2 parents 149988a + f829c73 commit a6880a0

File tree

6 files changed

+163
-37
lines changed

6 files changed

+163
-37
lines changed

dev/tests/api-functional/framework/Magento/TestFramework/TestCase/Webapi/Adapter/Rest.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ public function __construct()
6060

6161
/**
6262
* {@inheritdoc}
63-
* @throws \Exception
63+
* @throws \LogicException
64+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
6465
*/
65-
public function call($serviceInfo, $arguments = [])
66+
public function call($serviceInfo, $arguments = [], $storeCode = null)
6667
{
67-
$resourcePath = '/' . $this->defaultStoreCode . $this->_getRestResourcePath($serviceInfo);
68+
$storeCode = !is_null($storeCode) ? (string)$storeCode : $this->defaultStoreCode;
69+
$resourcePath = '/' . $storeCode . $this->_getRestResourcePath($serviceInfo);
6870
$httpMethod = $this->_getRestHttpMethod($serviceInfo);
6971
//Get a valid token
7072
$accessCredentials = \Magento\TestFramework\Authentication\OauthHelper::getApiAccessCredentials();

dev/tests/api-functional/framework/Magento/TestFramework/TestCase/Webapi/Adapter/Soap.php

+12-7
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public function __construct()
5353
/**
5454
* {@inheritdoc}
5555
*/
56-
public function call($serviceInfo, $arguments = [])
56+
public function call($serviceInfo, $arguments = [], $storeCode = null)
5757
{
5858
$soapOperation = $this->_getSoapOperation($serviceInfo);
5959
$arguments = $this->_converter->convertKeysToCamelCase($arguments);
60-
$soapResponse = $this->_getSoapClient($serviceInfo)->$soapOperation($arguments);
60+
$soapResponse = $this->_getSoapClient($serviceInfo, $storeCode)->$soapOperation($arguments);
6161
//Convert to snake case for tests to use same assertion data for both SOAP and REST tests
6262
$result = (is_array($soapResponse) || is_object($soapResponse))
6363
? $this->toSnakeCase($this->_converter->convertStdObjectToArray($soapResponse, true))
@@ -71,12 +71,14 @@ public function call($serviceInfo, $arguments = [])
7171
* Get proper SOAP client instance that is initialized with with WSDL corresponding to requested service interface.
7272
*
7373
* @param string $serviceInfo PHP service interface name, should include version if present
74+
* @param string|null $storeCode
7475
* @return \Zend\Soap\Client
7576
*/
76-
protected function _getSoapClient($serviceInfo)
77+
protected function _getSoapClient($serviceInfo, $storeCode = null)
7778
{
7879
$wsdlUrl = $this->generateWsdlUrl(
79-
[$this->_getSoapServiceName($serviceInfo) . $this->_getSoapServiceVersion($serviceInfo)]
80+
[$this->_getSoapServiceName($serviceInfo) . $this->_getSoapServiceVersion($serviceInfo)],
81+
$storeCode
8082
);
8183
/** Check if there is SOAP client initialized with requested WSDL available */
8284
if (!isset($this->_soapClients[$wsdlUrl])) {
@@ -117,16 +119,19 @@ public function instantiateSoapClient($wsdlUrl, $token = null)
117119
* 'catalogProductV1',
118120
* 'customerV2'
119121
* );</pre>
122+
* @param string|null $storeCode
120123
* @return string
121124
*/
122-
public function generateWsdlUrl($services)
125+
public function generateWsdlUrl($services, $storeCode = null)
123126
{
124127
/** Sort list of services to avoid having different WSDL URLs for the identical lists of services. */
125128
//TODO: This may change since same resource of multiple versions may be allowed after namespace changes
126129
ksort($services);
127130
/** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
128-
$storeManager = Bootstrap::getObjectManager()->get('Magento\Store\Model\StoreManagerInterface');
129-
$storeCode = $storeManager->getStore()->getCode();
131+
$storeCode = !is_null($storeCode)
132+
? (string)$storeCode
133+
: Bootstrap::getObjectManager()->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getCode();
134+
130135
/** TESTS_BASE_URL is initialized in PHPUnit configuration */
131136
$wsdlUrl = rtrim(TESTS_BASE_URL, '/') . self::WSDL_BASE_PATH . '/' . $storeCode . '?wsdl=1&services=';
132137
$wsdlResourceArray = [];

dev/tests/api-functional/framework/Magento/TestFramework/TestCase/Webapi/AdapterInterface.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ interface AdapterInterface
3232
* );
3333
* </pre>
3434
* @param array $arguments
35+
* @param string|null $storeCode if store code not provided, default store code will be used
3536
* @return array|string|int|float|bool
3637
*/
37-
public function call($serviceInfo, $arguments = []);
38+
public function call($serviceInfo, $arguments = [], $storeCode = null);
3839
}

dev/tests/api-functional/framework/Magento/TestFramework/TestCase/WebapiAbstract.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,16 @@ protected function tearDown()
155155
* @param array $serviceInfo
156156
* @param array $arguments
157157
* @param string|null $webApiAdapterCode
158+
* @param string|null $storeCode
158159
* @return array|int|string|float|bool Web API call results
159160
*/
160-
protected function _webApiCall($serviceInfo, $arguments = [], $webApiAdapterCode = null)
161+
protected function _webApiCall($serviceInfo, $arguments = [], $webApiAdapterCode = null, $storeCode = null)
161162
{
162163
if (is_null($webApiAdapterCode)) {
163164
/** Default adapter code is defined in PHPUnit configuration */
164165
$webApiAdapterCode = strtolower(TESTS_WEB_API_ADAPTER);
165166
}
166-
return $this->_getWebApiAdapter($webApiAdapterCode)->call($serviceInfo, $arguments);
167+
return $this->_getWebApiAdapter($webApiAdapterCode)->call($serviceInfo, $arguments, $storeCode);
167168
}
168169

169170
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
4+
*/
5+
6+
namespace Magento\Catalog\Api;
7+
8+
use Magento\Catalog\Api\Data\ProductInterface as Product;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
use Magento\TestFramework\TestCase\WebapiAbstract;
11+
use Magento\Webapi\Model\Rest\Config as RestConfig;
12+
13+
class ProductRepositoryMultiStoreTest extends WebapiAbstract
14+
{
15+
const SERVICE_NAME = 'catalogProductRepositoryV1';
16+
const SERVICE_VERSION = 'V1';
17+
const RESOURCE_PATH = '/V1/products';
18+
const STORE_CODE_FROM_FIXTURE = 'fixturestore';
19+
20+
private $productData = [
21+
[
22+
Product::SKU => 'simple',
23+
Product::NAME => 'Simple Related Product',
24+
Product::TYPE_ID => 'simple',
25+
Product::PRICE => 10
26+
],
27+
[
28+
Product::SKU => 'simple_with_cross',
29+
Product::NAME => 'Simple Product With Related Product',
30+
Product::TYPE_ID => 'simple',
31+
Product::PRICE => 10
32+
],
33+
];
34+
35+
/**
36+
* Create another store one time for testSearch
37+
* @magentoApiDataFixture Magento/Core/_files/store.php
38+
*/
39+
public function testCreateAnotherStore()
40+
{
41+
/** @var $store \Magento\Store\Model\Store */
42+
$store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\Store\Model\Store');
43+
$store->load('fixturestore');
44+
$this->assertNotNull($store->getId());
45+
}
46+
47+
/**
48+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
49+
* @depends testCreateAnotherStore
50+
*/
51+
public function testGetMultiStore()
52+
{
53+
$productData = $this->productData[0];
54+
$nameInFixtureStore = 'Name in fixture store';
55+
/** @var $store \Magento\Store\Model\Group */
56+
$store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\Store\Model\Group');
57+
$store->load(self::STORE_CODE_FROM_FIXTURE);
58+
$this->assertNotNull($store->getId(), 'Precondition failed: fixture store was not created.');
59+
$sku = $productData[Product::SKU];
60+
/** @var \Magento\Catalog\Model\Product $product */
61+
$product = Bootstrap::getObjectManager()->create('Magento\Catalog\Model\Product');
62+
$product->load($product->getIdBySku($sku));
63+
$product->setName($nameInFixtureStore)->setStoreId($store->getId())->save();
64+
$serviceInfo = [
65+
'rest' => [
66+
'resourcePath' => self::RESOURCE_PATH . '/' . $sku,
67+
'httpMethod' => RestConfig::HTTP_METHOD_GET
68+
],
69+
'soap' => [
70+
'service' => self::SERVICE_NAME,
71+
'serviceVersion' => self::SERVICE_VERSION,
72+
'operation' => self::SERVICE_NAME . 'get'
73+
]
74+
];
75+
76+
$requestData = ['id' => $sku, 'productSku' => $sku];
77+
$defaultStoreResponse = $this->_webApiCall($serviceInfo, $requestData);
78+
$nameInDefaultStore = 'Simple Product';
79+
$this->assertEquals(
80+
$nameInDefaultStore,
81+
$defaultStoreResponse[Product::NAME],
82+
'Product name in default store is invalid.'
83+
);
84+
$fixtureStoreResponse = $this->_webApiCall($serviceInfo, $requestData, null, self::STORE_CODE_FROM_FIXTURE);
85+
$this->assertEquals(
86+
$nameInFixtureStore,
87+
$fixtureStoreResponse[Product::NAME],
88+
'Product name in fixture store is invalid.'
89+
);
90+
}
91+
92+
/**
93+
* Remove test store
94+
*/
95+
public static function tearDownAfterClass()
96+
{
97+
parent::tearDownAfterClass();
98+
/** @var \Magento\Framework\Registry $registry */
99+
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\Framework\Registry');
100+
101+
$registry->unregister('isSecureArea');
102+
$registry->register('isSecureArea', true);
103+
104+
/** @var $store \Magento\Store\Model\Store */
105+
$store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\Store\Model\Store');
106+
$store->load('fixturestore');
107+
if ($store->getId()) {
108+
$store->delete();
109+
}
110+
111+
$registry->unregister('isSecureArea');
112+
$registry->register('isSecureArea', false);
113+
}
114+
}

dev/tests/integration/testsuite/Magento/Core/_files/store.php

+27-24
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,31 @@
55
*/
66

77
$store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\Store\Model\Store');
8-
$websiteId = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
9-
'Magento\Store\Model\StoreManagerInterface'
10-
)->getWebsite()->getId();
11-
$groupId = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
12-
'Magento\Store\Model\StoreManagerInterface'
13-
)->getWebsite()->getDefaultGroupId();
14-
$store->setCode(
15-
'fixturestore'
16-
)->setWebsiteId(
17-
$websiteId
18-
)->setGroupId(
19-
$groupId
20-
)->setName(
21-
'Fixture Store'
22-
)->setSortOrder(
23-
10
24-
)->setIsActive(
25-
1
26-
);
27-
$store->save();
8+
$storeCode = 'fixturestore';
9+
if (!$store->load($storeCode)->getId()) {
10+
$websiteId = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
11+
'Magento\Store\Model\StoreManagerInterface'
12+
)->getWebsite()->getId();
13+
$groupId = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
14+
'Magento\Store\Model\StoreManagerInterface'
15+
)->getWebsite()->getDefaultGroupId();
16+
$store->setCode(
17+
$storeCode
18+
)->setWebsiteId(
19+
$websiteId
20+
)->setGroupId(
21+
$groupId
22+
)->setName(
23+
'Fixture Store'
24+
)->setSortOrder(
25+
10
26+
)->setIsActive(
27+
1
28+
);
29+
$store->save();
2830

29-
/* Refresh stores memory cache */
30-
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
31-
'Magento\Store\Model\StoreManagerInterface'
32-
)->reinitStores();
31+
/* Refresh stores memory cache */
32+
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
33+
'Magento\Store\Model\StoreManagerInterface'
34+
)->reinitStores();
35+
}

0 commit comments

Comments
 (0)