RepositoryTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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\TaxClass;
  7. use Magento\Framework\Exception\CouldNotDeleteException;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Tax\Model\TaxClass\Repository;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class RepositoryTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /** @var Repository */
  16. protected $model;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $searchResultFactory;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $searchResultMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $taxClassResourceMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $classModelRegistryMock;
  33. /**
  34. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  35. */
  36. protected $objectManager;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $taxClassCollectionFactory;
  41. /**
  42. * @var \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $extensionAttributesJoinProcessorMock;
  45. /**
  46. * @var \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface |
  47. * \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $collectionProcessor;
  50. /**
  51. * @return void
  52. */
  53. protected function setUp()
  54. {
  55. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  56. $this->searchResultFactory = $this->createPartialMock(
  57. \Magento\Tax\Api\Data\TaxClassSearchResultsInterfaceFactory::class,
  58. ['create']
  59. );
  60. $this->searchResultMock = $this->createMock(\Magento\Tax\Api\Data\TaxClassSearchResultsInterface::class);
  61. $this->classModelRegistryMock = $this->createMock(\Magento\Tax\Model\ClassModelRegistry::class);
  62. $this->taxClassCollectionFactory = $this->createPartialMock(
  63. \Magento\Tax\Model\ResourceModel\TaxClass\CollectionFactory::class,
  64. ['create']
  65. );
  66. $this->taxClassResourceMock = $this->createMock(\Magento\Tax\Model\ResourceModel\TaxClass::class);
  67. $this->extensionAttributesJoinProcessorMock = $this->createPartialMock(
  68. \Magento\Framework\Api\ExtensionAttribute\JoinProcessor::class,
  69. ['process']
  70. );
  71. $this->collectionProcessor = $this->createMock(
  72. \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
  73. );
  74. $this->model = $this->objectManager->getObject(
  75. \Magento\Tax\Model\TaxClass\Repository::class,
  76. [
  77. 'classModelRegistry' => $this->classModelRegistryMock,
  78. 'taxClassResource' => $this->taxClassResourceMock,
  79. 'searchResultsFactory' => $this->searchResultFactory,
  80. 'taxClassCollectionFactory' => $this->taxClassCollectionFactory,
  81. 'joinProcessor' => $this->extensionAttributesJoinProcessorMock,
  82. 'collectionProcessor' => $this->collectionProcessor
  83. ]
  84. );
  85. }
  86. /**
  87. * @return void
  88. */
  89. public function testDelete()
  90. {
  91. $taxClass = $this->createMock(\Magento\Tax\Model\ClassModel::class);
  92. $taxClass->expects($this->once())->method('getClassId')->willReturn(1);
  93. $this->taxClassResourceMock->expects($this->once())->method('delete')->with($taxClass);
  94. $this->classModelRegistryMock->expects($this->once())->method('remove')->with(1);
  95. $this->assertTrue($this->model->delete($taxClass));
  96. }
  97. /**
  98. * @return void
  99. * @expectedException \Magento\Framework\Exception\CouldNotDeleteException
  100. * @expectedExceptionMessage Some Message
  101. */
  102. public function testDeleteResourceException()
  103. {
  104. $taxClass = $this->createMock(\Magento\Tax\Model\ClassModel::class);
  105. $taxClass->expects($this->once())->method('getClassId')->willReturn(1);
  106. $this->taxClassResourceMock
  107. ->expects($this->once())
  108. ->method('delete')
  109. ->willThrowException(new CouldNotDeleteException(__('Some Message')));
  110. $this->model->delete($taxClass);
  111. }
  112. /**
  113. * @return void
  114. */
  115. public function testDeleteWithException()
  116. {
  117. $taxClass = $this->createMock(\Magento\Tax\Model\ClassModel::class);
  118. $taxClass->expects($this->once())->method('getClassId')->willReturn(1);
  119. $this->taxClassResourceMock
  120. ->expects($this->once())
  121. ->method('delete')
  122. ->willThrowException(new \Exception('Some Message'));
  123. $this->assertFalse($this->model->delete($taxClass));
  124. }
  125. /**
  126. * @return void
  127. */
  128. public function testGet()
  129. {
  130. $taxClass = $this->createMock(\Magento\Tax\Api\Data\TaxClassInterface::class);
  131. $classId = 1;
  132. $this->classModelRegistryMock
  133. ->expects($this->once())
  134. ->method('retrieve')
  135. ->with($classId)
  136. ->willReturn($taxClass);
  137. $this->assertEquals($taxClass, $this->model->get($classId));
  138. }
  139. /**
  140. * @return void
  141. */
  142. public function testDeleteById()
  143. {
  144. $taxClass = $this->createMock(\Magento\Tax\Model\ClassModel::class);
  145. $classId = 1;
  146. $this->classModelRegistryMock
  147. ->expects($this->once())
  148. ->method('retrieve')
  149. ->with($classId)
  150. ->willReturn($taxClass);
  151. $taxClass->expects($this->once())->method('getClassId')->willReturn(1);
  152. $this->taxClassResourceMock->expects($this->once())->method('delete')->with($taxClass);
  153. $this->classModelRegistryMock->expects($this->once())->method('remove')->with(1);
  154. $this->assertTrue($this->model->deleteById($classId));
  155. }
  156. /**
  157. * @return void
  158. */
  159. public function testGetList()
  160. {
  161. $taxClassOne = $this->createMock(\Magento\Tax\Api\Data\TaxClassInterface::class);
  162. $taxClassTwo = $this->createMock(\Magento\Tax\Api\Data\TaxClassInterface::class);
  163. $searchCriteria = $this->createMock(\Magento\Framework\Api\SearchCriteriaInterface::class);
  164. $collection = $this->createPartialMock(
  165. \Magento\Tax\Model\ResourceModel\TaxClass\Collection::class,
  166. ['setItems', 'getSize', 'getItems']
  167. );
  168. $this->extensionAttributesJoinProcessorMock->expects($this->once())
  169. ->method('process')
  170. ->with($collection);
  171. $this->collectionProcessor->expects($this->once())
  172. ->method('process')
  173. ->with($searchCriteria, $collection);
  174. $collection->expects($this->any())->method('getSize')->willReturn(2);
  175. $collection->expects($this->any())->method('setItems')->with([$taxClassOne, $taxClassTwo]);
  176. $collection->expects($this->any())->method('getItems')->willReturn([$taxClassOne, $taxClassTwo]);
  177. $this->searchResultMock->expects($this->once())->method('setSearchCriteria')->with($searchCriteria);
  178. $this->searchResultMock->expects($this->once())->method('setTotalCount')->with(2);
  179. $this->searchResultFactory->expects($this->once())->method('create')->willReturn($this->searchResultMock);
  180. $this->taxClassCollectionFactory->expects($this->once())->method('create')->willReturn($collection);
  181. $this->assertEquals($this->searchResultMock, $this->model->getList($searchCriteria));
  182. }
  183. /**
  184. * @return void
  185. */
  186. public function testSave()
  187. {
  188. $taxClass = $this->createMock(\Magento\Tax\Model\ClassModel::class);
  189. $taxClass->expects($this->any())->method('getClassName')->willReturn('Class Name');
  190. $taxClass->expects($this->any())->method('getClassType')->willReturn('PRODUCT');
  191. $taxClass->expects($this->any())->method('getClassId')->willReturn(10);
  192. $this->classModelRegistryMock->expects($this->once())->method('registerTaxClass')->with($taxClass);
  193. $originTaxClass = $this->createMock(\Magento\Tax\Model\ClassModel::class);
  194. $originTaxClass->expects($this->once())->method('getClassType')->willReturn('PRODUCT');
  195. $this->classModelRegistryMock
  196. ->expects($this->once())
  197. ->method('retrieve')
  198. ->with(10)
  199. ->willReturn($originTaxClass);
  200. $this->taxClassResourceMock->expects($this->once())->method('save')->with($taxClass);
  201. $this->assertEquals(10, $this->model->save($taxClass));
  202. }
  203. /**
  204. * @return void
  205. * @expectedException \Magento\Framework\Exception\InputException
  206. * @expectedExceptionMessage Updating classType is not allowed.
  207. */
  208. public function testSaveWithInputException()
  209. {
  210. $taxClass = $this->createMock(\Magento\Tax\Model\ClassModel::class);
  211. $originalTax = $this->createMock(\Magento\Tax\Model\ClassModel::class);
  212. $taxClass->expects($this->exactly(2))->method('getClassId')->willReturn(10);
  213. $this->classModelRegistryMock->expects($this->once())->method('retrieve')->with(10)->willReturn($originalTax);
  214. $originalTax->expects($this->once())->method('getClassType')->willReturn('PRODUCT');
  215. $taxClass->expects($this->once())->method('getClassType')->willReturn('PRODUCT2');
  216. $this->model->save($taxClass);
  217. }
  218. /**
  219. * @return void
  220. * @expectedException \Magento\Framework\Exception\LocalizedException
  221. * @expectedExceptionMessage Something went wrong
  222. */
  223. public function testSaveWithLocalizedException()
  224. {
  225. $taxClass = $this->createMock(\Magento\Tax\Model\ClassModel::class);
  226. $taxClass->expects($this->any())->method('getClassName')->willReturn('Class Name');
  227. $taxClass->expects($this->atLeastOnce())->method('getClassType')->willReturn('PRODUCT');
  228. $taxClass->expects($this->any())->method('getClassId')->willReturn(10);
  229. $originTaxClass = $this->createMock(\Magento\Tax\Model\ClassModel::class);
  230. $originTaxClass->expects($this->once())->method('getClassType')->willReturn('PRODUCT');
  231. $this->classModelRegistryMock
  232. ->expects($this->once())
  233. ->method('retrieve')
  234. ->with(10)
  235. ->willReturn($originTaxClass);
  236. $this->taxClassResourceMock->expects($this->once())->method('save')->with($taxClass)
  237. ->willThrowException(new LocalizedException(__("Something went wrong")));
  238. $this->model->save($taxClass);
  239. }
  240. /**
  241. * @return void
  242. * @expectedException \Magento\Framework\Exception\LocalizedException
  243. * @expectedExceptionMessage A class with the same name already exists for ClassType PRODUCT.
  244. */
  245. public function testSaveWithSameClassException()
  246. {
  247. $taxClass = $this->createMock(\Magento\Tax\Model\ClassModel::class);
  248. $taxClass->expects($this->any())->method('getClassName')->willReturn('Class Name');
  249. $taxClass->expects($this->atLeastOnce())->method('getClassType')->willReturn('PRODUCT');
  250. $taxClass->expects($this->any())->method('getClassId')->willReturn(10);
  251. $originTaxClass = $this->createMock(\Magento\Tax\Model\ClassModel::class);
  252. $originTaxClass->expects($this->once())->method('getClassType')->willReturn('PRODUCT');
  253. $this->classModelRegistryMock
  254. ->expects($this->once())
  255. ->method('retrieve')
  256. ->with(10)
  257. ->willReturn($originTaxClass);
  258. $this->taxClassResourceMock->expects($this->once())->method('save')->with($taxClass)
  259. ->willThrowException(new LocalizedException(__('Class name and class type')));
  260. $this->model->save($taxClass);
  261. }
  262. /**
  263. * @param string $classType
  264. * @return void
  265. * @dataProvider validateTaxClassDataProvider
  266. * @expectedException \Magento\Framework\Exception\InputException
  267. * @expectedExceptionMessage One or more input exceptions have occurred.
  268. */
  269. public function testSaveWithValidateTaxClassDataException($classType)
  270. {
  271. $taxClass = $this->createMock(\Magento\Tax\Model\ClassModel::class);
  272. $taxClass->expects($this->any())->method('getClassName')->willReturn('');
  273. $taxClass->expects($this->atLeastOnce())->method('getClassType')->willReturn($classType);
  274. $taxClass->expects($this->any())->method('getClassId')->willReturn(10);
  275. $originTaxClass = $this->createMock(\Magento\Tax\Model\ClassModel::class);
  276. $originTaxClass->expects($this->once())->method('getClassType')->willReturn($classType);
  277. $this->classModelRegistryMock
  278. ->expects($this->once())
  279. ->method('retrieve')
  280. ->with(10)
  281. ->willReturn($originTaxClass);
  282. $this->model->save($taxClass);
  283. }
  284. /**
  285. * @return array
  286. */
  287. public function validateTaxClassDataProvider()
  288. {
  289. return [
  290. [''],
  291. ['ERROR']
  292. ];
  293. }
  294. }