Skip to content

Commit

Permalink
Merge pull request #61 from Icinga/introduce-test-case
Browse files Browse the repository at this point in the history
Introduce `TestCase` and `SqlAssertions`
  • Loading branch information
nilmerg authored Dec 13, 2022
2 parents 47e61cb + 5c4f9a3 commit cb4f5a2
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 200 deletions.
24 changes: 1 addition & 23 deletions tests/DeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,8 @@
use ipl\Sql\Delete;
use ipl\Sql\QueryBuilder;

class DeleteTest extends \PHPUnit\Framework\TestCase
class DeleteTest extends TestCase
{
/**
* The DELETE query to test
*
* @var Delete
*/
protected $query;

/**
* The SQL query builder
*
* @var QueryBuilder
*/
protected $queryBuilder;

public function setupTestTest()
{
$this->query = new Delete();
Expand Down Expand Up @@ -53,12 +39,4 @@ public function testFromWithArray()
$this->assertSame(['t1' => 'table'], $this->query->getFrom());
$this->assertCorrectStatementAndValues('DELETE FROM table t1', []);
}

protected function assertCorrectStatementAndValues($statement, $values)
{
list($actualStatement, $actualValues) = $this->queryBuilder->assembleDelete($this->query);

$this->assertSame($statement, $actualStatement);
$this->assertSame($values, $actualValues);
}
}
32 changes: 1 addition & 31 deletions tests/HavingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,10 @@
namespace ipl\Tests\Sql;

use ipl\Sql\Expression;
use ipl\Sql\QueryBuilder;
use ipl\Sql\Select;
use ipl\Sql\Sql;

class HavingTest extends \PHPUnit\Framework\TestCase
class HavingTest extends TestCase
{
/**
* The SELECT query to test
*
* @var Select
*/
protected $query;

/**
* The SQL query builder
*
* @var QueryBuilder
*/
protected $queryBuilder;

public function setupTest()
{
$this->query = new Select();
$this->queryBuilder = new QueryBuilder(new TestAdapter());
}

public function testHavingStringFormat()
{
$this->setupTest();
Expand Down Expand Up @@ -95,12 +73,4 @@ public function testResetHaving()
$this->query->getHaving()
);
}

protected function assertCorrectStatementAndValues($statement, $values)
{
list($actualStatement, $actualValues) = $this->queryBuilder->assembleSelect($this->query);

$this->assertSame($statement, $actualStatement);
$this->assertSame($values, $actualValues);
}
}
32 changes: 4 additions & 28 deletions tests/InsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,14 @@

use ipl\Sql\Expression;
use ipl\Sql\Insert;
use ipl\Sql\QueryBuilder;
use ipl\Sql\Select;

class InsertTest extends \PHPUnit\Framework\TestCase
class InsertTest extends TestCase
{
/**
* The INSERT query to test
*
* @var Insert
*/
protected $query;

/**
* The SQL query builder
*
* @var QueryBuilder
*/
protected $queryBuilder;
protected $queryClass = Insert::class;

public function setupTest()
{
$this->query = new Insert();
$this->queryBuilder = new QueryBuilder(new TestAdapter());
}
/** @var Insert */
protected $query;

public function testEmptyInsertInto()
{
Expand Down Expand Up @@ -157,12 +141,4 @@ public function testInsertIntoStatementWithColumnsAndValues()

$this->assertCorrectStatementAndValues('INSERT INTO table (c1,c2) VALUES(?,?)', ['v1', 'v2']);
}

protected function assertCorrectStatementAndValues($statement, $values)
{
list($actualStatement, $actualValues) = $this->queryBuilder->assembleInsert($this->query);

$this->assertSame($statement, $actualStatement);
$this->assertSame($values, $actualValues);
}
}
47 changes: 47 additions & 0 deletions tests/Lib/SqlAssertions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace ipl\Tests\Sql\Lib;

use ipl\Sql\Delete;
use ipl\Sql\Insert;
use ipl\Sql\QueryBuilder;
use ipl\Sql\Select;
use ipl\Sql\Update;

trait SqlAssertions
{
/** @var string The adapter to use */
protected $adapterClass = TestAdapter::class;

/** @var QueryBuilder */
protected $queryBuilder;

protected function setUp(): void
{
$this->queryBuilder = new QueryBuilder(new $this->adapterClass());
}

/**
* Assert that the given statement equals the given SQL once assembled
*
* @param string $sql
* @param Delete|Insert|Select|Update $statement
* @param ?array $values
* @param string $message
*
* @return void
*/
public function assertSql(string $sql, $statement, array $values = null, string $message = ''): void
{
// Reduce whitespaces to just one space
$sql = preg_replace('/\s+/', ' ', trim($sql));

list($stmt, $bind) = $this->queryBuilder->assemble($statement);

$this->assertSame($sql, $stmt, $message);

if ($values !== null) {
$this->assertSame($values, $bind, $message);
}
}
}
12 changes: 12 additions & 0 deletions tests/Lib/TestAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace ipl\Tests\Sql\Lib;

