RepositoryTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model\TaxClass;
  7. use Magento\Framework\Exception\InputException;
  8. use Magento\Tax\Api\Data\TaxClassInterfaceFactory;
  9. use Magento\Tax\Api\TaxClassManagementInterface;
  10. use Magento\Tax\Model\ClassModel as TaxClassModel;
  11. use Magento\TestFramework\Helper\Bootstrap;
  12. class RepositoryTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var Repository
  16. */
  17. private $taxClassRepository;
  18. /**
  19. * @var TaxClassInterfaceFactory
  20. */
  21. private $taxClassFactory;
  22. /**
  23. * @var TaxClassModel
  24. */
  25. private $taxClassModel;
  26. /**
  27. * @var \Magento\Framework\ObjectManagerInterface
  28. */
  29. private $objectManager;
  30. /**
  31. * @var array
  32. */
  33. private $predefinedTaxClasses;
  34. const SAMPLE_TAX_CLASS_NAME = 'Wholesale Customer';
  35. protected function setUp()
  36. {
  37. $this->objectManager = Bootstrap::getObjectManager();
  38. $this->taxClassRepository = $this->objectManager->create(\Magento\Tax\Api\TaxClassRepositoryInterface::class);
  39. $this->taxClassFactory = $this->objectManager->create(\Magento\Tax\Api\Data\TaxClassInterfaceFactory::class);
  40. $this->taxClassModel = $this->objectManager->create(\Magento\Tax\Model\ClassModel::class);
  41. $this->predefinedTaxClasses = [
  42. TaxClassManagementInterface::TYPE_PRODUCT => 'Taxable Goods',
  43. TaxClassManagementInterface::TYPE_CUSTOMER => 'Retail Customer',
  44. ];
  45. }
  46. /**
  47. * @magentoDbIsolation enabled
  48. */
  49. public function testSave()
  50. {
  51. $taxClassDataObject = $this->taxClassFactory->create();
  52. $taxClassDataObject->setClassName(self::SAMPLE_TAX_CLASS_NAME)
  53. ->setClassType(TaxClassManagementInterface::TYPE_CUSTOMER);
  54. $taxClassId = $this->taxClassRepository->save($taxClassDataObject);
  55. $this->assertEquals(self::SAMPLE_TAX_CLASS_NAME, $this->taxClassModel->load($taxClassId)->getClassName());
  56. }
  57. /**
  58. * @magentoDbIsolation enabled
  59. * @expectedException \Magento\Framework\Exception\InputException
  60. * @expectedExceptionMessage A class with the same name already exists for ClassType PRODUCT.
  61. */
  62. public function testSaveThrowsExceptionIfGivenTaxClassNameIsNotUnique()
  63. {
  64. //ClassType and name combination has to be unique.
  65. //Testing against existing Tax classes which are already setup when the instance is installed
  66. $taxClassDataObject = $this->taxClassFactory->create();
  67. $taxClassDataObject->setClassName($this->predefinedTaxClasses[TaxClassModel::TAX_CLASS_TYPE_PRODUCT])
  68. ->setClassType(TaxClassManagementInterface::TYPE_PRODUCT);
  69. $this->taxClassRepository->save($taxClassDataObject);
  70. }
  71. /**
  72. * @magentoDbIsolation enabled
  73. */
  74. public function testSaveThrowsExceptionIfGivenDataIsInvalid()
  75. {
  76. $taxClassDataObject = $this->taxClassFactory->create();
  77. $taxClassDataObject->setClassName(null)
  78. ->setClassType('');
  79. try {
  80. $this->taxClassRepository->save($taxClassDataObject);
  81. } catch (InputException $e) {
  82. $errors = $e->getErrors();
  83. $this->assertEquals('"class_name" is required. Enter and try again.', $errors[0]->getMessage());
  84. $this->assertEquals('"class_type" is required. Enter and try again.', $errors[1]->getMessage());
  85. }
  86. }
  87. /**
  88. * @magentoDbIsolation enabled
  89. */
  90. public function testGet()
  91. {
  92. $taxClassName = 'Get Me';
  93. $taxClassDataObject = $this->taxClassFactory->create();
  94. $taxClassDataObject->setClassName($taxClassName)
  95. ->setClassType(TaxClassManagementInterface::TYPE_CUSTOMER);
  96. $taxClassId = $this->taxClassRepository->save($taxClassDataObject);
  97. $data = $this->taxClassRepository->get($taxClassId);
  98. $this->assertEquals($taxClassId, $data->getClassId());
  99. $this->assertEquals($taxClassName, $data->getClassName());
  100. $this->assertEquals(TaxClassManagementInterface::TYPE_CUSTOMER, $data->getClassType());
  101. }
  102. /**
  103. * @magentoDbIsolation enabled
  104. */
  105. public function testGetList()
  106. {
  107. $taxClassName = 'Get Me';
  108. $taxClassDataObject = $this->taxClassFactory->create();
  109. $taxClassDataObject->setClassName($taxClassName)
  110. ->setClassType(TaxClassManagementInterface::TYPE_CUSTOMER);
  111. $taxClassId = $this->taxClassRepository->save($taxClassDataObject);
  112. /** @var \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder */
  113. $searchCriteriaBuilder = Bootstrap::getObjectManager()->create(
  114. \Magento\Framework\Api\SearchCriteriaBuilder::class
  115. );
  116. /** @var \Magento\Tax\Api\Data\TaxClassSearchResultsInterface */
  117. $searchResult = $this->taxClassRepository->getList($searchCriteriaBuilder->create());
  118. $items = $searchResult->getItems();
  119. /** @var \Magento\Tax\Api\Data\TaxClassInterface */
  120. $taxClass = array_pop($items);
  121. $this->assertInstanceOf(\Magento\Tax\Api\Data\TaxClassInterface::class, $taxClass);
  122. $this->assertEquals($taxClassName, $taxClass->getClassName());
  123. $this->assertEquals($taxClassId, $taxClass->getClassId());
  124. $this->assertEquals(TaxClassManagementInterface::TYPE_CUSTOMER, $taxClass->getClassType());
  125. }
  126. /**
  127. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  128. * @expectedExceptionMessage No such entity with class_id = -9999
  129. */
  130. public function testGetThrowsExceptionIfRequestedTaxClassDoesNotExist()
  131. {
  132. $this->taxClassRepository->get(-9999);
  133. }
  134. /**
  135. * @magentoDbIsolation enabled
  136. */
  137. public function testDeleteById()
  138. {
  139. $taxClassName = 'Delete Me';
  140. $taxClassDataObject = $this->taxClassFactory->create();
  141. $taxClassDataObject->setClassName($taxClassName)
  142. ->setClassType(TaxClassModel::TAX_CLASS_TYPE_CUSTOMER);
  143. $taxClassId = $this->taxClassRepository->save($taxClassDataObject);
  144. $this->assertTrue($this->taxClassRepository->deleteById($taxClassId));
  145. // Verify if the tax class is deleted
  146. $this->expectException(\Magento\Framework\Exception\NoSuchEntityException::class);
  147. $this->expectExceptionMessage("No such entity with class_id = $taxClassId");
  148. $this->taxClassRepository->deleteById($taxClassId);
  149. }
  150. /**
  151. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  152. * @expectedExceptionMessage No such entity with class_id = 99999
  153. */
  154. public function testDeleteByIdThrowsExceptionIfTargetTaxClassDoesNotExist()
  155. {
  156. $nonexistentTaxClassId = 99999;
  157. $this->taxClassRepository->deleteById($nonexistentTaxClassId);
  158. }
  159. /**
  160. * @magentoDbIsolation enabled
  161. */
  162. public function testSaveWithExistingTaxClass()
  163. {
  164. $taxClassName = 'New Class Name';
  165. $taxClassDataObject = $this->taxClassFactory->create();
  166. $taxClassDataObject->setClassName($taxClassName)
  167. ->setClassType(TaxClassModel::TAX_CLASS_TYPE_CUSTOMER);
  168. $taxClassId = $this->taxClassRepository->save($taxClassDataObject);
  169. $this->assertEquals($taxClassName, $this->taxClassModel->load($taxClassId)->getClassName());
  170. $updatedTaxClassName = 'Updated Class Name';
  171. $taxClassDataObject = $this->taxClassFactory->create();
  172. $taxClassDataObject->setClassName($updatedTaxClassName)
  173. ->setClassId($taxClassId)
  174. ->setClassType(TaxClassModel::TAX_CLASS_TYPE_CUSTOMER);
  175. $this->assertEquals($taxClassId, $this->taxClassRepository->save($taxClassDataObject));
  176. $this->assertEquals($updatedTaxClassName, $this->taxClassModel->load($taxClassId)->getClassName());
  177. }
  178. /**
  179. * @magentoDbIsolation enabled
  180. * @expectedException \Magento\Framework\Exception\InputException
  181. * @expectedExceptionMessage Updating classType is not allowed.
  182. */
  183. public function testSaveThrowsExceptionIfTargetTaxClassHasDifferentClassType()
  184. {
  185. $taxClassName = 'New Class Name';
  186. $taxClassDataObject = $this->taxClassFactory->create();
  187. $taxClassDataObject->setClassName($taxClassName)
  188. ->setClassType(TaxClassModel::TAX_CLASS_TYPE_CUSTOMER);
  189. $taxClassId = $this->taxClassRepository->save($taxClassDataObject);
  190. $this->assertEquals($taxClassName, $this->taxClassModel->load($taxClassId)->getClassName());
  191. $updatedTaxClassName = 'Updated Class Name';
  192. $taxClassDataObject = $this->taxClassFactory->create();
  193. $taxClassDataObject->setClassName($updatedTaxClassName)
  194. ->setClassId($taxClassId)
  195. ->setClassType(TaxClassModel::TAX_CLASS_TYPE_PRODUCT);
  196. $this->taxClassRepository->save($taxClassDataObject);
  197. }
  198. }