diff --git a/src/Domain/Blog/Series/MysqlSeriesRepository.php b/src/Domain/Blog/Series/MysqlSeriesRepository.php index b8e4945..cd84e51 100644 --- a/src/Domain/Blog/Series/MysqlSeriesRepository.php +++ b/src/Domain/Blog/Series/MysqlSeriesRepository.php @@ -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` = ( diff --git a/tests/unit/Domain/Blog/Series/MysqlSeriesRepositoryTest.php b/tests/unit/Domain/Blog/Series/MysqlSeriesRepositoryTest.php new file mode 100644 index 0000000..5e31db7 --- /dev/null +++ b/tests/unit/Domain/Blog/Series/MysqlSeriesRepositoryTest.php @@ -0,0 +1,226 @@ +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 + ); + } +} diff --git a/tests/unit/Domain/Stream/Activity/MysqlActivityRepositoryTest.php b/tests/unit/Domain/Stream/Activity/MysqlActivityRepositoryTest.php index d3ac846..aa74b73 100644 --- a/tests/unit/Domain/Stream/Activity/MysqlActivityRepositoryTest.php +++ b/tests/unit/Domain/Stream/Activity/MysqlActivityRepositoryTest.php @@ -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 )" ); @@ -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]); } } @@ -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]); } } @@ -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]); } } @@ -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]); } } diff --git a/tests/unit/Domain/Stream/Changelog/MysqlChangelogRepositoryTest.php b/tests/unit/Domain/Stream/Changelog/MysqlChangelogRepositoryTest.php index 99739c8..7b81e30 100644 --- a/tests/unit/Domain/Stream/Changelog/MysqlChangelogRepositoryTest.php +++ b/tests/unit/Domain/Stream/Changelog/MysqlChangelogRepositoryTest.php @@ -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 )" ); @@ -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]);