Skip to content

Commit

Permalink
SF4 Compatibility (#493)
Browse files Browse the repository at this point in the history
Changes required for SF4 compatibility
  • Loading branch information
gnat42 authored Jun 28, 2018
1 parent 7d151e8 commit 942d501
Show file tree
Hide file tree
Showing 23 changed files with 169 additions and 82 deletions.
17 changes: 11 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,25 @@ matrix:

include:
- php: 5.6
env: SYMFONY_VERSION=2.3.*
- php: 5.6
env: SYMFONY_VERSION=2.7.*
- php: 7.0
- php: 7.1
env: SYMFONY_VERSION=2.7.*
- php: 7.2
env: SYMFONY_VERSION=2.7.*
- php: 7.1
env: SYMFONY_VERSION=2.8.*
- php: 7.2
env: SYMFONY_VERSION=2.8.*
- php: 5.6
env: SYMFONY_VERSION=^3.0
- php: 7.0
env: SYMFONY_VERSION=^3.0
- php: 7.1
env: SYMFONY_VERSION=^3.0

- php: 7.2
env: SYMFONY_VERSION=^3.0
- php: 7.1
env: SYMFONY_VERSION=^4.0
- php: 7.2
env: SYMFONY_VERSION=^4.0

before_install:
- travis_retry composer self-update
Expand Down
2 changes: 1 addition & 1 deletion Controller/TranslateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function setSourceLanguage($lang)

/**
* @Route("/", name="jms_translation_index", options = {"i18n" = false})
* @Template
* @Template("@JMSTranslation/Translate/index.html.twig")
* @param Request $request
* @return array
*/
Expand Down
7 changes: 6 additions & 1 deletion DependencyInjection/Compiler/MountDumpersPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace JMS\TranslationBundle\DependencyInjection\Compiler;

use JMS\TranslationBundle\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -39,7 +40,11 @@ public function process(ContainerBuilder $container)
throw new RuntimeException(sprintf('The "alias" attribute must be set for tag "translation.dumper" for service "%s".', $id));
}

$def = new DefinitionDecorator('jms_translation.dumper.symfony_adapter');
if (class_exists('Symfony\Component\DependencyInjection\ChildDefinition')) {
$def = new ChildDefinition('jms_translation.dumper.symfony_adapter');
} else {
$def = new DefinitionDecorator('jms_translation.dumper.symfony_adapter');
}
$def->addArgument(new Reference($id))->addArgument($attr[0]['alias']);
$container->setDefinition($id = 'jms_translation.dumper.wrapped_symfony_dumper.'.($i++), $def);

Expand Down
7 changes: 6 additions & 1 deletion DependencyInjection/Compiler/MountLoadersPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace JMS\TranslationBundle\DependencyInjection\Compiler;

use JMS\TranslationBundle\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -39,7 +40,11 @@ public function process(ContainerBuilder $container)
throw new RuntimeException(sprintf('The attribute "alias" must be defined for tag "translation.loader" for service "%s".', $id));
}

$def = new DefinitionDecorator('jms_translation.loader.symfony_adapter');
if (class_exists('Symfony\Component\DependencyInjection\ChildDefinition')) {
$def = new ChildDefinition('jms_translation.loader.symfony_adapter');
} else {
$def = new DefinitionDecorator('jms_translation.loader.symfony_adapter');
}
$def->addArgument(new Reference($id));
$container->setDefinition($id = 'jms_translation.loader.wrapped_symfony_loader.'.($i++), $def);

Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/JMSTranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');

if (!class_exists('Symfony\Component\ClassLoader\ClassLoader')) {
$loader->load('console.xml');
}

$container->setParameter('jms_translation.source_language', $config['source_language']);
$container->setParameter('jms_translation.locales', $config['locales']);

Expand Down
16 changes: 16 additions & 0 deletions Resources/config/console.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="jms_translation.command.extract" class="JMS\TranslationBundle\Command\ExtractTranslationCommand" public="false">
<tag name="console.command" command="translation:extract" />
</service>

<service id="jms_translation.command.list_resources" class="JMS\TranslationBundle\Command\ResourcesListCommand" public="false">
<tag name="console.command" command="translation:list-resources" />
</service>
</services>
</container>
10 changes: 5 additions & 5 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,27 @@

