CreditmemoTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\Sales\Model\ResourceModel\OrderFactory;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. use Magento\Sales\Model\ResourceModel\Order\Creditmemo\Item\CollectionFactory;
  10. use Magento\Sales\Model\ResourceModel\Order\Creditmemo\Item\Collection as ItemCollection;
  11. use Magento\Framework\App\Config\ScopeConfigInterface;
  12. /**
  13. * Class CreditmemoTest
  14. *
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class CreditmemoTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var OrderFactory |\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $orderFactory;
  23. /**
  24. * @var \Magento\Sales\Model\Order\Creditmemo
  25. */
  26. protected $creditmemo;
  27. /**
  28. * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $scopeConfigMock;
  31. /**
  32. * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $cmItemCollectionFactoryMock;
  35. protected function setUp()
  36. {
  37. $this->orderFactory = $this->createPartialMock(\Magento\Sales\Model\OrderFactory::class, ['create']);
  38. $this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
  39. $objectManagerHelper = new ObjectManagerHelper($this);
  40. $this->cmItemCollectionFactoryMock = $this->getMockBuilder(
  41. \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Item\CollectionFactory::class
  42. )->disableOriginalConstructor()
  43. ->setMethods(['create'])
  44. ->getMock();
  45. $arguments = [
  46. 'context' => $this->createMock(\Magento\Framework\Model\Context::class),
  47. 'registry' => $this->createMock(\Magento\Framework\Registry::class),
  48. 'localeDate' => $this->createMock(
  49. \Magento\Framework\Stdlib\DateTime\TimezoneInterface::class
  50. ),
  51. 'dateTime' => $this->createMock(\Magento\Framework\Stdlib\DateTime::class),
  52. 'creditmemoConfig' => $this->createMock(
  53. \Magento\Sales\Model\Order\Creditmemo\Config::class
  54. ),
  55. 'orderFactory' => $this->orderFactory,
  56. 'cmItemCollectionFactory' => $this->cmItemCollectionFactoryMock,
  57. 'calculatorFactory' => $this->createMock(\Magento\Framework\Math\CalculatorFactory::class),
  58. 'storeManager' => $this->createMock(\Magento\Store\Model\StoreManagerInterface::class),
  59. 'commentFactory' => $this->createMock(\Magento\Sales\Model\Order\Creditmemo\CommentFactory::class),
  60. 'commentCollectionFactory' => $this->createMock(
  61. \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Comment\CollectionFactory::class
  62. ),
  63. 'scopeConfig' => $this->scopeConfigMock
  64. ];
  65. $this->creditmemo = $objectManagerHelper->getObject(
  66. \Magento\Sales\Model\Order\Creditmemo::class,
  67. $arguments
  68. );
  69. }
  70. public function testGetOrder()
  71. {
  72. $orderId = 100000041;
  73. $this->creditmemo->setOrderId($orderId);
  74. $entityName = 'creditmemo';
  75. $order = $this->createPartialMock(
  76. \Magento\Sales\Model\Order::class,
  77. ['load', 'setHistoryEntityName', '__wakeUp']
  78. );
  79. $this->creditmemo->setOrderId($orderId);
  80. $order->expects($this->atLeastOnce())
  81. ->method('setHistoryEntityName')
  82. ->with($entityName)
  83. ->will($this->returnSelf());
  84. $order->expects($this->atLeastOnce())
  85. ->method('load')
  86. ->with($orderId)
  87. ->will($this->returnValue($order));
  88. $this->orderFactory->expects($this->atLeastOnce())
  89. ->method('create')
  90. ->will($this->returnValue($order));
  91. $this->assertEquals($order, $this->creditmemo->getOrder());
  92. }
  93. public function testGetEntityType()
  94. {
  95. $this->assertEquals('creditmemo', $this->creditmemo->getEntityType());
  96. }
  97. public function testIsValidGrandTotalGrandTotalEmpty()
  98. {
  99. $this->creditmemo->setGrandTotal(0);
  100. $this->assertFalse($this->creditmemo->isValidGrandTotal());
  101. }
  102. public function testIsValidGrandTotalGrandTotal()
  103. {
  104. $this->creditmemo->setGrandTotal(0);
  105. $this->assertFalse($this->creditmemo->isValidGrandTotal());
  106. }
  107. public function testIsValidGrandTotal()
  108. {
  109. $this->creditmemo->setGrandTotal(1);
  110. $this->assertTrue($this->creditmemo->isValidGrandTotal());
  111. }
  112. public function testGetIncrementId()
  113. {
  114. $this->creditmemo->setIncrementId('test_increment_id');
  115. $this->assertEquals('test_increment_id', $this->creditmemo->getIncrementId());
  116. }
  117. public function testGetItemsCollectionWithId()
  118. {
  119. $id = 1;
  120. $this->creditmemo->setId($id);
  121. $items = [];
  122. $itemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Creditmemo\Item::class)
  123. ->disableOriginalConstructor()
  124. ->getMock();
  125. $itemMock->expects($this->once())
  126. ->method('setCreditmemo')
  127. ->with($this->creditmemo);
  128. $items[] = $itemMock;
  129. /** @var ItemCollection|\PHPUnit_Framework_MockObject_MockObject $itemCollectionMock */
  130. $itemCollectionMock = $this->getMockBuilder(
  131. \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Item\Collection::class
  132. )
  133. ->disableOriginalConstructor()
  134. ->getMock();
  135. $itemCollectionMock->expects($this->once())
  136. ->method('setCreditmemoFilter')
  137. ->with($id)
  138. ->will($this->returnValue($items));
  139. $this->cmItemCollectionFactoryMock->expects($this->any())
  140. ->method('create')
  141. ->will($this->returnValue($itemCollectionMock));
  142. $itemsCollection = $this->creditmemo->getItemsCollection();
  143. $this->assertEquals($items, $itemsCollection);
  144. }
  145. public function testGetItemsCollectionWithoutId()
  146. {
  147. $items = [];
  148. $itemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Creditmemo\Item::class)
  149. ->disableOriginalConstructor()
  150. ->getMock();
  151. $itemMock->expects($this->never())
  152. ->method('setCreditmemo');
  153. $items[] = $itemMock;
  154. /** @var ItemCollection|\PHPUnit_Framework_MockObject_MockObject $itemCollectionMock */
  155. $itemCollectionMock = $this->getMockBuilder(
  156. \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Item\Collection::class
  157. )
  158. ->disableOriginalConstructor()
  159. ->getMock();
  160. $itemCollectionMock->expects($this->once())
  161. ->method('setCreditmemoFilter')
  162. ->with(null)
  163. ->will($this->returnValue($items));
  164. $this->cmItemCollectionFactoryMock->expects($this->any())
  165. ->method('create')
  166. ->will($this->returnValue($itemCollectionMock));
  167. $itemsCollection = $this->creditmemo->getItemsCollection();
  168. $this->assertEquals($items, $itemsCollection);
  169. }
  170. }