1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
4
|
|
|
use HexMakina\Crudites\Grammar\Query\Select; |
5
|
|
|
use HexMakina\Crudites\CruditesException; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class SelectTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
public function testConstructor() |
12
|
|
|
{ |
13
|
|
|
$columns = ['id', 'person' => 'name']; |
14
|
|
|
$table = 'users'; |
15
|
|
|
$table_alias = 'u'; |
16
|
|
|
|
17
|
|
|
$select = new Select($columns, $table, $table_alias); |
18
|
|
|
|
19
|
|
|
$this->assertEquals('SELECT id,name AS `person` FROM `users`', (string)$select); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testStatementWithTable() |
23
|
|
|
{ |
24
|
|
|
$columns = ['id', 'name']; |
25
|
|
|
$table = 'users'; |
26
|
|
|
$select = new Select($columns, $table); |
27
|
|
|
|
28
|
|
|
$expected = 'SELECT id,name FROM `users`'; |
29
|
|
|
$this->assertEquals($expected, $select->statement()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testTableLabel() |
33
|
|
|
{ |
34
|
|
|
$columns = ['id', 'name']; |
35
|
|
|
$table = 'users'; |
36
|
|
|
$table_alias = 'u'; |
37
|
|
|
|
38
|
|
|
$select = new Select($columns, $table, $table_alias); |
39
|
|
|
|
40
|
|
|
$this->assertEquals($table_alias, $select->tableLabel()); |
41
|
|
|
$this->assertEquals($table_alias, $select->tableLabel(null)); |
42
|
|
|
$this->assertEquals('forced_value', $select->tableLabel('forced_value')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testSelectAlso() |
46
|
|
|
{ |
47
|
|
|
$columns = ['id', 'name']; |
48
|
|
|
$table = 'users'; |
49
|
|
|
$select = new Select($columns, $table); |
50
|
|
|
|
51
|
|
|
$additional_columns = ['email', 'age']; |
52
|
|
|
$select->selectAlso($additional_columns); |
53
|
|
|
|
54
|
|
|
$expected = 'SELECT id,name,email,age FROM `users`'; |
55
|
|
|
$this->assertEquals($expected, $select->statement()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testSelectAlsoWithEmptyArray() |
59
|
|
|
{ |
60
|
|
|
$this->expectException(\InvalidArgumentException::class); |
61
|
|
|
$this->expectExceptionMessage('EMPTY_SETTER_ARRAY'); |
62
|
|
|
|
63
|
|
|
$columns = ['id', 'name']; |
64
|
|
|
$table = 'users'; |
65
|
|
|
$select = new Select($columns, $table); |
66
|
|
|
|
67
|
|
|
$select->selectAlso([]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|