Create and test a service using translator container with Symfony2

Here is the code to create a service which uses the translator component from Symfony2. The service is also tested.

Create the service

in messages.en.yml :
cb.texta1: Travel from %A% to %B%
in services.yml :
cb.labelbuilder:
  class: AppBundle\Service\LabelBuilder
  arguments: [@translator]
LabelBuilder.php
namespace AppBundle\Service;

use Symfony\Component\Translation\TranslatorInterface;

class LabelBuilder {
    /**
     * @var TranslatorInterface
     */
    private $translator;

    public function __construct(TranslatorInterface $translator) {
        $this->translator = $translator;
    }

    public function buildText1($labelA, $labelB) {
        return $this->translator->trans(
            'cb.texta1',
            [
                '%A%' => $labelA,
                '%B%' => $labelB
            ]
        );
    }
}

Use the service in your controller

$this->get('cb.labelbuilder')->buildText1($lblA, $lblB);

To test with PHPUnit

Source SO LabelBuilderTest.php
namespace AppBundle\Service;

use AppBundle\Entity\StatPriceDuration;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class LabelBuilderTest extends WebTestCase {

    protected static $translation;

    public static function setUpBeforeClass() {
        $kernel = static::createKernel();
        $kernel->boot();
        self::$translation = $kernel->getContainer()->get('translator');
    }

    public function testBuildText1() {
        $builder = new LabelBuilder(self::$translation);

        $result = $builder->buildText1("Paris", "Lille");

        $this->assertEquals(
            trim($result),
            "Travel from Paris to Lille"
        );
    }
}
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply