TaxRuleRepositoryTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Test\Unit\Model;
  7. use Magento\Framework\Api\SortOrder;
  8. use Magento\Framework\Exception\AlreadyExistsException;
  9. use Magento\Framework\Exception\CouldNotSaveException;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Magento\Framework\Exception\NoSuchEntityException;
  12. use \Magento\Tax\Model\TaxRuleRepository;
  13. /**
  14. * Class TaxRuleRepositoryTest
  15. * @package Magento\Tax\Test\Unit\Model
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. */
  18. class TaxRuleRepositoryTest extends \PHPUnit\Framework\TestCase
  19. {
  20. /**
  21. * @var \Magento\Tax\Model\TaxRuleRepository
  22. */
  23. protected $model;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $taxRuleRegistry;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $searchResultFactory;
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $searchResultsMock;
  36. /**
  37. * @var \PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $ruleFactory;
  40. /**
  41. * @var \PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $collectionFactory;
  44. /**
  45. * @var \PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $resource;
  48. /**
  49. * @var \PHPUnit_Framework_MockObject_MockObject
  50. */
  51. protected $extensionAttributesJoinProcessorMock;
  52. /**
  53. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  54. */
  55. protected $objectManager;
  56. /**
  57. * @var \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface |
  58. * \PHPUnit_Framework_MockObject_MockObject
  59. */
  60. private $collectionProcessor;
  61. protected function setUp()
  62. {
  63. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  64. $this->taxRuleRegistry =
  65. $this->createMock(\Magento\Tax\Model\Calculation\TaxRuleRegistry::class);
  66. $this->taxRuleRegistry = $this->createMock(\Magento\Tax\Model\Calculation\TaxRuleRegistry::class);
  67. $this->searchResultFactory = $this->createPartialMock(
  68. \Magento\Tax\Api\Data\TaxRuleSearchResultsInterfaceFactory::class,
  69. ['create']
  70. );
  71. $this->searchResultsMock = $this->createMock(\Magento\Tax\Api\Data\TaxRuleSearchResultsInterface::class);
  72. $this->ruleFactory = $this->createMock(\Magento\Tax\Model\Calculation\RuleFactory::class);
  73. $this->collectionFactory = $this->createPartialMock(
  74. \Magento\Tax\Model\ResourceModel\Calculation\Rule\CollectionFactory::class,
  75. ['create']
  76. );
  77. $this->resource = $this->createMock(\Magento\Tax\Model\ResourceModel\Calculation\Rule::class);
  78. $this->extensionAttributesJoinProcessorMock = $this->createPartialMock(
  79. \Magento\Framework\Api\ExtensionAttribute\JoinProcessor::class,
  80. ['process']
  81. );
  82. $this->collectionProcessor = $this->createMock(
  83. \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
  84. );
  85. $this->model = new TaxRuleRepository(
  86. $this->taxRuleRegistry,
  87. $this->searchResultFactory,
  88. $this->ruleFactory,
  89. $this->collectionFactory,
  90. $this->resource,
  91. $this->extensionAttributesJoinProcessorMock,
  92. $this->collectionProcessor
  93. );
  94. }
  95. public function testGet()
  96. {
  97. $rule = $this->createMock(\Magento\Tax\Model\Calculation\Rule::class);
  98. $this->taxRuleRegistry->expects($this->once())->method('retrieveTaxRule')->with(10)->willReturn($rule);
  99. $this->assertEquals($rule, $this->model->get(10));
  100. }
  101. public function testDelete()
  102. {
  103. $rule = $this->createMock(\Magento\Tax\Model\Calculation\Rule::class);
  104. $rule->expects($this->once())->method('getId')->willReturn(10);
  105. $this->resource->expects($this->once())->method('delete')->with($rule);
  106. $this->taxRuleRegistry->expects($this->once())->method('removeTaxRule')->with(10);
  107. $this->assertTrue($this->model->delete($rule));
  108. }
  109. public function testDeleteById()
  110. {
  111. $rule = $this->createMock(\Magento\Tax\Model\Calculation\Rule::class);
  112. $this->taxRuleRegistry->expects($this->once())->method('retrieveTaxRule')->with(10)->willReturn($rule);
  113. $rule->expects($this->once())->method('getId')->willReturn(10);
  114. $this->resource->expects($this->once())->method('delete')->with($rule);
  115. $this->taxRuleRegistry->expects($this->once())->method('removeTaxRule')->with(10);
  116. $this->assertTrue($this->model->deleteById(10));
  117. }
  118. public function testSave()
  119. {
  120. $rule = $this->createMock(\Magento\Tax\Model\Calculation\Rule::class);
  121. $rule->expects($this->once())->method('getId')->willReturn(10);
  122. $this->taxRuleRegistry->expects($this->once())->method('retrieveTaxRule')->with(10)->willReturn($rule);
  123. $this->resource->expects($this->once())->method('save')->with($rule);
  124. $this->taxRuleRegistry->expects($this->once())->method('registerTaxRule')->with($rule);
  125. $this->assertEquals($rule, $this->model->save($rule));
  126. }
  127. /**
  128. * @dataProvider saveExceptionsDataProvider
  129. * @param $exceptionObject
  130. * @param $exceptionName
  131. * @param $exceptionMessage
  132. * @throws \Exception
  133. * @throws CouldNotSaveException
  134. * @throws \Magento\Framework\Exception\InputException
  135. * @throws NoSuchEntityException
  136. */
  137. public function testSaveWithExceptions($exceptionObject, $exceptionName, $exceptionMessage)
  138. {
  139. $rule = $this->createMock(\Magento\Tax\Model\Calculation\Rule::class);
  140. $rule->expects($this->once())->method('getId')->willReturn(10);
  141. $this->taxRuleRegistry->expects($this->once())->method('retrieveTaxRule')->with(10)->willReturn($rule);
  142. $this->resource->expects($this->once())->method('save')->with($rule)
  143. ->willThrowException($exceptionObject);
  144. $this->taxRuleRegistry->expects($this->never())->method('registerTaxRule');
  145. $this->expectException($exceptionName);
  146. $this->expectExceptionMessage($exceptionMessage);
  147. $this->model->save($rule);
  148. }
  149. /**
  150. * @return array
  151. */
  152. public function saveExceptionsDataProvider()
  153. {
  154. return [
  155. [
  156. new LocalizedException(__('Could not save')), CouldNotSaveException::class,
  157. 'Could not save'
  158. ], [
  159. new AlreadyExistsException(__('Entity already exists')), AlreadyExistsException::class,
  160. 'Entity already exists'
  161. ], [
  162. new NoSuchEntityException(__('No such entity')), NoSuchEntityException::class,
  163. 'No such entity'
  164. ]
  165. ];
  166. }
  167. public function testGetList()
  168. {
  169. $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
  170. $collectionMock =
  171. $this->createMock(\Magento\Tax\Model\ResourceModel\Calculation\Rule\Collection::class);
  172. $this->createMock(\Magento\Tax\Model\ResourceModel\Calculation\Rule\Collection::class);
  173. $this->extensionAttributesJoinProcessorMock->expects($this->once())
  174. ->method('process')
  175. ->with($collectionMock);
  176. $this->collectionProcessor->expects($this->once())
  177. ->method('process')
  178. ->with($searchCriteriaMock, $collectionMock);
  179. $this->searchResultsMock->expects($this->once())->method('setSearchCriteria')->with($searchCriteriaMock);
  180. $this->collectionFactory->expects($this->once())->method('create')->willReturn($collectionMock);
  181. $collectionMock->expects($this->once())->method('getItems')->willReturn([]);
  182. $this->searchResultsMock->expects($this->once())->method('setItems')->with([]);
  183. $this->searchResultFactory->expects($this->once())->method('create')->willReturn($this->searchResultsMock);
  184. $this->assertEquals($this->searchResultsMock, $this->model->getList($searchCriteriaMock));
  185. }
  186. }