Skip to content

Commit

Permalink
Adds potential repository test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobemerick committed Nov 28, 2015
1 parent de1ee7d commit d92e416
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
2 changes: 1 addition & 1 deletion phpunit.xml
@@ -1,4 +1,4 @@
<phpunit bootstrap="bootstrap.php">
<phpunit bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite name="unit">
<directory>tests/unit</directory>
Expand Down
6 changes: 6 additions & 0 deletions tests/bootstrap.php
@@ -0,0 +1,6 @@
<?php

error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');

require_once 'vendor/autoload.php';
79 changes: 79 additions & 0 deletions tests/unit/domain/blog/MysqlPostRepositoryTest.php
@@ -0,0 +1,79 @@
<?php

namespace Jacobemerick\Web\Domain\Blog;

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

class MysqlPostRepositoryTest extends \PHPUnit_Framework_TestCase
{

protected $connections;

public function __construct()
{
$extendedPdo = $this->newExtendedPdo();
$this->connections = new ConnectionLocator(function () use ($extendedPdo) {
return $extendedPdo;
});
}

protected function newExtendedPdo()
{
$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(20) NOT NULL,
`date` date NOT NULL,
`body` text NOT NULL,
`display` integer NOT NULL
)"
);
return $extendedPdo;
}

public function newMysqlPostRepository()
{
return new MysqlPostRepository($this->connections);
}

public function testFindByUri()
{
$test_post = [
'title' => 'test title',
'path' => 'test-uri',
'category' => 'test',
'date' => date('Y-m-d H:i:s'),
'body' => 'test content',
'display' => 1,
];

$this->connections->getDefault()->perform("
INSERT INTO jpemeric_blog.post
(title, path, category, date, body, display)
VALUES
(:title, :path, :category, :date, :body, :display)",
$test_post);

$post = $this->newMysqlPostRepository()->findByUri($test_post['path']);
$this->assertSame($test_post['path'], $post['path']);
}

public function testIsInstanceOfPostRepository()
{
$this->assertInstanceOf(
'Jacobemerick\Web\Domain\Blog\PostRepository',
$this->newMysqlPostRepository()
);
}

public static function tearDownAfterClass()
{
// $this->connections->getDefault()->disconnect();
unlink('jpemeric_blog.db');
}
}

0 comments on commit d92e416

Please sign in to comment.