BuilderTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\Sql;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. class BuilderTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Rule\Model\Condition\Sql\Builder|\PHPUnit_Framework_MockObject_MockObject
  12. */
  13. protected $_builder;
  14. protected function setUp()
  15. {
  16. $expressionMock = $this->createMock(\Magento\Rule\Model\Condition\Sql\Expression::class);
  17. $expressionFactory = $this->createPartialMock(
  18. \Magento\Rule\Model\Condition\Sql\ExpressionFactory::class,
  19. ['create']
  20. );
  21. $expressionFactory->expects($this->any())
  22. ->method('create')
  23. ->will($this->returnValue($expressionMock));
  24. $this->_builder = (new ObjectManagerHelper($this))->getObject(
  25. \Magento\Rule\Model\Condition\Sql\Builder::class,
  26. ['expressionFactory' => $expressionFactory]
  27. );
  28. }
  29. public function testAttachConditionToCollection()
  30. {
  31. $collection = $this->createPartialMock(
  32. \Magento\Eav\Model\Entity\Collection\AbstractCollection::class,
  33. [
  34. 'getResource',
  35. 'getSelect',
  36. 'getStoreId',
  37. 'getDefaultStoreId',
  38. ]
  39. );
  40. $combine = $this->createPartialMock(\Magento\Rule\Model\Condition\Combine::class, ['getConditions']);
  41. $resource = $this->createPartialMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class, ['getConnection']);
  42. $select = $this->createPartialMock(\Magento\Framework\DB\Select::class, ['where']);
  43. $select->expects($this->never())
  44. ->method('where');
  45. $connection = $this->getMockForAbstractClass(
  46. \Magento\Framework\DB\Adapter\AdapterInterface::class,
  47. [],
  48. '',
  49. false
  50. );
  51. $collection->expects($this->once())
  52. ->method('getResource')
  53. ->will($this->returnValue($resource));
  54. $collection->expects($this->any())
  55. ->method('getSelect')
  56. ->will($this->returnValue($select));
  57. $resource->expects($this->once())
  58. ->method('getConnection')
  59. ->will($this->returnValue($connection));
  60. $combine->expects($this->any())
  61. ->method('getConditions')
  62. ->will($this->returnValue([]));
  63. $this->_builder->attachConditionToCollection($collection, $combine);
  64. }
  65. /**
  66. * Test for attach condition to collection with operator in html format
  67. *
  68. * @covers \Magento\Rule\Model\Condition\Sql\Builder::attachConditionToCollection()
  69. * @return void;
  70. */
  71. public function testAttachConditionAsHtmlToCollection()
  72. {
  73. $abstractCondition = $this->getMockForAbstractClass(
  74. \Magento\Rule\Model\Condition\AbstractCondition::class,
  75. [],
  76. '',
  77. false,
  78. false,
  79. true,
  80. ['getOperatorForValidate', 'getMappedSqlField', 'getAttribute', 'getBindArgumentValue']
  81. );
  82. $abstractCondition->expects($this->once())->method('getMappedSqlField')->will($this->returnValue('argument'));
  83. $abstractCondition->expects($this->once())->method('getOperatorForValidate')->will($this->returnValue('&gt;'));
  84. $abstractCondition->expects($this->at(1))->method('getAttribute')->will($this->returnValue('attribute'));
  85. $abstractCondition->expects($this->at(2))->method('getAttribute')->will($this->returnValue('attribute'));
  86. $abstractCondition->expects($this->once())->method('getBindArgumentValue')->will($this->returnValue(10));
  87. $conditions = [$abstractCondition];
  88. $collection = $this->createPartialMock(
  89. \Magento\Eav\Model\Entity\Collection\AbstractCollection::class,
  90. [
  91. 'getResource',
  92. 'getSelect'
  93. ]
  94. );
  95. $combine = $this->createPartialMock(
  96. \Magento\Rule\Model\Condition\Combine::class,
  97. [
  98. 'getConditions',
  99. 'getValue',
  100. 'getAggregator'
  101. ]
  102. );
  103. $resource = $this->createPartialMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class, ['getConnection']);
  104. $select = $this->createPartialMock(\Magento\Framework\DB\Select::class, ['where']);
  105. $select->expects($this->never())->method('where');
  106. $connection = $this->getMockForAbstractClass(
  107. \Magento\Framework\DB\Adapter\AdapterInterface::class,
  108. ['quoteInto'],
  109. '',
  110. false
  111. );
  112. $connection->expects($this->once())->method('quoteInto')->with(' > ?', 10)->will($this->returnValue(' > 10'));
  113. $collection->expects($this->once())->method('getResource')->will($this->returnValue($resource));
  114. $resource->expects($this->once())->method('getConnection')->will($this->returnValue($connection));
  115. $combine->expects($this->once())->method('getValue')->willReturn('attribute');
  116. $combine->expects($this->once())->method('getAggregator')->willReturn(' AND ');
  117. $combine->expects($this->at(0))->method('getConditions')->will($this->returnValue($conditions));
  118. $combine->expects($this->at(1))->method('getConditions')->will($this->returnValue($conditions));
  119. $combine->expects($this->at(2))->method('getConditions')->will($this->returnValue($conditions));
  120. $combine->expects($this->at(3))->method('getConditions')->will($this->returnValue($conditions));
  121. $this->_builder->attachConditionToCollection($collection, $combine);
  122. }
  123. }