forked from thecodingmachine/tdbm-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionalTest.php
More file actions
192 lines (152 loc) · 7.03 KB
/
Copy pathFunctionalTest.php
File metadata and controls
192 lines (152 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace TheCodingMachine\TDBM\Bundle\Tests;
use Doctrine\DBAL\Connection;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\ApplicationTester;
use Symfony\Component\HttpKernel\KernelInterface;
use TheCodingMachine\FluidSchema\TdbmFluidSchema;
use TheCodingMachine\TDBM\Bundle\DependencyInjection\Configuration;
use TheCodingMachine\TDBM\Bundle\Tests\Fixtures\PublicService;
use TheCodingMachine\TDBM\Bundle\Tests\GeneratedDb1\Beans\Country;
use TheCodingMachine\TDBM\Bundle\Tests\GeneratedDb1\Daos\CountryDao;
use TheCodingMachine\TDBM\Bundle\Tests\GeneratedDb2\Beans\Person;
use TheCodingMachine\TDBM\Bundle\Tests\GeneratedDb2\Daos\PersonDao;
use TheCodingMachine\TDBM\TDBMService;
use Throwable;
class FunctionalTest extends KernelTestCase
{
private const DEFAULT_CONFIGURATION = [
'dao_namespace' => 'App\Daos',
'bean_namespace' => 'App\Beans',
'connection' => 'doctrine.dbal.default_connection',
'naming' => [
'bean_prefix' => '',
'bean_suffix' => '',
'base_bean_prefix' => 'Abstract',
'base_bean_suffix' => '',
'dao_prefix' => '',
'dao_suffix' => 'Dao',
'base_dao_prefix' => 'Abstract',
'base_dao_suffix' => 'Dao',
'exceptions' => [],
],
];
private static $multiDb = false;
protected static function createKernel(array $options = []): KernelInterface
{
return new TdbmTestingKernel(self::$multiDb);
}
public function testServiceWiring(): void
{
self::$multiDb = true;
self::bootKernel();
$tdbmService = self::$kernel->getContainer()->get(TDBMService::class);
$this->assertInstanceOf(TDBMService::class, $tdbmService);
}
public function testDefaultConfiguration(): void
{
$processor = new Processor();
$configuration = new Configuration();
$expected = self::DEFAULT_CONFIGURATION;
$expected['databases'] = [];
$this->assertEquals($expected, $processor->processConfiguration($configuration, []));
}
public function testTwoDatabasesConfiguration(): void
{
$processor = new Processor();
$configuration = new Configuration();
$expected = self::DEFAULT_CONFIGURATION;
$expected['databases'] = ['test1' => self::DEFAULT_CONFIGURATION, 'test2' => self::DEFAULT_CONFIGURATION];
$this->assertEquals(
$expected,
$processor->processConfiguration($configuration, [['databases' => ['test1' => [], 'test2' => []]]])
);
}
public function testExceptionsConfiguration(): void
{
$processor = new Processor();
$configuration = new Configuration();
$expected = self::DEFAULT_CONFIGURATION;
$expected['databases'] = [];
$expected['naming']['exceptions'] = ['object' => 'AnObject'];
$this->assertEquals($expected, $processor->processConfiguration($configuration, [['naming' => ['exceptions' => ['object' => 'AnObject']]]]));
}
public function testEndToEnd(): void
{
self::$multiDb = true;
$container = self::bootKernel()->getContainer(); // Cannot use self::getContainer() here, as it tries to retrieve test.service_container.
/**
* @var Connection $connectionRoot
*/
$connectionRoot = $container->get('doctrine.dbal.root_connection');
$connectionRoot->createSchemaManager()->dropAndCreateDatabase('test_tdbmbundle');
$connectionRoot->createSchemaManager()->dropAndCreateDatabase('test_tdbmbundle2');
/**
* @var Connection $connection1
*/
$connection1 = $container->get('doctrine.dbal.default_connection');
$fromSchema1 = $connection1->createSchemaManager()->createSchema();
$toSchema1 = clone $fromSchema1;
$db = new TdbmFluidSchema($toSchema1, new \TheCodingMachine\FluidSchema\DefaultNamingStrategy($connection1->getDatabasePlatform()));
$db->table('country')
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement')
->column('label')->string(255)->unique();
$sqlStmts = $toSchema1->getMigrateFromSql($fromSchema1, $connection1->getDatabasePlatform());
foreach ($sqlStmts as $sqlStmt) {
//echo $sqlStmt."\n";
$connection1->exec($sqlStmt);
}
/**
* @var Connection $connection2
*/
$connection2 = $container->get('doctrine.dbal.other_connection');
$fromSchema2 = $connection2->createSchemaManager()->createSchema();
$toSchema2 = clone $fromSchema2;
$db = new TdbmFluidSchema($toSchema2, new \TheCodingMachine\FluidSchema\DefaultNamingStrategy($connection2->getDatabasePlatform()));
$db->table('person')
->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement')
->column('name')->string(255);
$sqlStmts = $toSchema2->getMigrateFromSql($fromSchema2, $connection2->getDatabasePlatform());
foreach ($sqlStmts as $sqlStmt) {
//echo $sqlStmt."\n";
$connection2->exec($sqlStmt);
}
$application = new Application(self::$kernel);
$application->setAutoExit(false);
$applicationTester = new ApplicationTester($application);
$applicationTester->run(['command' => 'tdbm:generate'], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
$this->assertStringContainsString('Finished regenerating DAOs and beans', $applicationTester->getDisplay());
$this->assertFileExists(__DIR__ . '/../tdbm.lock.yml');
$applicationTester = new ApplicationTester($application);
$applicationTester->run(['command' => 'tdbm:generate:other'], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
$this->assertStringContainsString('Finished regenerating DAOs and beans', $applicationTester->getDisplay());
$this->assertFileExists(__DIR__ . '/../tdbm.other.lock.yml');
}
/**
* @depends testEndToEnd
*/
public function testEndToEnd2(): void
{
self::$multiDb = true;
$container = self::bootKernel()->getContainer(); // Cannot use self::getContainer() here, as it tries to retrieve test.service_container.
// PublicService is a dirty trick to access CountryDao and PersonDao that are private services.
$publicService = $container->get(PublicService::class);
/**
* @var CountryDao $countryDao
*/
$countryDao = $publicService->getCountryDao();
$country = new Country('Foo');
$countryDao->save($country);
/**
* @var PersonDao $personDao
*/
$personDao = $publicService->getPersonDao();
$person = new Person('Foo');
$personDao->save($person);
$this->assertSame(1, $personDao->findAll()->count());
}
}