1
|
|
|
<?php |
2
|
|
|
use PHPUnit\Framework\TestCase; |
3
|
|
|
use HexMakina\Crudites\Grammar\Clause\Where; |
4
|
|
|
use HexMakina\Crudites\Grammar\Predicate; |
5
|
|
|
|
6
|
|
|
class WhereTest extends TestCase |
7
|
|
|
{ |
8
|
|
|
public function testConstructor() |
9
|
|
|
{ |
10
|
|
|
$where = new Where(); |
11
|
|
|
$this->assertInstanceOf(Where::class, $where); |
12
|
|
|
$this->assertEquals('', $where->__toString()); |
13
|
|
|
|
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
|
|
|
|
31
|
|
|
public function testAndPredicate() |
32
|
|
|
{ |
33
|
|
|
$where = new Where(); |
34
|
|
|
$this->assertEquals([], $where->bindings()); |
35
|
|
|
|
36
|
|
|
$where->andPredicate((new Predicate(['table', 'field'], '='))->withValue(3)); |
37
|
|
|
$this->assertEquals(['table_field' => 3], $where->bindings()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testAndIsNull() |
41
|
|
|
{ |
42
|
|
|
$where = new Where(); |
43
|
|
|
$where->andIsNull('expression'); |
44
|
|
|
$this->assertEquals('WHERE expression IS NULL', (string)$where); |
45
|
|
|
|
46
|
|
|
$where = new Where(); |
47
|
|
|
$where->andIsNull(['field']); |
48
|
|
|
$this->assertEquals('WHERE `field` IS NULL', (string)$where); |
49
|
|
|
|
50
|
|
|
$where = new Where(); |
51
|
|
|
$where->andIsNull(['table', 'field']); |
52
|
|
|
$this->assertEquals('WHERE `table`.`field` IS NULL', (string)$where); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testAndFields() |
56
|
|
|
{ |
57
|
|
|
$where = new Where(); |
58
|
|
|
$where->andFields(['field' => 'value']); |
59
|
|
|
$this->assertEquals('WHERE `field` = :andFields_field', (string)$where); |
60
|
|
|
$this->assertEquals(['andFields_field' => 'value'], $where->bindings()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testAndIn() |
64
|
|
|
{ |
65
|
|
|
$where = new Where(); |
66
|
|
|
$where->andIn('field', ['value1', 'value2']); |
67
|
|
|
$this->assertEquals('WHERE field IN (:andIn_field_0,:andIn_field_1)', (string)$where); |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
} |