|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
|
4
|
|
|
|
|
5
|
|
|
use HexMakina\Crudites\Grammar\Query\Select; |
|
6
|
|
|
use HexMakina\Crudites\Grammar\Clause\Where; |
|
7
|
|
|
use HexMakina\Crudites\Grammar\Predicate; |
|
8
|
|
|
|
|
9
|
|
|
class SelectTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
public function testConstructor() |
|
12
|
|
|
{ |
|
13
|
|
|
$columns = ['id', 'contact' => 'email', 'person' => ['name']]; |
|
14
|
|
|
$table = 'users'; |
|
15
|
|
|
$table_alias = 'u'; |
|
16
|
|
|
|
|
17
|
|
|
$select = new Select($columns, $table, $table_alias); |
|
18
|
|
|
|
|
19
|
|
|
$this->assertEquals($table, $select->table()); |
|
20
|
|
|
$this->assertEquals($table_alias, $select->alias()); |
|
21
|
|
|
$this->assertEquals($table_alias, $select->base()); |
|
22
|
|
|
$this->assertEquals('SELECT id,email AS `contact`,`name` AS `person` FROM `users` `u`', (string)$select); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testStatementWithTable() |
|
26
|
|
|
{ |
|
27
|
|
|
$columns = ['id', 'name']; |
|
28
|
|
|
$table = 'users'; |
|
29
|
|
|
$select = new Select($columns, $table); |
|
30
|
|
|
|
|
31
|
|
|
$expected = 'SELECT id,name FROM `users`'; |
|
32
|
|
|
$this->assertEquals($expected, $select->statement()); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testSelectAlso() |
|
36
|
|
|
{ |
|
37
|
|
|
$columns = ['id', 'name']; |
|
38
|
|
|
$table = 'users'; |
|
39
|
|
|
$select = new Select($columns, $table); |
|
40
|
|
|
|
|
41
|
|
|
$additional_columns = ['email', 'age']; |
|
42
|
|
|
$select->selectAlso($additional_columns); |
|
43
|
|
|
|
|
44
|
|
|
$expected = 'SELECT id,name,email,age FROM `users`'; |
|
45
|
|
|
$this->assertEquals($expected, $select->statement()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testSelectAlsoWithEmptyArray() |
|
49
|
|
|
{ |
|
50
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
51
|
|
|
$this->expectExceptionMessage('EMPTY_SETTER_ARRAY'); |
|
52
|
|
|
|
|
53
|
|
|
$columns = ['id', 'name']; |
|
54
|
|
|
$table = 'users'; |
|
55
|
|
|
$select = new Select($columns, $table); |
|
56
|
|
|
$select->selectAlso([]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testSelectWhere() |
|
60
|
|
|
{ |
|
61
|
|
|
$columns = ['id', 'name']; |
|
62
|
|
|
$table = 'users'; |
|
63
|
|
|
$select = new Select($columns, $table); |
|
64
|
|
|
|
|
65
|
|
|
$where = new Where([new Predicate('id', '=', 1)]); |
|
66
|
|
|
$select->add($where); |
|
67
|
|
|
|
|
68
|
|
|
$expected = 'SELECT id,name FROM `users` WHERE id = 1'; |
|
69
|
|
|
$this->assertEquals($expected, (string)$select); |
|
70
|
|
|
$this->assertEmpty($select->bindings()); |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$where->andPredicate((new Predicate('name', 'LIKE'))->withValue('John%')); |
|
74
|
|
|
$expected_bindings = ['name' => 'John%']; |
|
75
|
|
|
$expected .= ' AND name LIKE :name'; |
|
76
|
|
|
$this->assertEquals($expected, (string)$select); |
|
77
|
|
|
$this->assertEquals($expected_bindings, $where->bindings()); |
|
78
|
|
|
$this->assertEquals($expected_bindings, $select->bindings()); |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
$where->andIsNull(['email']); |
|
82
|
|
|
$expected .= ' AND `email` IS NULL'; |
|
83
|
|
|
|
|
84
|
|
|
$this->assertEquals($expected, (string)$select); |
|
85
|
|
|
$this->assertEquals($expected_bindings, $where->bindings()); |
|
86
|
|
|
$this->assertEquals($expected_bindings, $select->bindings()); |
|
87
|
|
|
|
|
88
|
|
|
$where->andFields(['age' => 23]); |
|
89
|
|
|
$expected_bindings['andFields_age'] = 23; |
|
90
|
|
|
$expected .= ' AND `age` = :andFields_age'; |
|
91
|
|
|
$this->assertEquals($expected, (string)$select); |
|
92
|
|
|
$this->assertEquals($expected_bindings, $where->bindings()); |
|
93
|
|
|
$this->assertEquals($expected_bindings, $select->bindings()); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|