Skip to content

Commit

Permalink
Functional tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuczek committed Feb 12, 2015
1 parent bcfd0ff commit afd5040
Show file tree
Hide file tree
Showing 12 changed files with 313 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ composer.lock
Propel/map
Propel/om

Tests/app/tmp/
15 changes: 10 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ services:
- mongodb

env:
- SYMFONY_VERSION=2.3.*
- SYMFONY_VERSION=2.5.*
- SYMFONY_VERSION=2.6.*
- SYMFONY_VERSION=2.3.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test
- SYMFONY_VERSION=2.5.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test
- SYMFONY_VERSION=2.6.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test

before_script:
- phpenv config-add travis-php.ini

- sh -c "if [ '$DB' = 'pdo_mysql' ]; then mysql -e 'create database IF NOT EXISTS $DB_NAME' -u$DB_USER; fi"

- composer require symfony/framework-bundle:${SYMFONY_VERSION} --no-update
- composer require symfony/validator:${SYMFONY_VERSION} --no-update
- composer require symfony/finder:${SYMFONY_VERSION} --no-update
- composer require symfony/doctrine-bridge:${SYMFONY_VERSION} --no-update
- composer update --dev
- composer update -vvv

script: phpunit
script:
- phpunit
- phpunit --group command
5 changes: 5 additions & 0 deletions Resources/doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,8 @@ Command options:
* `--format`: Force the output format.

*Note that it's not required to export translations to make them appear on your website as the `DatabaseLoader` will load them.*

TESTING
=======

[Read the documentation for testing ](./test.md)
27 changes: 27 additions & 0 deletions Resources/doc/testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Testing

run test from console

``` bash
$ phpunit.phar
```

or you can setup vars for doctrine pdo driver like this

``` bash
$ export DB_NAME=acme && export DB_USER=acme && export DB_PASSWD=acme && export DB=mysql && export DB_HOST=acme && phpunit.phar
```

according to default credentials for travis CI you must run

``` bash
$ export DB_NAME=lexik_test && export DB_USER=root && unset DB_PASSWD && unset DB && unset DB_HOST && phpunit.phar
```

Available variables are:
- ORM - orm system, currently we support only doctrine2, we should also support propel and mongo
- DB_NAME - database name (default: lexik_translation_test)
- DB_USER - database user name (default: root)
- DB_PASSWD - database user password (default: null)
- DB_ENGINE - database engine (default: pdo_mysql)
- DB_PORT - database port (default: null)
102 changes: 87 additions & 15 deletions Tests/Command/ImportTranslationsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,117 @@

namespace Lexik\Bundle\TranslationBundle\Tests\Command;

use Doctrine\Bundle\DoctrineBundle\Command\Proxy\CreateSchemaDoctrineCommand;
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\DropSchemaDoctrineCommand;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Tester\CommandTester;

use Lexik\Bundle\TranslationBundle\Command\ImportTranslationsCommand;
use Symfony\Component\HttpKernel\Kernel;

/**
* Test the translations import command, with option and arguments
*
* @covers Lexik\Bundle\TranslationBundle\Command\ImportTranslationsCommand
*/
class ImportTranslationsCommandTest extends WebTestCase
{

/**
* Test execute with all the options
* @var Application
*/
public function testExecute()
private static $application;

/**
*
*/
public function setUp()
{
static::$kernel = static::createKernel();
static::$kernel->boot();

$application = new Application(static::$kernel);
$application->add(new ImportTranslationsCommand());
static::$application = new Application(static::$kernel);

static::addDoctrineCommands();

static::rebuildDatabase();
}

/**
*
*/
private static function addDoctrineCommands()
{
static::$application->add(new DropSchemaDoctrineCommand());
static::$application->add(new CreateSchemaDoctrineCommand());
}

/**
*
*/
private static function rebuildDatabase()
{
$connection = static::$kernel->getContainer()->get('doctrine.dbal.default_connection');

$command = $application->find("lexik:translations:import");
$dbPath = $connection->getDatabase();

static::runCommand("doctrine:schema:drop", array('--force' => true));
static::runCommand("doctrine:schema:create");
}


private static function runCommand($commandName, $options = array())
{
$options["-e"] = self::$kernel->getEnvironment();

$options['command'] = $commandName;

$input = new ArrayInput($options);
$output = new NullOutput();

static::$application->setAutoExit(false);
$result = self::$application->run($input, $output);
}

/**
* Test execute with all the options
*
* @group command
*/
public function testExecute()
{
static::$application->add(new ImportTranslationsCommand());

$command = static::$application->find("lexik:translations:import");
$command->setContainer(static::$kernel->getContainer());

$commandTester = new CommandTester($command);
$commandTester->execute(array(
'command' => $command->getName(),
'bundle' => 'LexikTranslationBundle',
'--cache-clear' => true,
'--force' => true,
'--locales' => array('en', 'fr'),
));
$commandTester->execute(
array(
'command' => $command->getName(),
'bundle' => 'LexikTranslationBundle',
'--cache-clear' => true,
'--force' => true,
'--locales' => array('en', 'fr'),
)
);

$resultLines = explode("\n", $commandTester->getDisplay());

$this->assertEquals('# LexikTranslationBundle:', $resultLines[0]);
$this->assertContains('translations/LexikTranslationBundle.en.yml" ... 11 translations', $resultLines[1]);
$this->assertContains('translations/LexikTranslationBundle.fr.yml" ... 11 translations', $resultLines[2]);
$this->assertLanguageDumped($resultLines[1]);
$this->assertLanguageDumped($resultLines[2]);
$this->assertEquals('Removing translations cache files ...', $resultLines[3]);
}
}

