From afd50403221aa3ad448deb05ca49b8acb089ad92 Mon Sep 17 00:00:00 2001 From: Krystian Kuczek Date: Wed, 11 Feb 2015 20:23:32 +0100 Subject: [PATCH] Functional tests. --- .gitignore | 1 + .travis.yml | 15 ++- Resources/doc/index.md | 5 + Resources/doc/testing.md | 27 +++++ .../Command/ImportTranslationsCommandTest.php | 102 ++++++++++++++--- Tests/app/AppKernel.php | 104 ++++++++++++++++++ Tests/app/test/bundles.php | 7 ++ Tests/app/test/config.yml | 10 ++ Tests/app/test/database.php | 30 +++++ Tests/bootstrap.php | 11 +- composer.json | 6 +- phpunit.xml.dist | 32 +++--- 12 files changed, 313 insertions(+), 37 deletions(-) create mode 100644 Resources/doc/testing.md create mode 100644 Tests/app/AppKernel.php create mode 100644 Tests/app/test/bundles.php create mode 100644 Tests/app/test/config.yml create mode 100644 Tests/app/test/database.php diff --git a/.gitignore b/.gitignore index 1ec298db..6db1fa57 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ composer.lock Propel/map Propel/om +Tests/app/tmp/ diff --git a/.travis.yml b/.travis.yml index 3b4a9b5f..ac1a501b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/Resources/doc/index.md b/Resources/doc/index.md index 6348b2ab..d97f8106 100644 --- a/Resources/doc/index.md +++ b/Resources/doc/index.md @@ -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) diff --git a/Resources/doc/testing.md b/Resources/doc/testing.md new file mode 100644 index 00000000..dc07fa5b --- /dev/null +++ b/Resources/doc/testing.md @@ -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) diff --git a/Tests/Command/ImportTranslationsCommandTest.php b/Tests/Command/ImportTranslationsCommandTest.php index f12e94c4..cbcfea14 100644 --- a/Tests/Command/ImportTranslationsCommandTest.php +++ b/Tests/Command/ImportTranslationsCommandTest.php @@ -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]); } -} \ No newline at end of file + + /** + * @param $result + */ + private function assertLanguageDumped($result) + { + $this->assertRegExp('/translations\/LexikTranslationBundle\.((fr)|(en))\.yml" \.\.\. 16 translations/', $result); + } +} diff --git a/Tests/app/AppKernel.php b/Tests/app/AppKernel.php new file mode 100644 index 00000000..80f15750 --- /dev/null +++ b/Tests/app/AppKernel.php @@ -0,0 +1,104 @@ + + * + * 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 + */ +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 + } + + +} diff --git a/Tests/app/test/bundles.php b/Tests/app/test/bundles.php new file mode 100644 index 00000000..76ffd5b4 --- /dev/null +++ b/Tests/app/test/bundles.php @@ -0,0 +1,7 @@ +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"); +} diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php index d9f7acc9..cf5569de 100644 --- a/Tests/bootstrap.php +++ b/Tests/bootstrap.php @@ -1,7 +1,16 @@ =2.4", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index acb3da41..71df5239 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -10,13 +10,17 @@ stopOnFailure="false" syntaxCheck="false" bootstrap="Tests/bootstrap.php" -> + > ./Tests/ + + + + ./ @@ -26,21 +30,21 @@ - + - - util - orm - odm - propel - exporter - loader - importer - translator - - + + util + orm + odm + propel + exporter + loader + importer + translator + + - +