CombineTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\Condition;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. class CombineTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Rule\Model\Condition\Combine | \PHPUnit_Framework_MockObject_MockObject
  12. */
  13. private $combine;
  14. /**
  15. * @var \Magento\Rule\Model\ConditionFactory | \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $conditionFactoryMock;
  18. /**
  19. * @var \Psr\Log\LoggerInterface | \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $loggerMock;
  22. /**
  23. * @var \Magento\SalesRule\Model\Rule\Condition\Product | \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $conditionObjectMock;
  26. /**
  27. * Sets up the Mocks.
  28. * This method is called before a test is executed.
  29. */
  30. protected function setUp()
  31. {
  32. $this->conditionFactoryMock = $this->getMockBuilder(\Magento\Rule\Model\ConditionFactory::class)
  33. ->disableOriginalConstructor()
  34. ->setMethods([])
  35. ->getMock();
  36. $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  37. ->disableOriginalConstructor()
  38. ->setMethods([])
  39. ->getMock();
  40. $this->conditionObjectMock = $this->getMockBuilder(\Magento\SalesRule\Model\Rule\Condition\Product::class)
  41. ->disableOriginalConstructor()
  42. ->setMethods([])
  43. ->getMock();
  44. $this->combine = (new ObjectManagerHelper($this))->getObject(
  45. \Magento\Rule\Model\Condition\Combine::class,
  46. [
  47. "conditionFactory" => $this->conditionFactoryMock,
  48. "logger" => $this->loggerMock,
  49. ]
  50. );
  51. }
  52. /**
  53. *
  54. * @covers \Magento\Rule\Model\Condition\AbstractCondition::getValueName
  55. *
  56. * @dataProvider optionValuesData
  57. *
  58. * @param string|array $value
  59. * @param string $expectingData
  60. */
  61. public function testGetValueName($value, $expectingData)
  62. {
  63. $this->combine
  64. ->setValueOption(['option_key' => 'option_value'])
  65. ->setValue($value);
  66. $this->assertEquals($expectingData, $this->combine->getValueName());
  67. }
  68. /**
  69. * @return array
  70. */
  71. public function optionValuesData()
  72. {
  73. return [
  74. ['option_key', 'option_value'],
  75. ['option_value', 'option_value'],
  76. [['option_key'], 'option_value'],
  77. ['', '...'],
  78. ];
  79. }
  80. public function testLoadArray()
  81. {
  82. $array['conditions'] = [
  83. [
  84. 'type' => 'test',
  85. 'attribute' => '',
  86. 'operator' => '',
  87. 'value' => '',
  88. ],
  89. ];
  90. $this->conditionObjectMock->expects($this->once())
  91. ->method('loadArray')
  92. ->with($array['conditions'][0], 'conditions');
  93. $this->conditionFactoryMock->expects($this->once())
  94. ->method('create')
  95. ->with($array['conditions'][0]['type'])
  96. ->willReturn($this->conditionObjectMock);
  97. $this->loggerMock->expects($this->never())
  98. ->method('critical');
  99. $result = $this->combine->loadArray($array);
  100. $this->assertInstanceOf(\Magento\Rule\Model\Condition\Combine::class, $result);
  101. }
  102. public function testLoadArrayLoggerCatchException()
  103. {
  104. $array['conditions'] = [
  105. [
  106. 'type' => '',
  107. 'attribute' => '',
  108. 'operator' => '',
  109. 'value' => '',
  110. ],
  111. ];
  112. $this->conditionObjectMock->expects($this->never())
  113. ->method('loadArray');
  114. $this->conditionFactoryMock->expects($this->once())
  115. ->method('create')
  116. ->with($array['conditions'][0]['type'])
  117. ->willThrowException(new \Exception('everything is fine, it is test'));
  118. $this->loggerMock->expects($this->once())
  119. ->method('critical')
  120. ->with();
  121. $result = $this->combine->loadArray($array);
  122. $this->assertInstanceOf(\Magento\Rule\Model\Condition\Combine::class, $result);
  123. }
  124. }