<services>
<!-- Controllers -->
<service id="jms_translation.controller.translate_controller" class="%jms_translation.controller.translate_controller.class%">
<service id="jms_translation.controller.translate_controller" class="%jms_translation.controller.translate_controller.class%" public="true">
<argument type="service" id="jms_translation.config_factory"/>
<argument type="service" id="jms_translation.loader_manager"/>
<call method="setSourceLanguage">
<argument>%jms_translation.source_language%</argument>
</call>
</service>
<service id="jms_translation.controller.api_controller" class="%jms_translation.controller.api_controller.class%">
<service id="jms_translation.controller.api_controller" class="%jms_translation.controller.api_controller.class%" public="true">
<argument id="jms_translation.config_factory" type="service"/>
<argument id="jms_translation.updater" type="service"/>
</service>


<service id="jms_translation.updater" class="%jms_translation.updater.class%">
<service id="jms_translation.updater" class="%jms_translation.updater.class%" public="true">
<argument type="service" id="jms_translation.loader_manager" />
<argument type="service" id="jms_translation.extractor_manager" />
<argument type="service" id="logger" />
<argument type="service" id="jms_translation.file_writer" />
</service>

<service id="jms_translation.config_factory" class="%jms_translation.config_factory.class%" />
<service id="jms_translation.config_factory" class="%jms_translation.config_factory.class%" public="true"/>

<service id="jms_translation.file_source_factory" class="%jms_translation.file_source_factory.class%">
<argument>%kernel.root_dir%</argument>
Expand Down Expand Up @@ -159,7 +159,7 @@
</call>
</service>

<service id="jms_translation.twig_extension" class="%jms_translation.twig_extension.class%">
<service id="jms_translation.twig_extension" class="%jms_translation.twig_extension.class%" public="true">
<argument type="service" id="translator" />
<argument>%kernel.debug%</argument>
<tag name="twig.extension" />
Expand Down
27 changes: 27 additions & 0 deletions Tests/BaseTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Created by PhpStorm.
* User: gnat
* Date: 27/06/18
* Time: 2:39 PM
*/

namespace JMS\TranslationBundle\Tests;

use PHPUnit\Framework\TestCase;

