Skip to content

Commit

Permalink
fixup! Added loaders and dumpers for nette database and Doctrine DBAL.
Browse files Browse the repository at this point in the history
  • Loading branch information
fprochazka committed Oct 17, 2015
1 parent 2447a72 commit 0dbe3e8
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/Kdyby/Translation/Loader/DoctrineLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public function getLocales()
$qb = $this->db->createQueryBuilder()
->addSelect('DISTINCT ' . $this->db->quoteIdentifier($this->config->getLocaleColumn()) . ' AS locale')
->from($this->db->quoteIdentifier($this->config->getTableName()));
return array_column($qb->execute()->fetchAll(), 'locale');
$locales = array_column($qb->execute()->fetchAll(), 'locale');
return array_combine($locales, $locales);

} catch (TableNotFoundException $e) {
throw new DatabaseException($e->getMessage(), 0, $e);
Expand Down
2 changes: 1 addition & 1 deletion src/Kdyby/Translation/Loader/NetteDbLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected function getLastUpdate($locale)
->limit(1)
->fetchField('updated_at');

if ($updatedAt === NULL) {
if (empty($updatedAt)) {
$updatedAt = new \DateTime();
$updatedAt->setTimestamp(0);
}
Expand Down
27 changes: 13 additions & 14 deletions tests/KdybyTests/Translation/TranslationDumper.doctrine.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TranslationDumperDoctrineTest extends TestCase
/**
* @var Doctrine\DBAL\Connection
*/
private $connection;
private $db;

/**
* @var \Nette\DI\Container
Expand All @@ -46,16 +46,24 @@ class TranslationDumperDoctrineTest extends TestCase
parent::setUp();
Tester\Environment::lock('db', dirname(TEMP_DIR));
$this->container = $this->createContainer('doctrine');
$this->connection = $this->container->getByType('Doctrine\DBAL\Connection');
$this->connection->executeUpdate(file_get_contents(__DIR__ . '/../init.sql'));
$this->db = $this->container->getByType('Doctrine\DBAL\Connection');
$this->db->executeUpdate(file_get_contents(__DIR__ . '/../init.sql'));
}



public function tearDown()
{
parent::tearDown();
$this->db->executeUpdate(file_get_contents(__DIR__ . '/../clear.sql'));
}



public function testChangeTranslations()
{
$loader = new TranslationLoader();
$loader->addLoader('database', $dbLoader = new Kdyby\Translation\Loader\DoctrineLoader($this->connection, new Configuration()));
$loader->addLoader('database', $dbLoader = new Kdyby\Translation\Loader\DoctrineLoader($this->db, new Configuration()));

$catalogue = new Kdyby\Translation\MessageCatalogue('cs_CZ');
$loader->loadResource('database', Kdyby\Translation\Resource\DatabaseResource::DOCTRINE, NULL, $catalogue);
Expand All @@ -79,7 +87,7 @@ class TranslationDumperDoctrineTest extends TestCase
public function testAddTranslations()
{
$loader = new TranslationLoader();
$loader->addLoader('database', $dbLoader = new Kdyby\Translation\Loader\DoctrineLoader($this->connection, new Configuration()));
$loader->addLoader('database', $dbLoader = new Kdyby\Translation\Loader\DoctrineLoader($this->db, new Configuration()));

$catalogue = new Kdyby\Translation\MessageCatalogue('cs_CZ');
$loader->loadResource('database', Kdyby\Translation\Resource\DatabaseResource::DOCTRINE, NULL, $catalogue);
Expand All @@ -96,15 +104,6 @@ class TranslationDumperDoctrineTest extends TestCase
Assert::true($catalogue->defines('farewell', 'front'));
}



public function tearDown()
{
parent::tearDown();

$this->connection->executeUpdate(file_get_contents(__DIR__ . '/../clear.sql'));
}

}


Expand Down
16 changes: 8 additions & 8 deletions tests/KdybyTests/Translation/TranslationLoader.doctrine.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TranslationLoaderDoctrineTest extends TestCase
/**
* @var Doctrine\DBAL\Connection
*/
private $connection;
private $db;

/**
* @var \Nette\DI\Container
Expand All @@ -49,16 +49,16 @@ class TranslationLoaderDoctrineTest extends TestCase
parent::setUp();
Tester\Environment::lock('db', dirname(TEMP_DIR));
$this->container = $this->createContainer('doctrine');
$this->connection = $this->container->getByType('Doctrine\DBAL\Connection');
$this->connection->executeUpdate(file_get_contents(__DIR__ . '/../init.sql'));
$this->db = $this->container->getByType('Doctrine\DBAL\Connection');
$this->db->executeUpdate(file_get_contents(__DIR__ . '/../init.sql'));
}



public function tearDown()
{
parent::tearDown();
$this->connection->executeUpdate(file_get_contents(__DIR__ . '/../clear.sql'));
$this->db->executeUpdate(file_get_contents(__DIR__ . '/../clear.sql'));
}


Expand All @@ -68,7 +68,7 @@ class TranslationLoaderDoctrineTest extends TestCase
$loader = new TranslationLoader();
Assert::same(array(), $loader->getLoaders());

$loader->addLoader('database', $dbLoader = new DoctrineLoader($this->connection, new Configuration()));
$loader->addLoader('database', $dbLoader = new DoctrineLoader($this->db, new Configuration()));
Assert::same(array('database' => $dbLoader), $loader->getLoaders());
}

Expand All @@ -77,7 +77,7 @@ class TranslationLoaderDoctrineTest extends TestCase
public function testLoadResources()
{
$loader = new TranslationLoader();
$loader->addLoader('database', $dbLoader = new DoctrineLoader($this->connection, new Configuration()));
$loader->addLoader('database', $dbLoader = new DoctrineLoader($this->db, new Configuration()));

$catalogue = new MessageCatalogue('cs_CZ');
$loader->loadResource('database', DatabaseResource::DOCTRINE, NULL, $catalogue);
Expand All @@ -96,8 +96,8 @@ class TranslationLoaderDoctrineTest extends TestCase

public function testLoadLocales()
{
$dbLoader = new DoctrineLoader($this->connection, new Configuration());
Assert::same(array('cs_CZ', 'en'), $dbLoader->getLocales());
$dbLoader = new DoctrineLoader($this->db, new Configuration());
Assert::same(array('cs_CZ' => 'cs_CZ', 'en' => 'en'), $dbLoader->getLocales());
}

}
Expand Down
107 changes: 107 additions & 0 deletions tests/KdybyTests/Translation/TranslationLoader.netteDb.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/**
* Test: Kdyby\Translation\TranslationLoader.
*
* @testCase KdybyTests\Translation\TranslationLoaderNetteDbTest
* @author Filip Procházka <[email protected]>
* @package Kdyby\Translation
*/

namespace KdybyTests\Translation;

use Kdyby;
use Kdyby\Translation\DI\Configuration;
use Kdyby\Translation\Loader\NetteDbLoader;
use Kdyby\Translation\MessageCatalogue;
use Kdyby\Translation\Resource\DatabaseResource;
use Kdyby\Translation\TranslationLoader;
use Nette;
use Symfony;
use Tester;
use Tester\Assert;
use Doctrine;

require_once __DIR__ . '/../bootstrap.php';



/**
* @author Filip Procházka <[email protected]>
*/
class TranslationLoaderNetteDbTest extends TestCase
{

/**
* @var \Nette\Database\Context
*/
private $db;

/**
* @var \Nette\DI\Container
*/
private $container;



protected function setUp()
{
parent::setUp();
Tester\Environment::lock('db', dirname(TEMP_DIR));
$this->container = $this->createContainer('netteDb');
$this->db = $this->container->getByType('Nette\Database\Context');
Nette\Database\Helpers::loadFromFile($this->db->getConnection(), __DIR__ . '/../init.sql');
}



public function tearDown()
{
parent::tearDown();
Nette\Database\Helpers::loadFromFile($this->db->getConnection(), __DIR__ . '/../clear.sql');
}



public function testAddLoaders()
{
$loader = new TranslationLoader();
Assert::same(array(), $loader->getLoaders());

$loader->addLoader('database', $dbLoader = new NetteDbLoader($this->db, new Configuration()));
Assert::same(array('database' => $dbLoader), $loader->getLoaders());
}



public function testLoadResources()
{
$loader = new TranslationLoader();
$loader->addLoader('database', $dbLoader = new NetteDbLoader($this->db, new Configuration()));

$catalogue = new MessageCatalogue('cs_CZ');
$loader->loadResource('database', DatabaseResource::NETTE_DB, NULL, $catalogue);

Assert::true($catalogue->defines('header', 'front'));
Assert::true($catalogue->defines('hello', 'messages'));

$catalogue = new MessageCatalogue('en');
$loader->loadResource('database', DatabaseResource::NETTE_DB, NULL, $catalogue);

Assert::true($catalogue->defines('header', 'front'));
Assert::true($catalogue->defines('hello', 'messages'));
}



public function testLoadLocales()
{
$dbLoader = new NetteDbLoader($this->db, new Configuration());
Assert::same(array('cs_CZ' => 'cs_CZ', 'en' => 'en'), $dbLoader->getLocales());
}

}



\run(new TranslationLoaderNetteDbTest());

0 comments on commit 0dbe3e8

Please sign in to comment.