Skip to content

Commit

Permalink
Merge pull request #2 from andrew72ru/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
andrew72ru authored Mar 23, 2020
2 parents f1240dd + 80875c7 commit 4baaa86
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
vendor
composer.lock
.phpunit.result.cache
var/*
tests/output
44 changes: 25 additions & 19 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,35 @@
"require": {
"php": "^7.2",
"doctrine/common": "^2.8",
"doctrine/doctrine-bundle": "^1.8",
"doctrine/orm": "^2.6",
"doctrine/doctrine-bundle": "^1.8|^2.0",
"doctrine/orm": "^2.6|^3.0",
"doctrine/persistence": "^1.0",
"ramsey/uuid": "^3.8",
"ramsey/uuid-doctrine": "^1.5",
"symfony/config": "^4.1",
"symfony/dependency-injection": "^4.1",
"symfony/doctrine-bridge": "^4.1",
"symfony/finder": "^4.1",
"symfony/framework-bundle": "^4.1",
"symfony/polyfill-mbstring": "^1.7",
"symfony/translation": "^4.1",
"symfony/twig-bridge": "^4.1",
"symfony/twig-bundle": "^4.1",
"twig/extensions": "^1.5",
"symfony/config": "^4.1|^5.0",
"symfony/dependency-injection": "^4.1|^5.0",
"symfony/doctrine-bridge": "^4.1|^5.0",
"symfony/finder": "^4.1|^5.0",
"symfony/framework-bundle": "^4.1|^5.0",
"symfony/polyfill-mbstring": "^1",
"symfony/translation": "^4.1|^5.0",
"symfony/twig-bridge": "^4.1|^5.0",
"symfony/twig-bundle": "^4.1|^5.0",
"twig/twig": "^2.4",
"symfony/yaml": "^4.1"
"symfony/yaml": "^4.1|^5.0"
},
"require-dev": {
"phpunit/phpunit": "^8.1",
"symfony/console": "^4.1",
"symfony/css-selector": "^4.1",
"symfony/dom-crawler": "^4.1",
"symfony/phpunit-bridge": "^4.1",
"symfony/var-dumper": "^4.1"
"symfony/console": "^4.1|^5.0",
"symfony/css-selector": "^4.1|^5.0",
"symfony/dom-crawler": "^4.1|^5.0",
"symfony/phpunit-bridge": "^4.1|^5.0",
"symfony/var-dumper": "^4.1|^5.0"
},
"extra": {
"branch-alias": {
"dev-master": "develop"
}
},
"config": {
"sort-packages": true
Expand All @@ -48,6 +52,8 @@
}
},
"autoload-dev": {
"Creative\\DbI18nBundle\\Tests\\": "tests/"
"psr-4": {
"Creative\\DbI18nBundle\\Tests\\": "tests/"
}
}
}
9 changes: 9 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="./vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<env name="APP_ENV" value="test" />
<env name="APP_DEBUG" value="true" />
<env name="SHELL_VERBOSITY" value="-1" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[total]=999999" />
<env name="SYMFONY_PHPUNIT_VERSION" value="8.1" />
<env name="KERNEL_CLASS" value="Creative\DbI18nBundle\Tests\Kernel" />
</php>

<testsuites>
Expand All @@ -22,4 +27,8 @@
<directory>src</directory>
</whitelist>
</filter>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
</phpunit>
8 changes: 4 additions & 4 deletions src/Command/MigrateToDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use Creative\DbI18nBundle\Interfaces\EntityInterface;
use Creative\DbI18nBundle\Interfaces\TranslationRepositoryInterface;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -62,7 +62,7 @@ class MigrateToDatabaseCommand extends Command
private $entityClass;

/**
* @var RegistryInterface
* @var ManagerRegistry
*/
private $doctrine;

