Repository navigation
Expand file tree
/
Copy pathEasyStatementTest.php
More file actions
142 lines (115 loc) · 4.16 KB
/
Copy pathEasyStatementTest.php
File metadata and controls
142 lines (115 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace ParagonIE\EasyDB\Tests;
use ParagonIE\EasyDB\EasyStatement;
use ParagonIE\EasyDB\Exception\MustBeNonEmpty;
use PHPUnit\Framework\TestCase as TestCase;
use RuntimeException;
/**
* @package ParagonIE\EasyDB\Tests
*/
class EasyStatementTest extends TestCase
{
public function testBasicAndOr()
{
$statement = EasyStatement::open()
->with('id = ?', 1)
->andWith('last_login > ?', 'today')
->orWith('last_login IS NULL');
$this->assertSql($statement, 'id = ? AND last_login > ? OR last_login IS NULL');
$this->assertValues($statement, [1, 'today']);
}
public function testLogicalIn()
{
$statement = EasyStatement::open()
->in('role_id IN (?*)', [1, 2, 3])
->orIn('user_id IN (?*)', [100]);
$this->assertSql($statement, 'role_id IN (?, ?, ?) OR user_id IN (?)');
$this->assertValues($statement, [1, 2, 3, 100]);
$statement = EasyStatement::open()
->orIn('role_id IN (?*)', [4, 5, 6]);
$this->assertSql($statement, 'role_id IN (?, ?, ?)');
$this->assertValues($statement, [4, 5, 6]);
}
public function testEmptyIn()
{
try {
$statement = EasyStatement::open()
->in('role_id IN (?*)', [1, 2, 3])
->orIn('user_id IN (?*)', []);
$this->fail("Does not throw MustBeNonEmpty by default!");
} catch (MustBeNonEmpty $ex) {
$statement = EasyStatement::open()
->setEmptyInStatementsAllowed(true)
->in('role_id IN (?*)', [1, 2, 3])
->orIn('user_id IN (?*)', []);
}
$this->assertSql($statement, 'role_id IN (?, ?, ?)');
$this->assertValues($statement, [1, 2, 3]);
$statement = EasyStatement::open()
->setEmptyInStatementsAllowed(true)
->group()
->with('user_id = ?', 100)
->orWith('user_id = ?', 101)
->orGroup()
->in('role_id IN (?*)', [])
->endGroup()
->endGroup();
$this->assertSql($statement, '(user_id = ? OR user_id = ? OR (1 = 0))');
$this->assertValues($statement, [100, 101]);
}
public function testGroupingWithAnd()
{
$statement = EasyStatement::open()
->with('id = ?', 1);
$group = $statement->group()
->with('last_login > ?', 'today')
->orWith('last_login IS NULL');
$this->assertSame($statement, $group->endGroup());
$this->assertSql($group, 'last_login > ? OR last_login IS NULL');
$this->assertSql($statement, 'id = ? AND (' . $group->sql() . ')');
$this->assertValues($statement, [1, 'today']);
}
public function testGroupingWithOr()
{
$statement = EasyStatement::open()
->orGroup()
->with('failed_logins > ?', 5)
->andWith('last_login IS NULL')
->end()
->orGroup()
->with('role = ?', 'banned')
->end();
$this->assertSql($statement, '(failed_logins > ? AND last_login IS NULL) OR (role = ?)');
$this->assertValues($statement, [5, 'banned']);
}
public function testGroupParent()
{
$this->expectException(RuntimeException::class);
EasyStatement::open()->endGroup();
}
private function assertSql(EasyStatement $statement, $expected)
{
$this->assertSame($expected, $statement->sql());
$this->assertSame($expected, (string) $statement);
}
private function assertValues(EasyStatement $statement, array $values)
{
$this->assertSame($values, $statement->values());
}
public function testEmpty()
{
$stmt = EasyStatement::open();
$this->assertSql($stmt, '1 = 1');
}
public function testPrecedence()
{
$sth1 = EasyStatement::open();
$sth1->with("a=1");
$sth1->orWith("a=2");
$sth1->orWith("a=3");
$sth2 = EasyStatement::open();
$sth2->with("status=1");
$sth2->andWith($sth1);
$this->assertSql($sth2, 'status=1 AND (a=1 OR a=2 OR a=3)');
}
}