From 0dbe3e88ff4f3a0aa5844b6a25b54521fe7daf25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Proch=C3=A1zka?= Date: Sat, 17 Oct 2015 20:13:02 +0200 Subject: [PATCH] fixup! Added loaders and dumpers for nette database and Doctrine DBAL. --- .../Translation/Loader/DoctrineLoader.php | 3 +- .../Translation/Loader/NetteDbLoader.php | 2 +- .../TranslationDumper.doctrine.phpt | 27 +++-- .../TranslationLoader.doctrine.phpt | 16 +-- .../TranslationLoader.netteDb.phpt | 107 ++++++++++++++++++ 5 files changed, 131 insertions(+), 24 deletions(-) create mode 100644 tests/KdybyTests/Translation/TranslationLoader.netteDb.phpt diff --git a/src/Kdyby/Translation/Loader/DoctrineLoader.php b/src/Kdyby/Translation/Loader/DoctrineLoader.php index 8e34cfdf..5c366bab 100644 --- a/src/Kdyby/Translation/Loader/DoctrineLoader.php +++ b/src/Kdyby/Translation/Loader/DoctrineLoader.php @@ -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); diff --git a/src/Kdyby/Translation/Loader/NetteDbLoader.php b/src/Kdyby/Translation/Loader/NetteDbLoader.php index 412c2b67..326fc127 100644 --- a/src/Kdyby/Translation/Loader/NetteDbLoader.php +++ b/src/Kdyby/Translation/Loader/NetteDbLoader.php @@ -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); } diff --git a/tests/KdybyTests/Translation/TranslationDumper.doctrine.phpt b/tests/KdybyTests/Translation/TranslationDumper.doctrine.phpt index d07fbd28..8c28853c 100644 --- a/tests/KdybyTests/Translation/TranslationDumper.doctrine.phpt +++ b/tests/KdybyTests/Translation/TranslationDumper.doctrine.phpt @@ -32,7 +32,7 @@ class TranslationDumperDoctrineTest extends TestCase /** * @var Doctrine\DBAL\Connection */ - private $connection; + private $db; /** * @var \Nette\DI\Container @@ -46,8 +46,16 @@ 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')); } @@ -55,7 +63,7 @@ class TranslationDumperDoctrineTest extends TestCase 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); @@ -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); @@ -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')); - } - } diff --git a/tests/KdybyTests/Translation/TranslationLoader.doctrine.phpt b/tests/KdybyTests/Translation/TranslationLoader.doctrine.phpt index 227e5888..ac2ed5f2 100644 --- a/tests/KdybyTests/Translation/TranslationLoader.doctrine.phpt +++ b/tests/KdybyTests/Translation/TranslationLoader.doctrine.phpt @@ -35,7 +35,7 @@ class TranslationLoaderDoctrineTest extends TestCase /** * @var Doctrine\DBAL\Connection */ - private $connection; + private $db; /** * @var \Nette\DI\Container @@ -49,8 +49,8 @@ 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')); } @@ -58,7 +58,7 @@ class TranslationLoaderDoctrineTest extends TestCase public function tearDown() { parent::tearDown(); - $this->connection->executeUpdate(file_get_contents(__DIR__ . '/../clear.sql')); + $this->db->executeUpdate(file_get_contents(__DIR__ . '/../clear.sql')); } @@ -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()); } @@ -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); @@ -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()); } } diff --git a/tests/KdybyTests/Translation/TranslationLoader.netteDb.phpt b/tests/KdybyTests/Translation/TranslationLoader.netteDb.phpt new file mode 100644 index 00000000..7e39f88c --- /dev/null +++ b/tests/KdybyTests/Translation/TranslationLoader.netteDb.phpt @@ -0,0 +1,107 @@ + + * @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 + */ +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());