1
+ <?php declare (strict_types=1 );
2
+
3
+ namespace TgDatabase \Criterion ;
4
+
5
+ use PHPUnit \Framework \TestCase ;
6
+ use TgDatabase \Restrictions ;
7
+ use TgDatabase \Criterion ;
8
+ use TgDatabase \Criteria ;
9
+ use TgDatabase \Impl \CriteriaImpl ;
10
+ use TgDatabase \Database ;
11
+
12
+ /**
13
+ * Tests the SimpleExpression.
14
+ * @author ralph
15
+ *
16
+ */
17
+ final class SimpleExpressionTest extends TestCase {
18
+
19
+ protected ?Criteria $ criteria = NULL ;
20
+
21
+ public function testSimple (): void {
22
+ $ expr = Restrictions::eq ('aName ' , 'aValue ' );
23
+ $ this ->testSqlString ('`aName` = \'aValue \'' , $ expr );
24
+ }
25
+
26
+ public function testSimpleWithExplicitAlias (): void {
27
+ $ expr = Restrictions::eq (array ('a ' , 'aName ' ), 'aValue ' );
28
+ $ this ->testSqlString ('`a`.`aName` = \'aValue \'' , $ expr );
29
+ }
30
+
31
+ public function testSimpleWithImplicitAlias (): void {
32
+ $ expr = Restrictions::eq ('aName ' , 'aValue ' );
33
+ $ this ->testSqlString ('`a`.`aName` = \'aValue \'' , $ expr , 'a ' );
34
+ }
35
+
36
+ public function testIgnoreCase (): void {
37
+ $ expr = Restrictions::eq ('aName ' , 'aValue ' )->ignoreCase ();
38
+ $ this ->testSqlString ('LOWER(`aName`) = \'avalue \'' , $ expr );
39
+ }
40
+
41
+ protected function testSqlString (string $ expected , Criterion $ expr , $ alias = NULL ): void {
42
+ $ criteria = $ this ->getCriteria ($ alias );
43
+ if ($ criteria != NULL ) {
44
+ $ this ->assertEquals ($ expected , $ expr ->toSqlString ($ criteria ,$ criteria ));
45
+ }
46
+ }
47
+
48
+ protected function getCriteria ($ alias ): ?Criteria {
49
+ if (getenv ('DB_TEST_HOST ' ) != NULL ) {
50
+ if ($ this ->criteria == NULL ) {
51
+ $ config = array (
52
+ 'host ' => getenv ('DB_TEST_HOST ' ),
53
+ 'port ' => intval (getenv ('DB_TEST_PORT ' )),
54
+ 'dbname ' => getenv ('DB_TEST_NAME ' ),
55
+ 'tablePrefix ' => getenv ('DB_TEST_PREFIX ' ),
56
+ 'user ' => getenv ('DB_TEST_USER ' ),
57
+ 'pass ' => getenv ('DB_TEST_PASS ' ),
58
+ );
59
+ $ this ->database = new Database ($ config );
60
+ $ this ->criteria = new CriteriaImpl ($ this ->database , 'dual ' , NULL , $ alias );
61
+ }
62
+ }
63
+ return $ this ->criteria ;
64
+ }
65
+ }
0 commit comments