RuleRepositoryTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Test\Unit\Model;
  7. /**
  8. * Class RuleRepositoryTest
  9. *
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class RuleRepositoryTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\SalesRule\Model\RuleRepository
  16. */
  17. protected $ruleRepository;
  18. /**
  19. * @var \Magento\SalesRule\Model\RuleFactory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $ruleFactory;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $searchResultFactory;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $searchResultsMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $collectionFactory;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $extensionAttributesJoinProcessorMock;
  38. /**
  39. * @var \Magento\SalesRule\Model\Converter\ToDataModel|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $toDataModelConverter;
  42. /**
  43. * @var \Magento\SalesRule\Model\Converter\ToModel|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $toModelConverter;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $collectionProcessor;
  50. protected function setUp()
  51. {
  52. $this->ruleFactory = $this->createPartialMock(\Magento\SalesRule\Model\RuleFactory::class, ['create']);
  53. $className = \Magento\SalesRule\Model\Converter\ToDataModel::class;
  54. $this->toDataModelConverter = $this->createMock($className);
  55. $className = \Magento\SalesRule\Model\Converter\ToModel::class;
  56. $this->toModelConverter = $this->createMock($className);
  57. $className = \Magento\SalesRule\Api\Data\RuleSearchResultInterfaceFactory::class;
  58. $this->searchResultFactory = $this->createPartialMock($className, ['create']);
  59. $className = \Magento\SalesRule\Api\Data\RuleSearchResultInterface::class;
  60. $this->searchResultsMock = $this->createMock($className);
  61. $className = \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory::class;
  62. $this->collectionFactory = $this->createPartialMock($className, ['create']);
  63. $className = \Magento\Framework\Api\ExtensionAttribute\JoinProcessor::class;
  64. $this->extensionAttributesJoinProcessorMock = $this->createPartialMock($className, ['process']);
  65. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  66. $this->collectionProcessor = $this->createMock(
  67. \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
  68. );
  69. $this->ruleRepository = $objectManager->getObject(
  70. \Magento\SalesRule\Model\RuleRepository::class,
  71. [
  72. 'ruleFactory' => $this->ruleFactory,
  73. 'toDataModelConverter' => $this->toDataModelConverter,
  74. 'toModelConverter' => $this->toModelConverter,
  75. 'searchResultFactory' => $this->searchResultFactory,
  76. 'extensionAttributesJoinProcessor' => $this->extensionAttributesJoinProcessorMock,
  77. 'ruleCollectionFactory' => $this->collectionFactory,
  78. 'collectionProcessor' => $this->collectionProcessor
  79. ]
  80. );
  81. }
  82. public function testDeleteById()
  83. {
  84. $model = $this->createMock(\Magento\SalesRule\Model\Rule::class);
  85. $this->ruleFactory->expects($this->once())->method('create')->willReturn($model);
  86. $model->expects($this->once())->method('load')->with(10)->willReturnSelf();
  87. $model->expects($this->once())->method('getId')->willReturn(10);
  88. $model->expects($this->once())->method('delete');
  89. $this->assertTrue($this->ruleRepository->deleteById(10));
  90. }
  91. public function testGetById()
  92. {
  93. $model = $this->createMock(\Magento\SalesRule\Model\Rule::class);
  94. $this->ruleFactory->expects($this->once())->method('create')->willReturn($model);
  95. $model->expects($this->once())->method('load')->with(10)->willReturnSelf();
  96. $model->expects($this->once())->method('getId')->willReturn(10);
  97. $model->expects($this->once())->method('getStoreLabels');
  98. $rule = $this->createMock(\Magento\SalesRule\Model\Data\Rule::class);
  99. $this->toDataModelConverter->expects($this->once())->method('toDataModel')->with($model)->willReturn($rule);
  100. $this->assertEquals($rule, $this->ruleRepository->getById(10));
  101. }
  102. public function testSave()
  103. {
  104. $rule = $this->createMock(\Magento\SalesRule\Model\Data\Rule::class);
  105. $model = $this->createMock(\Magento\SalesRule\Model\Rule::class);
  106. $this->toModelConverter->expects($this->once())->method('toModel')->with($rule)->willReturn($model);
  107. $model->expects($this->once())->method('save');
  108. $model->expects($this->once())->method('getId')->willReturn(10);
  109. $model->expects($this->once())->method('load')->with(10);
  110. $model->expects($this->once())->method('getStoreLabels');
  111. $this->toDataModelConverter->expects($this->once())->method('toDataModel')->with($model)->willReturn($rule);
  112. $this->assertEquals($rule, $this->ruleRepository->save($rule));
  113. }
  114. public function testGetList()
  115. {
  116. $collectionSize = 1;
  117. /**
  118. * @var \Magento\Framework\Api\SearchCriteriaInterface $searchCriteriaMock
  119. */
  120. $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
  121. $collectionMock = $this->createMock(\Magento\SalesRule\Model\ResourceModel\Rule\Collection::class);
  122. $this->extensionAttributesJoinProcessorMock->expects($this->once())
  123. ->method('process')
  124. ->with($collectionMock, \Magento\SalesRule\Api\Data\RuleInterface::class);
  125. $this->searchResultsMock->expects($this->once())->method('setSearchCriteria')->with($searchCriteriaMock);
  126. $this->collectionFactory->expects($this->once())->method('create')->willReturn($collectionMock);
  127. $collectionMock->expects($this->once())->method('getSize')->willReturn($collectionSize);
  128. $this->searchResultsMock->expects($this->once())->method('setTotalCount')->with($collectionSize);
  129. $this->collectionProcessor->expects($this->once())
  130. ->method('process')
  131. ->with($searchCriteriaMock, $collectionMock);
  132. $collectionMock->expects($this->once())->method('getItems')->willReturn([]);
  133. $this->searchResultsMock->expects($this->once())->method('setItems')->with([]);
  134. $this->searchResultFactory->expects($this->once())->method('create')->willReturn($this->searchResultsMock);
  135. $this->assertEquals($this->searchResultsMock, $this->ruleRepository->getList($searchCriteriaMock));
  136. }
  137. }