Skip to content

Commit 411e1cc

Browse files
committed
Adding end to end unit tests
1 parent 45f8ce5 commit 411e1cc

File tree

6 files changed

+142
-51
lines changed

6 files changed

+142
-51
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
/Tests/GeneratedDb*
12
/vendor/
23
/composer.lock
34
/build
4-
/cache
5+
/cache
6+
/.phpunit.result.cache
7+
/var
8+
/tdbm*.lock.yml

Tests/FunctionalTest.php

+76
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
namespace TheCodingMachine\TDBM\Bundle\Tests;
44

5+
use Doctrine\DBAL\Connection;
56
use PHPUnit\Framework\TestCase;
67
use Symfony\Component\Config\Definition\Processor;
8+
use Symfony\Bundle\FrameworkBundle\Console\Application;
9+
use Symfony\Component\Console\Tester\ApplicationTester;
10+
use TheCodingMachine\FluidSchema\TdbmFluidSchema;
711
use TheCodingMachine\TDBM\Bundle\DependencyInjection\Configuration;
812
use TheCodingMachine\TDBM\TDBMService;
13+
use Throwable;
914

1015
class FunctionalTest extends TestCase
1116
{
@@ -72,4 +77,75 @@ public function testExceptionsConfiguration(): void
7277

7378
$this->assertEquals($expected, $processor->processConfiguration($configuration, [['naming' => ['exceptions' => ['object' => 'AnObject']]]]));
7479
}
80+
81+
public function testEndToEnd(): void
82+
{
83+
$kernel = new TdbmTestingKernel(true);
84+
$kernel->boot();
85+
$container = $kernel->getContainer();
86+
87+
/**
88+
* @var Connection $connectionRoot
89+
*/
90+
$connectionRoot = $container->get('doctrine.dbal.root_connection');
91+
92+
$connectionRoot->getSchemaManager()->dropAndCreateDatabase('test_tdbmbundle');
93+
$connectionRoot->getSchemaManager()->dropAndCreateDatabase('test_tdbmbundle2');
94+
95+
/**
96+
* @var Connection $connection1
97+
*/
98+
$connection1 = $container->get('doctrine.dbal.default_connection');
99+
100+
$fromSchema1 = $connection1->getSchemaManager()->createSchema();
101+
$toSchema1 = clone $fromSchema1;
102+
103+
$db = new TdbmFluidSchema($toSchema1, new \TheCodingMachine\FluidSchema\DefaultNamingStrategy($connection1->getDatabasePlatform()));
104+
105+
$db->table('country')
106+
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement')
107+
->column('label')->string(255)->unique();
108+
109+
$sqlStmts = $toSchema1->getMigrateFromSql($fromSchema1, $connection1->getDatabasePlatform());
110+
111+
foreach ($sqlStmts as $sqlStmt) {
112+
//echo $sqlStmt."\n";
113+
$connection1->executeStatement($sqlStmt);
114+
}
115+
116+
117+
/**
118+
* @var Connection $connection2
119+
*/
120+
$connection2 = $container->get('doctrine.dbal.other_connection');
121+
122+
$fromSchema2 = $connection2->getSchemaManager()->createSchema();
123+
$toSchema2 = clone $fromSchema2;
124+
125+
$db = new TdbmFluidSchema($toSchema2, new \TheCodingMachine\FluidSchema\DefaultNamingStrategy($connection2->getDatabasePlatform()));
126+
127+
$db->table('person')
128+
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement')
129+
->column('name')->string(255);
130+
131+
$sqlStmts = $toSchema2->getMigrateFromSql($fromSchema2, $connection2->getDatabasePlatform());
132+
133+
foreach ($sqlStmts as $sqlStmt) {
134+
//echo $sqlStmt."\n";
135+
$connection2->executeStatement($sqlStmt);
136+
}
137+
138+
$application = new Application($kernel);
139+
$application->setAutoExit(false);
140+
141+
$applicationTester = new ApplicationTester($application);
142+
$applicationTester->run(['command' => 'tdbm:generate']);
143+
$this->assertStringContainsString('Finished regenerating DAOs and beans', $applicationTester->getDisplay());
144+
$this->assertFileExists(__DIR__.'/../tdbm.lock.yml');
145+
146+
$applicationTester = new ApplicationTester($application);
147+
$applicationTester->run(['command' => 'tdbm:generate:other']);
148+
$this->assertStringContainsString('Finished regenerating DAOs and beans', $applicationTester->getDisplay());
149+
$this->assertFileExists(__DIR__.'/../tdbm.other.lock.yml');
150+
}
75151
}

Tests/TdbmTestingKernel.php

+57-10
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ class TdbmTestingKernel extends Kernel
1717
use MicroKernelTrait;
1818

1919
const CONFIG_EXTS = '.{php,xml,yaml,yml}';
20+
/**
21+
* @var bool
22+
*/
23+
private $multiDb;
2024

21-
public function __construct()
25+
public function __construct(bool $multiDb = false)
2226
{
2327
parent::__construct('test', true);
28+
$this->multiDb = $multiDb;
2429
}
2530

2631
public function registerBundles()
@@ -40,16 +45,58 @@ public function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
4045
));
4146
$container->loadFromExtension('doctrine', array(
4247
'dbal' => [
43-
'driver' => 'pdo_mysql',
44-
'server_version' => '5.7',
45-
'charset'=> 'utf8mb4',
46-
'default_table_options' => [
47-
'charset' => 'utf8mb4',
48-
'collate' => 'utf8mb4_unicode_ci',
49-
],
50-
'url' => '%env(resolve:DATABASE_URL)%'
48+
'default_connection' => 'default',
49+
'connections' => [
50+
'default' => [
51+
'driver' => 'pdo_mysql',
52+
'server_version' => '5.7',
53+
'charset'=> 'utf8mb4',
54+
'default_table_options' => [
55+
'charset' => 'utf8mb4',
56+
'collate' => 'utf8mb4_unicode_ci',
57+
],
58+
'url' => '%env(resolve:DATABASE_URL)%'
59+
],
60+
'other' => [
61+
'driver' => 'pdo_mysql',
62+
'server_version' => '5.7',
63+
'charset'=> 'utf8mb4',
64+
'default_table_options' => [
65+
'charset' => 'utf8mb4',
66+
'collate' => 'utf8mb4_unicode_ci',
67+
],
68+
'url' => '%env(resolve:DATABASE_URL2)%'
69+
],
70+
'root' => [
71+
'driver' => 'pdo_mysql',
72+
'server_version' => '5.7',
73+
'charset'=> 'utf8mb4',
74+
'default_table_options' => [
75+
'charset' => 'utf8mb4',
76+
'collate' => 'utf8mb4_unicode_ci',
77+
],
78+
'url' => '%env(resolve:DATABASE_URL_ROOT)%'
79+
],
80+
]
81+
5182
]
5283
));
84+
85+
if ($this->multiDb) {
86+
$container->loadFromExtension('tdbm', array(
87+
'dao_namespace' => 'TheCodingMachine\TDBM\Bundle\Tests\GeneratedDb1\Daos',
88+
'bean_namespace' => 'TheCodingMachine\TDBM\Bundle\Tests\GeneratedDb1\Beans',
89+
'connection' => 'doctrine.dbal.default_connection',
90+
'databases' => [
91+
'other' => [
92+
'dao_namespace' => 'TheCodingMachine\TDBM\Bundle\Tests\GeneratedDb2\Daos',
93+
'bean_namespace' => 'TheCodingMachine\TDBM\Bundle\Tests\GeneratedDb2\Beans',
94+
'connection' => 'doctrine.dbal.other_connection',
95+
]
96+
]
97+
));
98+
99+
}
53100
});
54101
$confDir = $this->getProjectDir().'/Tests/Fixtures/config';
55102

@@ -65,6 +112,6 @@ protected function configureRoutes(RoutingConfigurator $routes)
65112

66113
public function getCacheDir()
67114
{
68-
return __DIR__.'/../cache/'.spl_object_hash($this);
115+
return __DIR__.'/../cache/'.($this->multiDb?"multidb":"singledb");
69116
}
70117
}

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"symfony/security-bundle": "^4.1.9 || ^5",
3333
"symfony/yaml": "^4.1.9 || ^5",
3434
"phpunit/phpunit": "^8.5.8",
35-
"phpstan/phpstan": "^0.12"
35+
"phpstan/phpstan": "^0.12",
36+
"thecodingmachine/tdbm-fluid-schema-builder": "^1.0.0"
3637
},
3738
"scripts": {
3839
"phpstan": "phpstan analyse TdbmBundle.php DependencyInjection/ Utils/ -c phpstan.neon --level=8 --no-progress"

phpunit.xml

-39
This file was deleted.

phpunit.xml.dist

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
<php>
3232
<env name="DATABASE_URL" value="mysql://root:@127.0.0.1:3306/test_tdbmbundle" />
33+
<env name="DATABASE_URL2" value="mysql://root:@127.0.0.1:3306/test_tdbmbundle2" />
34+
<env name="DATABASE_URL_ROOT" value="mysql://root:@127.0.0.1:3306/" />
3335
</php>
3436

3537
<logging>

0 commit comments

Comments
 (0)