Skip to content

Commit b295eed

Browse files
committed
#14 - Unit Test for SimpleExpression
1 parent 4cd444e commit b295eed

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ composer.phar
66
# composer.lock
77
/composer.lock
88
/nbproject/private/
9+
/set-local-test-env.sh

set-test-env.sh

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
#######################################################
4+
#
5+
# TEST HOWTO
6+
#
7+
# Copy this file to e.g. set-local-test-env.sh and
8+
# set the various variables below. Then source the
9+
# file on you bash shell:
10+
#
11+
# ~/git/php-database$ . ./set-local-test-env.sh
12+
#
13+
# and start the PHP Unit tests:
14+
#
15+
# ~/git/php-database$ ./vendor/bin/phpunit tests
16+
#
17+
#######################################################
18+
19+
# The Database connect information
20+
DB_TEST_HOST=www.example.com
21+
DB_TEST_PORT=3306
22+
DB_TEST_NAME=test
23+
DB_TEST_PREFIX=test
24+
DB_TEST_USER=username
25+
DB_TEST_PASS=password
26+
27+
export DB_TEST_HOST
28+
export DB_TEST_PORT
29+
export DB_TEST_NAME
30+
export DB_TEST_PREFIX
31+
export DB_TEST_USER
32+
export DB_TEST_PASS
33+

test.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Test script
22

33
composer update
4-
RC=./vendor/phpunit/phpunit/phpunit tests
5-
rm -rf vendor composer.lock
4+
./vendor/phpunit/phpunit/phpunit tests
5+
RC=$?
66
exit $RC
77

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)