AddressRepositoryTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Model\Order;
  7. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. /**
  10. * Unit test for order address repository class.
  11. *
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class AddressRepositoryTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * Subject of testing.
  18. *
  19. * @var \Magento\Sales\Model\Order\AddressRepository
  20. */
  21. protected $subject;
  22. /**
  23. * Sales resource metadata.
  24. *
  25. * @var \Magento\Sales\Model\ResourceModel\Metadata|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $metadata;
  28. /**
  29. * @var \Magento\Sales\Api\Data\OrderAddressSearchResultInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $searchResultFactory;
  32. /**
  33. * @var CollectionProcessorInterface |\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $collectionProcessorMock;
  36. protected function setUp()
  37. {
  38. $objectManager = new ObjectManager($this);
  39. $this->metadata = $this->createPartialMock(
  40. \Magento\Sales\Model\ResourceModel\Metadata::class,
  41. ['getNewInstance', 'getMapper']
  42. );
  43. $this->searchResultFactory = $this->createPartialMock(
  44. \Magento\Sales\Api\Data\OrderAddressSearchResultInterfaceFactory::class,
  45. ['create']
  46. );
  47. $this->collectionProcessorMock = $this->getMockBuilder(CollectionProcessorInterface::class)
  48. ->getMock();
  49. $this->subject = $objectManager->getObject(
  50. \Magento\Sales\Model\Order\AddressRepository::class,
  51. [
  52. 'metadata' => $this->metadata,
  53. 'searchResultFactory' => $this->searchResultFactory,
  54. 'collectionProcessor' => $this->collectionProcessorMock,
  55. ]
  56. );
  57. }
  58. /**
  59. * @param int|null $id
  60. * @param int|null $entityId
  61. * @dataProvider getDataProvider
  62. */
  63. public function testGet($id, $entityId)
  64. {
  65. if (!$id) {
  66. $this->expectException(
  67. \Magento\Framework\Exception\InputException::class
  68. );
  69. $this->subject->get($id);
  70. } else {
  71. $address = $this->createPartialMock(\Magento\Sales\Model\Order\Address::class, ['load', 'getEntityId']);
  72. $address->expects($this->once())
  73. ->method('load')
  74. ->with($id)
  75. ->willReturn($address);
  76. $address->expects($this->once())
  77. ->method('getEntityId')
  78. ->willReturn($entityId);
  79. $this->metadata->expects($this->once())
  80. ->method('getNewInstance')
  81. ->willReturn($address);
  82. if (!$entityId) {
  83. $this->expectException(
  84. \Magento\Framework\Exception\NoSuchEntityException::class
  85. );
  86. $this->subject->get($id);
  87. } else {
  88. $this->assertEquals($address, $this->subject->get($id));
  89. $address->expects($this->never())
  90. ->method('load')
  91. ->with($id)
  92. ->willReturn($address);
  93. $address->expects($this->never())
  94. ->method('getEntityId')
  95. ->willReturn($entityId);
  96. $this->metadata->expects($this->never())
  97. ->method('getNewInstance')
  98. ->willReturn($address);
  99. // Retrieve Address from registry.
  100. $this->assertEquals($address, $this->subject->get($id));
  101. }
  102. }
  103. }
  104. /**
  105. * @return array
  106. */
  107. public function getDataProvider()
  108. {
  109. return [
  110. [null, null],
  111. [1, null],
  112. [1, 1]
  113. ];
  114. }
  115. public function testGetList()
  116. {
  117. $searchCriteria = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
  118. $collection = $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Address\Collection::class);
  119. $this->collectionProcessorMock->expects($this->once())
  120. ->method('process')
  121. ->with($searchCriteria, $collection);
  122. $this->searchResultFactory->expects($this->once())
  123. ->method('create')
  124. ->willReturn($collection);
  125. $this->assertEquals($collection, $this->subject->getList($searchCriteria));
  126. }
  127. public function testDelete()
  128. {
  129. $address = $this->createPartialMock(\Magento\Sales\Model\Order\Address::class, ['getEntityId']);
  130. $address->expects($this->once())
  131. ->method('getEntityId')
  132. ->willReturn(1);
  133. $mapper = $this->getMockForAbstractClass(
  134. \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
  135. [],
  136. '',
  137. false,
  138. true,
  139. true,
  140. ['delete']
  141. );
  142. $mapper->expects($this->once())
  143. ->method('delete')
  144. ->with($address);
  145. $this->metadata->expects($this->any())
  146. ->method('getMapper')
  147. ->willReturn($mapper);
  148. $this->assertTrue($this->subject->delete($address));
  149. }
  150. /**
  151. * @expectedException \Magento\Framework\Exception\CouldNotDeleteException
  152. * @expectedExceptionMessage The order address couldn't be deleted.
  153. */
  154. public function testDeleteWithException()
  155. {
  156. $address = $this->createPartialMock(\Magento\Sales\Model\Order\Address::class, ['getEntityId']);
  157. $address->expects($this->never())
  158. ->method('getEntityId');
  159. $mapper = $this->getMockForAbstractClass(
  160. \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
  161. [],
  162. '',
  163. false,
  164. true,
  165. true,
  166. ['delete']
  167. );
  168. $mapper->expects($this->once())
  169. ->method('delete')
  170. ->willThrowException(new \Exception('error'));
  171. $this->metadata->expects($this->any())
  172. ->method('getMapper')
  173. ->willReturn($mapper);
  174. $this->subject->delete($address);
  175. }
  176. public function testSave()
  177. {
  178. $address = $this->createPartialMock(\Magento\Sales\Model\Order\Address::class, ['getEntityId']);
  179. $address->expects($this->any())
  180. ->method('getEntityId')
  181. ->willReturn(1);
  182. $mapper = $this->getMockForAbstractClass(
  183. \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
  184. [],
  185. '',
  186. false,
  187. true,
  188. true,
  189. ['save']
  190. );
  191. $mapper->expects($this->once())
  192. ->method('save')
  193. ->with($address);
  194. $this->metadata->expects($this->any())
  195. ->method('getMapper')
  196. ->willReturn($mapper);
  197. $this->assertEquals($address, $this->subject->save($address));
  198. }
  199. /**
  200. * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  201. * @expectedExceptionMessage The order address couldn't be saved.
  202. */
  203. public function testSaveWithException()
  204. {
  205. $address = $this->createPartialMock(\Magento\Sales\Model\Order\Address::class, ['getEntityId']);
  206. $address->expects($this->never())
  207. ->method('getEntityId');
  208. $mapper = $this->getMockForAbstractClass(
  209. \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
  210. [],
  211. '',
  212. false,
  213. true,
  214. true,
  215. ['save']
  216. );
  217. $mapper->expects($this->once())
  218. ->method('save')
  219. ->willThrowException(new \Exception('error'));
  220. $this->metadata->expects($this->any())
  221. ->method('getMapper')
  222. ->willReturn($mapper);
  223. $this->assertEquals($address, $this->subject->save($address));
  224. }
  225. public function testCreate()
  226. {
  227. $address = $this->createPartialMock(\Magento\Sales\Model\Order\Address::class, ['getEntityId']);
  228. $this->metadata->expects($this->once())
  229. ->method('getNewInstance')
  230. ->willReturn($address);
  231. $this->assertEquals($address, $this->subject->create());
  232. }
  233. }