Skip to content

Commit

Permalink
Adds start to blog series repository test
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobemerick committed Feb 21, 2016
1 parent 89e88ac commit 8c8014f
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/Domain/Blog/Series/MysqlSeriesRepository.php
Expand Up @@ -26,10 +26,10 @@ public function __construct(ConnectionLocator $connections)
public function getSeriesForPost($post)
{
$query = "
SELECT `series`.`title` AS `series_title`, `series`.`description` AS `series_descriptions`,
`post.id` AS `post`, `post`.`title`, `post`.`category`, `post`.`path`
SELECT `series`.`title` AS `series_title`, `series`.`description` AS `series_description`,
`post`.`id` AS `post`, `post`.`title`, `post`.`category`, `post`.`path`
FROM `jpemeric_blog`.`series`
INNER JOIN `jpemeric_blog`.`series_post` ON `series_post`.`series` = `series.`id`
INNER JOIN `jpemeric_blog`.`series_post` ON `series_post`.`series` = `series`.`id`
INNER JOIN `jpemeric_blog`.`post` ON `post`.`id` = `series_post`.`post` AND
`post`.`display` = :is_active
WHERE `series`.`id` = (
Expand Down
226 changes: 226 additions & 0 deletions tests/unit/Domain/Blog/Series/MysqlSeriesRepositoryTest.php
@@ -0,0 +1,226 @@
<?php

namespace Jacobemerick\Web\Domain\Blog\Series;

use Aura\Sql\ConnectionLocator;
use Aura\Sql\ExtendedPdo;
use PHPUnit_Framework_TestCase;

class MysqlSeriesRepositoryTest extends PHPUnit_Framework_TestCase
{

protected static $connection;

public static function setUpBeforeClass()
{
$extendedPdo = new ExtendedPdo('sqlite::memory:');
$extendedPdo->exec("ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`");

$extendedPdo->exec("
CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`post` (
`id` integer PRIMARY KEY AUTOINCREMENT,
`title` varchar(60) NOT NULL,
`path` varchar(60) NOT NULL,
`category` varchar(15) NOT NULL,
`date` datetime,
`body` text,
`display` integer(1) NOT NULL
)"
);
$extendedPdo->exec("
CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`series` (
`id` integer PRIMARY KEY AUTOINCREMENT,
`title` text NOT NULL,
`description` text NOT NULL
)"
);
$extendedPdo->exec("
CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`series_post` (
`series` integer NOT NULL,
`post` integer NOT NULL,
`order` integer(1) NOT NULL
)"
);

self::$connection = new ConnectionLocator(function () use ($extendedPdo) {
return $extendedPdo;
});
}

public function testIsInstanceOfSeriesRepository()
{
$repository = new MysqlSeriesRepository(self::$connection);

$this->assertInstanceOf(
'Jacobemerick\Web\Domain\Blog\Series\MysqlSeriesRepository',
$repository
);
}

public function testImplementsSeriesInterface()
{
$repository = new MysqlSeriesRepository(self::$connection);

$this->assertInstanceOf(
'Jacobemerick\Web\Domain\Blog\Series\SeriesRepositoryInterface',
$repository
);
}

public function testConstructSetsConnections()
{
$respository = new MysqlSeriesRepository(self::$connection);

$this->assertAttributeSame(
self::$connection,
'connections',
$respository
);
}

public function testGetSeriesForPost()
{
$testPostData = [
[
'id' => rand(1, 100),
'title' => 'test one',
'category' => 'test category',
'path' => 'test-one',
'display' => 1,
],
[
'id' => rand(101, 200),
'title' => 'test two',
'category' => 'test category',
'path' => 'test-two',
'display' => 1,
],
];

$testSeriesData = [
[
'id' => rand(1, 100),
'title' => 'test one',
'description' => 'test description',
],
];

$testSeriesPostData = [
[
'series' => reset($testSeriesData)['id'],
'post' => $testPostData[0]['id'],
'order' => 1,
],
[
'series' => reset($testSeriesData)['id'],
'post' => $testPostData[1]['id'],
'order' => 2,
],
];

array_walk($testPostData, [$this, 'insertPostData']);
array_walk($testSeriesData, [$this, 'insertSeriesData']);
array_walk($testSeriesPostData, [$this, 'insertSeriesPostData']);

$repository = new MysqlSeriesRepository(self::$connection);
$data = $repository->getSeriesForPost(reset($testPostData)['id']);

$this->assertNotFalse($data);
$this->assertInternalType('array', $data);
foreach ($testPostData as $key => $testPostRow) {
$this->assertInternalType('array', $data[$key]);
$this->assertArrayHasKey('series_title', $data[$key]);
$this->assertEquals(reset($testSeriesData)['title'], $data[$key]['series_title']);
$this->assertArrayHasKey('series_description', $data[$key]);
$this->assertEquals(reset($testSeriesData)['description'], $data[$key]['series_description']);
$this->assertArrayHasKey('post', $data[$key]);
$this->assertEquals($testPostRow['id'], $data[$key]['post']);
$this->assertArrayHasKey('title', $data[$key]);
$this->assertEquals($testPostRow['title'], $data[$key]['title']);
$this->assertArrayHasKey('category', $data[$key]);
$this->assertEquals($testPostRow['category'], $data[$key]['category']);
$this->assertArrayHasKey('path', $data[$key]);
$this->assertEquals($testPostRow['path'], $data[$key]['path']);
}
}

