LabelsTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Shipping\Test\Unit\Model\Shipping;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. use Magento\Sales\Model\Order\Shipment;
  9. use Magento\Store\Model\ScopeInterface;
  10. /**
  11. * Class LabelsTest
  12. *
  13. * Test class for \Magento\Shipping\Model\Shipping\Labels
  14. *
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class LabelsTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var \Magento\Shipping\Model\Shipping\Labels
  21. */
  22. protected $labels;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $request;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $scopeConfig;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $region;
  35. /**
  36. * @var \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $carrierFactory;
  39. /**
  40. * @var \PHPUnit_Framework_MockObject_MockObject
  41. */
  42. private $user;
  43. protected function setUp()
  44. {
  45. $this->request = $this->getMockBuilder(\Magento\Shipping\Model\Shipment\Request::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $requestFactory = $this->getMockBuilder(\Magento\Shipping\Model\Shipment\RequestFactory::class)
  49. ->disableOriginalConstructor()
  50. ->setMethods(['create'])
  51. ->getMock();
  52. $requestFactory->expects(static::any())->method('create')->willReturn($this->request);
  53. $this->carrierFactory = $this->getMockBuilder(\Magento\Shipping\Model\CarrierFactory::class)
  54. ->disableOriginalConstructor()
  55. ->setMethods(['create'])
  56. ->getMock();
  57. $storeManager = $this->getStoreManager();
  58. $this->user = $this->getMockBuilder(\Magento\User\Model\User::class)
  59. ->disableOriginalConstructor()
  60. ->setMethods(['getFirstname', 'getLastname', 'getEmail', 'getName'])
  61. ->getMock();
  62. $authSession = $this->getMockBuilder(\Magento\Backend\Model\Auth\Session::class)
  63. ->disableOriginalConstructor()
  64. ->setMethods(['getUser'])
  65. ->getMock();
  66. $authSession->expects(static::any())->method('getUser')->willReturn($this->user);
  67. $regionFactory = $this->getRegionFactory();
  68. $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config::class)
  69. ->disableOriginalConstructor()
  70. ->setMethods(['getValue'])
  71. ->getMock();
  72. $objectManagerHelper = new ObjectManagerHelper($this);
  73. $this->labels = $objectManagerHelper->getObject(
  74. \Magento\Shipping\Model\Shipping\Labels::class,
  75. [
  76. 'shipmentRequestFactory' => $requestFactory,
  77. 'carrierFactory' => $this->carrierFactory,
  78. 'storeManager' => $storeManager,
  79. 'scopeConfig' => $this->scopeConfig,
  80. 'authSession' => $authSession,
  81. 'regionFactory' => $regionFactory
  82. ]
  83. );
  84. }
  85. /**
  86. * @dataProvider requestToShipmentDataProvider
  87. */
  88. public function testRequestToShipment($regionId)
  89. {
  90. $carrier = $this->getMockBuilder(\Magento\Shipping\Model\Carrier\AbstractCarrier::class)
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $this->carrierFactory->expects(static::any())->method('create')->willReturn($carrier);
  94. $order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $this->user->expects($this->atLeastOnce())->method('getFirstname')->willReturn('John');
  98. $this->user->expects($this->atLeastOnce())->method('getLastname')->willReturn('Doe');
  99. $this->user->expects($this->once())->method('getName')->willReturn('John Doe');
  100. $this->user->expects($this->once())->method('getEmail')->willReturn('admin@admin.test.com');
  101. $shippingMethod = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  102. ->disableOriginalConstructor()
  103. ->setMethods(['getCarrierCode'])
  104. ->getMock();
  105. $shippingMethod->expects(static::once())
  106. ->method('getCarrierCode')
  107. ->willReturn('usps');
  108. $order->expects(static::exactly(2))
  109. ->method('getShippingMethod')
  110. ->with(true)
  111. ->willReturn($shippingMethod);
  112. $address = $this->getRecipientAddress();
  113. $order->expects(static::once())
  114. ->method('getShippingAddress')
  115. ->willReturn($address);
  116. $order->expects(static::once())
  117. ->method('getWeight')
  118. ->willReturn(2);
  119. $storeId = 33;
  120. $shipment = $this->getMockBuilder(\Magento\Sales\Model\Order\Shipment::class)
  121. ->disableOriginalConstructor()
  122. ->getMock();
  123. $shipment->expects(static::once())->method('getOrder')->willReturn($order);
  124. $shipment->expects(static::once())->method('getStoreId')->willReturn($storeId);
  125. $shipment->expects(static::once())->method('getPackages')->willReturn('');
  126. $this->scopeConfig->expects(static::any())
  127. ->method('getValue')
  128. ->willReturnMap([
  129. [Shipment::XML_PATH_STORE_REGION_ID, ScopeInterface::SCOPE_STORE, $storeId, $regionId],
  130. [Shipment::XML_PATH_STORE_ADDRESS1, ScopeInterface::SCOPE_STORE, $storeId, 'Beverly Heals'],
  131. ['general/store_information', ScopeInterface::SCOPE_STORE, $storeId, [
  132. 'name' => 'General Store', 'phone' => '(244)1500301'
  133. ]],
  134. [Shipment::XML_PATH_STORE_CITY, ScopeInterface::SCOPE_STORE, $storeId, 'LA'],
  135. [Shipment::XML_PATH_STORE_ZIP, ScopeInterface::SCOPE_STORE, $storeId, '90304'],
  136. [Shipment::XML_PATH_STORE_COUNTRY_ID, ScopeInterface::SCOPE_STORE, $storeId, 'US'],
  137. [Shipment::XML_PATH_STORE_ADDRESS2, ScopeInterface::SCOPE_STORE, $storeId, '1st Park Avenue'],
  138. ]);
  139. $this->labels->requestToShipment($shipment);
  140. }
  141. /**
  142. * @expectedException \Magento\Framework\Exception\LocalizedException
  143. * @dataProvider requestToShipmentLocalizedExceptionDataProvider
  144. */
  145. public function testRequestToShipmentLocalizedException($isShipmentCarrierNotNull)
  146. {
  147. $order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  148. ->disableOriginalConstructor()
  149. ->getMock();
  150. $shipment = $this->getMockBuilder(\Magento\Sales\Model\Order\Shipment::class)
  151. ->disableOriginalConstructor()
  152. ->getMock();
  153. $shippingMethod = $this->getMockBuilder(\Magento\Framework\DataObject::class)
  154. ->disableOriginalConstructor()
  155. ->setMethods(['getCarrierCode'])
  156. ->getMock();
  157. $order->expects($this->atLeastOnce())
  158. ->method('getShippingMethod')
  159. ->with(true)
  160. ->willReturn($shippingMethod);
  161. $this->carrierFactory
  162. ->expects(static::any())
  163. ->method('create')
  164. ->willReturn($isShipmentCarrierNotNull ? $shippingMethod : null);
  165. $shipment->expects($this->once())->method('getOrder')->willReturn($order);
  166. $this->labels->requestToShipment($shipment);
  167. }
  168. /**
  169. * @return \PHPUnit_Framework_MockObject_MockObject
  170. */
  171. protected function getStoreManager()
  172. {
  173. $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  174. ->disableOriginalConstructor()
  175. ->getMock();
  176. $store->expects(static::any())
  177. ->method('getBaseCurrencyCode')
  178. ->willReturn('USD');
  179. $storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManager::class)
  180. ->disableOriginalConstructor()
  181. ->setMethods(['getStore'])
  182. ->getMock();
  183. $storeManager->expects(static::any())->method('getStore')->willReturn($store);
  184. return $storeManager;
  185. }
  186. /**
  187. * @return \PHPUnit_Framework_MockObject_MockObject
  188. */
  189. protected function getRegionFactory()
  190. {
  191. $this->region = $this->getMockBuilder(\Magento\Directory\Model\Region::class)
  192. ->disableOriginalConstructor()
  193. ->setMethods(['load', 'getCode'])
  194. ->getMock();
  195. $regionFactory = $this->getMockBuilder(\Magento\Directory\Model\RegionFactory::class)
  196. ->disableOriginalConstructor()
  197. ->setMethods(['create'])
  198. ->getMock();
  199. $regionFactory->expects(static::any())->method('create')->willReturn($this->region);
  200. return $regionFactory;
  201. }
  202. /**
  203. * @return \PHPUnit_Framework_MockObject_MockObject
  204. */
  205. protected function getRecipientAddress()
  206. {
  207. $address = $this->getMockBuilder(\Magento\Sales\Model\Order\Address::class)
  208. ->disableOriginalConstructor()
  209. ->getMock();
  210. $address->expects(static::exactly(2))
  211. ->method('getRegionCode')
  212. ->willReturn('CO');
  213. $address->expects(static::exactly(2))
  214. ->method('getFirstname')
  215. ->willReturn('Chimi');
  216. $address->expects(static::exactly(2))
  217. ->method('getLastname')
  218. ->willReturn('Chung');
  219. $address->expects(static::once())
  220. ->method('getCompany')
  221. ->willReturn('Software LLC');
  222. $address->expects(static::once())
  223. ->method('getTelephone')
  224. ->willReturn('(231) 324-123-31');
  225. $address->expects(static::once())
  226. ->method('getEmail')
  227. ->willReturn('chimi.chung@test.com');
  228. $address->expects(static::exactly(4))
  229. ->method('getStreetLine')
  230. ->willReturn('66 Pearl St');
  231. $address->expects(static::once())
  232. ->method('getCity')
  233. ->willReturn('Denver');
  234. $address->expects(static::once())
  235. ->method('getPostcode')
  236. ->willReturn('80203');
  237. $address->expects(static::once())
  238. ->method('getCountryId')
  239. ->willReturn(1);
  240. return $address;
  241. }
  242. /**
  243. * Data provider to testRequestToShipment
  244. * @return array
  245. */
  246. public function requestToShipmentDataProvider()
  247. {
  248. return [
  249. [
  250. 'CA'
  251. ],
  252. [
  253. null
  254. ]
  255. ];
  256. }
  257. /**
  258. * Data provider to testRequestToShipmentLocalizedException
  259. * @return array
  260. */
  261. public function requestToShipmentLocalizedExceptionDataProvider()
  262. {
  263. return [
  264. [
  265. true
  266. ],
  267. [
  268. false
  269. ]
  270. ];
  271. }
  272. }