ConditionFactoryTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Rule\Test\Unit\Model;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. class ConditionFactoryTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Rule\Model\ConditionFactory
  12. */
  13. protected $conditionFactory;
  14. /**
  15. * @var ObjectManagerHelper
  16. */
  17. protected $objectManagerHelper;
  18. /**
  19. * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $objectManagerMock;
  22. protected function setUp()
  23. {
  24. $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  25. $this->objectManagerHelper = new ObjectManagerHelper($this);
  26. $this->conditionFactory = $this->objectManagerHelper->getObject(
  27. \Magento\Rule\Model\ConditionFactory::class,
  28. [
  29. 'objectManager' => $this->objectManagerMock
  30. ]
  31. );
  32. }
  33. public function testExceptingToCallMethodCreateInObjectManager()
  34. {
  35. $type = \Magento\Rule\Model\Condition\Combine::class;
  36. $origin = $this->getMockBuilder($type)
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->objectManagerMock
  40. ->expects($this->once())
  41. ->method('create')
  42. ->with($type)
  43. ->willReturn($origin);
  44. $this->conditionFactory->create($type);
  45. }
  46. public function testExceptingClonedObject()
  47. {
  48. $type = \Magento\Rule\Model\Condition\Combine::class;
  49. $origin = $this->getMockBuilder($type)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->objectManagerMock->expects($this->once())
  53. ->method('create')
  54. ->with($type)
  55. ->willReturn($origin);
  56. $cloned = $this->conditionFactory->create($type);
  57. $this->assertNotSame($cloned, $origin);
  58. }
  59. public function testCreateExceptionClass()
  60. {
  61. $type = 'type';
  62. $this->objectManagerMock
  63. ->expects($this->never())
  64. ->method('create');
  65. $this->expectException(\InvalidArgumentException::class);
  66. $this->expectExceptionMessage('Class does not exist');
  67. $this->conditionFactory->create($type);
  68. }
  69. public function testCreateExceptionType()
  70. {
  71. $type = \Magento\Rule\Model\ConditionFactory::class;
  72. $this->objectManagerMock
  73. ->expects($this->never())
  74. ->method('create')
  75. ->with($type)
  76. ->willReturn(new \stdClass());
  77. $this->expectException(\InvalidArgumentException::class);
  78. $this->expectExceptionMessage('Class does not implement condition interface');
  79. $this->conditionFactory->create($type);
  80. }
  81. }