ConditionResolverTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Test\Unit\ReportXml\DB;
  7. use Magento\Analytics\ReportXml\DB\ConditionResolver;
  8. use Magento\Analytics\ReportXml\DB\SelectBuilder;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\Framework\DB\Adapter\AdapterInterface;
  11. use Magento\Framework\DB\Sql\Expression;
  12. class ConditionResolverTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $resourceConnectionMock;
  18. /**
  19. * @var ConditionResolver
  20. */
  21. private $conditionResolver;
  22. /**
  23. * @var SelectBuilder|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $selectBuilderMock;
  26. /**
  27. * @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $connectionMock;
  30. /**
  31. * @return void
  32. */
  33. protected function setUp()
  34. {
  35. $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->selectBuilderMock = $this->getMockBuilder(SelectBuilder::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->conditionResolver = new ConditionResolver($this->resourceConnectionMock);
  45. }
  46. public function testGetFilter()
  47. {
  48. $condition = ["type" => "variable", "_value" => "1", "attribute" => "id", "operator" => "neq"];
  49. $valueCondition = ["type" => "value", "_value" => "2", "attribute" => "first_name", "operator" => "eq"];
  50. $identifierCondition = [
  51. "type" => "identifier",
  52. "_value" => "other_field",
  53. "attribute" => "last_name",
  54. "operator" => "eq"];
  55. $filter = [["glue" => "AND", "condition" => [$valueCondition]]];
  56. $filterConfig = [
  57. ["glue" => "OR", "condition" => [$condition], 'filter' => $filter],
  58. ["glue" => "OR", "condition" => [$identifierCondition]],
  59. ];
  60. $aliasName = 'n';
  61. $this->selectBuilderMock->expects($this->any())
  62. ->method('setParams')
  63. ->with(array_merge([], [$condition['_value']]));
  64. $this->selectBuilderMock->expects($this->once())
  65. ->method('getParams')
  66. ->willReturn([]);
  67. $this->selectBuilderMock->expects($this->any())
  68. ->method('getColumns')
  69. ->willReturn(['price' => new Expression("(n.price = 400)")]);
  70. $this->resourceConnectionMock->expects($this->once())
  71. ->method('getConnection')
  72. ->willReturn($this->connectionMock);
  73. $this->connectionMock->expects($this->any())
  74. ->method('quote')
  75. ->willReturn("'John'");
  76. $this->connectionMock->expects($this->exactly(4))
  77. ->method('quoteIdentifier')
  78. ->willReturnMap([
  79. ['n.id', false, '`n`.`id`'],
  80. ['n.first_name', false, '`n`.`first_name`'],
  81. ['n.last_name', false, '`n`.`last_name`'],
  82. ['other_field', false, '`other_field`'],
  83. ]);
  84. $result = "(`n`.`id` != 1 OR ((`n`.`first_name` = 'John'))) OR (`n`.`last_name` = `other_field`)";
  85. $this->assertEquals(
  86. $result,
  87. $this->conditionResolver->getFilter($this->selectBuilderMock, $filterConfig, $aliasName)
  88. );
  89. }
  90. }