Skip to content

Commit 8634624

Browse files
committed
#14 - Test InExpression (with TestHelper)
1 parent b295eed commit 8634624

File tree

4 files changed

+89
-22
lines changed

4 files changed

+89
-22
lines changed

composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
"TgDatabase\\" : "src/TgDatabase/"
2424
}
2525
},
26+
"autoload-dev" : {
27+
"psr-4" : {
28+
"TgDatabase\\" : "tests/TgDatabase/"
29+
}
30+
},
2631
"extra": {
2732
"branch-alias": {
2833
"dev-master": "1.0-dev"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\TestHelper;
10+
11+
/**
12+
* Tests the InExpression.
13+
* @author ralph
14+
*
15+
*/
16+
final class InExpressionTest extends TestCase {
17+
18+
protected ?Criteria $criteria = NULL;
19+
20+
public function testSimple(): void {
21+
$expr = Restrictions::in('aName', array('aValue1', 'aValue2'));
22+
$this->testSqlString('`aName` IN (\'aValue1\',\'aValue2\')', $expr);
23+
}
24+
25+
public function testWithIgnoreCase(): void {
26+
$expr = Restrictions::in('aName', array('aValue1', 'aValue2'))->ignoreCase();
27+
$this->testSqlString('LOWER(`aName`) IN (\'avalue1\',\'avalue2\')', $expr);
28+
}
29+
30+
protected function testSqlString(string $expected, Criterion $expr, $alias = NULL): void {
31+
$criteria = TestHelper::createCriteria(NULL, NULL, $alias);
32+
if ($criteria != NULL) {
33+
$this->assertEquals($expected, $expr->toSqlString($criteria,$criteria));
34+
}
35+
}
36+
}

tests/TgDatabase/Criterion/SimpleExpressionTest.php

+2-22
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
use TgDatabase\Restrictions;
77
use TgDatabase\Criterion;
88
use TgDatabase\Criteria;
9-
use TgDatabase\Impl\CriteriaImpl;
10-
use TgDatabase\Database;
9+
use TgDatabase\TestHelper;
1110

1211
/**
1312
* Tests the SimpleExpression.
@@ -16,8 +15,6 @@
1615
*/
1716
final class SimpleExpressionTest extends TestCase {
1817

19-
protected ?Criteria $criteria = NULL;
20-
2118
public function testSimple(): void {
2219
$expr = Restrictions::eq('aName', 'aValue');
2320
$this->testSqlString('`aName` = \'aValue\'', $expr);
@@ -39,27 +36,10 @@ public function testIgnoreCase(): void {
3936
}
4037

4138
protected function testSqlString(string $expected, Criterion $expr, $alias = NULL): void {
42-
$criteria = $this->getCriteria($alias);
39+
$criteria = TestHelper::createCriteria(NULL, NULL, $alias);
4340
if ($criteria != NULL) {
4441
$this->assertEquals($expected, $expr->toSqlString($criteria,$criteria));
4542
}
4643
}
4744

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-
}
6545
}

tests/TgDatabase/TestHelper.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace TgDatabase;
4+
5+
use TgDatabase\Criteria;
6+
use TgDatabase\Impl\CriteriaImpl;
7+
use TgDatabase\Database;
8+
9+
/**
10+
* Abstract test to ease database, DAO and Criteria creation.
11+
* @author ralph
12+
*
13+
*/
14+
class TestHelper {
15+
16+
protected static ?Database $database = NULL;
17+
18+
public static function getDatabase(): ?Database {
19+
if (self::hasTestDatabase()) {
20+
if (self::$database == NULL) {
21+
$config = array(
22+
'host' => getenv('DB_TEST_HOST'),
23+
'port' => intval(getenv('DB_TEST_PORT')),
24+
'dbname' => getenv('DB_TEST_NAME'),
25+
'tablePrefix' => getenv('DB_TEST_PREFIX'),
26+
'user' => getenv('DB_TEST_USER'),
27+
'pass' => getenv('DB_TEST_PASS'),
28+
);
29+
self::$database = new Database($config);
30+
}
31+
}
32+
return self::$database;
33+
}
34+
35+
public static function createCriteria($tableName = 'dual', $modelClass = NULL, $alias = NULL): ?Criteria {
36+
if (self::hasTestDatabase()) {
37+
return new CriteriaImpl(self::getDatabase(), $tableName, $modelClass, $alias);
38+
}
39+
return NULL;
40+
}
41+
42+
public static function hasTestDatabase(): bool {
43+
return getenv('DB_TEST_HOST') != NULL;
44+
}
45+
}
46+

0 commit comments

Comments
 (0)