Added Config; concealed MongoDB dependency
This commit is contained in:
parent
1e3e3f94d8
commit
011d803bd0
7 changed files with 57 additions and 16 deletions
6
src/Config.php
Normal file
6
src/Config.php
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?php
|
||||||
|
namespace Szurubooru;
|
||||||
|
|
||||||
|
final class Config extends \ArrayObject
|
||||||
|
{
|
||||||
|
}
|
|
@ -7,10 +7,10 @@ abstract class AbstractDao implements ICrudDao
|
||||||
protected $collection;
|
protected $collection;
|
||||||
protected $entityName;
|
protected $entityName;
|
||||||
|
|
||||||
public function __construct(\MongoDB $mongoDb, $collectionName, $entityName)
|
public function __construct(\Szurubooru\Config $config, $collectionName, $entityName)
|
||||||
{
|
{
|
||||||
$this->db = $mongoDb;
|
$this->db = (new \Szurubooru\DatabaseConnection($config))->getDatabase();
|
||||||
$this->collection = $mongoDb->selectCollection($collectionName);
|
$this->collection = $this->db->selectCollection($collectionName);
|
||||||
$this->entityName = $entityName;
|
$this->entityName = $entityName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,8 @@ namespace Szurubooru\Dao;
|
||||||
|
|
||||||
final class PostDao extends AbstractDao implements ICrudDao
|
final class PostDao extends AbstractDao implements ICrudDao
|
||||||
{
|
{
|
||||||
public function __construct(\MongoDB $mongoDb)
|
public function __construct(\Szurubooru\Config $config)
|
||||||
{
|
{
|
||||||
parent::__construct($mongoDb, 'posts', '\Szurubooru\Entities\Post');
|
parent::__construct($config, 'posts', '\Szurubooru\Entities\Post');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
34
src/DatabaseConnection.php
Normal file
34
src/DatabaseConnection.php
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
<?php
|
||||||
|
namespace Szurubooru;
|
||||||
|
|
||||||
|
final class DatabaseConnection
|
||||||
|
{
|
||||||
|
private $database;
|
||||||
|
private $connection;
|
||||||
|
|
||||||
|
public function __construct(\Szurubooru\Config $config)
|
||||||
|
{
|
||||||
|
$connectionString = $this->getConnectionString($config);
|
||||||
|
$this->connection = new \MongoClient($connectionString);
|
||||||
|
$this->database = $this->connection->selectDb($config->databaseName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getConnection()
|
||||||
|
{
|
||||||
|
return $this->connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDatabase()
|
||||||
|
{
|
||||||
|
return $this->database;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getConnectionString(\Szurubooru\Config $config)
|
||||||
|
{
|
||||||
|
return sprintf(
|
||||||
|
'mongodb://%s:%d/%s',
|
||||||
|
$config->databaseHost,
|
||||||
|
$config->databasePort,
|
||||||
|
$config->databaseName);
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,9 +5,9 @@ final class UpgradeService
|
||||||
{
|
{
|
||||||
private $db;
|
private $db;
|
||||||
|
|
||||||
public function __construct(\MongoDB $db)
|
public function __construct(\Szurubooru\Config $config)
|
||||||
{
|
{
|
||||||
$this->db = $db;
|
$this->db = (new DatabaseConnection($config))->getDatabase();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function prepareForUsage()
|
public function prepareForUsage()
|
||||||
|
|
|
@ -12,10 +12,11 @@ abstract class AbstractDatabaseTest extends \PHPUnit_Framework_TestCase
|
||||||
$host = 'localhost';
|
$host = 'localhost';
|
||||||
$port = 27017;
|
$port = 27017;
|
||||||
$database = 'test';
|
$database = 'test';
|
||||||
$connectingString = sprintf('mongodb://%s:%d/%s', $host, $port, $database);
|
$this->config = new \Szurubooru\Config();
|
||||||
$this->connection = new \Mongo($connectingString);
|
$this->config->databaseHost = 'localhost';
|
||||||
$this->db = $this->connection->selectDb($database);
|
$this->config->databasePort = 27017;
|
||||||
$this->upgradeService = new \Szurubooru\UpgradeService($this->db);
|
$this->config->databaseName = 'test';
|
||||||
|
$this->upgradeService = new \Szurubooru\UpgradeService($this->config);
|
||||||
$this->upgradeService->prepareForUsage();
|
$this->upgradeService->prepareForUsage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest
|
||||||
{
|
{
|
||||||
public function testSaving()
|
public function testSaving()
|
||||||
{
|
{
|
||||||
$postDao = new \Szurubooru\Dao\PostDao($this->db);
|
$postDao = new \Szurubooru\Dao\PostDao($this->config);
|
||||||
|
|
||||||
$post = new \Szurubooru\Entities\Post();
|
$post = new \Szurubooru\Entities\Post();
|
||||||
$post->name = 'test2';
|
$post->name = 'test2';
|
||||||
|
@ -24,7 +24,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest
|
||||||
|
|
||||||
public function testGettingAll()
|
public function testGettingAll()
|
||||||
{
|
{
|
||||||
$postDao = new \Szurubooru\Dao\PostDao($this->db);
|
$postDao = new \Szurubooru\Dao\PostDao($this->config);
|
||||||
|
|
||||||
$post1 = new \Szurubooru\Entities\Post();
|
$post1 = new \Szurubooru\Entities\Post();
|
||||||
$post1->name = 'test2';
|
$post1->name = 'test2';
|
||||||
|
@ -45,7 +45,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest
|
||||||
|
|
||||||
public function testGettingById()
|
public function testGettingById()
|
||||||
{
|
{
|
||||||
$postDao = new \Szurubooru\Dao\PostDao($this->db);
|
$postDao = new \Szurubooru\Dao\PostDao($this->config);
|
||||||
|
|
||||||
$post1 = new \Szurubooru\Entities\Post();
|
$post1 = new \Szurubooru\Entities\Post();
|
||||||
$post1->name = 'test2';
|
$post1->name = 'test2';
|
||||||
|
@ -63,7 +63,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest
|
||||||
|
|
||||||
public function testDeletingAll()
|
public function testDeletingAll()
|
||||||
{
|
{
|
||||||
$postDao = new \Szurubooru\Dao\PostDao($this->db);
|
$postDao = new \Szurubooru\Dao\PostDao($this->config);
|
||||||
|
|
||||||
$post1 = new \Szurubooru\Entities\Post();
|
$post1 = new \Szurubooru\Entities\Post();
|
||||||
$post1->name = 'test2';
|
$post1->name = 'test2';
|
||||||
|
@ -84,7 +84,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest
|
||||||
|
|
||||||
public function testDeletingById()
|
public function testDeletingById()
|
||||||
{
|
{
|
||||||
$postDao = new \Szurubooru\Dao\PostDao($this->db);
|
$postDao = new \Szurubooru\Dao\PostDao($this->config);
|
||||||
|
|
||||||
$post1 = new \Szurubooru\Entities\Post();
|
$post1 = new \Szurubooru\Entities\Post();
|
||||||
$post1->name = 'test2';
|
$post1->name = 'test2';
|
||||||
|
|
Loading…
Reference in a new issue