public function testGetSeriesForPostOrder() {}

public function testGetSeriesForPostSeriesFilter() {}

public function testGetSeriesForPostInactive() {}

public function testGetSeriesForPostFailure() {}

protected function tearDown()
{
self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`series`");
}

public static function tearDownAfterClass()
{
self::$connection->getDefault()->disconnect();
unlink('jpemeric_blog.db');
}

protected function insertPostData(array $data)
{
$defaultData = [
'id' => null,
'title' => '',
'path' => '',
'category' => '',
'date' => '',
'body' => '',
'display' => 0,
];

$data = array_merge($defaultData, $data);

return self::$connection->getDefault()->perform("
INSERT INTO `jpemeric_blog`.`post`
(id, title, path, category, date, body, display)
VALUES
(:id, :title, :path, :category, :date, :body, :display)",
$data
);
}

protected function insertSeriesData(array $data)
{
$defaultData = [
'id' => null,
'title' => '',
'description' => '',
];

$data = array_merge($defaultData, $data);

return self::$connection->getDefault()->perform("
INSERT INTO `jpemeric_blog`.`series`
(id, title, description)
VALUES
(:id, :title, :description)",
$data
);
}

protected function insertSeriesPostData(array $data)
{
$defaultData = [
'series' => '',
'post' => '',
'order' => 0,
];

$data = array_merge($defaultData, $data);

return self::$connection->getDefault()->perform("
INSERT INTO `jpemeric_blog`.`series_post`
(series, post, `order`)
VALUES
(:series, :post, :order)",
$data
);
}
}
27 changes: 14 additions & 13 deletions tests/unit/Domain/Stream/Activity/MysqlActivityRepositoryTest.php
Expand Up @@ -15,17 +15,18 @@ 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`.`activity` (
`id` integer PRIMARY KEY AUTOINCREMENT,
`message` text NOT NULL,
`message_long` text NOT NULL,
`datetime` datetime NOT NULL,
`metadata` text NOT NULL,
`type` varchar(10) NOT NULL,
`type_id` integer NOT NULL,
`created_at` datetime,
`updated_at` datetime
`id` integer PRIMARY KEY AUTOINCREMENT,
`message` text NOT NULL,
`message_long` text NOT NULL,
`datetime` datetime NOT NULL,
`metadata` text NOT NULL,
`type` varchar(10) NOT NULL,
`type_id` integer NOT NULL,
`created_at` datetime,
`updated_at` datetime
)"
);

Expand Down Expand Up @@ -118,7 +119,7 @@ public function testGetActivities()
$this->assertNotFalse($data);
$this->assertInternalType('array', $data);
foreach ($testData as $key => $testRow) {
$this->assertInternalType('array', $testRow);
$this->assertInternalType('array', $data[$key]);
$this->assertArraySubset($testRow, $data[$key]);
}
}
Expand Down Expand Up @@ -161,7 +162,7 @@ public function testGetActivitiesRange()
$testData = array_slice($testData, 1, 2);

foreach ($testData as $key => $testRow) {
$this->assertInternalType('array', $testRow);
$this->assertInternalType('array', $data[$key]);
$this->assertArraySubset($testRow, $data[$key]);
}
}
Expand Down Expand Up @@ -255,7 +256,7 @@ public function testGetActivitiesByType()
$testData = array_values($testData);

foreach ($testData as $key => $testRow) {
$this->assertInternalType('array', $testRow);
$this->assertInternalType('array', $data[$key]);
$this->assertArraySubset($testRow, $data[$key]);
}
}
Expand Down Expand Up @@ -325,7 +326,7 @@ public function testGetActivitiesByTypeRange()
$testData = array_slice($testData, 1, 2);

foreach ($testData as $key => $testRow) {
$this->assertInternalType('array', $testRow);
$this->assertInternalType('array', $data[$key]);
$this->assertArraySubset($testRow, $data[$key]);
}
}
Expand Down
21 changes: 11 additions & 10 deletions tests/unit/Domain/Stream/Changelog/MysqlChangelogRepositoryTest.php
Expand Up @@ -15,17 +15,18 @@ 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
`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
)"
);

Expand Down Expand Up @@ -86,7 +87,7 @@ public function testGetChanges()
$this->assertNotFalse($data);
$this->assertInternalType('array', $data);
foreach ($testData as $key => $testRow) {
$this->assertInternalType('array', $testRow);
$this->assertInternalType('array', $data[$key]);
$this->assertArraySubset($testRow, $data[$key]);
$this->assertArrayHasKey('id', $data[$key]);
$this->assertArrayHasKey('message', $data[$key]);
Expand Down

0 comments on commit 8c8014f

Please sign in to comment.