use ipl\Sql\Adapter\BaseAdapter;

/**
* ANSI SQL test adapter
*/
class TestAdapter extends BaseAdapter
{
}
33 changes: 3 additions & 30 deletions tests/Mssql/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,11 @@
namespace ipl\Tests\Sql\Mssql;

use ipl\Sql\Adapter\Mssql;
use ipl\Sql\QueryBuilder;
use ipl\Sql\Select;
use ipl\Tests\Sql\TestCase;

class SelectTest extends \PHPUnit\Framework\TestCase
class SelectTest extends TestCase
{
/**
* The SELECT query to test
*
* @var Select
*/
protected $query;

/**
* The SQL query builder
*
* @var QueryBuilder
*/
protected $queryBuilder;

public function setupTest()
{
$this->query = new Select();
$this->queryBuilder = new QueryBuilder(new Mssql());
}
protected $adapterClass = Mssql::class;

public function testLimitOffset()
{
Expand Down Expand Up @@ -63,12 +44,4 @@ public function testAutomaticallyFixesLimitWithoutOrder()

$this->assertCorrectStatementAndValues('SELECT a FROM b ORDER BY 1 OFFSET 30 ROWS FETCH NEXT 10 ROWS ONLY');
}

protected function assertCorrectStatementAndValues($statement, array $values = [])
{
list($actualStatement, $actualValues) = $this->queryBuilder->assembleSelect($this->query);

$this->assertSame($statement, $actualStatement);
$this->assertSame($values, $actualValues);
}
}
31 changes: 1 addition & 30 deletions tests/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,12 @@
namespace ipl\Tests\Sql;

use ipl\Sql\Expression;
use ipl\Sql\QueryBuilder;
use ipl\Sql\Select;
use ipl\Sql\Sql;
use UnexpectedValueException;

class SelectTest extends \PHPUnit\Framework\TestCase
class SelectTest extends TestCase
{
/**
* The SELECT query to test
*
* @var Select
*/
protected $query;

/**
* The SQL query builder
*
* @var QueryBuilder
*/
protected $queryBuilder;

public function setupTest()
{
$this->query = new Select();
$this->queryBuilder = new QueryBuilder(new TestAdapter());
}

public function testDistinct()
{
$this->setupTest();
Expand Down Expand Up @@ -821,12 +800,4 @@ public function testInvalidOderByDirectionsThrowAnError()

$this->queryBuilder->assembleSelect($this->query);
}

protected function assertCorrectStatementAndValues($statement, $values)
{
list($actualStatement, $actualValues) = $this->queryBuilder->assembleSelect($this->query);

$this->assertSame($statement, $actualStatement);
$this->assertSame($values, $actualValues);
}
}
43 changes: 43 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace ipl\Tests\Sql;

use ipl\Sql\Select;
use ipl\Tests\Sql\Lib\SqlAssertions;

abstract class TestCase extends \PHPUnit\Framework\TestCase
{
use SqlAssertions {
SqlAssertions::setUp as sqlAssertionsSetUp;
}

/** @var string The statement to use */
protected $queryClass = Select::class;

/** @var Select The statement in use */
protected $query;

protected function setUp(): void
{
$this->query = new $this->queryClass();
$this->sqlAssertionsSetUp();
}

/** @deprecated Unused. */
protected function setupTest()
{
}

/**
* @deprecated Use {@see self::assertSql} instead.
*
* @param string $statement
* @param array $values
*
* @return void
*/
protected function assertCorrectStatementAndValues($statement, $values = null)
{
$this->assertSql($statement, $this->query, $values);
}
}
32 changes: 4 additions & 28 deletions tests/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,15 @@
namespace ipl\Tests\Sql;

use ipl\Sql\Expression;
use ipl\Sql\QueryBuilder;
use ipl\Sql\Select;
use ipl\Sql\Update;

class UpdateTest extends \PHPUnit\Framework\TestCase
class UpdateTest extends TestCase
{
/**
* The UPDATE query to test
*
* @var Update
*/
protected $query;

/**
* The SQL query builder
*
* @var QueryBuilder
*/
protected $queryBuilder;
protected $queryClass = Update::class;

public function setupTest()
{
$this->query = new Update();
$this->queryBuilder = new QueryBuilder(new TestAdapter());
}
/** @var Update */
protected $query;

public function testEmptyUpdateTable()
{
Expand Down Expand Up @@ -100,12 +84,4 @@ public function testUpdateStatementWithSet()

$this->assertCorrectStatementAndValues('UPDATE table SET c1 = ?, c2 = ?', ['v1', 'v2']);
}

protected function assertCorrectStatementAndValues($statement, $values)
{
list($actualStatement, $actualValues) = $this->queryBuilder->assembleUpdate($this->query);

$this->assertSame($statement, $actualStatement);
$this->assertSame($values, $actualValues);
}
}
Loading

0 comments on commit cb4f5a2

Please sign in to comment.