First version of functional tests working

This commit is contained in:
Alejandro Celaya 2017-10-23 11:20:55 +02:00
parent e282521040
commit c2feffa50c
5 changed files with 96 additions and 14 deletions

View File

@ -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,

View File

@ -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);

View File

@ -1,7 +1,6 @@
<?php
declare(strict_types=1);
use Doctrine\ORM\EntityManagerInterface;
use ShlinkioTest\Shlink\Common\DbUnit\DatabaseTestCase;
use Symfony\Component\Process\Process;
use Zend\ServiceManager\ServiceManager;
@ -13,17 +12,10 @@ if (! file_exists('.env')) {
/** @var ServiceManager $sm */
$sm = require __DIR__ . '/config/container.php';
$sm->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');

View File

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Repository;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Repository\TagRepository;
use ShlinkioTest\Shlink\Common\DbUnit\DatabaseTestCase;
class TagRepositoryTest extends DatabaseTestCase
{
const ENTITIES_TO_EMPTY = [
Tag::class,
];
/**
* @var TagRepository
*/
private $repo;
protected function setUp()
{
$this->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));
}
}

15
phpunit-func.xml Normal file
View File

@ -0,0 +1,15 @@
<phpunit bootstrap="./func_tests_bootstrap.php" colors="true">
<testsuites>
<testsuite name="Shlink functional database tests">
<directory>./module/*/test-func</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./module/*/src/Repository</directory>
<directory suffix=".php">./module/*/src/**/Repository</directory>
<directory suffix=".php">./module/*/src/**/**/Repository</directory>
</whitelist>
</filter>
</phpunit>