Добавлен цикл проверки сайтов

This commit is contained in:
2024-08-12 15:14:49 +03:00
parent 0b56cd37b5
commit 6b7199a326
115 changed files with 15964 additions and 88 deletions

View File

@ -0,0 +1,284 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/16/14
* Time: 8:56 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Builder;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
class GenericBuilderTest extends \PHPUnit_Framework_TestCase
{
/**
* @var GenericBuilder
*/
private $writer;
/**
*
*/
public function setUp()
{
$this->writer = new GenericBuilder();
}
/**
* @test
*/
public function itShouldCreateSelectObject()
{
$className = '\NilPortugues\Sql\QueryBuilder\Manipulation\Select';
$this->assertInstanceOf($className, $this->writer->select());
}
/**
* @test
*/
public function itShouldCreateInsertObject()
{
$className = '\NilPortugues\Sql\QueryBuilder\Manipulation\Insert';
$this->assertInstanceOf($className, $this->writer->insert());
}
/**
* @test
*/
public function itShouldCreateUpdateObject()
{
$className = '\NilPortugues\Sql\QueryBuilder\Manipulation\Update';
$this->assertInstanceOf($className, $this->writer->update());
}
/**
* @test
*/
public function itShouldCreateDeleteObject()
{
$className = '\NilPortugues\Sql\QueryBuilder\Manipulation\Delete';
$this->assertInstanceOf($className, $this->writer->delete());
}
/**
* @test
*/
public function itShouldCreateIntersectObject()
{
$className = '\NilPortugues\Sql\QueryBuilder\Manipulation\Intersect';
$this->assertInstanceOf($className, $this->writer->intersect());
}
/**
* @test
*/
public function itShouldCreateMinusObject()
{
$className = '\NilPortugues\Sql\QueryBuilder\Manipulation\Minus';
$this->assertInstanceOf($className, $this->writer->minus(new Select('table1'), new Select('table2')));
}
/**
* @test
*/
public function itShouldCreateUnionObject()
{
$className = '\NilPortugues\Sql\QueryBuilder\Manipulation\Union';
$this->assertInstanceOf($className, $this->writer->union());
}
/**
* @test
*/
public function itShouldCreateUnionAllObject()
{
$className = '\NilPortugues\Sql\QueryBuilder\Manipulation\UnionAll';
$this->assertInstanceOf($className, $this->writer->unionAll());
}
/**
* @test
*/
public function itCanAcceptATableNameForSelectInsertUpdateDeleteQueries()
{
$table = 'user';
$queries = [
'select' => $this->writer->select($table),
'insert' => $this->writer->insert($table),
'update' => $this->writer->update($table),
'delete' => $this->writer->delete($table),
];
foreach ($queries as $type => $query) {
$this->assertEquals($table, $query->getTable()->getName(), "Checking table in $type query");
}
}
/**
* @test
*/
public function itCanAcceptATableAndColumnsForSelect()
{
$table = 'user';
$columns = ['id', 'role'];
$expected = <<<QUERY
SELECT
user.id,
user.role
FROM
user
QUERY;
$select = $this->writer->select($table, $columns);
$this->assertSame($expected, $this->writer->writeFormatted($select));
}
/**
* @test
*/
public function itCanAcceptATableAndValuesForInsert()
{
$table = 'user';
$values = ['id' => 1, 'role' => 'admin'];
$expected = <<<QUERY
INSERT INTO user (user.id, user.role)
VALUES
(:v1, :v2)
QUERY;
$insert = $this->writer->insert($table, $values);
$this->assertSame($expected, $this->writer->writeFormatted($insert));
}
/**
* @test
*/
public function itCanAcceptATableAndValuesForUpdate()
{
$table = 'user';
$values = ['id' => 1, 'role' => 'super-admin'];
$expected = <<<QUERY
UPDATE
user
SET
user.id = :v1,
user.role = :v2
QUERY;
$update = $this->writer->update($table, $values);
$this->assertSame($expected, $this->writer->writeFormatted($update));
}
/**
* @test
*/
public function itShouldOutputHumanReadableQuery()
{
$selectRole = $this->writer->select();
$selectRole
->setTable('role')
->setColumns(array('role_name'))
->limit(1)
->where()
->equals('role_id', 3);
$select = $this->writer->select();
$select->setTable('user')
->setColumns(array('user_id', 'username'))
->setSelectAsColumn(array('user_role' => $selectRole))
->setSelectAsColumn(array($selectRole))
->where()
->equals('user_id', 4);
$expected = <<<QUERY
SELECT
user.user_id,
user.username,
(
SELECT
role.role_name
FROM
role
WHERE
(role.role_id = :v1)
LIMIT
:v2,
:v3
) AS "user_role",
(
SELECT
role.role_name
FROM
role
WHERE
(role.role_id = :v4)
LIMIT
:v5,
:v6
) AS "role"
FROM
user
WHERE
(user.user_id = :v7)
QUERY;
$this->assertSame($expected, $this->writer->writeFormatted($select));
}
/**
* @test
*/
public function it_should_inject_the_builder()
{
$query = $this->writer->select();
$this->assertSame($this->writer, $query->getBuilder());
}
/**
* @test
*/
public function itShouldWriteWhenGettingSql()
{
$query = $this->writer->select()
->setTable('user');
$expected = $this->writer->write($query);
$this->assertSame($expected, $query->getSql());
}
/**
* @test
*/
public function itShouldWriteFormattedWhenGettingFormattedSql()
{
$query = $this->writer->select()
->setTable('user');
$formatted = true;
$expected = $this->writer->writeFormatted($query);
$this->assertSame($expected, $query->getSql($formatted));
}
/**
* @test
*/
public function itShouldWriteSqlWhenCastToString()
{
$query = $this->writer->select()
->setTable('user');
$expected = $this->writer->write($query);
$this->assertSame($expected, (string) $query);
}
}

View File

@ -0,0 +1,74 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/4/14
* Time: 12:40 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Builder;
use NilPortugues\Sql\QueryBuilder\Builder\MySqlBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
/**
* Class MySqlBuilderTest.
*/
class MySqlBuilderTest extends \PHPUnit_Framework_TestCase
{
/**
* @var MySqlBuilder
*/
protected $writer;
/**
*
*/
protected function setUp()
{
$this->writer = new MySqlBuilder();
}
/**
*
*/
protected function tearDown()
{
$this->writer = null;
}
/**
* @test
*/
public function itShouldWrapTableNames()
{
$query = new Select('user');
$expected = 'SELECT `user`.* FROM `user`';
$this->assertSame($expected, $this->writer->write($query));
}
/**
* @test
*/
public function itShouldWrapColumnNames()
{
$query = new Select('user', array('user_id', 'name'));
$expected = 'SELECT `user`.`user_id`, `user`.`name` FROM `user`';
$this->assertSame($expected, $this->writer->write($query));
}
/**
* @test
*/
public function itShouldWrapColumnAlias()
{
$query = new Select('user', array('userId' => 'user_id', 'name' => 'name'));
$expected = 'SELECT `user`.`user_id` AS `userId`, `user`.`name` AS `name` FROM `user`';
$this->assertSame($expected, $this->writer->write($query));
}
}

View File