class BaseTestCase extends TestCase
{
public function createMock($class)
{
if (method_exists('PHPUnit\Framework\TestCase', 'createMock')) {
return parent::createMock($class);
}

return $this->getMockBuilder($class)
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->getMock();
}
}
23 changes: 18 additions & 5 deletions Tests/Functional/Controller/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use JMS\TranslationBundle\Tests\Functional\BaseTestCase;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\HttpKernel\Kernel;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
Expand All @@ -12,20 +13,32 @@ class ApiControllerTest extends BaseTestCase
{
public function testUpdateAction()
{
$isSf4 = version_compare(Kernel::VERSION,'4.0.0') >= 0;

// Add a file
$file = __DIR__.'/../Fixture/TestBundle/Resources/translations/navigation.en.yml';
file_put_contents($file, 'main.home: Home');
$file = $isSf4 ? __DIR__.'/../Fixture/TestBundle/Resources/translations/navigation.en.yaml' : __DIR__.'/../Fixture/TestBundle/Resources/translations/navigation.en.yml';
$written = file_put_contents($file, 'main.home: Home');
$this->assertTrue($written !== false && $written > 0);

// Start application
$client = static::createClient();
$client->request('POST', '/_trans/api/configs/app/domains/navigation/locales/en/messages?id=main.home', array('_method'=>'PUT', 'message'=>'Away'));
if($client->getResponse()->getStatusCode() !== 200) {
file_put_contents('/tmp/test-update.html',$client->getResponse());
}
$this->assertEquals(200, $client->getResponse()->getStatusCode());

// Verify that the file has new content
$array = Yaml::parse(file_get_contents($file));
$this->assertTrue(isset($array['main']));
$this->assertTrue(isset($array['main']['home']));
$this->assertEquals('Away', $array['main']['home']);

if ($isSf4) {
$this->assertTrue(isset($array['main.home']),print_r($array,true));
$this->assertEquals('Away', $array['main.home']);
} else {
$this->assertTrue(isset($array['main']));
$this->assertTrue(isset($array['main']['home']));
$this->assertEquals('Away', $array['main']['home']);
}

// Remove the file
unlink($file);
Expand Down
3 changes: 3 additions & 0 deletions Tests/Functional/Controller/TranslateControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public function testIndexAction()
{
$client = static::createClient();
$crawler = $client->request('GET', '/_trans/');
if($client->getResponse()->getStatusCode() !== 200) {
file_put_contents('/tmp/test-index.html',$client->getResponse());
}

$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertGreaterThan(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AppleController
{
/**
* @Route("/view")
* @Template
* @Template("@Test/Apple/view.html.twig")
*/
public function viewAction()
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/TranslationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function testTranschoiceWhenTranslationNotYetExtracted()
{
$client = $this->createClient();
$client->request('GET', '/apples/view');
$response = $client->getResponse();
$response = $client->getResponse();

$this->assertEquals(200, $response->getStatusCode(), substr($response, 0, 2000));
$this->assertEquals("There are 5 apples\n\nThere are 5 apples", $response->getContent());
Expand Down
6 changes: 4 additions & 2 deletions Tests/Functional/config/bundle.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
jms_translation:
configs:
app:
dirs: [%kernel.root_dir%, %kernel.root_dir%/Fixture/TestBundle]
output_dir: %kernel.root_dir%/Fixture/TestBundle/Resources/translations
dirs:
- "%kernel.root_dir%"
- "%kernel.root_dir%/Fixture/TestBundle"
output_dir: "%kernel.root_dir%/Fixture/TestBundle/Resources/translations"
ignored_domains: [routes]
excluded_names: ["*TestCase.php", "*Test.php"]
excluded_dirs: [cache, data, logs]
4 changes: 2 additions & 2 deletions Tests/Functional/config/twig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ framework:
engines: [twig, php]

twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
5 changes: 3 additions & 2 deletions Tests/Model/FileSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
namespace JMS\TranslationBundle\Tests\Model;

use JMS\TranslationBundle\Model\FileSource;
use JMS\TranslationBundle\Tests\BaseTestCase;

class FileSourceTest extends \PHPUnit_Framework_TestCase
class FileSourceTest extends BaseTestCase
{
public function testGetPath()
{
Expand Down Expand Up @@ -101,7 +102,7 @@ public function getEqualityTests()
false,
);

$source = $this->getMock('JMS\TranslationBundle\Model\SourceInterface');
$source = $this->createMock('JMS\TranslationBundle\Model\SourceInterface');
$source
->expects($this->once())
->method('equals')
Expand Down
4 changes: 2 additions & 2 deletions Tests/Model/Message/XliffMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ public function testMerge()
$messageWrite = new XliffMessage('foo');
$messageWrite->setDesc('foo');
$messageWrite->setMeaning('foo');
$messageWrite->addSource($s1 = $this->getMock('JMS\TranslationBundle\Model\SourceInterface'));
$messageWrite->addSource($s1 = $this->createMock('JMS\TranslationBundle\Model\SourceInterface'));

$messageRead = new XliffMessage('foo');
$messageRead->setDesc('bar');
$messageRead->setApproved(true);
$messageRead->setState(XliffMessage::STATE_TRANSLATED);
$messageRead->addSource($s2 = $this->getMock('JMS\TranslationBundle\Model\SourceInterface'));
$messageRead->addSource($s2 = $this->createMock('JMS\TranslationBundle\Model\SourceInterface'));

$messageRead2 = new Message('foo');
$messageRead2->setDesc('bar');
Expand Down
19 changes: 6 additions & 13 deletions Tests/Model/MessageCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Model\MessageCollection;
use JMS\TranslationBundle\Model\FileSource;
use JMS\TranslationBundle\Tests\BaseTestCase;

class MessageCollectionTest extends \PHPUnit_Framework_TestCase
class MessageCollectionTest extends BaseTestCase
{
public function testAdd()
{
Expand All @@ -34,13 +35,9 @@ public function testAdd()

public function testAddMerges()
{
$m2 = $this->getMockBuilder('JMS\TranslationBundle\Model\Message')
->disableOriginalConstructor()
->getMock();
$m2 = $this->createMock('JMS\TranslationBundle\Model\Message');

$m1 = $this->getMockBuilder('JMS\TranslationBundle\Model\Message')
->disableOriginalConstructor()
->getMock();
$m1 = $this->createMock('JMS\TranslationBundle\Model\Message');
$m1->expects($this->once())
->method('merge')
->with($m2);
Expand Down Expand Up @@ -79,16 +76,12 @@ public function testSet()

public function testSetDoesNotMerge()
{
$m2 = $this->getMockBuilder('JMS\TranslationBundle\Model\Message')
->disableOriginalConstructor()
->getMock();
$m2 = $this->createMock('JMS\TranslationBundle\Model\Message');
$m2->expects($this->any())
->method('getId')
->will($this->returnValue('foo'));

$m1 = $this->getMockBuilder('JMS\TranslationBundle\Model\Message')
->disableOriginalConstructor()
->getMock();
$m1 = $this->createMock('JMS\TranslationBundle\Model\Message');
$m1->expects($this->never())
->method('merge');
$m1->expects($this->any())
Expand Down
Loading

0 comments on commit 942d501

Please sign in to comment.