Expand All @@ -76,10 +76,10 @@ class MigrateToDatabaseCommand extends Command
*
* @param ContainerInterface $container
* @param TranslatorInterface $translator
* @param RegistryInterface $doctrine
* @param ManagerRegistry $doctrine
* @param string|null $name
*/
public function __construct(ContainerInterface $container, TranslatorInterface $translator, RegistryInterface $doctrine, string $name = null)
public function __construct(ContainerInterface $container, TranslatorInterface $translator, ManagerRegistry $doctrine, string $name = null)
{
parent::__construct($name);
$this->container = $container;
Expand Down
6 changes: 4 additions & 2 deletions src/DependencyInjection/DbI18nExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('db_i18n.entity', $config['entity']);
$container->setParameter('db_i18n.domain', $config['domain']);
$container->setParameter('db_i18n.root_dir', __DIR__ . '/../');
$container->setParameter('db_i18n.translation_dir', __DIR__ . '/../Resources/translations');
$container->setParameter('db_i18n.translation_dir', $container->getParameter('kernel.cache_dir'));

$localeNames = [$container->getParameter('kernel.default_locale')];
if ($container->hasParameter('locales') && is_array($locales = $container->getParameter('locales'))) {
$this->makeLocaleFiles($locales, $container->getParameter('db_i18n.translation_dir'), $container->getParameter('db_i18n.domain'));
$localeNames = $locales;
}
$this->makeLocaleFiles($localeNames, $container->getParameter('db_i18n.translation_dir'), $container->getParameter('db_i18n.domain'));

$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('config.yaml');
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/TranslationRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Creative\DbI18nBundle\Interfaces;

use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\Persistence\ObjectRepository;

interface TranslationRepositoryInterface extends ObjectRepository
{
Expand Down
18 changes: 11 additions & 7 deletions src/Loader/DbLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
use Creative\DbI18nBundle\Interfaces\DbLoaderInterface;
use Creative\DbI18nBundle\Interfaces\EntityInterface;
use Creative\DbI18nBundle\Interfaces\TranslationRepositoryInterface;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Translation\Exception\InvalidResourceException;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
Expand All @@ -25,7 +24,7 @@
class DbLoader implements LoaderInterface, DbLoaderInterface
{
/**
* @var EntityManagerInterface
* @var ManagerRegistry
*/
private $doctrine;

Expand All @@ -37,9 +36,9 @@ class DbLoader implements LoaderInterface, DbLoaderInterface
/**
* DbLoader constructor.
* @param ContainerInterface $container
* @param EntityManagerInterface $doctrine
* @param ManagerRegistry $doctrine
*/
public function __construct(ContainerInterface $container, EntityManagerInterface $doctrine)
public function __construct(ContainerInterface $container, ManagerRegistry $doctrine)
{
$this->doctrine = $doctrine;
$this->entityClass = $container->getParameter('db_i18n.entity');
Expand Down Expand Up @@ -73,10 +72,15 @@ public function load($resource, $locale, $domain = 'messages')
}

/**
* @return TranslationRepositoryInterface|ObjectRepository
* {@inheritDoc}
*/
public function getRepository(): TranslationRepositoryInterface
{
return $this->doctrine->getRepository($this->entityClass);
$repository = $this->doctrine->getRepository($this->entityClass);
if ($repository instanceof TranslationRepositoryInterface) {
return $repository;
}

throw new \RuntimeException(\sprintf('Cannot load repository %s', TranslationRepositoryInterface::class));
}
}
6 changes: 3 additions & 3 deletions src/Repository/TranslationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Creative\DbI18nBundle\Interfaces\TranslationRepositoryInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Doctrine\Persistence\ManagerRegistry;

/**
* @method Translation|null find($id, $lockMode = null, $lockVersion = null)
Expand All @@ -19,9 +19,9 @@ class TranslationRepository extends ServiceEntityRepository implements Translati
/**
* TranslationRepository constructor.
*
* @param RegistryInterface $registry
* @param ManagerRegistry $registry
*/
public function __construct(RegistryInterface $registry)
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Translation::class);
}
Expand Down
8 changes: 7 additions & 1 deletion src/Resources/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ services:

translation.loader.db:
class: Creative\DbI18nBundle\Loader\DbLoader
public: '%kernel.debug%'
arguments:
- '@service_container'
- '@doctrine.orm.entity_manager'
- '@doctrine'
tags:
- { name: translation.loader, alias: db }

Creative\DbI18nBundle\Interfaces\TranslationRepositoryInterface:
class: Creative\DbI18nBundle\Repository\TranslationRepository
tags:
- { name: doctrine.repository_service }

Creative\DbI18nBundle\Command\MigrateToDatabaseCommand:
arguments:
- '@service_container'
- '@translator.default'
- '@doctrine'
tags: [ console.command ]
47 changes: 47 additions & 0 deletions tests/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* 23.03.2020
*/

declare(strict_types=1);

namespace Creative\DbI18nBundle\Tests;

use Creative\DbI18nBundle\DbI18nBundle;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
{
use MicroKernelTrait;

/**
* @inheritDoc
*/
public function registerBundles()
{
return [
new FrameworkBundle(),
new DoctrineBundle(),
new DbI18nBundle(),
];
}

/**
* @inheritDoc
*/
protected function configureRoutes(\Symfony\Component\Routing\RouteCollectionBuilder $routes)
{
}

/**
* @inheritDoc
*/
protected function configureContainer(\Symfony\Component\DependencyInjection\ContainerBuilder $c, \Symfony\Component\Config\Loader\LoaderInterface $loader)
{
$loader->load(__DIR__ . '/doctrine.yaml');
$loader->load(__DIR__ . '/../src/Resources/config', 'glob');
}
}
57 changes: 57 additions & 0 deletions tests/Loader/DbLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* 23.03.2020
*/

declare(strict_types=1);

namespace Creative\DbI18nBundle\Tests\Loader;

use Creative\DbI18nBundle\Entity\Translation;
use Creative\DbI18nBundle\Loader\DbLoader;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Translation\MessageCatalogue;

class DbLoaderTest extends KernelTestCase
{
public function testIsServiceConfigured(): void
{
self::bootKernel();
$this->assertInstanceOf(DbLoader::class, self::$container->get('translation.loader.db'));
}

protected function setUp(): void
{
self::bootKernel();
$doctrine = self::$container->get('doctrine');
/** @var EntityManager $em */
$em = $doctrine->getManager();

$schemaTool = new SchemaTool($em);

$schemaTool->dropSchema([$em->getClassMetadata(Translation::class)]);
$schemaTool->updateSchema([$em->getClassMetadata(Translation::class)]);

$item = (new Translation())
->setDomain('db_messages')
->setKey('translatable.key')
->setLocale('en')
->setTranslation('This is a translation of key');
$doctrine = self::$container->get('doctrine');
$em = $doctrine->getManager();
$em->persist($item);
$em->flush();

parent::setUp();
}

public function testLoadCatalogue(): void
{
$service = self::$container->get('translation.loader.db');
$cat = $service->load(null, 'en', 'db_messages');
$this->assertInstanceOf(MessageCatalogue::class, $cat);
$this->assertSame('This is a translation of key', $cat->get('translatable.key', 'db_messages'));
}
}
2 changes: 1 addition & 1 deletion tests/TranslationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Creative\DbI18nBundle\Tests;

use Creative\DbI18nBundle\Entity\Translation;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase as TestCase;

class TranslationTest extends TestCase
{
Expand Down
21 changes: 21 additions & 0 deletions tests/doctrine.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
parameters:
env(DATABASE_URL): ''

doctrine:
dbal:
url: 'sqlite:///%kernel.cache_dir%/../../app.db'
driver: 'pdo_sqlite'
charset: utf8
types:
uuid: Ramsey\Uuid\Doctrine\UuidType
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App

0 comments on commit 4baaa86

Please sign in to comment.