@ -0,0 +1,187 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 10:45 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Builder\Syntax\ColumnWriter;
use NilPortugues\Sql\QueryBuilder\Builder\Syntax\PlaceholderWriter;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
use NilPortugues\Sql\QueryBuilder\Syntax\Column;
/**
* Class ColumnWriterTest.
*/
class ColumnWriterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ColumnWriter
*/
private $columnWriter;
/**
* @var GenericBuilder
*/
private $writer;
/**
* @var Select
*/
private $query;
/**
*
*/
protected function setUp()
{
$this->writer = new GenericBuilder();
$this->query = new Select();
$this->columnWriter = new ColumnWriter(new GenericBuilder(), new PlaceholderWriter());
}
/**
* @test
*/
public function itShouldWriteColumn()
{
$column = new Column('user_id', 'user');
$result = $this->columnWriter->writeColumn($column);
$this->assertSame('user.user_id', $result);
}
/**
* @test
*/
public function itShouldWriteValueAsColumns()
{
$select = new Select('user');
$select->setValueAsColumn('1', 'user_id');
$result = $this->columnWriter->writeValueAsColumns($select);
$this->assertInstanceOf('NilPortugues\Sql\QueryBuilder\Syntax\Column', $result[0]);
}
/**
* @test
*/
public function itShouldWriteFuncAsColumns()
{
$select = new Select('user');
$select->setFunctionAsColumn('MAX', ['user_id'], 'max_value');
$result = $this->columnWriter->writeFuncAsColumns($select);
$this->assertInstanceOf('NilPortugues\Sql\QueryBuilder\Syntax\Column', $result[0]);
}
/**
* @test
*/
public function itShouldWriteColumnWithAlias()
{
$column = new Column('user_id', 'user', 'userId');
$result = $this->columnWriter->writeColumnWithAlias($column);
$this->assertSame('user.user_id AS "userId"', $result);
}
/**
* @test
*/
public function itShouldBeAbleToWriteColumnAsASelectStatement()
{
$selectRole = new Select();
$selectRole
->setTable('role')
->setColumns(array('role_name'))
->limit(1)
->where()
->equals('role_id', 3);
$this->query
->setTable('user')
->setColumns(array('user_id', 'username'))
->setSelectAsColumn(array('user_role' => $selectRole))
->setSelectAsColumn(array($selectRole))
->where()
->equals('user_id', 4);
$expected = 'SELECT user.user_id, user.username, '.
'(SELECT role.role_name FROM role WHERE (role.role_id = :v1) LIMIT :v2, :v3) AS "user_role", '.
'(SELECT role.role_name FROM role WHERE (role.role_id = :v4) LIMIT :v5, :v6) AS "role" '.
'FROM user WHERE (user.user_id = :v7)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 3, ':v2' => 1, ':v3' => 0, ':v4' => 3, ':v5' => 1, ':v6' => 0, ':v7' => 4);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToWriteColumnAsAValueStatement()
{
$this->query
->setTable('user')
->setColumns(array('user_id', 'username'))
->setValueAsColumn('10', 'priority')
->where()
->equals('user_id', 1);
$expected = 'SELECT user.user_id, user.username, :v1 AS "priority" FROM user WHERE (user.user_id = :v2)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 10, ':v2' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToWriteColumnAsAFuncWithBracketsStatement()
{
$this->query
->setTable('user')
->setColumns(array('user_id', 'username'))
->setFunctionAsColumn('MAX', array('user_id'), 'max_id')
->where()
->equals('user_id', 1);
$expected = 'SELECT user.user_id, user.username, MAX(user_id) AS "max_id" FROM user WHERE (user.user_id = :v1)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToWriteColumnAsAFuncWithoutBracketsStatement()
{
$this->query
->setTable('user')
->setColumns(array('user_id', 'username'))
->setFunctionAsColumn('CURRENT_TIMESTAMP', array(), 'server_time')
->where()
->equals('user_id', 1);
$expected = 'SELECT user.user_id, user.username, CURRENT_TIMESTAMP AS "server_time" FROM user WHERE (user.user_id = :v1)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
}

View File

@ -0,0 +1,106 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 10:45 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\Delete;
/**
* Class DeleteWriterTest.
*/
class DeleteWriterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var GenericBuilder
*/
private $writer;
/**
* @var Delete
*/
private $query;
/**
*
*/
protected function setUp()
{
$this->writer = new GenericBuilder();
$this->query = new Delete();
}
/**
* @test
*/
public function itShouldWriteDeleteAllTableContentsQuery()
{
$this->query->setTable('user');
$expected = 'DELETE FROM user';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldWriteDeleteRowLimit1()
{
$this->query
->setTable('user')
->limit(1);
$expected = 'DELETE FROM user LIMIT :v1';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToWriteCommentInQuery()
{
$this->query
->setTable('user')
->setComment('This is a comment');
$expected = <<<SQL
-- This is a comment
DELETE FROM user
SQL;
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldWriteDeleteRowWithWhereConditionAndLimit1()
{
$this->query->setTable('user');
$conditions = $this->query->where();
$conditions
->equals('user_id', 10)
->equals('user_id', 20)
->equals('user_id', 30);
$this->query->limit(1);
$expected = <<<SQL
DELETE FROM user WHERE (user.user_id = :v1) AND (user.user_id = :v2) AND (user.user_id = :v3) LIMIT :v4
SQL;
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 10, ':v2' => 20, ':v3' => 30, ':v4' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
}

View File

@ -0,0 +1,104 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 10:45 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\Insert;
/**
* Class InsertWriterTest.
*/
class InsertWriterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var GenericBuilder
*/
private $writer;
/**
* @var Insert
*/
private $query;
/**
* @var string
*/
private $exceptionClass = '\NilPortugues\Sql\QueryBuilder\Manipulation\QueryException';
/**
*
*/
protected function setUp()
{
$this->writer = new GenericBuilder();
$this->query = new Insert();
}
/**
* @test
*/
public function itShouldThrowQueryExceptionBecauseNoColumnsWereDefined()
{
$this->setExpectedException($this->exceptionClass, 'No columns were defined for the current schema.');
$this->query->setTable('user');
$this->writer->write($this->query);
}
/**
* @test
*/
public function itShouldWriteInsertQuery()
{
$valueArray = array(
'user_id' => 1,
'name' => 'Nil',
'contact' => 'contact@nilportugues.com',
);
$this->query
->setTable('user')
->setValues($valueArray);
$expected = 'INSERT INTO user (user.user_id, user.name, user.contact) VALUES (:v1, :v2, :v3)';
$this->assertSame($expected, $this->writer->write($this->query));
$this->assertEquals(\array_values($valueArray), \array_values($this->query->getValues()));
$expected = array(':v1' => 1, ':v2' => 'Nil', ':v3' => 'contact@nilportugues.com');
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToWriteCommentInQuery()
{
$valueArray = array(
'user_id' => 1,
'name' => 'Nil',
'contact' => 'contact@nilportugues.com',
);
$this->query
->setTable('user')
->setComment('This is a comment')
->setValues($valueArray);
$expected = "-- This is a comment\n".'INSERT INTO user (user.user_id, user.name, user.contact) VALUES (:v1, :v2, :v3)';
$this->assertSame($expected, $this->writer->write($this->query));
$this->assertEquals(\array_values($valueArray), \array_values($this->query->getValues()));
$expected = array(':v1' => 1, ':v2' => 'Nil', ':v3' => 'contact@nilportugues.com');
$this->assertEquals($expected, $this->writer->getValues());
}
}

View File

@ -0,0 +1,118 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 7:34 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Builder\Syntax\IntersectWriter;
use NilPortugues\Sql\QueryBuilder\Manipulation\Intersect;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
/**
* Class IntersectWriterTest.
*/
class IntersectWriterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var GenericBuilder
*/
private $writer;
/**
* @var IntersectWriter
*/
private $intersectWriter;
/**
*
*/
public function setUp()
{
$this->intersectWriter = new IntersectWriter(new GenericBuilder());
$this->writer = new GenericBuilder();
}
public function tearDown()
{
$this->intersectWriter = null;
$this->writer = null;
}
/**
* @test
*/
public function itShouldWriteIntersect()
{
$intersect = new Intersect();
$intersect->add(new Select('user'));
$intersect->add(new Select('user_email'));
$expected = <<<SQL
SELECT user.* FROM user
INTERSECT
SELECT user_email.* FROM user_email
SQL;
$this->assertEquals($expected, $this->intersectWriter->write($intersect));
}
/**
* @test
*/
public function itShouldWriteIntersectFromGenericBuilder()
{
$intersect = $this->writer->intersect();
$intersect->add(new Select('user'));
$intersect->add(new Select('user_email'));
$expected = <<<SQL
SELECT user.* FROM user
INTERSECT
SELECT user_email.* FROM user_email
SQL;
$this->assertEquals($expected, $this->writer->write($intersect));
}
/**
* @test
*/
public function itShouldNotResetPlaceholders()
{
$select1 = (new Select('table1'))
->where()
->between('column', 1, 2)
->end();
$select2 = (new Select('table2'))
->where()
->between('column', 3, 4)
->end();
$union = (new Intersect())
->add($select1)
->add($select2);
$expectedSql = <<<SQL
SELECT table1.* FROM table1 WHERE (table1.column BETWEEN :v1 AND :v2)
INTERSECT
SELECT table2.* FROM table2 WHERE (table2.column BETWEEN :v3 AND :v4)
SQL;
$expectedParams = [
':v1' => 1,
':v2' => 2,
':v3' => 3,
':v4' => 4,
];
$this->assertEquals($expectedSql, $this->writer->write($union));
$this->assertEquals($expectedParams, $this->writer->getValues());
}
}

View File

@ -0,0 +1,77 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 7:34 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Builder\Syntax\MinusWriter;
use NilPortugues\Sql\QueryBuilder\Manipulation\Minus;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
/**
* Class MinusWriterTest.
*/
class MinusWriterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var MinusWriter
*/
private $minusWriter;
/**
* @var GenericBuilder
*/
private $writer;
/**
*
*/
public function setUp()
{
$this->minusWriter = new MinusWriter(new GenericBuilder());
$this->writer = new GenericBuilder();
}
public function tearDown()
{
$this->minusWriter = null;
$this->writer = null;
}
/**
* @test
*/
public function itShouldWriteMinus()
{
$minus = new Minus(new Select('user'), new Select('user_email'));
$expected = <<<SQL
SELECT user.* FROM user
MINUS
SELECT user_email.* FROM user_email
SQL;
$this->assertEquals($expected, $this->minusWriter->write($minus));
}
/**
* @test
*/
public function itShouldWriteUnionAllFromGenericBuilder()
{
$minus = $this->writer->minus(new Select('user'), new Select('user_email'));
$expected = <<<SQL
SELECT user.* FROM user
MINUS
SELECT user_email.* FROM user_email
SQL;
$this->assertEquals($expected, $this->writer->write($minus));
}
}

View File

@ -0,0 +1,72 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 10:46 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\Syntax\PlaceholderWriter;
/**
* Class PlaceholderWriterTest.
*/
class PlaceholderWriterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PlaceholderWriter
*/
private $writer;
/**
*
*/
protected function setUp()
{
$this->writer = new PlaceholderWriter();
}
/**
* @test
*/
public function itShouldAddValueAndReturnPlaceholder()
{
$result = $this->writer->add(1);
$this->assertEquals(':v1', $result);
}
/**
* @test
*/
public function itShouldAddValueAndGetReturnsArrayHoldingPlaceholderData()
{
$this->writer->add(1);
$this->assertEquals(array(':v1' => 1), $this->writer->get());
}
/**
* @test
*/
public function itShouldTranslatePhpNullToSqlNullValue()
{
$this->writer->add('');
$this->writer->add(null);
$this->assertEquals(array(':v1' => 'NULL', ':v2' => 'NULL'), $this->writer->get());
}
/**
* @test
*/
public function itShouldTranslatePhpBoolToSqlBoolValue()
{
$this->writer->add(true);
$this->writer->add(false);
$this->assertEquals(array(':v1' => 1, ':v2' => 0), $this->writer->get());
}
}

View File

@ -0,0 +1,625 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 10:46 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Syntax\Column;
use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
/**
* Class SelectWriterTest.
*/
class SelectWriterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var GenericBuilder
*/
private $writer;
/**
* @var Select
*/
private $query;
/**
* @var string
*/
private $exceptionClass = '\NilPortugues\Sql\QueryBuilder\Manipulation\QueryException';
/**
*
*/
protected function setUp()
{
$this->writer = new GenericBuilder();
$this->query = new Select();
}
/**
* @test
*/
public function itShouldBeCloneableWithoutKeepingReferences()
{
$query1 = new Select('user');
$query2 = clone $query1;
$query2->setTable('users');
$this->assertFalse($query1->getTable() == $query2->getTable());
}
/**
* @test
*/
public function itShouldBeConstructedWithConstructor()
{
$this->query = new Select('user');
$expected = 'SELECT user.* FROM user';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldBeAbleToWriteCommentInQuery()
{
$this->query = new Select('user');
$this->query->setComment('This is a comment');
$expected = <<<SQL
-- This is a comment
SELECT user.* FROM user
SQL;
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldThrowExceptionWhenGettingColumnsButNoTableIsSet()
{
$this->setExpectedException($this->exceptionClass);
$this->query = new Select();
$this->query->getColumns();
}
/**
* @test
*/
public function itShouldBeConstructedWithConstructorWithColumns()
{
$this->query = new Select('user', array('user_id', 'name'));
$expected = 'SELECT user.user_id, user.name FROM user';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldSelectAll()
{
$this->query->setTable('user');
$expected = 'SELECT user.* FROM user';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldSelectAllDistinct()
{
$this->query->setTable('user')->distinct();
$expected = 'SELECT DISTINCT user.* FROM user';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldSelectAllWithLimit1()
{
$this->query->setTable('user')->limit(1);
$expected = 'SELECT user.* FROM user LIMIT :v1, :v2';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 0);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldSelectAllWithLimit1Offset2()
{
$this->query->setTable('user')->limit(1, 2);
$expected = 'SELECT user.* FROM user LIMIT :v1, :v2';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 2);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldSelectAllGetFirst20()
{
$this->query->setTable('user')->limit(0, 20);
$expected = 'SELECT user.* FROM user LIMIT :v1, :v2';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 0, ':v2' => 20);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldAllowColumnAlias()
{
$this->query
->setTable('user')
->setColumns(
array(
'userId' => 'user_id', // Alias -> column name
'username' => 'name',
'email' => 'email',
)
);
$expected = 'SELECT user.user_id AS "userId", user.name AS "username", user.email AS "email" FROM user';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldAllowColumnOrder()
{
$this->query
->setTable('user')
->orderBy('user_id', OrderBy::ASC);
$expected = 'SELECT user.* FROM user ORDER BY user.user_id ASC';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldAllowColumnOrderUsingColumnAlias()
{
$tableName = 'user';
$this->query
->setTable($tableName)
->setColumns(
array(
'userId' => 'user_id', // Alias -> column name
'username' => 'name',
'email' => 'email',
)
)
->orderBy('user_id', OrderBy::ASC)
->orderBy('email', OrderBy::DESC);
$expected =
'SELECT user.user_id AS "userId", user.name AS "username", user.email AS "email" FROM '.
'user ORDER BY user.user_id ASC, user.email DESC';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldBeAbleToDoALeftJoin()
{
$this->query
->setTable('user')
->leftJoin('news', 'user_id', 'author_id', array('title', 'body', 'created_at', 'updated_at'));
$expected = 'SELECT user.*, news.title, news.body, news.created_at, news.updated_at FROM user LEFT JOIN '.
'news ON (news.author_id = user.user_id)';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldBeAbleToDoARightJoin()
{
$this->query
->setTable('user')
->rightJoin('news', 'user_id', 'author_id', array('title', 'body', 'created_at', 'updated_at'));
$expected = 'SELECT user.*, news.title, news.body, news.created_at, news.updated_at FROM user RIGHT JOIN '.
'news ON (news.author_id = user.user_id)';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldBeAbleToDoAInnerJoin()
{
$this->query
->setTable('user')
->innerJoin('news', 'user_id', 'author_id', array('title', 'body', 'created_at', 'updated_at'));
$expected = 'SELECT user.*, news.title, news.body, news.created_at, news.updated_at FROM user INNER JOIN '.
'news ON (news.author_id = user.user_id)';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldBeAbleToDoACrossJoin()
{
$this->query
->setTable('user')
->crossJoin('news', 'user_id', 'author_id', array('title', 'body', 'created_at', 'updated_at'));
$expected = 'SELECT user.*, news.title, news.body, news.created_at, news.updated_at FROM user CROSS JOIN '.
'news ON (news.author_id = user.user_id)';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldBeAbleToDoALeftJoinWithOrderByOnJoinedTable()
{
$this->query
->setTable('user')
->setColumns(
array(
'userId' => 'user_id',
'username' => 'name',
'email' => 'email',
'created_at',
)
)
->orderBy('user_id', OrderBy::DESC)
->leftJoin('news', 'user_id', 'author_id', array('title', 'body', 'created_at', 'updated_at'))
->orderBy('created_at', OrderBy::DESC);
$expected = 'SELECT user.user_id AS "userId", user.name AS "username", user.email AS "email", user.created_at,'.
' news.title, news.body, news.created_at, news.updated_at FROM user LEFT JOIN news ON (news.author_id '.
'= user.user_id) ORDER BY user.user_id DESC, news.created_at DESC';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldBeAbleToDoAJoin()
{
$this->query
->setTable('user')
->join('news', 'user_id', 'author_id', array('title', 'body', 'created_at', 'updated_at'));
$expected = 'SELECT user.*, news.title, news.body, news.created_at, news.updated_at FROM user JOIN '.
'news ON (news.author_id = user.user_id)';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldBeAbleToDoAJoinWithOrderByOnJoinedTable()
{
$this->query
->setTable('user')
->setColumns(
array(
'userId' => 'user_id',
'username' => 'name',
'email' => 'email',
'created_at',
)
)
->orderBy('user_id', OrderBy::DESC)
->join('news', 'user_id', 'author_id', array('title', 'body', 'created_at', 'updated_at'))
->orderBy('created_at', OrderBy::DESC);
$expected = 'SELECT user.user_id AS "userId", user.name AS "username", user.email AS "email", user.created_at,'.
' news.title, news.body, news.created_at, news.updated_at FROM user JOIN news ON (news.author_id ='.
' user.user_id) ORDER BY user.user_id DESC, news.created_at DESC';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldBeAbleToDoAJoinWithCustomColumns()
{
$this->query
->setTable('user')
->setColumns(
array(
'userId' => 'user_id',
'username' => 'name',
'email' => 'email',
'created_at',
)
)
->orderBy('user_id', OrderBy::DESC)
->join('news', 'user_id', 'author_id', array('title', 'body', 'created_at', 'updated_at'))
->orderBy('created_at', OrderBy::DESC)
->join('articles', new Column('news_id', 'article'), new Column('id', 'news'));
$expected = 'SELECT user.user_id AS "userId", user.name AS "username", user.email AS "email", user.created_at,'.
' news.title, news.body, news.created_at, news.updated_at FROM user JOIN news ON (news.author_id ='.
' user.user_id) JOIN articles ON (news.id = article.news_id) ORDER BY user.user_id DESC, news.created_at DESC';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldBeAbleToDoAnAddWithMultipleJoins()
{
$this->query->setTable('user');
for ($i = 1; $i <= 5; ++$i) {
//Select QueryInterface for "news" table
$select = new Select();
$select
->setTable('news'.$i)
->setColumns(array('title'.$i));
//Select query for user table, being joined with "newsX" select.
$this->query->addJoin($select, 'user_id', 'author_id'.$i);
}
$expected = 'SELECT user.*, news1.title1, news2.title2, news3.title3, news4.title4, news5.title5 '.
'FROM user JOIN news1 ON (news1.author_id1 = user.user_id) JOIN news2 ON (news2.author_id2 = user.user_id)'.
' JOIN news3 ON (news3.author_id3 = user.user_id) JOIN news4 ON (news4.author_id4 = user.user_id) '.
'JOIN news5 ON (news5.author_id5 = user.user_id)';
$this->assertSame($expected, $this->writer->write($this->query));
}
/**
* @test
*/
public function itShouldBeAbleToOn()
{
$this->query
->setTable('user')
->setColumns(
array(
'userId' => 'user_id',
'username' => 'name',
'email' => 'email',
'created_at',
)
)
->orderBy('user_id', OrderBy::DESC)
->join('news', 'user_id', 'author_id', array('title', 'body', 'created_at', 'updated_at'))
->orderBy('created_at', OrderBy::DESC)
->on()
->eq('author_id', 1);
$this->query->limit(1, 10);
$expected = 'SELECT user.user_id AS "userId", user.name AS "username", user.email AS "email", user.created_at,'.
' news.title, news.body, news.created_at, news.updated_at FROM user JOIN news ON '.
'(news.author_id = user.user_id) AND (news.author_id = :v1) ORDER BY '.
'user.user_id DESC, news.created_at DESC LIMIT :v2, :v3';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 1, ':v3' => 10);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToCountTotalRows()
{
$this->query
->setTable('user')
->count()
->groupBy(array('user_id', 'name'))
->having()
->equals('user_id', 1)
->equals('user_id', 2);
$expected = 'SELECT COUNT(*) FROM user GROUP BY user.user_id, user.name HAVING (user.user_id = :v1) AND (user.user_id = :v2)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 2);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToCountTotalRowsSettingDefaultColumn()
{
$this->query
->setTable('user')
->count('user_id')
->groupBy(array('user_id', 'name'))
->having()
->equals('user_id', 1)
->equals('user_id', 2);
$expected = 'SELECT COUNT(user.user_id) FROM user GROUP BY user.user_id, user.name HAVING (user.user_id = :v1) AND (user.user_id = :v2)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 2);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToCountTotalRowsSettingDefaultColumnWithAlias()
{
$this->query
->setTable('user')
->count('user_id', 'total_users')
->groupBy(array('user_id', 'name'))
->having()
->equals('user_id', 1)
->equals('user_id', 2);
$expected = 'SELECT COUNT(user.user_id) AS "total_users" FROM user GROUP BY user.user_id, user.name HAVING (user.user_id = :v1) AND (user.user_id = :v2)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 2);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToGroupByOperator()
{
$this->query
->setTable('user')
->setColumns(
array(
'userId' => 'user_id',
'username' => 'name',
'email' => 'email',
'created_at',
)
)
->groupBy(array('user_id', 'name'))
->having()
->equals('user_id', 1)
->equals('user_id', 2);
$expected = 'SELECT user.user_id AS "userId", user.name AS "username", user.email AS "email", user.created_at'.
' FROM user GROUP BY user.user_id, user.name HAVING (user.user_id = :v1) AND (user.user_id = :v2)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 2);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldThrowExceptionInvalidHavingConjunction()
{
$this->setExpectedException($this->exceptionClass);
$this->query
->setTable('user')
->setColumns(
array(
'userId' => 'user_id',
'username' => 'name',
'email' => 'email',
'created_at',
)
)
->groupBy(array('user_id', 'name'))
->having('AAAAAAAAAAAAAAAA');
}
/**
* @test
*/
public function itShouldBeAbleToSetHavingOperatorToOr()
{
$this->query
->setTable('user')
->setColumns(
array(
'userId' => 'user_id',
'username' => 'name',
'email' => 'email',
'created_at',
)
)
->groupBy(array('user_id', 'name'))
->having('OR')
->equals('user_id', 1)
->equals('user_id', 2);
$expected = 'SELECT user.user_id AS "userId", user.name AS "username", user.email AS "email", user.created_at'.
' FROM user GROUP BY user.user_id, user.name HAVING (user.user_id = :v1) OR (user.user_id = :v2)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 2);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldAllowSelectQueryToActAsAColumn()
{
$table1 = new Select('Table1');
$table1
->where()
->equals('table1_id', 1);
$table2 = new Select('Table2');
$table2
->where()
->eq($table1, 2);
$expected = 'SELECT Table2.* FROM Table2 WHERE ((SELECT Table1.* FROM Table1 '.
'WHERE (Table1.table1_id = :v1)) = :v2)';
$this->assertSame($expected, $this->writer->write($table2));
$expected = array(':v1' => 1, ':v2' => 2);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldWriteJoin()
{
$this->query
->isJoin(true)
->setTable('user')
->on()
->equals('user_id', 1);
$expected = 'JOIN user ON (user.user_id = :v1)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
}

View File

@ -0,0 +1,118 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 7:34 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Builder\Syntax\UnionAllWriter;
use NilPortugues\Sql\QueryBuilder\Manipulation\UnionAll;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
/**
* Class UnionAllWriterTest.
*/
class UnionAllWriterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var UnionAllWriter
*/
private $unionAllWriter;
/**
* @var GenericBuilder
*/
private $writer;
/**
*
*/
public function setUp()
{
$this->unionAllWriter = new UnionAllWriter(new GenericBuilder());
$this->writer = new GenericBuilder();
}
public function tearDown()
{
$this->unionAllWriter = null;
$this->writer = null;
}
/**
* @test
*/
public function itShouldWriteUnionAll()
{
$union = new UnionAll();
$union->add(new Select('user'));
$union->add(new Select('user_email'));
$expected = <<<SQL
SELECT user.* FROM user
UNION ALL
SELECT user_email.* FROM user_email
SQL;
$this->assertEquals($expected, $this->unionAllWriter->write($union));
}
/**
* @test
*/
public function itShouldWriteUnionAllFromGenericBuilder()
{
$unionAll = $this->writer->unionAll();
$unionAll->add(new Select('user'));
$unionAll->add(new Select('user_email'));
$expected = <<<SQL
SELECT user.* FROM user
UNION ALL
SELECT user_email.* FROM user_email
SQL;
$this->assertEquals($expected, $this->writer->write($unionAll));
}
/**
* @test
*/
public function itShouldNotResetPlaceholders()
{
$select1 = (new Select('table1'))
->where()
->between('column', 1, 2)
->end();
$select2 = (new Select('table2'))
->where()
->between('column', 3, 4)
->end();
$union = (new UnionAll())
->add($select1)
->add($select2);
$expectedSql = <<<SQL
SELECT table1.* FROM table1 WHERE (table1.column BETWEEN :v1 AND :v2)
UNION ALL
SELECT table2.* FROM table2 WHERE (table2.column BETWEEN :v3 AND :v4)
SQL;
$expectedParams = [
':v1' => 1,
':v2' => 2,
':v3' => 3,
':v4' => 4,
];
$this->assertEquals($expectedSql, $this->writer->write($union));
$this->assertEquals($expectedParams, $this->writer->getValues());
}
}

View File

@ -0,0 +1,118 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 7:34 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Builder\Syntax\UnionWriter;
use NilPortugues\Sql\QueryBuilder\Manipulation\Union;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
/**
* Class UnionWriterTest.
*/
class UnionWriterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var UnionWriter
*/
private $unionWriter;
/**
* @var GenericBuilder
*/
private $writer;
/**
*
*/
public function setUp()
{
$this->unionWriter = new UnionWriter(new GenericBuilder());
$this->writer = new GenericBuilder();
}
public function tearDown()
{
$this->unionWriter = null;
$this->writer = null;
}
/**
* @test
*/
public function itShouldWriteUnion()
{
$union = new Union();
$union->add(new Select('user'));
$union->add(new Select('user_email'));
$expected = <<<SQL
SELECT user.* FROM user
UNION
SELECT user_email.* FROM user_email
SQL;
$this->assertEquals($expected, $this->unionWriter->write($union));
}
/**
* @test
*/
public function itShouldWriteUnionFromGenericBuilder()
{
$unionAll = $this->writer->union();
$unionAll->add(new Select('user'));
$unionAll->add(new Select('user_email'));
$expected = <<<SQL
SELECT user.* FROM user
UNION
SELECT user_email.* FROM user_email
SQL;
$this->assertEquals($expected, $this->writer->write($unionAll));
}
/**
* @test
*/
public function itShouldNotResetPlaceholders()
{
$select1 = (new Select('table1'))
->where()
->between('column', 1, 2)
->end();
$select2 = (new Select('table2'))
->where()
->between('column', 3, 4)
->end();
$union = (new Union())
->add($select1)
->add($select2);
$expectedSql = <<<SQL
SELECT table1.* FROM table1 WHERE (table1.column BETWEEN :v1 AND :v2)
UNION
SELECT table2.* FROM table2 WHERE (table2.column BETWEEN :v3 AND :v4)
SQL;
$expectedParams = [
':v1' => 1,
':v2' => 2,
':v3' => 3,
':v4' => 4,
];
$this->assertEquals($expectedSql, $this->writer->write($union));
$this->assertEquals($expectedParams, $this->writer->getValues());
}
}

View File

@ -0,0 +1,140 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 10:47 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\Update;
/**
* Class UpdateWriterTest.
*/
class UpdateWriterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var array
*/
private $valueArray = array();
/**
* @var GenericBuilder
*/
private $writer;
/**
* @var Update
*/
private $query;
/**
* @var string
*/
private $exceptionClass = '\NilPortugues\Sql\QueryBuilder\Manipulation\QueryException';
/**
*
*/
protected function setUp()
{
$this->writer = new GenericBuilder();
$this->query = new Update();
$this->valueArray = array(
'user_id' => 1,
'name' => 'Nil',
'contact' => 'contact@nilportugues.com',
);
}
/**
* @test
*/
public function itShouldThrowQueryException()
{
$this->setExpectedException($this->exceptionClass);
$this->query->setTable('user');
$this->writer->write($this->query);
}
/**
* @test
*/
public function itShouldWriteUpdateQuery()
{
$this->query
->setTable('user')
->setValues($this->valueArray);
$expected = 'UPDATE user SET user.user_id = :v1, user.name = :v2, user.contact = :v3';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 'Nil', ':v3' => 'contact@nilportugues.com');
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToWriteCommentInQuery()
{
$this->query
->setTable('user')
->setValues($this->valueArray)
->setComment('This is a comment');
$expected = <<<SQL
-- This is a comment
UPDATE user SET user.user_id = :v1, user.name = :v2, user.contact = :v3
SQL;
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 'Nil', ':v3' => 'contact@nilportugues.com');
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldWriteUpdateQueryWithWhereConstrain()
{
$this->query
->setTable('user')
->setValues($this->valueArray)
->where()
->equals('user_id', 1);
$expected = 'UPDATE user SET user.user_id = :v1, user.name = :v2, user.contact = :v3 WHERE (user.user_id = :v4)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 'Nil', ':v3' => 'contact@nilportugues.com', ':v4' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldWriteUpdateQueryWithWhereConstrainAndLimit1()
{
$this->query
->setTable('user')
->setValues($this->valueArray)
->where()
->equals('user_id', 1);
$this->query->limit(1);
$expected = 'UPDATE user SET user.user_id = :v1, user.name = :v2, user.contact = :v3 WHERE (user.user_id = :v4) LIMIT :v5';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 'Nil', ':v3' => 'contact@nilportugues.com', ':v4' => 1, ':v5' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
}

View File

@ -0,0 +1,561 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/13/14
* Time: 12:46 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
/**
* Class WhereWriterTest.
*/
class WhereWriterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var GenericBuilder
*/
private $writer;
/**
* @var Select
*/
private $query;
/**
*
*/
protected function setUp()
{
$this->writer = new GenericBuilder();
$this->query = new Select();
}
/**
* @test
*/
public function itShouldAllowWhereConditions()
{
$this->query
->setTable('user')
->where()
->equals('user_id', 1)
->like('name', '%N%');
$expected = 'SELECT user.* FROM user WHERE (user.user_id = :v1) AND (user.name LIKE :v2)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => '%N%');
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldAllowWhereOrConditions()
{
$this->query
->setTable('user')
->where('OR')
->equals('user_id', 1)
->like('name', '%N%');
$this->assertSame('OR', $this->query->getWhereOperator());
$expected = 'SELECT user.* FROM user WHERE (user.user_id = :v1) OR (user.name LIKE :v2)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => '%N%');
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToLetWhereStatementNotBeEqualTo()
{
$column = 'user_id';
$value = 1;
$this->query
->setTable('user')
->where()
->notEquals($column, $value);
$expected = 'SELECT user.* FROM user WHERE (user.user_id <> :v1)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToLetWhereStatementBeGreaterThan()
{
$column = 'user_id';
$value = 1;
$this->query
->setTable('user')
->where()
->greaterThan($column, $value);
$expected = 'SELECT user.* FROM user WHERE (user.user_id > :v1)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToLetWhereStatementBeGreaterThanOrEqual()
{
$column = 'user_id';
$value = 1;
$this->query
->setTable('user')
->where()
->greaterThanOrEqual($column, $value);
$expected = 'SELECT user.* FROM user WHERE (user.user_id >= :v1)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToLetWhereStatementBeLessThan()
{
$column = 'user_id';
$value = 1;
$this->query
->setTable('user')
->where()
->lessThan($column, $value);
$expected = 'SELECT user.* FROM user WHERE (user.user_id < :v1)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToLetWhereStatementBeLessThanOrEqual()
{
$column = 'user_id';
$value = 1;
$this->query
->setTable('user')
->where()
->lessThanOrEqual($column, $value);
$expected = 'SELECT user.* FROM user WHERE (user.user_id <= :v1)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToLetWhereStatementBeLike()
{
$column = 'user_id';
$value = 1;
$this->query
->setTable('user')
->where()
->like($column, $value);
$expected = 'SELECT user.* FROM user WHERE (user.user_id LIKE :v1)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToLetWhereStatementBeNotLike()
{
$column = 'user_id';
$value = 1;
$this->query
->setTable('user')
->where()
->notLike($column, $value);
$expected = 'SELECT user.* FROM user WHERE (user.user_id NOT LIKE :v1)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToLetWhereStatementAccumulateInConditions()
{
$column = 'user_id';
$this->query
->setTable('user')
->where()
->in($column, array(1, 2, 3));
$expected = 'SELECT user.* FROM user WHERE (user.user_id IN (:v1, :v2, :v3))';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 2, ':v3' => 3);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToLetWhereStatementAccumulateNotInConditions()
{
$column = 'user_id';
$this->query
->setTable('user')
->where()
->notIn($column, array(1, 2, 3));
$expected = 'SELECT user.* FROM user WHERE (user.user_id NOT IN (:v1, :v2, :v3))';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 2, ':v3' => 3);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToLetWhereStatementWriteBetweenConditions()
{
$column = 'user_id';
$this->query
->setTable('user')
->where()
->between($column, 1, 2);
$expected = 'SELECT user.* FROM user WHERE (user.user_id BETWEEN :v1 AND :v2)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 2);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToLetWhereStatementWriteNotBetweenConditions()
{
$column = 'user_id';
$this->query
->setTable('user')
->where()
->notBetween($column, 1, 2);
$expected = 'SELECT user.* FROM user WHERE (user.user_id NOT BETWEEN :v1 AND :v2)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 2);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToLetWhereStatementSetNullValueCondition()
{
$column = 'user_id';
$this->query
->setTable('user')
->where()
->isNull($column);
$expected = 'SELECT user.* FROM user WHERE (user.user_id IS NULL)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array();
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToLetWhereStatementSetIsNotNullValueCondition()
{
$column = 'user_id';
$this->query
->setTable('user')
->where()
->isNotNull($column);
$expected = 'SELECT user.* FROM user WHERE (user.user_id IS NOT NULL)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array();
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToLetWhereStatementSetBitClauseValueCondition()
{
$column = 'user_id';
$this->query
->setTable('user')
->where()
->addBitClause($column, 1);
$expected = 'SELECT user.* FROM user WHERE (ISNULL(user.user_id, 0) = :v1)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToLetWhereStatementSubconditions()
{
$column = 'user_id';
$this->query
->setTable('user')
->where()
->equals($column, 1)
->equals($column, 2)
->subWhere('OR')
->lessThan($column, 10)
->greaterThan($column, 100);
$expected = 'SELECT user.* FROM user WHERE (user.user_id = :v1) AND (user.user_id = :v2) '.
'AND ((user.user_id < :v3) OR (user.user_id > :v4))';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => 2, ':v3' => 10, ':v4' => 100);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldAllowSelectWhereButNotWriteCondition()
{
$table1 = new Select('Table1');
$table1
->where();
$expected = 'SELECT Table1.* FROM Table1';
$this->assertSame($expected, $this->writer->write($table1));
}
/**
* @test
*/
public function itShouldAllowHavingConditions()
{
$this->query
->setTable('user')
->having()
->greaterThan('user_id', 1)
->like('name', '%N%');
$expected = 'SELECT user.* FROM user HAVING (user.user_id > :v1) AND (user.name LIKE :v2)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 1, ':v2' => '%N%');
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToUseSelectStatementsInWhere()
{
$selectRole = new Select();
$selectRole
->setTable('role')
->setColumns(array('role_name'))
->limit(1)
->where()
->equals('role_id', 3);
$this->query
->setTable('user')
->setColumns(array('user_id', 'role_id'))
->where()
->equals('role_id', $selectRole);
$expected = 'SELECT user.user_id, user.role_id FROM user WHERE '.
'(user.role_id = (SELECT role.role_name FROM role WHERE (role.role_id = :v1) LIMIT :v2, :v3))';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 3, ':v2' => 1, ':v3' => 0);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToSelectWithFullMatchSearchUsingMatchInNaturalMode()
{
$this->query
->setTable('user')
->setColumns(array('user_id', 'role_id'))
->where()
->match(array('username', 'email'), array('Nil'));
$expected = 'SELECT user.user_id, user.role_id FROM user '.
'WHERE (MATCH(user.username, user.email) AGAINST(:v1))';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 'Nil');
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToSelectWithFullMatchSearchUsingMatchInBooleanMode()
{
$this->query
->setTable('user')
->setColumns(array('user_id', 'role_id'))
->where()
->matchBoolean(array('username', 'email'), array('Nil'));
$expected = 'SELECT user.user_id, user.role_id FROM user '.
'WHERE (MATCH(user.username, user.email) AGAINST(:v1 IN BOOLEAN MODE))';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 'Nil');
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToSelectWithFullMatchSearchUsingMatchInQueryExpansionMode()
{
$this->query
->setTable('user')
->setColumns(array('user_id', 'role_id'))
->where()
->matchWithQueryExpansion(array('username', 'email'), array('Nil'));
$expected = 'SELECT user.user_id, user.role_id FROM user '.
'WHERE (MATCH(user.username, user.email) AGAINST(:v1 WITH QUERY EXPANSION))';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 'Nil');
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToDoWhereExists()
{
$select = new Select('banned_user');
$select->where()->equals('user_id', 1);
$this->query
->setTable('user')
->setColumns(array('user_id', 'role_id'))
->where()
->exists($select)
->equals('user', 'Nil');
$expected = 'SELECT user.user_id, user.role_id FROM user WHERE (user.user = :v1) AND '.
'EXISTS (SELECT banned_user.* FROM banned_user WHERE (banned_user.user_id = :v2))';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 'Nil', ':v2' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldBeAbleToDoWhereNotExists()
{
$select = new Select('banned_user');
$select->where()->equals('user_id', 1);
$this->query
->setTable('user')
->setColumns(array('user_id', 'role_id'))
->where()
->notExists($select)
->equals('user', 'Nil');
$expected = 'SELECT user.user_id, user.role_id FROM user WHERE (user.user = :v1) AND '.
'NOT EXISTS (SELECT banned_user.* FROM banned_user WHERE (banned_user.user_id = :v2))';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => 'Nil', ':v2' => 1);
$this->assertEquals($expected, $this->writer->getValues());
}
/**
* @test
*/
public function itShouldAllowWhereConditionAsLiteral()
{
$this->query
->setTable('user')
->where()
->asLiteral('(username is not null and status=:status)')
->notEquals('name', '%N%');
$expected = 'SELECT user.* FROM user WHERE (username is not null and status=:status) AND (user.name <> :v1)';
$this->assertSame($expected, $this->writer->write($this->query));
$expected = array(':v1' => '%N%');
$this->assertEquals($expected, $this->writer->getValues());
}
}

View File

@ -0,0 +1,150 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 10:47 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Builder\Syntax;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Builder\Syntax\PlaceholderWriter;
use NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory;
/**
* Class WriterFactoryTest.
*/
class WriterFactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PlaceholderWriter
*/
private $placeholder;
/**
* @var GenericBuilder
*/
private $writer;
/**
*
*/
public function setUp()
{
$this->writer = new GenericBuilder();
$this->placeholder = new PlaceholderWriter();
}
/**
* @test
*/
public function itShouldCreateColumnWriter()
{
$writer = WriterFactory::createColumnWriter($this->writer, $this->placeholder);
$this->assertSame('NilPortugues\Sql\QueryBuilder\Builder\Syntax\ColumnWriter', \get_class($writer));
}
/**
* @test
*/
public function itShouldCreateWhereWriter()
{
$writer = WriterFactory::createWhereWriter($this->writer, $this->placeholder);
$this->assertSame('NilPortugues\Sql\QueryBuilder\Builder\Syntax\WhereWriter', \get_class($writer));
}
/**
* @test
*/
public function itShouldCreateSelectWriter()
{
$writer = WriterFactory::createSelectWriter($this->writer, $this->placeholder);
$this->assertSame('NilPortugues\Sql\QueryBuilder\Builder\Syntax\SelectWriter', \get_class($writer));
}
/**
* @test
*/
public function itShouldCreateInsertWriter()
{
$writer = WriterFactory::createInsertWriter($this->writer, $this->placeholder);
$this->assertSame('NilPortugues\Sql\QueryBuilder\Builder\Syntax\InsertWriter', \get_class($writer));
}
/**
* @test
*/
public function itShouldCreateUpdateWriter()
{
$writer = WriterFactory::createUpdateWriter($this->writer, $this->placeholder);
$this->assertSame('NilPortugues\Sql\QueryBuilder\Builder\Syntax\UpdateWriter', \get_class($writer));
}
/**
* @test
*/
public function itShouldCreateDeleteWriter()
{
$writer = WriterFactory::createDeleteWriter($this->writer, $this->placeholder);
$this->assertSame('NilPortugues\Sql\QueryBuilder\Builder\Syntax\DeleteWriter', \get_class($writer));
}
/**
* @test
*/
public function itShouldCreatePlaceholderWriter()
{
$writer = WriterFactory::createPlaceholderWriter();
$this->assertSame('NilPortugues\Sql\QueryBuilder\Builder\Syntax\PlaceholderWriter', \get_class($writer));
}
/**
* @test
*/
public function itShouldCreateIntersectWriter()
{
$writer = WriterFactory::createIntersectWriter($this->writer);
$this->assertSame('NilPortugues\Sql\QueryBuilder\Builder\Syntax\IntersectWriter', \get_class($writer));
}
/**
* @test
*/
public function itShouldCreateMinusWriter()
{
$writer = WriterFactory::createMinusWriter($this->writer);
$this->assertSame('NilPortugues\Sql\QueryBuilder\Builder\Syntax\MinusWriter', \get_class($writer));
}
/**
* @test
*/
public function itShouldCreateUnion()
{
$writer = WriterFactory::createUnionWriter($this->writer);
$this->assertSame('NilPortugues\Sql\QueryBuilder\Builder\Syntax\UnionWriter', \get_class($writer));
}
/**
* @test
*/
public function itShouldCreateUnionAll()
{
$writer = WriterFactory::createUnionAllWriter($this->writer);
$this->assertSame('NilPortugues\Sql\QueryBuilder\Builder\Syntax\UnionAllWriter', \get_class($writer));
}
}

View File

@ -0,0 +1,74 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/7/14
* Time: 11:44 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Manipulation;
/**
* Class BaseQueryTest.
*/
class BaseQueryTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \NilPortugues\Tests\Sql\QueryBuilder\Manipulation\Resources\DummyQuery
*/
private $query;
/**
* @var string
*/
private $whereClass = '\NilPortugues\Sql\QueryBuilder\Syntax\Where';
/**
*
*/
protected function setUp()
{
$this->query = new Resources\DummyQuery();
$this->query->setTable('tablename');
}
/**
*
*/
protected function tearDown()
{
$this->query = null;
}
/**
* @test
*/
public function itShouldBeAbleToSetTableName()
{
$this->assertSame('tablename', $this->query->getTable()->getName());
}
/**
* @test
*/
public function itShouldGetWhere()
{
$this->assertNull($this->query->getWhere());
$this->query->where();
$this->assertInstanceOf($this->whereClass, $this->query->getWhere());
}
/**
* @test
*/
public function itShouldGetWhereOperator()
{
$this->assertSame('AND', $this->query->getWhereOperator());
$this->query->where('OR');
$this->assertSame('OR', $this->query->getWhereOperator());
}
}

View File

@ -0,0 +1,56 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 1:37 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\Delete;
/**
* Class DeleteTest.
*/
class DeleteTest extends \PHPUnit_Framework_TestCase
{
/**
* @var GenericBuilder
*/
private $writer;
/**
* @var Delete
*/
private $query;
/**
*
*/
protected function setUp()
{
$this->query = new Delete();
}
/**
* @test
*/
public function itShouldGetPartName()
{
$this->assertSame('DELETE', $this->query->partName());
}
/**
* @test
*/
public function itShouldReturnLimit1()
{
$this->query->limit(1);
$this->assertSame(1, $this->query->getLimitStart());
}
}

View File

@ -0,0 +1,77 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 1:37 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Manipulation\Insert;
/**
* Class InsertTest.
*/
class InsertTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Insert
*/
private $query;
/**
*
*/
protected function setUp()
{
$this->query = new Insert();
}
/**
* @test
*/
public function itShouldGetPartName()
{
$this->assertSame('INSERT', $this->query->partName());
}
/**
* @test
*/
public function itShouldSetValues()
{
$values = ['user_id' => 1, 'username' => 'nilportugues'];
$this->query->setValues($values);
$this->assertSame($values, $this->query->getValues());
}
/**
* @test
*/
public function itShouldGetColumns()
{
$values = ['user_id' => 1, 'username' => 'nilportugues'];
$this->query->setValues($values);
$columns = $this->query->getColumns();
$this->assertInstanceOf('NilPortugues\Sql\QueryBuilder\Syntax\Column', $columns[0]);
}
/**
* @test
*/
public function itShouldSetNullableValues()
{
$values = ['user_id' => 1, 'description' => null, 'isVisible' => false];
$this->query->setValues($values);
$this->assertSame($values, $this->query->getValues());
}
}

View File

@ -0,0 +1,89 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 7:26 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Manipulation\Intersect;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
/**
* Class IntersectTest.
*/
class IntersectTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Intersect
*/
private $query;
/**
* @var string
*/
private $exceptionClass = '\NilPortugues\Sql\QueryBuilder\Manipulation\QueryException';
/**
*
*/
protected function setUp()
{
$this->query = new Intersect();
}
/**
* @test
*/
public function itShouldGetPartName()
{
$this->assertSame('INTERSECT', $this->query->partName());
}
/**
* @test
*/
public function itShouldThrowExceptionForUnsupportedGetTable()
{
$this->setExpectedException($this->exceptionClass);
$this->query->getTable();
}
/**
* @test
*/
public function itShouldThrowExceptionForUnsupportedGetWhere()
{
$this->setExpectedException($this->exceptionClass);
$this->query->getWhere();
}
/**
* @test
*/
public function itShouldThrowExceptionForUnsupportedWhere()
{
$this->setExpectedException($this->exceptionClass);
$this->query->where();
}
/**
* @test
*/
public function itShouldGetIntersectSelects()
{
$this->assertEquals(array(), $this->query->getIntersects());
$select1 = new Select('user');
$select2 = new Select('user_email');
$this->query->add($select1);
$this->query->add($select2);
$this->assertEquals(array($select1, $select2), $this->query->getIntersects());
}
}

View File

@ -0,0 +1,82 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 7:26 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Manipulation\Minus;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
/**
* Class MinusTest.
*/
class MinusTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Minus
*/
private $query;
/**
* @var string
*/
private $exceptionClass = '\NilPortugues\Sql\QueryBuilder\Manipulation\QueryException';
/**
*
*/
protected function setUp()
{
$this->query = new Minus(new Select('user'), new Select('user_email'));
}
/**
* @test
*/
public function itShouldGetPartName()
{
$this->assertSame('MINUS', $this->query->partName());
}
/**
* @test
*/
public function itShouldThrowExceptionForUnsupportedGetTable()
{
$this->setExpectedException($this->exceptionClass);
$this->query->getTable();
}
/**
* @test
*/
public function itShouldThrowExceptionForUnsupportedGetWhere()
{
$this->setExpectedException($this->exceptionClass);
$this->query->getWhere();
}
/**
* @test
*/
public function itShouldThrowExceptionForUnsupportedWhere()
{
$this->setExpectedException($this->exceptionClass);
$this->query->where();
}
/**
* @test
*/
public function itShouldGetMinusSelects()
{
$this->assertEquals(new Select('user'), $this->query->getFirst());
$this->assertEquals(new Select('user_email'), $this->query->getSecond());
}
}

View File

@ -0,0 +1,98 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/16/14
* Time: 8:50 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryFactory;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
/**
* Class QueryFactoryTest.
*/
class QueryFactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function itShouldCreateSelectObject()
{
$className = '\NilPortugues\Sql\QueryBuilder\Manipulation\Select';
$this->assertInstanceOf($className, QueryFactory::createSelect());
}
/**
* @test
*/
public function itShouldCreateInsertObject()
{
$className = '\NilPortugues\Sql\QueryBuilder\Manipulation\Insert';
$this->assertInstanceOf($className, QueryFactory::createInsert());
}
/**
* @test
*/
public function itShouldCreateUpdateObject()
{
$className = '\NilPortugues\Sql\QueryBuilder\Manipulation\Update';
$this->assertInstanceOf($className, QueryFactory::createUpdate());
}
/**
* @test
*/
public function itShouldCreateDeleteObject()
{
$className = '\NilPortugues\Sql\QueryBuilder\Manipulation\Delete';
$this->assertInstanceOf($className, QueryFactory::createDelete());
}
/**
* @test
*/
public function itShouldCreateMinusObject()
{
$className = '\NilPortugues\Sql\QueryBuilder\Manipulation\Minus';
$this->assertInstanceOf($className, QueryFactory::createMinus(new Select('table1'), new Select('table2')));
}
/**
* @test
*/
public function itShouldCreateUnionObject()
{
$className = '\NilPortugues\Sql\QueryBuilder\Manipulation\Union';
$this->assertInstanceOf($className, QueryFactory::createUnion());
}
/**
* @test
*/
public function itShouldCreateUnionAllObject()
{
$className = '\NilPortugues\Sql\QueryBuilder\Manipulation\UnionAll';
$this->assertInstanceOf($className, QueryFactory::createUnionAll());
}
/**
* @test
*/
public function itShouldCreateWhereObject()
{
$mockClass = '\NilPortugues\Sql\QueryBuilder\Manipulation\QueryInterface';
$query = $this->getMockBuilder($mockClass)
->disableOriginalConstructor()
->getMock();
$className = '\NilPortugues\Sql\QueryBuilder\Syntax\Where';
$this->assertInstanceOf($className, QueryFactory::createWhere($query));
}
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:58 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Manipulation\Resources;
use NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery;
/**
* Class DummyQuery.
*/
class DummyQuery extends AbstractBaseQuery
{
/**
* @return string
*/
public function partName()
{
return 'DUMMY';
}
}

View File

@ -0,0 +1,101 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 1:36 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy;
/**
* Class SelectTest.
*/
class SelectTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Select
*/
private $query;
/**
*
*/
protected function setUp()
{
$this->query = new Select();
}
/**
* @test
*/
public function itShouldGetPartName()
{
$this->assertSame('SELECT', $this->query->partName());
}
/**
* @test
*/
public function itShouldSetParentOrderByAlso()
{
$columns = [
'id',
'phase_id',
'league_id',
'date',
];
$parentTable = 'events';
$this->query->setTable($parentTable);
$this->query->setColumns($columns);
$sorts = [
[
'field' => 'league_id',
'direction' => 1,
],
[
'field' => 'start_date',
'direction' => 0,
'table' => 'phases',
'joinBy' => 'phase_id',
'joinWith' => 'id',
],
[
'field' => 'date',
'direction' => 1,
],
];
if (is_array($sorts)) {
foreach ($sorts as $sort) {
$order = (int)$sort['direction'] > 0 ? OrderBy::ASC : OrderBy::DESC;
if (count($sort) == 5) {
$this->query->leftJoin(
$sort['table'],
$sort['joinBy'],
$sort['joinWith']
)->orderBy($sort['field'], $order);
} else {
$this->query->orderBy($sort['field'], $order);
}
}
}
$returnedOrders = $this->query->getAllOrderBy();
foreach ($returnedOrders as $id => $orderByObject) {
$column = $orderByObject->getColumn();
$table = $column->getTable();
$expectedColumn = $sorts[$id]['field'];
$expectedTable = array_key_exists('table', $sorts[$id]) ? $sorts[$id]['table'] : $parentTable;
$expectedDirection = (int)$sorts[$id]['direction'] > 0 ? OrderBy::ASC : OrderBy::DESC;
$this->assertSame($expectedColumn, $column->getName());
$this->assertSame($expectedTable, $table->getName());
$this->assertSame($expectedDirection, $orderByObject->getDirection());
}
$this->assertCount(count($sorts), $returnedOrders);
}
}

View File

@ -0,0 +1,89 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 7:26 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Manipulation\UnionAll;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
/**
* Class UnionAllTest.
*/
class UnionAllTest extends \PHPUnit_Framework_TestCase
{
/**
* @var UnionAll
*/
private $query;
/**
* @var string
*/
private $exceptionClass = '\NilPortugues\Sql\QueryBuilder\Manipulation\QueryException';
/**
*
*/
protected function setUp()
{
$this->query = new UnionAll();
}
/**
* @test
*/
public function itShouldGetPartName()
{
$this->assertSame('UNION ALL', $this->query->partName());
}
/**
* @test
*/
public function itShouldThrowExceptionForUnsupportedGetTable()
{
$this->setExpectedException($this->exceptionClass);
$this->query->getTable();
}
/**
* @test
*/
public function itShouldThrowExceptionForUnsupportedGetWhere()
{
$this->setExpectedException($this->exceptionClass);
$this->query->getWhere();
}
/**
* @test
*/
public function itShouldThrowExceptionForUnsupportedWhere()
{
$this->setExpectedException($this->exceptionClass);
$this->query->where();
}
/**
* @test
*/
public function itShouldGetIntersectSelects()
{
$this->assertEquals(array(), $this->query->getUnions());
$select1 = new Select('user');
$select2 = new Select('user_email');
$this->query->add($select1);
$this->query->add($select2);
$this->assertEquals(array($select1, $select2), $this->query->getUnions());
}
}

View File

@ -0,0 +1,89 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 9/12/14
* Time: 7:26 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Manipulation\Union;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
/**
* Class UnionTest.
*/
class UnionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Union
*/
private $query;
/**
* @var string
*/
private $exceptionClass = '\NilPortugues\Sql\QueryBuilder\Manipulation\QueryException';
/**
*
*/
protected function setUp()
{
$this->query = new Union();
}
/**
* @test
*/
public function itShouldGetPartName()
{
$this->assertSame('UNION', $this->query->partName());
}
/**
* @test
*/
public function itShouldThrowExceptionForUnsupportedGetTable()
{
$this->setExpectedException($this->exceptionClass);
$this->query->getTable();
}
/**
* @test
*/
public function itShouldThrowExceptionForUnsupportedGetWhere()
{
$this->setExpectedException($this->exceptionClass);
$this->query->getWhere();
}
/**
* @test
*/
public function itShouldThrowExceptionForUnsupportedWhere()
{
$this->setExpectedException($this->exceptionClass);
$this->query->where();
}
/**
* @test
*/
public function itShouldGetIntersectSelects()
{
$this->assertEquals(array(), $this->query->getUnions());
$select1 = new Select('user');
$select2 = new Select('user_email');
$this->query->add($select1);
$this->query->add($select2);
$this->assertEquals(array($select1, $select2), $this->query->getUnions());
}
}

View File

@ -0,0 +1,73 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 1:37 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Manipulation;
use NilPortugues\Sql\QueryBuilder\Manipulation\Update;
/**
* Class UpdateTest.
*/
class UpdateTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Update
*/
private $query;
/**
*
*/
protected function setUp()
{
$this->query = new Update();
}
/**
* @test
*/
public function itShouldGetPartName()
{
$this->assertSame('UPDATE', $this->query->partName());
}
/**
* @test
*/
public function itShouldReturnLimit1()
{
$this->query->limit(1);
$this->assertSame(1, $this->query->getLimitStart());
}
/**
* @test
*/
public function itShouldReturnValues()
{
$values = ['user_id' => 1];
$this->query->setValues($values);
$this->assertSame($values, $this->query->getValues());
}
/**
* @test
*/
public function itShouldSetNullableValues()
{
$values = ['user_id' => 1, 'description' => null, 'isVisible' => false];
$this->query->setValues($values);
$this->assertSame($values, $this->query->getValues());
}
}

View File

@ -0,0 +1,96 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/2/14
* Time: 11:54 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Syntax;
use NilPortugues\Sql\QueryBuilder\Syntax\Column;
use NilPortugues\Sql\QueryBuilder\Syntax\Table;
/**
* Class ColumnTest.
*/
class ColumnTest extends \PHPUnit_Framework_TestCase
{
/**
* @var string
*/
protected $tableClass = '\NilPortugues\Sql\QueryBuilder\Syntax\Table';
/**
* @var string
*/
protected $queryException = '\NilPortugues\Sql\QueryBuilder\Manipulation\QueryException';
/**
* @test
*/
public function itShouldReturnPartName()
{
$column = new Column('id', 'user');
$this->assertSame('COLUMN', $column->partName());
}
/**
* @test
*/
public function itShouldConstruct()
{
$column = new Column('id', 'user');
$this->assertEquals('id', $column->getName());
$this->assertInstanceOf($this->tableClass, $column->getTable());
$this->assertEquals('user', $column->getTable()->getName());
}
/**
* @test
*/
public function itShouldSetColumnName()
{
$column = new Column('id', 'user');
$column->setName('user_id');
$this->assertEquals('user_id', $column->getName());
}
/**
* @test
*/
public function itShouldSetTableName()
{
$tableName = 'user';
$column = new Column('id', $tableName);
$column->setTable(new Table($tableName));
$this->assertInstanceOf($this->tableClass, $column->getTable());
$this->assertEquals($tableName, $column->getTable()->getName());
}
/**
* @test
*/
public function itShouldSetAliasName()
{
$column = new Column('user_id', 'user', 'userId');
$this->assertEquals('userId', $column->getAlias());
}
/**
* @test
*/
public function itShouldThrowExceptionIfAliasOnAllSelection()
{
$this->setExpectedException($this->queryException);
new Column('*', 'user', 'userId');
}
}

View File

@ -0,0 +1,63 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:07 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Syntax;
use NilPortugues\Sql\QueryBuilder\Syntax\Column;
use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy;
/**
* Class OrderByTest.
*/
class OrderByTest extends \PHPUnit_Framework_TestCase
{
/**
* @var string
*/
protected $columnClass = '\NilPortugues\Sql\QueryBuilder\Syntax\Column';
/**
* @test
*/
public function itShouldConstructOrderBy()
{
$column = new Column('registration_date', 'user');
$order = new OrderBy($column, OrderBy::ASC);
$this->assertInstanceOf($this->columnClass, $order->getColumn());
$this->assertEquals(OrderBy::ASC, $order->getDirection());
}
/**
* @test
*/
public function itShouldGetOrderByDirection()
{
$column = new Column('registration_date', 'user');
$order = new OrderBy($column, OrderBy::ASC);
$this->assertEquals(OrderBy::ASC, $order->getDirection());
$order->setDirection(OrderBy::DESC);
$this->assertEquals(OrderBy::DESC, $order->getDirection());
}
/**
* @test
*/
public function itShouldThrowExceptionIfDirectionNotValid()
{
$column = new Column('registration_date', 'user');
$order = new OrderBy($column, OrderBy::ASC);
$this->setExpectedException('\InvalidArgumentException');
$order->setDirection('this is not a valid direction');
}
}

View File

@ -0,0 +1,97 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/2/14
* Time: 11:34 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Syntax;
use NilPortugues\Sql\QueryBuilder\Syntax\Table;
/**
* Class TableTest.
*/
class TableTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function testConstruct()
{
$table = new Table('user');
$this->assertEquals('user', $table->getName());
}
/**
* @test
*/
public function itShouldReturnNullIfTableNameHasNoAlias()
{
$table = new Table('user');
$this->assertNull($table->getAlias());
}
/**
* @test
*/
public function itShouldReturnAliasIfTableNameAliasHasBeenSet()
{
$table = new Table('user');
$table->setAlias('u');
$this->assertEquals('u', $table->getAlias());
}
/**
* @test
*/
public function itShouldReturnNullIfSchemaNotSet()
{
$table = new Table('user');
$this->assertNull($table->getSchema());
}
/**
* @test
*/
public function itShouldReturnSchemaIfSchemaHasValue()
{
$table = new Table('user', 'website');
$this->assertEquals('website', $table->getSchema());
}
/**
* @test
*/
public function itShouldReturnTheCompleteName()
{
$table = new Table('user');
$table->setAlias('p');
$table->setSchema('website');
$this->assertEquals('website.user AS p', $table->getCompleteName());
}
/**
* @test
*/
public function itShouldReturnFalseOnIsView()
{
$table = new Table('user_status');
$this->assertFalse($table->isView());
}
/**
* @test
*/
public function itShouldReturnTrueOnIsView()
{
$table = new Table('user_status');
$table->setView(true);
$this->assertTrue($table->isView());
}
}

View File

@ -0,0 +1,419 @@
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 6/3/14
* Time: 12:31 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Sql\QueryBuilder\Syntax;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
use NilPortugues\Sql\QueryBuilder\Syntax\Where;
use NilPortugues\Tests\Sql\QueryBuilder\Manipulation\Resources\DummyQuery;
/**
* Class WhereTest.
*/
class WhereTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Where
*/
protected $where;
/**
* @var string
*/
protected $whereClass = '\NilPortugues\Sql\QueryBuilder\Syntax\Where';
/**
* @var string
*/
protected $columnClass = '\NilPortugues\Sql\QueryBuilder\Syntax\Column';
/**
* @var string
*/
protected $queryException = '\NilPortugues\Sql\QueryBuilder\Manipulation\QueryException';
/**
*
*/
protected function setUp()
{
$query = new DummyQuery();
$query->setTable('users');
$this->where = new Where($query);
}
/**
* @test
*/
public function itShouldBeCloneable()
{
$this->assertEquals($this->where, clone $this->where);
}
/**
* @test
*/
public function itShouldBeEmptyOnConstruct()
{
$this->assertTrue($this->where->isEmpty());
}
/**
* @test
*/
public function itShouldReturnDefaultConjuctionAnd()
{
$this->assertSame('AND', $this->where->getConjunction());
}
/**
* @test
*/
public function itShouldReturnDefaultSubWhere()
{
$this->assertSame(array(), $this->where->getSubWheres());
}
/**
* @test
*/
public function itShouldReturnSubFilter()
{
$filter = $this->where->subWhere();
$this->assertSame(array(), $filter->getSubWheres());
$this->assertInstanceOf($this->whereClass, $filter);
}
/**
* @test
*/
public function itShouldReturnTheSameEqAndEqual()
{
$column = 'user_id';
$value = 1;
$this->assertSame(
$this->where->equals($column, $value),
$this->where->eq($column, $value)
);
}
/**
* @test
*/
public function itShouldNotBeEqualTo()
{
$column = 'user_id';
$value = 1;
$result = $this->where->notEquals($column, $value)->getComparisons();
$this->assertSame('<>', $result[0]['conjunction']);
$this->assertSame($column, $result[0]['subject']->getName());
$this->assertSame($value, $result[0]['target']);
}
/**
* @test
*/
public function itShouldBeGreaterThan()
{
$column = 'user_id';
$value = 1;
$result = $this->where->greaterThan($column, $value)->getComparisons();
$this->assertSame('>', $result[0]['conjunction']);
$this->assertSame($column, $result[0]['subject']->getName());
$this->assertSame($value, $result[0]['target']);
}
/**
* @test
*/
public function itShouldBeGreaterThanOrEqual()
{
$column = 'user_id';
$value = 1;
$result = $this->where->greaterThanOrEqual($column, $value)->getComparisons();
$this->assertSame('>=', $result[0]['conjunction']);
$this->assertSame($column, $result[0]['subject']->getName());
$this->assertSame($value, $result[0]['target']);
}
/**
* @test
*/
public function itShouldBeLessThan()
{
$column = 'user_id';
$value = 1;
$result = $this->where->lessThan($column, $value)->getComparisons();
$this->assertSame('<', $result[0]['conjunction']);
$this->assertSame($column, $result[0]['subject']->getName());
$this->assertSame($value, $result[0]['target']);
}
/**
* @test
*/
public function itShouldBeLessThanOrEqual()
{
$column = 'user_id';
$value = 1;
$result = $this->where->lessThanOrEqual($column, $value)->getComparisons();
$this->assertSame('<=', $result[0]['conjunction']);
$this->assertSame($column, $result[0]['subject']->getName());
$this->assertSame($value, $result[0]['target']);
}
/**
* @test
*/
public function itShouldBeLike()
{
$column = 'user_id';
$value = 1;
$result = $this->where->like($column, $value)->getComparisons();
$this->assertSame('LIKE', $result[0]['conjunction']);
$this->assertSame($column, $result[0]['subject']->getName());
$this->assertSame($value, $result[0]['target']);
}
/**
* @test
*/
public function itShouldBeNotLike()
{
$column = 'user_id';
$value = 1;
$result = $this->where->notLike($column, $value)->getComparisons();
$this->assertSame('NOT LIKE', $result[0]['conjunction']);
$this->assertSame($column, $result[0]['subject']->getName());
$this->assertSame($value, $result[0]['target']);
}
/**
* @test
*/
public function itShouldAccumulateMatchConditions()
{
$column = array('user_id');
$result = $this->where
->match($column, array(1, 2, 3))
->getMatches();
$expected = array(
0 => array(
'columns' => array('user_id'),
'values' => array(1, 2, 3),
'mode' => 'natural',
),
);
$this->assertEquals($expected, $result);
}
/**
* @test
*/
public function itShouldAccumulateMatchBooleanConditions()
{
$column = array('user_id');
$result = $this->where
->matchBoolean($column, array(1, 2, 3))
->getMatches();
$expected = array(
0 => array(
'columns' => array('user_id'),
'values' => array(1, 2, 3),
'mode' => 'boolean',
),
);
$this->assertEquals($expected, $result);
}
/**
* @test
*/
public function itShouldAccumulateMatchQueryExpansionConditions()
{
$column = array('user_id');
$result = $this->where
->matchWithQueryExpansion($column, array(1, 2, 3))
->getMatches();
$expected = array(
0 => array(
'columns' => array('user_id'),
'values' => array(1, 2, 3),
'mode' => 'query_expansion',
),
);
$this->assertEquals($expected, $result);
}
/**
* @test
*/
public function itShouldAccumulateInConditions()
{
$column = 'user_id';
$result = $this->where
->in($column, array(1, 2, 3))
->getIns();
$expected = array($column => array(1, 2, 3));
$this->assertEquals($expected, $result);
}
/**
* @test
*/
public function itShouldAccumulateNotInConditions()
{
$column = 'user_id';
$result = $this->where
->notIn($column, array(1, 2, 3))
->getNotIns();
$expected = array($column => array(1, 2, 3));
$this->assertEquals($expected, $result);
}
/**
* @test
*/
public function itShouldWriteBetweenConditions()
{
$column = 'user_id';
$result = $this->where
->between($column, 1, 2)
->getBetweens();
$this->assertInstanceOf($this->columnClass, $result[0]['subject']);
$this->assertEquals(1, $result[0]['a']);
$this->assertEquals(2, $result[0]['b']);
}
/**
* @test
*/
public function itShouldSetNullValueCondition()
{
$column = 'user_id';
$result = $this->where
->isNull($column)
->getNull();
$this->assertInstanceOf($this->columnClass, $result[0]['subject']);
}
/**
* @test
*/
public function itShouldSetIsNotNullValueCondition()
{
$column = 'user_id';
$result = $this->where
->isNotNull($column)
->getNotNull();
$this->assertInstanceOf($this->columnClass, $result[0]['subject']);
}
/**
* @test
*/
public function itShouldSetBitClauseValueCondition()
{
$column = 'user_id';
$result = $this->where
->addBitClause($column, 1)
->getBooleans();
$this->assertEquals(1, $result[0]['value']);
$this->assertInstanceOf($this->columnClass, $result[0]['subject']);
}
/**
* @test
*/
public function ItShouldChangeAndToOrOperator()
{
$result = $this->where->conjunction('OR');
$this->assertEquals('OR', $result->getConjunction());
}
/**
* @test
*/
public function itShouldThrowExceptionOnUnknownConjunction()
{
$this->setExpectedException($this->queryException);
$this->where->conjunction('NOT_VALID_CONJUNCTION');
}
/**
* @test
*/
public function itShouldSetExistsCondition()
{
$select1 = new Select('user');
$select1->where()->equals('user_id', 10);
$result = $this->where->exists($select1)->getExists();
$this->assertEquals(array($select1), $result);
}
/**
* @test
*/
public function itShouldSetNotExistsCondition()
{
$select1 = new Select('user');
$select1->where()->equals('user_id', 10);
$result = $this->where->notExists($select1)->getNotExists();
$this->assertEquals(array($select1), $result);
}
/**
* @test
*/
public function itShouldReturnLiterals()
{
$result = $this->where->asLiteral('(username is not null and status=:status)')->getComparisons();
$this->assertSame('(username is not null and status=:status)', $result[0]);
}
}