Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConditionManagerTest.php
Go to the documentation of this file.
1 <?php
8 
10 
11 class ConditionManagerTest extends \PHPUnit\Framework\TestCase
12 {
16  private $resource;
17 
19  private $conditionManager;
20 
24  private $connectionMock;
25 
26  protected function setUp()
27  {
28  $objectManager = new ObjectManager($this);
29 
30  $this->connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
31  ->disableOriginalConstructor()
32  ->setMethods(['quote', 'quoteIdentifier'])
33  ->getMockForAbstractClass();
34  $this->connectionMock->expects($this->any())
35  ->method('quote')
36  ->will(
37  $this->returnCallback(
38  function ($value) {
39  return sprintf('\'%s\'', $value);
40  }
41  )
42  );
43  $this->connectionMock->expects($this->any())
44  ->method('quoteIdentifier')
45  ->will(
46  $this->returnCallback(
47  function ($value) {
48  return sprintf('`%s`', $value);
49  }
50  )
51  );
52 
53  $this->resource = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56  $this->resource->expects($this->once())
57  ->method('getConnection')
58  ->will($this->returnValue($this->connectionMock));
59 
60  $this->conditionManager = $objectManager->getObject(
61  \Magento\Framework\Search\Adapter\Mysql\ConditionManager::class,
62  [
63  'resource' => $this->resource
64  ]
65  );
66  }
67 
73  public function testWrapBrackets($query, $expectedResult)
74  {
75  $actualResult = $this->conditionManager->wrapBrackets($query);
76  $this->assertEquals($expectedResult, $actualResult);
77  }
78 
84  public function wrapBracketsDataProvider()
85  {
86  return [
87  'validQuery' => [
88  'query' => 'a = b',
89  'expectedResult' => '(a = b)',
90  ],
91  'emptyQuery' => [
92  'query' => '',
93  'expectedResult' => '',
94  ],
95  'invalidQuery' => [
96  'query' => '1',
97  'expectedResult' => '(1)',
98  ]
99  ];
100  }
101 
102  public function testCombineQueries()
103  {
104  $queries = [
105  'a = b',
106  false,
107  true,
108  '',
109  0,
110  'test',
111  ];
112  $unionOperator = 'AND';
113  $expectedResult = 'a = b AND 1 AND 0 AND test';
114  $actualResult = $this->conditionManager->combineQueries($queries, $unionOperator);
115  $this->assertEquals($expectedResult, $actualResult);
116  }
117 
125  public function testGenerateCondition($field, $operator, $value, $expectedResult)
126  {
127  $actualResult = $this->conditionManager->generateCondition($field, $operator, $value);
128  $this->assertEquals($expectedResult, $actualResult);
129  }
130 
135  {
136  return [
137  [
138  'field' => 'a',
139  'operator' => '=',
140  'value' => 1,
141  'expectedResult' => '`a` = \'1\'',
142  ],
143  [
144  'field' => 'a',
145  'operator' => '=',
146  'value' => '123',
147  'expectedResult' => '`a` = \'123\''
148  ],
149  ];
150  }
151 }
$objectManager
Definition: bootstrap.php:17
$value
Definition: gender.phtml:16