FactoryTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. class FactoryTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @dataProvider createDataProvider
  11. *
  12. * @param string $classType
  13. * @param string $className
  14. * @param \PHPUnit_Framework_MockObject_MockObject $classTypeMock
  15. */
  16. public function testCreate($classType, $className, $classTypeMock)
  17. {
  18. $classMock = $this->createPartialMock(
  19. \Magento\Tax\Model\ClassModel::class,
  20. ['getClassType', 'getId', '__wakeup']
  21. );
  22. $classMock->expects($this->once())->method('getClassType')->will($this->returnValue($classType));
  23. $classMock->expects($this->once())->method('getId')->will($this->returnValue(1));
  24. $objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  25. $objectManager->expects(
  26. $this->once()
  27. )->method(
  28. 'create'
  29. )->with(
  30. $this->equalTo($className),
  31. $this->equalTo(['data' => ['id' => 1]])
  32. )->will(
  33. $this->returnValue($classTypeMock)
  34. );
  35. $taxClassFactory = new \Magento\Tax\Model\TaxClass\Factory($objectManager);
  36. $this->assertEquals($classTypeMock, $taxClassFactory->create($classMock));
  37. }
  38. /**
  39. * @return array
  40. */
  41. public function createDataProvider()
  42. {
  43. $customerClassMock = $this->createMock(\Magento\Tax\Model\TaxClass\Type\Customer::class);
  44. $productClassMock = $this->createMock(\Magento\Tax\Model\TaxClass\Type\Product::class);
  45. return [
  46. [
  47. \Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_CUSTOMER,
  48. \Magento\Tax\Model\TaxClass\Type\Customer::class,
  49. $customerClassMock,
  50. ],
  51. [
  52. \Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_PRODUCT,
  53. \Magento\Tax\Model\TaxClass\Type\Product::class,
  54. $productClassMock
  55. ]
  56. ];
  57. }
  58. public function testCreateWithWrongClassType()
  59. {
  60. $wrongClassType = 'TYPE';
  61. $classMock = $this->createPartialMock(
  62. \Magento\Tax\Model\ClassModel::class,
  63. ['getClassType', 'getId', '__wakeup']
  64. );
  65. $classMock->expects($this->once())->method('getClassType')->will($this->returnValue($wrongClassType));
  66. $objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  67. $taxClassFactory = new \Magento\Tax\Model\TaxClass\Factory($objectManager);
  68. $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
  69. $this->expectExceptionMessage(sprintf('Invalid type of tax class "%s"', $wrongClassType));
  70. $taxClassFactory->create($classMock);
  71. }
  72. }