Simplified injecting db connection for upgrades

This commit is contained in:
Marcin Kurczewski 2014-09-27 10:06:22 +02:00
parent 2fc6a23e46
commit df939e0343
5 changed files with 18 additions and 9 deletions

View file

@ -22,12 +22,6 @@ abstract class AbstractDao implements ICrudDao
}); });
} }
public function setDatabaseConnection(\Szurubooru\DatabaseConnection $databaseConnection)
{
$this->pdo = $databaseConnection->getPDO();
$this->fpdo = new \FluentPDO($this->pdo);
}
public function getTableName() public function getTableName()
{ {
return $this->tableName; return $this->tableName;
@ -189,6 +183,12 @@ abstract class AbstractDao implements ICrudDao
return $entities; return $entities;
} }
private function setDatabaseConnection(\Szurubooru\DatabaseConnection $databaseConnection)
{
$this->pdo = $databaseConnection->getPDO();
$this->fpdo = new \FluentPDO($this->pdo);
}
private function decorateQueryFromFilter($query, \Szurubooru\SearchServices\Filters\IFilter $filter) private function decorateQueryFromFilter($query, \Szurubooru\SearchServices\Filters\IFilter $filter)
{ {
foreach ($filter->getRequirements() as $requirement) foreach ($filter->getRequirements() as $requirement)

View file

@ -22,6 +22,11 @@ final class Injector
{ {
return self::$container->get($className); return self::$container->get($className);
} }
public static function set($className, $object)
{
return self::$container->set($className, $object);
}
} }
Injector::init(); Injector::init();

View file

@ -19,8 +19,6 @@ class Upgrade04 implements IUpgrade
public function run(\Szurubooru\DatabaseConnection $databaseConnection) public function run(\Szurubooru\DatabaseConnection $databaseConnection)
{ {
$this->postDao->setDatabaseConnection($databaseConnection);
$databaseConnection->getPDO()->exec('ALTER TABLE "posts" ADD COLUMN contentMimeType TEXT DEFAULT NULL'); $databaseConnection->getPDO()->exec('ALTER TABLE "posts" ADD COLUMN contentMimeType TEXT DEFAULT NULL');
$posts = $this->postDao->findAll(); $posts = $this->postDao->findAll();
@ -28,7 +26,7 @@ class Upgrade04 implements IUpgrade
{ {
if ($post->getContentType() !== \Szurubooru\Entities\Post::POST_TYPE_YOUTUBE) if ($post->getContentType() !== \Szurubooru\Entities\Post::POST_TYPE_YOUTUBE)
{ {
$fullPath = $this->fileService->getFullPath($this->postService->getPostContentPath($post)); $fullPath = $this->fileService->getFullPath($post->getContentPath());
$mime = \Szurubooru\Helpers\MimeHelper::getMimeTypeFromFile($fullPath); $mime = \Szurubooru\Helpers\MimeHelper::getMimeTypeFromFile($fullPath);
$post->setContentMimeType($mime); $post->setContentMimeType($mime);
$this->postDao->save($post); $this->postDao->save($post);

View file

@ -12,6 +12,7 @@ abstract class AbstractDatabaseTestCase extends \Szurubooru\Tests\AbstractTestCa
$config->set('database/dsn', 'sqlite::memory:'); $config->set('database/dsn', 'sqlite::memory:');
$this->databaseConnection = new \Szurubooru\DatabaseConnection($config); $this->databaseConnection = new \Szurubooru\DatabaseConnection($config);
\Szurubooru\Injector::set(\Szurubooru\DatabaseConnection::class, $this->databaseConnection);
$upgradeRepository = \Szurubooru\Injector::get(\Szurubooru\Upgrades\UpgradeRepository::class); $upgradeRepository = \Szurubooru\Injector::get(\Szurubooru\Upgrades\UpgradeRepository::class);
$upgradeService = new \Szurubooru\Services\UpgradeService($config, $this->databaseConnection, $upgradeRepository); $upgradeService = new \Szurubooru\Services\UpgradeService($config, $this->databaseConnection, $upgradeRepository);

View file

@ -36,6 +36,11 @@ abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
return __DIR__ . DIRECTORY_SEPARATOR . 'test_files' . DIRECTORY_SEPARATOR . $fileName; return __DIR__ . DIRECTORY_SEPARATOR . 'test_files' . DIRECTORY_SEPARATOR . $fileName;
} }
protected function setUp()
{
\Szurubooru\Injector::init();
}
protected function tearDown() protected function tearDown()
{ {
$this->cleanTestDirectory(); $this->cleanTestDirectory();