/**
* @param $result
*/
private function assertLanguageDumped($result)
{
$this->assertRegExp('/translations\/LexikTranslationBundle\.((fr)|(en))\.yml" \.\.\. 16 translations/', $result);
}
}
104 changes: 104 additions & 0 deletions Tests/app/AppKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Kernel;

/**
* App Test Kernel for functional tests.
*
* @author Johannes M. Schmitt <[email protected]>
*/
class AppKernel extends Kernel
{
private $testCase;
private $rootConfig;

public function __construct($testCase, $debug = true)
{
$environment = $testCase;

if (!is_dir(__DIR__ . '/' . $testCase)) {
throw new \InvalidArgumentException(sprintf('The test case "%s" does not exist.', $testCase));
}
$this->testCase = $testCase;

$rootConfig = __DIR__ . '/' . $testCase . '/config.yml';

$fs = new Filesystem();
if (!$fs->isAbsolutePath($rootConfig)
&& !file_exists(
$rootConfig = __DIR__ . '/' . $testCase . '/' . $rootConfig
)
) {
throw new \InvalidArgumentException(sprintf('The root config "%s" does not exist.', $rootConfig));
}
$this->rootConfig = $rootConfig;

parent::__construct($environment, $debug);
}

public function registerBundles()
{
if (!file_exists($filename = $this->getRootDir() . '/' . $this->testCase . '/bundles.php')) {
throw new \RuntimeException(sprintf('The bundles file "%s" does not exist.', $filename));
}

return include $filename;
}

public function getRootDir()
{
return __DIR__;
}

public function getCacheDir()
{
return $this->getRootDir() . '/tmp/' . $this->testCase . '/cache/' . $this->environment;
}

public function getLogDir()
{
return $this->getRootDir() . '/tmp/' . $this->testCase . '/logs';
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load($this->rootConfig);
}

public function serialize()
{
return serialize(array($this->testCase, $this->rootConfig, $this->getEnvironment(), $this->isDebug()));
}

public function unserialize($str)
{
call_user_func_array(array($this, '__construct'), unserialize($str));
}

protected function getKernelParameters()
{
$parameters = parent::getKernelParameters();
$parameters['kernel.test_case'] = $this->testCase;

return $parameters;
}

public function terminate(
\Symfony\Component\HttpFoundation\Request $request, \Symfony\Component\HttpFoundation\Response $response)
{
return parent::terminate($request, $response); // TODO: Change the autogenerated stub
}


}
7 changes: 7 additions & 0 deletions Tests/app/test/bundles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return array(
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new \Lexik\Bundle\TranslationBundle\LexikTranslationBundle(),
);
10 changes: 10 additions & 0 deletions Tests/app/test/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
imports:
- { resource: database.php }

framework:
fragments: ~
secret: afasfsf

lexik_translation:
fallback_locale: en
managed_locales: [en, pl, de, sv]
30 changes: 30 additions & 0 deletions Tests/app/test/database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

if (ORM_TYPE == "doctrine") {
$container->loadFromExtension(
'doctrine',
array(
'orm' => array(
'mappings' => array(
'Mapping' => array(
'type' => 'xml',
'prefix' => 'Lexik\Bundle\TranslationBundle\Entity',
'is_bundle' => false,
'dir' => '%kernel.root_dir%/../../Resources/config/doctrine'
)
)
),
'dbal' => array(
'charset' => 'UTF8',
'driver' => DB_ENGINE,
'host' => DB_HOST,
'port' => DB_PORT,
'dbname' => DB_NAME,
'user' => DB_USER,
'password' => DB_PASSWD
)
)
);
} else {
throw new \Exception("Currently only doctrine is supported");
}
11 changes: 10 additions & 1 deletion Tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<?php

define("ORM_TYPE", getenv("ORM") ?: "doctrine");

define("DB_ENGINE", getenv("DB_ENGINE") ?: "pdo_mysql");
define("DB_HOST", getenv("DB_HOST") ?: "localhost");
define("DB_PORT", getenv("DB_PORT") ?: null);
define('DB_NAME', getenv("DB_NAME") ?: "lexik_translation_test");
define("DB_USER", getenv("DB_USER") ?: "root");
define("DB_PASSWD", getenv("DB_PASSWD") ?: null);

if (file_exists($file = __DIR__.'/autoload.php')) {
require_once $file;
} elseif (file_exists($file = __DIR__.'/autoload.php.dist')) {
require_once $file;
}
}
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"keywords": ["Symfony2", "bundle", "translation", "i18n"],
"homepage": "https://github.com/lexik/LexikTranslationBundle",
"license": "MIT",
"minimum-stability": "stable",
"minimum-stability": "dev",
"prefer-stable": true,
"authors": [
{
Expand All @@ -26,7 +26,9 @@
"doctrine/data-fixtures": "1.0.*",
"doctrine/mongodb-odm": "dev-master",
"doctrine/mongodb-odm-bundle": "dev-master",
"propel/propel-bundle": "~1.2"
"propel/propel-bundle": "~1.2",
"doctrine/doctrine-bundle": "~1.3",
"phpunit/phpunit": "~4.3"
},
"suggest": {
"doctrine/orm": ">=2.4",
Expand Down
Loading

0 comments on commit afd5040

Please sign in to comment.