Permalink
Browse files

Adds unit test for changelog repository

  • Loading branch information...
jacobemerick committed Feb 21, 2016
1 parent e8c5811 commit ddb236d56925aa8088a0748ea953443b22ea88a0
@@ -27,7 +27,7 @@ public function __construct(ConnectionLocator $connections)
public function getChanges($limit = null, $offset = 0)
{
$query = "
SELECT `message`, `message_short`, `datetime`, `commit_link`
SELECT `id`, `message`, `message_short`, `datetime`, `commit_link`
FROM `jpemeric_stream`.`changelog`
ORDER BY `datetime` DESC";
if (!is_null($limit)) {
@@ -9,9 +9,34 @@
class MysqlChangelogRepositoryTest extends PHPUnit_Framework_TestCase
{
protected static $connection;
public static function setUpBeforeClass()
{
$extendedPdo = new ExtendedPdo('sqlite::memory:');
$extendedPdo->exec("ATTACH DATABASE `jpemeric_stream.db` AS `jpemeric_stream`");
$extendedPdo->exec("
CREATE TABLE IF NOT EXISTS `jpemeric_stream`.`changelog` (
`id` integer PRIMARY KEY AUTOINCREMENT,
`hash` char(40) NOT NULL,
`message` text,
`message_short` varchar(100),
`datetime` datetime NOT NULL,
`author` varchar(50) NOT NULL,
`commit_link` varchar(100) NOT NULL,
`created_at` datetime,
`updated_at` datetime
)"
);
self::$connection = new ConnectionLocator(function () use ($extendedPdo) {
return $extendedPdo;
});
}
public function testIsInstanceOfChangelogRepository()
{
$repository = new MysqlChangelogRepository(new ConnectionLocator());
$repository = new MysqlChangelogRepository(self::$connection);
$this->assertInstanceOf(
'Jacobemerick\Web\Domain\Stream\Changelog\MysqlChangelogRepository',
@@ -21,7 +46,7 @@ public function testIsInstanceOfChangelogRepository()
public function testImplementsChangelogInterface()
{
$repository = new MysqlChangelogRepository(new ConnectionLocator());
$repository = new MysqlChangelogRepository(self::$connection);
$this->assertInstanceOf(
'Jacobemerick\Web\Domain\Stream\Changelog\ChangelogRepositoryInterface',
@@ -31,13 +56,137 @@ public function testImplementsChangelogInterface()
public function testConstructSetsConnections()
{
$connection = new ConnectionLocator();
$respository = new MysqlChangelogRepository($connection);
$respository = new MysqlChangelogRepository(self::$connection);
$this->assertAttributeSame(
$connection,
self::$connection,
'connections',
$respository
);
}
public function testGetChanges()
{
$testData = [
[
'id' => rand(1, 100),
'message' => 'test one',
],
[
'id' => rand(101, 200),
'message' => 'test two',
],
];
array_walk($testData, [$this, 'insertData']);
$repository = new MysqlChangelogRepository(self::$connection);
$data = $repository->getChanges();
$this->assertNotFalse($data);
$this->assertInternalType('array', $data);
foreach ($testData as $key => $testRow) {
$this->assertInternalType('array', $testRow);
$this->assertArraySubset($testRow, $data[$key]);
}
}
public function testGetChangesFailure()
{
$repository = new MysqlChangelogRepository(self::$connection);
$data = $repository->getChanges();
$this->assertEmpty($data);
$this->assertInternalType('array', $data);
}
public function testGetChangesRange()
{
$testData = [
[
'id' => rand(1, 100),
'message' => 'test one',
],
[
'id' => rand(101, 200),
'message' => 'test two',
],
[
'id' => rand(201, 300),
'message' => 'test three',
],
];
array_walk($testData, [$this, 'insertData']);
$repository = new MysqlChangelogRepository(self::$connection);
$data = $repository->getChanges(2, 1);
$this->assertNotFalse($data);
$this->assertInternalType('array', $data);
$this->assertCount(2, $data);
$testData = array_slice($testData, 1, 2);
foreach ($testData as $key => $testRow) {
$this->assertInternalType('array', $testRow);
$this->assertArraySubset($testRow, $data[$key]);
}
}
public function testGetChangesRangeFailure()
{
$testData = [
[
'id' => rand(1, 100),
'message' => 'test one',
],
[
'id' => rand(101, 200),
'message' => 'test two',
],
];
array_walk($testData, [$this, 'insertData']);
$repository = new MysqlChangelogRepository(self::$connection);
$data = $repository->getChanges(1, 3);
$this->assertEmpty($data);
$this->assertInternalType('array', $data);
}
protected function tearDown()
{
self::$connection->getDefault()->perform("DELETE FROM `jpemeric_stream`.`changelog`");
}
public static function tearDownAfterClass()
{
self::$connection->getDefault()->disconnect();
unlink('jpemeric_stream.db');
}
protected function insertData(array $data)
{
$defaultData = [
'id' => null,
'hash' => '',
'message' => null,
'message_short' => null,
'datetime' => '',
'author' => '',
'commit_link' => '',
];
$data = array_merge($defaultData, $data);
return self::$connection->getDefault()->perform("
INSERT INTO `jpemeric_stream`.`changelog`
(id, hash, message, message_short, datetime, author, commit_link)
VALUES
(:id, :hash, :message, :message_short, :datetime, :author, :commit_link)",
$data
);
}
}

0 comments on commit ddb236d

Please sign in to comment.