Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConditionResolverTest.php
Go to the documentation of this file.
1 <?php
7 
13 
14 class ConditionResolverTest extends \PHPUnit\Framework\TestCase
15 {
19  private $resourceConnectionMock;
20 
24  private $conditionResolver;
25 
29  private $selectBuilderMock;
30 
34  private $connectionMock;
35 
39  protected function setUp()
40  {
41  $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
42  ->disableOriginalConstructor()
43  ->getMock();
44 
45  $this->selectBuilderMock = $this->getMockBuilder(SelectBuilder::class)
46  ->disableOriginalConstructor()
47  ->getMock();
48 
49  $this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
50  ->disableOriginalConstructor()
51  ->getMock();
52 
53  $this->conditionResolver = new ConditionResolver($this->resourceConnectionMock);
54  }
55 
56  public function testGetFilter()
57  {
58  $condition = ["type" => "variable", "_value" => "1", "attribute" => "id", "operator" => "neq"];
59  $valueCondition = ["type" => "value", "_value" => "2", "attribute" => "first_name", "operator" => "eq"];
60  $identifierCondition = [
61  "type" => "identifier",
62  "_value" => "other_field",
63  "attribute" => "last_name",
64  "operator" => "eq"];
65  $filter = [["glue" => "AND", "condition" => [$valueCondition]]];
66  $filterConfig = [
67  ["glue" => "OR", "condition" => [$condition], 'filter' => $filter],
68  ["glue" => "OR", "condition" => [$identifierCondition]],
69  ];
70  $aliasName = 'n';
71  $this->selectBuilderMock->expects($this->any())
72  ->method('setParams')
73  ->with(array_merge([], [$condition['_value']]));
74 
75  $this->selectBuilderMock->expects($this->once())
76  ->method('getParams')
77  ->willReturn([]);
78 
79  $this->selectBuilderMock->expects($this->any())
80  ->method('getColumns')
81  ->willReturn(['price' => new Expression("(n.price = 400)")]);
82 
83  $this->resourceConnectionMock->expects($this->once())
84  ->method('getConnection')
85  ->willReturn($this->connectionMock);
86 
87  $this->connectionMock->expects($this->any())
88  ->method('quote')
89  ->willReturn("'John'");
90  $this->connectionMock->expects($this->exactly(4))
91  ->method('quoteIdentifier')
92  ->willReturnMap([
93  ['n.id', false, '`n`.`id`'],
94  ['n.first_name', false, '`n`.`first_name`'],
95  ['n.last_name', false, '`n`.`last_name`'],
96  ['other_field', false, '`other_field`'],
97  ]);
98 
99  $result = "(`n`.`id` != 1 OR ((`n`.`first_name` = 'John'))) OR (`n`.`last_name` = `other_field`)";
100  $this->assertEquals(
101  $result,
102  $this->conditionResolver->getFilter($this->selectBuilderMock, $filterConfig, $aliasName)
103  );
104  }
105 }