Skip to content

Commit 2fc077e

Browse files
committed
moving to phpunit
1 parent b379683 commit 2fc077e

File tree

10 files changed

+126
-453
lines changed

10 files changed

+126
-453
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ all:
22
@echo "Future build step will go here";
33

44
test:
5-
@prove --exec 'php' tests/*_*.php
5+
@phpunit --bootstrap src/SQLParser.php --testdox tests

tests/01_lex.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/02_collapse.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/04_table_props.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

tests/10_full.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/CollapseTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
use PHPUnit\Framework\TestCase;
3+
4+
final class CollapseTest extends TestCase{
5+
6+
function collapse_test($in, $out){
7+
$obj = new iamcal\SQLParser();
8+
$this->assertEquals($obj->lex($in), $out);
9+
}
10+
11+
function testCollapsing(){
12+
13+
$this->collapse_test('a b', array('a', 'b'));
14+
$this->collapse_test('UNIQUE key', array('UNIQUE KEY'));
15+
}
16+
}

tests/FullTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
use PHPUnit\Framework\TestCase;
3+
4+
final class FullTest extends TestCase{
5+
6+
function full_test($sql, $expected){
7+
8+
$obj = new iamcal\SQLParser();
9+
$obj->parse($sql);
10+
11+
$lines = array();
12+
foreach ($obj->tables as $table){
13+
$lines[] = "TABLE:{$table['name']}";
14+
$lines[] = "SQL:{$table['sql']}";
15+
foreach ($table['fields'] as $field){
16+
$lines[] = "-FIELD:{$field['name']}:{$field['type']}";
17+
}
18+
}
19+
20+
$this->assertEquals($lines, $expected);
21+
}
22+
23+
24+
function testBasicCases(){
25+
26+
$this->full_test("CREATE TABLE table_name (a INT);\n" .
27+
"-- ignored comment\n\n" .
28+
"CREATE TABLE t2 (b VARCHAR)\n\n;\n",
29+
array(
30+
"TABLE:table_name",
31+
"SQL:CREATE TABLE table_name (a INT);",
32+
"-FIELD:a:INT",
33+
"TABLE:t2",
34+
"SQL:CREATE TABLE t2 (b VARCHAR)\n\n;",
35+
"-FIELD:b:VARCHAR",
36+
));
37+
}
38+
}

tests/LexTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
use PHPUnit\Framework\TestCase;
3+
4+
final class LexTest extends TestCase{
5+
6+
private function lex_test($str, $tokens){
7+
$obj = new iamcal\SQLParser();
8+
9+
$this->assertEquals($obj->lex($str), $tokens);
10+
}
11+
12+
public function test_simple_word_tokens(){
13+
14+
$this->lex_test('hello world', array('hello', 'world'));
15+
}
16+
17+
public function test_strip_comments_whitespace(){
18+
19+
$this->lex_test("hello \nworld-- foo\nyeah", array('hello', 'world', 'yeah'));
20+
}
21+
}

tests/TablePropsTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
use PHPUnit\Framework\TestCase;
3+
4+
final class TablePropsTest extends TestCase{
5+
6+
function table_props_test($tokens, $props_expect){
7+
8+
$obj = new iamcal\SQLParser();
9+
$i = 0;
10+
$props = $obj->parse_table_props($tokens, $i);
11+
12+
$this->assertEquals($props, $props_expect);
13+
}
14+
15+
16+
function testEqualsIsOptional(){
17+
18+
# the equals is optional
19+
20+
$this->table_props_test(array('ENGINE', '=', 'INNODB'), array('ENGINE' => 'INNODB'));
21+
$this->table_props_test(array('ENGINE', 'INNODB'), array('ENGINE' => 'INNODB'));
22+
}
23+
24+
function testDefaultCharset(){
25+
26+
# lots of ways to say this
27+
28+
$this->table_props_test(array('DEFAULT CHARACTER SET', 'foo'), array('CHARSET' => 'foo'));
29+
$this->table_props_test(array('CHARACTER SET', 'foo'), array('CHARSET' => 'foo'));
30+
$this->table_props_test(array('DEFAULT CHARSET', 'foo'), array('CHARSET' => 'foo'));
31+
$this->table_props_test(array('CHARSET', 'foo'), array('CHARSET' => 'foo'));
32+
}
33+
34+
function testDefaultCollation(){
35+
36+
$this->table_props_test(array('DEFAULT COLLATE', 'bar'), array('COLLATE' => 'bar'));
37+
$this->table_props_test(array('COLLATE', 'bar'), array('COLLATE' => 'bar'));
38+
}
39+
40+
function testTwoWordProps(){
41+
42+
# more two-word props
43+
44+
$this->table_props_test(array('DATA DIRECTORY', '=', 'baz'), array('DATA DIRECTORY' => 'baz'));
45+
$this->table_props_test(array('INDEX DIRECTORY', '=', 'baz'), array('INDEX DIRECTORY' => 'baz'));
46+
}
47+
48+
49+
# TODO: case conversion, multiple options, optional commas
50+
}

0 commit comments

Comments
 (0)