InvoiceRepositoryTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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;
  7. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  8. /**
  9. * Class InvoiceRepositoryTest
  10. */
  11. class InvoiceRepositoryTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Sales\Model\Order\InvoiceRepository
  15. */
  16. protected $invoice;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $invoiceMetadata;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $searchResultFactory;
  25. /**
  26. * @var CollectionProcessorInterface |\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $collectionProcessorMock;
  29. protected function setUp()
  30. {
  31. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  32. $this->invoiceMetadata = $this->createMock(\Magento\Sales\Model\ResourceModel\Metadata::class);
  33. $this->searchResultFactory = $this->getMockBuilder(
  34. \Magento\Sales\Api\Data\InvoiceSearchResultInterfaceFactory::class
  35. )
  36. ->disableOriginalConstructor()
  37. ->setMethods(['create'])
  38. ->getMock();
  39. $this->collectionProcessorMock = $this->getMockBuilder(CollectionProcessorInterface::class)
  40. ->getMock();
  41. $this->invoice = $objectManager->getObject(
  42. \Magento\Sales\Model\Order\InvoiceRepository::class,
  43. [
  44. 'invoiceMetadata' => $this->invoiceMetadata,
  45. 'searchResultFactory' => $this->searchResultFactory,
  46. 'collectionProcessor' => $this->collectionProcessorMock,
  47. ]
  48. );
  49. $this->type = $this->createPartialMock(\Magento\Eav\Model\Entity\Type::class, ['fetchNewIncrementId']);
  50. }
  51. public function testGet()
  52. {
  53. $id = 1;
  54. $entity = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
  55. ->disableOriginalConstructor()
  56. ->getMock();
  57. $entity->expects($this->once())
  58. ->method('load')
  59. ->with($id)
  60. ->willReturn($entity);
  61. $entity->expects($this->once())
  62. ->method('getEntityId')
  63. ->willReturn($id);
  64. $this->invoiceMetadata->expects($this->once())
  65. ->method('getNewInstance')
  66. ->willReturn($entity);
  67. $this->assertEquals($entity, $this->invoice->get($id));
  68. }
  69. /**
  70. * @expectedException \Magento\Framework\Exception\InputException
  71. * @expectedExceptionMessage An ID is needed. Set the ID and try again.
  72. */
  73. public function testGetNoId()
  74. {
  75. $this->invoice->get(null);
  76. }
  77. /**
  78. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  79. * @expectedExceptionMessage The entity that was requested doesn't exist. Verify the entity and try again.
  80. */
  81. public function testGetEntityNoId()
  82. {
  83. $id = 1;
  84. $entity = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
  85. ->disableOriginalConstructor()
  86. ->getMock();
  87. $entity->expects($this->once())
  88. ->method('load')
  89. ->with($id)
  90. ->willReturn($entity);
  91. $entity->expects($this->once())
  92. ->method('getEntityId')
  93. ->willReturn(null);
  94. $this->invoiceMetadata->expects($this->once())
  95. ->method('getNewInstance')
  96. ->willReturn($entity);
  97. $this->assertNull($entity, $this->invoice->get($id));
  98. }
  99. public function testCreate()
  100. {
  101. $entity = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
  102. ->disableOriginalConstructor()
  103. ->getMock();
  104. $this->invoiceMetadata->expects($this->once())
  105. ->method('getNewInstance')
  106. ->willReturn($entity);
  107. $this->assertEquals($entity, $this->invoice->create());
  108. }
  109. public function testGetList()
  110. {
  111. $searchCriteria = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteria::class)
  112. ->disableOriginalConstructor()
  113. ->getMock();
  114. $collection = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Invoice\Collection::class)
  115. ->disableOriginalConstructor()
  116. ->getMock();
  117. $this->collectionProcessorMock->expects($this->once())
  118. ->method('process')
  119. ->with($searchCriteria, $collection);
  120. $this->searchResultFactory->expects($this->once())
  121. ->method('create')
  122. ->willReturn($collection);
  123. $this->assertEquals($collection, $this->invoice->getList($searchCriteria));
  124. }
  125. public function testDelete()
  126. {
  127. $entity = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
  128. ->disableOriginalConstructor()
  129. ->getMock();
  130. $entity->expects($this->once())
  131. ->method('getEntityId')
  132. ->willReturn(1);
  133. $mapper = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Invoice::class)
  134. ->disableOriginalConstructor()
  135. ->getMock();
  136. $mapper->expects($this->once())
  137. ->method('delete')
  138. ->with($entity);
  139. $this->invoiceMetadata->expects($this->any())
  140. ->method('getMapper')
  141. ->willReturn($mapper);
  142. $this->assertTrue($this->invoice->delete($entity));
  143. }
  144. public function testDeleteById()
  145. {
  146. $id = 1;
  147. $entity = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
  148. ->disableOriginalConstructor()
  149. ->getMock();
  150. $entity->expects($this->once())
  151. ->method('load')
  152. ->with($id)
  153. ->willReturn($entity);
  154. $entity->expects($this->any())
  155. ->method('getEntityId')
  156. ->willReturn($id);
  157. $this->invoiceMetadata->expects($this->once())
  158. ->method('getNewInstance')
  159. ->willReturn($entity);
  160. $mapper = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Invoice::class)
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $mapper->expects($this->once())
  164. ->method('delete')
  165. ->with($entity);
  166. $this->invoiceMetadata->expects($this->any())
  167. ->method('getMapper')
  168. ->willReturn($mapper);
  169. $this->assertTrue($this->invoice->deleteById($id));
  170. }
  171. public function testSave()
  172. {
  173. $entity = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
  174. ->disableOriginalConstructor()
  175. ->getMock();
  176. $entity->expects($this->any())
  177. ->method('getEntityId')
  178. ->willReturn(1);
  179. $mapper = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Invoice::class)
  180. ->disableOriginalConstructor()
  181. ->getMock();
  182. $mapper->expects($this->once())
  183. ->method('save')
  184. ->with($entity);
  185. $this->invoiceMetadata->expects($this->any())
  186. ->method('getMapper')
  187. ->willReturn($mapper);
  188. $this->assertEquals($entity, $this->invoice->save($entity));
  189. }
  190. }