diff options
Diffstat (limited to 'tests/Components')
-rw-r--r-- | tests/Components/Array2DTest.php | 27 | ||||
-rw-r--r-- | tests/Components/ArrayObjTest.php | 40 | ||||
-rw-r--r-- | tests/Components/ConditionTest.php | 26 | ||||
-rw-r--r-- | tests/Components/ExpressionTest.php | 40 | ||||
-rw-r--r-- | tests/Components/FragmentTest.php | 20 | ||||
-rw-r--r-- | tests/Components/FunctionCallTest.php | 24 | ||||
-rw-r--r-- | tests/Components/IntoKeywordTest.php | 25 | ||||
-rw-r--r-- | tests/Components/LimitKeywordTest.php | 40 | ||||
-rw-r--r-- | tests/Components/OptionsArrayTest.php | 65 | ||||
-rw-r--r-- | tests/Components/ReferenceTest.php | 25 |
10 files changed, 332 insertions, 0 deletions
diff --git a/tests/Components/Array2DTest.php b/tests/Components/Array2DTest.php new file mode 100644 index 0000000..bb646df --- /dev/null +++ b/tests/Components/Array2DTest.php @@ -0,0 +1,27 @@ +<?php + +namespace SqlParser\Tests\Components; + +use SqlParser\Parser; +use SqlParser\Components\Array2d; + +use SqlParser\Tests\TestCase; + +class Array2dTest extends TestCase +{ + + public function testParseErr1() + { + $parser = new Parser(); + Array2d::parse($parser, $this->getTokensList('(1, 2 +')); + // TODO: Assert errors. + } + + public function testParseErr2() + { + $parser = new Parser(); + Array2d::parse($parser, $this->getTokensList('(1, 2 TABLE')); + // TODO: Assert errors. + } + +} diff --git a/tests/Components/ArrayObjTest.php b/tests/Components/ArrayObjTest.php new file mode 100644 index 0000000..7694dfb --- /dev/null +++ b/tests/Components/ArrayObjTest.php @@ -0,0 +1,40 @@ +<?php + +namespace SqlParser\Tests\Components; + +use SqlParser\Components\ArrayObj; + +use SqlParser\Tests\TestCase; + +class ArrayObjTest extends TestCase +{ + + public function testBuildRaw() + { + $component = new ArrayObj(array('a', 'b'), array()); + $this->assertEquals('(a, b)', ArrayObj::build($component)); + } + + public function testBuildValues() + { + $component = new ArrayObj(array(), array('a', 'b')); + $this->assertEquals('(a, b)', ArrayObj::build($component)); + } + + /** + * @dataProvider testParseProvider + */ + public function testParse($test) + { + $this->runParserTest($test); + } + + public function testParseProvider() + { + return array( + array('parseArrayErr1'), + array('parseArrayErr2'), + array('parseArrayErr3'), + ); + } +} diff --git a/tests/Components/ConditionTest.php b/tests/Components/ConditionTest.php new file mode 100644 index 0000000..b3279fe --- /dev/null +++ b/tests/Components/ConditionTest.php @@ -0,0 +1,26 @@ +<?php + +namespace SqlParser\Tests\Components; + +use SqlParser\Parser; +use SqlParser\Components\Condition; + +use SqlParser\Tests\TestCase; + +class ConditionTest extends TestCase +{ + + public function testParse() + { + $component = Condition::parse(new Parser(), $this->getTokensList('/* id = */ id = 10')); + $this->assertEquals($component[0]->expr, 'id = 10'); + } + + public function testParseBetween() + { + $component = Condition::parse(new Parser(), $this->getTokensList('(id BETWEEN 10 AND 20) OR (id BETWEEN 30 AND 40)')); + $this->assertEquals($component[0]->expr, '(id BETWEEN 10 AND 20)'); + $this->assertEquals($component[1]->expr, 'OR'); + $this->assertEquals($component[2]->expr, '(id BETWEEN 30 AND 40)'); + } +} diff --git a/tests/Components/ExpressionTest.php b/tests/Components/ExpressionTest.php new file mode 100644 index 0000000..ecf5149 --- /dev/null +++ b/tests/Components/ExpressionTest.php @@ -0,0 +1,40 @@ +<?php + +namespace SqlParser\Tests\Components; + +use SqlParser\Parser; +use SqlParser\Components\Expression; + +use SqlParser\Tests\TestCase; + +class ExpressionTest extends TestCase +{ + + public function testParse() + { + $component = Expression::parse(new Parser(), $this->getTokensList('IF(film_id > 0, film_id, film_id)')); + $this->assertEquals($component->expr, 'IF(film_id > 0, film_id, film_id)'); + } + + public function testParseErr1() + { + $parser = new Parser(); + Expression::parse($parser, $this->getTokensList('(1))')); + $errors = $this->getErrorsAsArray($parser); + $this->assertEquals($errors[0][0], 'Unexpected bracket.'); + } + + public function testParseErr2() + { + $parser = new Parser(); + Expression::parse($parser, $this->getTokensList('tbl..col')); + $errors = $this->getErrorsAsArray($parser); + $this->assertEquals($errors[0][0], 'Unexpected dot.'); + } + + public function testBuild() + { + $component = new Expression('1 + 2', 'three'); + $this->assertEquals(Expression::build($component), '1 + 2 AS `three`'); + } +} diff --git a/tests/Components/FragmentTest.php b/tests/Components/FragmentTest.php new file mode 100644 index 0000000..64cc8b1 --- /dev/null +++ b/tests/Components/FragmentTest.php @@ -0,0 +1,20 @@ +<?php + +namespace SqlParser\Tests\Parser; + +use SqlParser\Component; +use SqlParser\Parser; +use SqlParser\TokensList; +use SqlParser\Components\ArrayObj; + +use SqlParser\Tests\TestCase; + +class ComponentTest extends TestCase +{ + + public function testDummy() + { + $this->assertEquals(null, Component::parse(new Parser(), new TokensList())); + $this->assertEquals(null, Component::build(new ArrayObj())); + } +} diff --git a/tests/Components/FunctionCallTest.php b/tests/Components/FunctionCallTest.php new file mode 100644 index 0000000..b7f71ca --- /dev/null +++ b/tests/Components/FunctionCallTest.php @@ -0,0 +1,24 @@ +<?php + +namespace SqlParser\Tests\Components; + +use SqlParser\Components\ArrayObj; +use SqlParser\Components\FunctionCall; + +use SqlParser\Tests\TestCase; + +class FunctionCallTest extends TestCase +{ + + public function testBuildArray() + { + $component = new FunctionCall('func', array('a', 'b')); + $this->assertEquals('func(a, b)', FunctionCall::build($component)); + } + + public function testBuildArrayObj() + { + $component = new FunctionCall('func', new ArrayObj(array('a', 'b'))); + $this->assertEquals('func(a, b)', FunctionCall::build($component)); + } +} diff --git a/tests/Components/IntoKeywordTest.php b/tests/Components/IntoKeywordTest.php new file mode 100644 index 0000000..ee30f09 --- /dev/null +++ b/tests/Components/IntoKeywordTest.php @@ -0,0 +1,25 @@ +<?php + +namespace SqlParser\Tests\Components; + +use SqlParser\Parser; +use SqlParser\Components\IntoKeyword; + +use SqlParser\Tests\TestCase; + +class IntoKeywordTest extends TestCase +{ + + public function testParse() + { + $component = IntoKeyword::parse(new Parser(), $this->getTokensList('OUTFILE "/tmp/outfile.txt"')); + $this->assertEquals($component->type, 'OUTFILE'); + $this->assertEquals($component->dest, '/tmp/outfile.txt'); + } + + public function testParseErr1() + { + $component = IntoKeyword::parse(new Parser(), $this->getTokensList('OUTFILE;')); + $this->assertEquals($component->type, 'OUTFILE'); + } +} diff --git a/tests/Components/LimitKeywordTest.php b/tests/Components/LimitKeywordTest.php new file mode 100644 index 0000000..ce9a760 --- /dev/null +++ b/tests/Components/LimitKeywordTest.php @@ -0,0 +1,40 @@ +<?php + +namespace SqlParser\Tests\Components; + +use SqlParser\Parser; +use SqlParser\Components\Limit; + +use SqlParser\Tests\TestCase; + +class LimitTest extends TestCase +{ + + public function testBuild() + { + $component = new Limit(1); + $this->assertEquals(Limit::build($component), '1'); + } + + public function testBuildWithOffset() + { + $component = new Limit(1, 2); + $this->assertEquals(Limit::build($component), '2, 1'); + } + + /** + * @dataProvider testParseProvider + */ + public function testParse($test) + { + $this->runParserTest($test); + } + + public function testParseProvider() + { + return array( + array('parseLimitErr1'), + array('parseLimitErr2'), + ); + } +} diff --git a/tests/Components/OptionsArrayTest.php b/tests/Components/OptionsArrayTest.php new file mode 100644 index 0000000..0c453da --- /dev/null +++ b/tests/Components/OptionsArrayTest.php @@ -0,0 +1,65 @@ +<?php + +namespace SqlParser\Tests\Components; + +use SqlParser\Parser; +use SqlParser\Components\OptionsArray; + +use SqlParser\Tests\TestCase; + +class OptionsArrayTest extends TestCase +{ + + public function testParse() + { + $component = OptionsArray::parse( + new Parser(), + $this->getTokensList('A B = (test) C'), + array( + 'A' => 1, + 'B' => array(2, 'var'), + 'C' => 3, + ) + ); + $this->assertEquals( + array( + 1 => 'A', + 2 => array( + 'name' => 'B', + 'value' => 'test', + 'value_' => 'test', + 'equal' => false, + ), + 3 => 'C', + ), + $component->options + ); + } + + public function testMerge() + { + $component = new OptionsArray(array('a')); + $component->merge(array('b', 'c')); + $this->assertEquals($component->options, array('a', 'b', 'c')); + } + + public function testBuild() + { + $component = new OptionsArray( + array( + 'ALL', + 'SQL_CALC_FOUND_ROWS', + array( + 'name' => 'MAX_STATEMENT_TIME', + 'value' => '42', + 'value_' => '42', + 'equal' => true, + ), + ) + ); + $this->assertEquals( + OptionsArray::build($component), + 'ALL SQL_CALC_FOUND_ROWS MAX_STATEMENT_TIME=42' + ); + } +} diff --git a/tests/Components/ReferenceTest.php b/tests/Components/ReferenceTest.php new file mode 100644 index 0000000..f223fb8 --- /dev/null +++ b/tests/Components/ReferenceTest.php @@ -0,0 +1,25 @@ +<?php + +namespace SqlParser\Tests\Components; + +use SqlParser\Parser; +use SqlParser\Components\Reference; + +use SqlParser\Tests\TestCase; + +class ReferenceTest extends TestCase +{ + + public function testParse() + { + $component = Reference::parse(new Parser(), $this->getTokensList('tbl (id)')); + $this->assertEquals('tbl', $component->table); + $this->assertEquals(array('id'), $component->columns); + } + + public function testBuild() + { + $component = new Reference('tbl', array('id')); + $this->assertEquals('`tbl` (`id`)', Reference::build($component)); + } +} |