Skip to content

Commit 80875c7

Browse files
author
Andrew Zhdanovskih
committed
Update all for use new packages and services
1 parent e1c2071 commit 80875c7

13 files changed

+170
-21
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
vendor
22
composer.lock
33
.phpunit.result.cache
4+
var/*
5+
tests/output

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"symfony/translation": "^4.1|^5.0",
2828
"symfony/twig-bridge": "^4.1|^5.0",
2929
"symfony/twig-bundle": "^4.1|^5.0",
30-
"twig/extensions": "^1.5",
3130
"twig/twig": "^2.4",
3231
"symfony/yaml": "^4.1|^5.0"
3332
},
@@ -53,6 +52,8 @@
5352
}
5453
},
5554
"autoload-dev": {
56-
"Creative\\DbI18nBundle\\Tests\\": "tests/"
55+
"psr-4": {
56+
"Creative\\DbI18nBundle\\Tests\\": "tests/"
57+
}
5758
}
5859
}

phpunit.xml.dist

+9
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
55
backupGlobals="false"
66
colors="true"
7+
bootstrap="./vendor/autoload.php"
78
>
89
<php>
910
<ini name="error_reporting" value="-1" />
1011
<env name="APP_ENV" value="test" />
12+
<env name="APP_DEBUG" value="true" />
1113
<env name="SHELL_VERBOSITY" value="-1" />
14+
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[total]=999999" />
15+
<env name="SYMFONY_PHPUNIT_VERSION" value="8.1" />
16+
<env name="KERNEL_CLASS" value="Creative\DbI18nBundle\Tests\Kernel" />
1217
</php>
1318

1419
<testsuites>
@@ -22,4 +27,8 @@
2227
<directory>src</directory>
2328
</whitelist>
2429
</filter>
30+
31+
<listeners>
32+
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
33+
</listeners>
2534
</phpunit>

src/Command/MigrateToDatabaseCommand.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
use Creative\DbI18nBundle\Interfaces\EntityInterface;
1111
use Creative\DbI18nBundle\Interfaces\TranslationRepositoryInterface;
12-
use Symfony\Bridge\Doctrine\RegistryInterface;
12+
use Doctrine\Persistence\ManagerRegistry;
1313
use Symfony\Component\Console\Command\Command;
1414
use Symfony\Component\Console\Exception\RuntimeException;
1515
use Symfony\Component\Console\Input\InputArgument;
@@ -62,7 +62,7 @@ class MigrateToDatabaseCommand extends Command
6262
private $entityClass;
6363

6464
/**
65-
* @var RegistryInterface
65+
* @var ManagerRegistry
6666
*/
6767
private $doctrine;
6868

@@ -76,10 +76,10 @@ class MigrateToDatabaseCommand extends Command
7676
*
7777
* @param ContainerInterface $container
7878
* @param TranslatorInterface $translator
79-
* @param RegistryInterface $doctrine
79+
* @param ManagerRegistry $doctrine
8080
* @param string|null $name
8181
*/
82-
public function __construct(ContainerInterface $container, TranslatorInterface $translator, RegistryInterface $doctrine, string $name = null)
82+
public function __construct(ContainerInterface $container, TranslatorInterface $translator, ManagerRegistry $doctrine, string $name = null)
8383
{
8484
parent::__construct($name);
8585
$this->container = $container;

src/DependencyInjection/DbI18nExtension.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ public function load(array $configs, ContainerBuilder $container): void
3535
$container->setParameter('db_i18n.entity', $config['entity']);
3636
$container->setParameter('db_i18n.domain', $config['domain']);
3737
$container->setParameter('db_i18n.root_dir', __DIR__ . '/../');
38-
$container->setParameter('db_i18n.translation_dir', __DIR__ . '/../Resources/translations');
38+
$container->setParameter('db_i18n.translation_dir', $container->getParameter('kernel.cache_dir'));
3939

40+
$localeNames = [$container->getParameter('kernel.default_locale')];
4041
if ($container->hasParameter('locales') && is_array($locales = $container->getParameter('locales'))) {
41-
$this->makeLocaleFiles($locales, $container->getParameter('db_i18n.translation_dir'), $container->getParameter('db_i18n.domain'));
42+
$localeNames = $locales;
4243
}
44+
$this->makeLocaleFiles($localeNames, $container->getParameter('db_i18n.translation_dir'), $container->getParameter('db_i18n.domain'));
4345

4446
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
4547
$loader->load('config.yaml');

src/Interfaces/TranslationRepositoryInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Creative\DbI18nBundle\Interfaces;
99

1010
use Doctrine\Common\Collections\Collection;
11-
use Doctrine\Common\Persistence\ObjectRepository;
11+
use Doctrine\Persistence\ObjectRepository;
1212

1313
interface TranslationRepositoryInterface extends ObjectRepository
1414
{

src/Loader/DbLoader.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
use Creative\DbI18nBundle\Interfaces\DbLoaderInterface;
1111
use Creative\DbI18nBundle\Interfaces\EntityInterface;
1212
use Creative\DbI18nBundle\Interfaces\TranslationRepositoryInterface;
13-
use Doctrine\Common\Persistence\ObjectRepository;
14-
use Doctrine\ORM\EntityManagerInterface;
13+
use Doctrine\Persistence\ManagerRegistry;
1514
use Symfony\Component\DependencyInjection\ContainerInterface;
1615
use Symfony\Component\Translation\Exception\InvalidResourceException;
1716
use Symfony\Component\Translation\Exception\NotFoundResourceException;
@@ -25,7 +24,7 @@
2524
class DbLoader implements LoaderInterface, DbLoaderInterface
2625
{
2726
/**
28-
* @var EntityManagerInterface
27+
* @var ManagerRegistry
2928
*/
3029
private $doctrine;
3130

@@ -37,9 +36,9 @@ class DbLoader implements LoaderInterface, DbLoaderInterface
3736
/**
3837
* DbLoader constructor.
3938
* @param ContainerInterface $container
40-
* @param EntityManagerInterface $doctrine
39+
* @param ManagerRegistry $doctrine
4140
*/
42-
public function __construct(ContainerInterface $container, EntityManagerInterface $doctrine)
41+
public function __construct(ContainerInterface $container, ManagerRegistry $doctrine)
4342
{
4443
$this->doctrine = $doctrine;
4544
$this->entityClass = $container->getParameter('db_i18n.entity');
@@ -73,10 +72,15 @@ public function load($resource, $locale, $domain = 'messages')
7372
}
7473

7574
/**
76-
* @return TranslationRepositoryInterface|ObjectRepository
75+
* {@inheritDoc}
7776
*/
7877
public function getRepository(): TranslationRepositoryInterface
7978
{
80-
return $this->doctrine->getRepository($this->entityClass);
79+
$repository = $this->doctrine->getRepository($this->entityClass);
80+
if ($repository instanceof TranslationRepositoryInterface) {
81+
return $repository;
82+
}
83+
84+
throw new \RuntimeException(\sprintf('Cannot load repository %s', TranslationRepositoryInterface::class));
8185
}
8286
}

src/Repository/TranslationRepository.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Creative\DbI18nBundle\Interfaces\TranslationRepositoryInterface;
77
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
88
use Doctrine\Common\Collections\ArrayCollection;
9-
use Symfony\Bridge\Doctrine\RegistryInterface;
9+
use Doctrine\Persistence\ManagerRegistry;
1010

1111
/**
1212
* @method Translation|null find($id, $lockMode = null, $lockVersion = null)
@@ -19,9 +19,9 @@ class TranslationRepository extends ServiceEntityRepository implements Translati
1919
/**
2020
* TranslationRepository constructor.
2121
*
22-
* @param RegistryInterface $registry
22+
* @param ManagerRegistry $registry
2323
*/
24-
public function __construct(RegistryInterface $registry)
24+
public function __construct(ManagerRegistry $registry)
2525
{
2626
parent::__construct($registry, Translation::class);
2727
}

src/Resources/config/config.yaml

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@ services:
99

1010
translation.loader.db:
1111
class: Creative\DbI18nBundle\Loader\DbLoader
12+
public: '%kernel.debug%'
1213
arguments:
1314
- '@service_container'
14-
- '@doctrine.orm.entity_manager'
15+
- '@doctrine'
1516
tags:
1617
- { name: translation.loader, alias: db }
1718

1819
Creative\DbI18nBundle\Interfaces\TranslationRepositoryInterface:
20+
class: Creative\DbI18nBundle\Repository\TranslationRepository
1921
tags:
2022
- { name: doctrine.repository_service }
2123

2224
Creative\DbI18nBundle\Command\MigrateToDatabaseCommand:
25+
arguments:
26+
- '@service_container'
27+
- '@translator.default'
28+
- '@doctrine'
2329
tags: [ console.command ]

tests/Kernel.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* 23.03.2020
4+
*/
5+
6+
declare(strict_types=1);
7+
8+
namespace Creative\DbI18nBundle\Tests;
9+
10+
use Creative\DbI18nBundle\DbI18nBundle;
11+
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
12+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
14+
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
15+
16+
class Kernel extends BaseKernel
17+
{
18+
use MicroKernelTrait;
19+
20+
/**
21+
* @inheritDoc
22+
*/
23+
public function registerBundles()
24+
{
25+
return [
26+
new FrameworkBundle(),
27+
new DoctrineBundle(),
28+
new DbI18nBundle(),
29+
];
30+
}
31+
32+
/**
33+
* @inheritDoc
34+
*/
35+
protected function configureRoutes(\Symfony\Component\Routing\RouteCollectionBuilder $routes)
36+
{
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
protected function configureContainer(\Symfony\Component\DependencyInjection\ContainerBuilder $c, \Symfony\Component\Config\Loader\LoaderInterface $loader)
43+
{
44+
$loader->load(__DIR__ . '/doctrine.yaml');
45+
$loader->load(__DIR__ . '/../src/Resources/config', 'glob');
46+
}
47+
}

tests/Loader/DbLoaderTest.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* 23.03.2020
4+
*/
5+
6+
declare(strict_types=1);
7+
8+
namespace Creative\DbI18nBundle\Tests\Loader;
9+
10+
use Creative\DbI18nBundle\Entity\Translation;
11+
use Creative\DbI18nBundle\Loader\DbLoader;
12+
use Doctrine\ORM\EntityManager;
13+
use Doctrine\ORM\Tools\SchemaTool;
14+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
15+
use Symfony\Component\Translation\MessageCatalogue;
16+
17+
class DbLoaderTest extends KernelTestCase
18+
{
19+
public function testIsServiceConfigured(): void
20+
{
21+
self::bootKernel();
22+
$this->assertInstanceOf(DbLoader::class, self::$container->get('translation.loader.db'));
23+
}
24+
25+
protected function setUp(): void
26+
{
27+
self::bootKernel();
28+
$doctrine = self::$container->get('doctrine');
29+
/** @var EntityManager $em */
30+
$em = $doctrine->getManager();
31+
32+
$schemaTool = new SchemaTool($em);
33+
34+
$schemaTool->dropSchema([$em->getClassMetadata(Translation::class)]);
35+
$schemaTool->updateSchema([$em->getClassMetadata(Translation::class)]);
36+
37+
$item = (new Translation())
38+
->setDomain('db_messages')
39+
->setKey('translatable.key')
40+
->setLocale('en')
41+
->setTranslation('This is a translation of key');
42+
$doctrine = self::$container->get('doctrine');
43+
$em = $doctrine->getManager();
44+
$em->persist($item);
45+
$em->flush();
46+
47+
parent::setUp();
48+
}
49+
50+
public function testLoadCatalogue(): void
51+
{
52+
$service = self::$container->get('translation.loader.db');
53+
$cat = $service->load(null, 'en', 'db_messages');
54+
$this->assertInstanceOf(MessageCatalogue::class, $cat);
55+
$this->assertSame('This is a translation of key', $cat->get('translatable.key', 'db_messages'));
56+
}
57+
}

tests/TranslationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Creative\DbI18nBundle\Tests;
99

1010
use Creative\DbI18nBundle\Entity\Translation;
11-
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
11+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase as TestCase;
1212

1313
class TranslationTest extends TestCase
1414
{

tests/doctrine.yaml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
parameters:
2+
env(DATABASE_URL): ''
3+
4+
doctrine:
5+
dbal:
6+
url: 'sqlite:///%kernel.cache_dir%/../../app.db'
7+
driver: 'pdo_sqlite'
8+
charset: utf8
9+
types:
10+
uuid: Ramsey\Uuid\Doctrine\UuidType
11+
orm:
12+
auto_generate_proxy_classes: true
13+
naming_strategy: doctrine.orm.naming_strategy.underscore
14+
auto_mapping: true
15+
mappings:
16+
App:
17+
is_bundle: false
18+
type: annotation
19+
dir: '%kernel.project_dir%/src/Entity'
20+
prefix: 'App\Entity'
21+
alias: App

0 commit comments

Comments
 (0)