ManagementTest.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\Tax\Api\Data\TaxClassInterfaceFactory;
  8. use Magento\Tax\Api\Data\TaxClassKeyInterface;
  9. use Magento\Tax\Api\TaxClassManagementInterface;
  10. use Magento\Tax\Model\TaxClass\Key;
  11. use Magento\TestFramework\Helper\Bootstrap;
  12. class ManagementTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var Repository
  16. */
  17. private $taxClassRepository;
  18. /**
  19. * @var Management
  20. */
  21. private $taxClassManagement;
  22. /**
  23. * @var TaxClassInterfaceFactory
  24. */
  25. private $taxClassFactory;
  26. /**
  27. * @var \Magento\Framework\ObjectManagerInterface
  28. */
  29. private $objectManager;
  30. /**
  31. * @var \Magento\Framework\Api\DataObjectHelper
  32. */
  33. private $dataObjectHelper;
  34. protected function setUp()
  35. {
  36. $this->objectManager = Bootstrap::getObjectManager();
  37. $this->taxClassRepository = $this->objectManager->create(\Magento\Tax\Api\TaxClassRepositoryInterface::class);
  38. $this->taxClassManagement = $this->objectManager->create(\Magento\Tax\Api\TaxClassManagementInterface::class);
  39. $this->taxClassFactory = $this->objectManager->create(\Magento\Tax\Api\Data\TaxClassInterfaceFactory::class);
  40. $this->dataObjectHelper = $this->objectManager->create(\Magento\Framework\Api\DataObjectHelper::class);
  41. }
  42. /**
  43. * @magentoDbIsolation enabled
  44. */
  45. public function testGetTaxClassId()
  46. {
  47. $taxClassName = 'Get Me';
  48. $taxClassDataObject = $this->taxClassFactory->create();
  49. $taxClassDataObject->setClassName($taxClassName)
  50. ->setClassType(TaxClassManagementInterface::TYPE_CUSTOMER);
  51. $taxClassId = $this->taxClassRepository->save($taxClassDataObject);
  52. /** @var \Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory $taxClassKeyFactory */
  53. $taxClassKeyFactory = $this->objectManager->create(\Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory::class);
  54. $taxClassKeyTypeId = $taxClassKeyFactory->create();
  55. $this->dataObjectHelper->populateWithArray(
  56. $taxClassKeyTypeId,
  57. [
  58. Key::KEY_TYPE => TaxClassKeyInterface::TYPE_ID,
  59. Key::KEY_VALUE => $taxClassId,
  60. ],
  61. \Magento\Tax\Api\Data\TaxClassKeyInterface::class
  62. );
  63. $this->assertEquals(
  64. $taxClassId,
  65. $this->taxClassManagement->getTaxClassId($taxClassKeyTypeId, TaxClassManagementInterface::TYPE_CUSTOMER)
  66. );
  67. $taxClassKeyTypeName = $taxClassKeyFactory->create();
  68. $this->dataObjectHelper->populateWithArray(
  69. $taxClassKeyTypeName,
  70. [
  71. Key::KEY_TYPE => TaxClassKeyInterface::TYPE_NAME,
  72. Key::KEY_VALUE => $taxClassName,
  73. ],
  74. \Magento\Tax\Api\Data\TaxClassKeyInterface::class
  75. );
  76. $this->assertEquals(
  77. $taxClassId,
  78. $this->taxClassManagement->getTaxClassId($taxClassKeyTypeId, TaxClassManagementInterface::TYPE_CUSTOMER)
  79. );
  80. $this->assertNull($this->taxClassManagement->getTaxClassId(null));
  81. $this->assertEquals(
  82. null,
  83. $this->taxClassManagement->getTaxClassId($taxClassKeyTypeName, TaxClassManagementInterface::TYPE_PRODUCT)
  84. );
  85. }
  86. }