-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from Icinga/introduce-test-case
Introduce `TestCase` and `SqlAssertions`
- Loading branch information
Showing
10 changed files
with
117 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.