1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Database\Tests\Query\Condition; |
4
|
|
|
|
5
|
|
|
use Mockery as m; |
6
|
|
|
use Nip\Database\Connections\Connection; |
7
|
|
|
use Nip\Database\Query\Select as SelectQuery; |
8
|
|
|
use Nip\Database\Tests\AbstractTest; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ConditionTest |
12
|
|
|
* @package Nip\Database\Tests\Query\Condition |
13
|
|
|
*/ |
14
|
|
|
class ConditionTest extends AbstractTest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var SelectQuery |
18
|
|
|
*/ |
19
|
|
|
protected $query; |
20
|
|
|
|
21
|
|
|
public function setUp(): void |
22
|
|
|
{ |
23
|
|
|
parent::setUp(); |
24
|
|
|
|
25
|
|
|
$adapterMock = m::mock('Nip\Database\Adapters\MySQLi')->shouldDeferMissing(); |
|
|
|
|
26
|
|
|
$adapterMock->shouldReceive('cleanData')->andReturnUsing(function ($data) { |
27
|
|
|
return $data; |
28
|
|
|
}); |
29
|
|
|
|
30
|
|
|
$connection = new Connection(false); |
|
|
|
|
31
|
|
|
$connection->setAdapter($adapterMock); |
32
|
|
|
$this->query = $connection->newQuery(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testParseString() |
36
|
|
|
{ |
37
|
|
|
$condition = $this->query->getCondition('name = value'); |
38
|
|
|
static::assertEquals('name = value', $condition->getString()); |
39
|
|
|
|
40
|
|
|
$condition = $this->query->getCondition('id = ?', 5); |
|
|
|
|
41
|
|
|
static::assertEquals('id = 5', $condition->getString()); |
42
|
|
|
|
43
|
|
|
$condition = $this->query->getCondition('MATCH title AGAINST (?)', 'lorem ipsum'); |
|
|
|
|
44
|
|
|
static::assertEquals("MATCH title AGAINST ('lorem ipsum')", $condition->getString()); |
45
|
|
|
|
46
|
|
|
$condition = $this->query->getCondition('pos BETWEEN ? AND ?', [1, 10]); |
47
|
|
|
static::assertEquals('pos BETWEEN 1 AND 10', $condition->getString()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testAndConditions() |
51
|
|
|
{ |
52
|
|
|
$condition = $this->query->getCondition("name LIKE '%lorem%'"); |
53
|
|
|
$condition = $condition->and_($this->query->getCondition("date > NOW()")); |
54
|
|
|
|
55
|
|
|
static::assertEquals("name LIKE '%lorem%' AND date > NOW()", $condition->getString()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testOrConditions() |
59
|
|
|
{ |
60
|
|
|
$condition = $this->query->getCondition("name LIKE '%lorem%'")->or_($this->query->getCondition('date > NOW()')); |
61
|
|
|
static::assertEquals("name LIKE '%lorem%' OR date > NOW()", $condition->getString()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testAndOrConditions() |
65
|
|
|
{ |
66
|
|
|
$condition1 = $this->query |
67
|
|
|
->getCondition("name LIKE '%lorem%'") |
68
|
|
|
->and_($this->query->getCondition("date > NOW()")); |
69
|
|
|
$condition2 = $this->query |
70
|
|
|
->getCondition("name LIKE '%ipsum%'") |
71
|
|
|
->and_($this->query->getCondition("date < NOW()")); |
72
|
|
|
$condition3 = $condition1->or_($condition2); |
73
|
|
|
|
74
|
|
|
static::assertEquals( |
75
|
|
|
"(name LIKE '%lorem%' AND date > NOW()) OR (name LIKE '%ipsum%' AND date < NOW())", |
76
|
|
|
$condition3->getString() |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testNestedConditions() |
81
|
|
|
{ |
82
|
|
|
$condition2 = $this->query |
83
|
|
|
->getCondition("date > NOW()")-> |
84
|
|
|
or_($this->query->getCondition("date < '24.10.2008'")); |
85
|
|
|
$condition = $this->query |
86
|
|
|
->getCondition("name LIKE '%lorem%'") |
87
|
|
|
->and_($condition2); |
88
|
|
|
|
89
|
|
|
static::assertEquals("name LIKE '%lorem%' AND (date > NOW() OR date < '24.10.2008')", $condition->getString()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testWhereIn() |
93
|
|
|
{ |
94
|
|
|
$condition = $this->query->getCondition("id in ?", [1, 2, 3]); |
95
|
|
|
static::assertEquals("id in (1, 2, 3)", $condition->getString()); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.