From c2feffa50c6ded51134f1fae55d10845843778e3 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 23 Oct 2017 11:20:55 +0200 Subject: [PATCH] First version of functional tests working --- composer.json | 8 ++- config/cli-config.php | 24 ++++++++- func_tests_bootstrap.php | 14 ++---- .../Repository/TagRepositoryTest.php | 49 +++++++++++++++++++ phpunit-func.xml | 15 ++++++ 5 files changed, 96 insertions(+), 14 deletions(-) create mode 100644 module/Core/test-func/Repository/TagRepositoryTest.php create mode 100644 phpunit-func.xml diff --git a/composer.json b/composer.json index 4e0d2e82..2443e2ff 100644 --- a/composer.json +++ b/composer.json @@ -69,7 +69,10 @@ "psr-4": { "ShlinkioTest\\Shlink\\CLI\\": "module/CLI/test", "ShlinkioTest\\Shlink\\Rest\\": "module/Rest/test", - "ShlinkioTest\\Shlink\\Core\\": "module/Core/test", + "ShlinkioTest\\Shlink\\Core\\": [ + "module/Core/test", + "module/Core/test-func" + ], "ShlinkioTest\\Shlink\\Common\\": [ "module/Common/test", "module/Common/test-func" @@ -85,7 +88,8 @@ "cs-fix": "phpcbf", "serve": "php -S 0.0.0.0:8000 -t public/", "test": "phpunit --coverage-clover build/clover.xml", - "pretty-test": "phpunit --coverage-html build/coverage" + "pretty-test": "phpunit --coverage-html build/coverage", + "func-test": "phpunit -c phpunit-func.xml" }, "config": { "process-timeout": 0, diff --git a/config/cli-config.php b/config/cli-config.php index fefcca28..b90e9ddf 100644 --- a/config/cli-config.php +++ b/config/cli-config.php @@ -4,9 +4,31 @@ declare(strict_types=1); use Doctrine\ORM\EntityManager; use Doctrine\ORM\Tools\Console\ConsoleRunner; use Interop\Container\ContainerInterface; +use Zend\ServiceManager\ServiceManager; -/** @var ContainerInterface $container */ +$isTest = false; +foreach ($_SERVER['argv'] as $i => $arg) { + if ($arg === '--test') { + unset($_SERVER['argv'][$i]); + $isTest = true; + break; + } +} + +/** @var ContainerInterface|ServiceManager $container */ $container = include __DIR__ . '/container.php'; + +// If in testing env, override DB connection to use an in-memory sqlite database +if ($isTest) { + $container->setAllowOverride(true); + $config = $container->get('config'); + $config['entity_manager']['connection'] = [ + 'driver' => 'pdo_sqlite', + 'memory' => true, + ]; + $container->setService('config', $config); +} + /** @var EntityManager $em */ $em = $container->get(EntityManager::class); diff --git a/func_tests_bootstrap.php b/func_tests_bootstrap.php index e3c624da..2f864a82 100644 --- a/func_tests_bootstrap.php +++ b/func_tests_bootstrap.php @@ -1,7 +1,6 @@ setAllowOverride(true); -$config = $sm->get('config'); -$config['entity_manager']['connection'] = [ - 'driver' => 'pdo_sqlite', - 'memory' => true, -]; -$sm->setService('config', $config); -$process = new Process('vendor/bin/doctrine-migrations migrations:migrate --no-interaction -q', __DIR__); +// Create database +$process = new Process('vendor/bin/doctrine orm:schema-tool:create --no-interaction -q --test', __DIR__); $process->inheritEnvironmentVariables() - ->setTimeout(60 * 5) // 5 minutes ->mustRun(); -DatabaseTestCase::$em = $sm->get(EntityManagerInterface::class); +DatabaseTestCase::$em = $sm->get('em'); diff --git a/module/Core/test-func/Repository/TagRepositoryTest.php b/module/Core/test-func/Repository/TagRepositoryTest.php new file mode 100644 index 00000000..b6a96d85 --- /dev/null +++ b/module/Core/test-func/Repository/TagRepositoryTest.php @@ -0,0 +1,49 @@ +repo = $this->getEntityManager()->getRepository(Tag::class); + } + + /** + * @test + */ + public function deleteByNameDoesNothingWhenEmptyListIsProvided() + { + $this->assertEquals(0, $this->repo->deleteByName([])); + } + + /** + * @test + */ + public function allTagsWhichMatchNameAreDeleted() + { + $names = ['foo', 'bar', 'baz']; + $toDelete = ['foo', 'baz']; + + foreach ($names as $name) { + $this->getEntityManager()->persist(new Tag($name)); + } + $this->getEntityManager()->flush(); + + $this->assertEquals(2, $this->repo->deleteByName($toDelete)); + } +} diff --git a/phpunit-func.xml b/phpunit-func.xml new file mode 100644 index 00000000..d125e506 --- /dev/null +++ b/phpunit-func.xml @@ -0,0 +1,15 @@ + + + + ./module/*/test-func + + + + + + ./module/*/src/Repository + ./module/*/src/**/Repository + ./module/*/src/**/**/Repository + + +