1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
4
|
|
|
use HexMakina\Crudites\Grammar\Clause\Where; |
5
|
|
|
use HexMakina\Crudites\Grammar\Predicate; |
6
|
|
|
|
7
|
|
|
class WhereTest extends TestCase |
8
|
|
|
{ |
9
|
|
|
public function testConstructor() |
10
|
|
|
{ |
11
|
|
|
$where = new Where(); |
12
|
|
|
$this->assertInstanceOf(Where::class, $where); |
13
|
|
|
$this->assertEquals('', $where->__toString()); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function testAnd() |
17
|
|
|
{ |
18
|
|
|
$where = new Where(); |
19
|
|
|
$where->and('1 = 1'); |
20
|
|
|
$this->assertEquals('WHERE 1 = 1', (string)$where); |
21
|
|
|
$this->assertEquals([], $where->bindings()); |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
$where = new Where(); |
25
|
|
|
$where->and(new Predicate('1', '=', '1')); |
26
|
|
|
$this->assertEquals('WHERE 1 = 1', (string)$where); |
27
|
|
|
$this->assertEquals([], $where->bindings()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testAndPredicate() |
31
|
|
|
{ |
32
|
|
|
$where = new Where(); |
33
|
|
|
$this->assertEquals([], $where->bindings()); |
34
|
|
|
|
35
|
|
|
$where->andPredicate((new Predicate(['table', 'field'], '='))->withValue(3)); |
36
|
|
|
$this->assertEquals(['table_field' => 3], $where->bindings()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testAndIsNull() |
40
|
|
|
{ |
41
|
|
|
$where = new Where(); |
42
|
|
|
$where->andIsNull('expression'); |
43
|
|
|
$this->assertEquals('WHERE expression IS NULL', (string)$where); |
44
|
|
|
|
45
|
|
|
$where = new Where(); |
46
|
|
|
$where->andIsNull(['field']); |
47
|
|
|
$this->assertEquals('WHERE `field` IS NULL', (string)$where); |
48
|
|
|
|
49
|
|
|
$where = new Where(); |
50
|
|
|
$where->andIsNull(['table', 'field']); |
51
|
|
|
$this->assertEquals('WHERE `table`.`field` IS NULL', (string)$where); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testAndFields() |
55
|
|
|
{ |
56
|
|
|
$where = new Where(); |
57
|
|
|
$where->andFields(['field' => 'value']); |
58
|
|
|
$this->assertEquals('WHERE `field` = :andFields_field', (string)$where); |
59
|
|
|
$this->assertEquals(['andFields_field' => 'value'], $where->bindings()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testAndIn() |
63
|
|
|
{ |
64
|
|
|
$where = new Where(); |
65
|
|
|
$where->andIn('field', ['value1', 'value2']); |
66
|
|
|
$this->assertEquals('WHERE field IN (:andIn_0,:andIn_1)', (string)$where); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testAndValue() |
70
|
|
|
{ |
71
|
|
|
$where = new Where(); |
72
|
|
|
|
73
|
|
|
$expression = 'expression + expression'; |
74
|
|
|
$expected_bind_label = 'andValue_expression_bind_label'; |
75
|
|
|
|
76
|
|
|
$where->andValue($expression, '>', 3, $expected_bind_label); |
77
|
|
|
$this->assertEquals('WHERE ' . $expression . ' > :' . $expected_bind_label, (string)$where); |
78
|
|
|
$this->assertEquals([$expected_bind_label => 3], $where->bindings()); |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
$where = new Where(); |
82
|
|
|
|
83
|
|
|
$where->andValue(['field'], '>', 3); |
84
|
|
|
$this->assertEquals('WHERE `field` > :field', (string)$where); |
85
|
|
|
$this->assertEquals(['field' => 3], $where->bindings()); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|