]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Command/InstallCommandTest.php
Fix utf8mb4 on vendor tables
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Command / InstallCommandTest.php
CommitLineData
0bf99bb1
J
1<?php
2
23634d5d 3namespace Tests\Wallabag\CoreBundle\Command;
0bf99bb1 4
7ab5eb95 5use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver;
619cc453
JB
6use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
7use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
2680b0bc 8use Doctrine\Bundle\MigrationsBundle\Command\MigrationsMigrateDoctrineCommand;
7ab5eb95 9use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
10use Doctrine\DBAL\Platforms\SqlitePlatform;
0bf99bb1 11use Symfony\Bundle\FrameworkBundle\Console\Application;
0bf99bb1
J
12use Symfony\Component\Console\Input\ArrayInput;
13use Symfony\Component\Console\Output\NullOutput;
619cc453 14use Symfony\Component\Console\Tester\CommandTester;
23634d5d
JB
15use Tests\Wallabag\CoreBundle\Mock\InstallCommandMock;
16use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
f808b016 17use Wallabag\CoreBundle\Command\InstallCommand;
0bf99bb1 18
769e19dc 19class InstallCommandTest extends WallabagCoreTestCase
0bf99bb1 20{
877787e5
JB
21 public static function setUpBeforeClass()
22 {
23 // disable doctrine-test-bundle
24 StaticDriver::setKeepStaticConnections(false);
25 }
26
27 public static function tearDownAfterClass()
28 {
29 // enable doctrine-test-bundle
30 StaticDriver::setKeepStaticConnections(true);
31 }
32
3c39f5ac
JB
33 public function setUp()
34 {
35 parent::setUp();
36
7ab5eb95 37 /** @var \Doctrine\DBAL\Connection $connection */
38 $connection = $this->getClient()->getContainer()->get('doctrine')->getConnection();
39 if ($connection->getDatabasePlatform() instanceof PostgreSqlPlatform) {
3c39f5ac
JB
40 /*
41 * LOG: statement: CREATE DATABASE "wallabag"
42 * ERROR: source database "template1" is being accessed by other users
43 * DETAIL: There is 1 other session using the database.
44 * STATEMENT: CREATE DATABASE "wallabag"
45 * FATAL: database "wallabag" does not exist
46 *
47 * http://stackoverflow.com/a/14374832/569101
48 */
6dfac457 49 $this->markTestSkipped('PostgreSQL spotted: can\'t find a good way to drop current database, skipping.');
3c39f5ac 50 }
7ab5eb95 51
52 if ($connection->getDatabasePlatform() instanceof SqlitePlatform) {
53 // Environnement variable useful only for sqlite to avoid the error "attempt to write a readonly database"
54 // We can't define always this environnement variable because pdo_mysql seems to use it
55 // and we have the error:
56 // SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
57 // check the manual that corresponds to your MariaDB server version for the right syntax to use
58 // near '/tmp/wallabag_testTYj1kp' at line 1
59 $databasePath = tempnam(sys_get_temp_dir(), 'wallabag_test');
60 putenv("TEST_DATABASE_PATH=$databasePath");
61
62 // The environnement has been changed, recreate the client in order to update connection
63 parent::setUp();
64 }
65
7ab5eb95 66 $this->resetDatabase($this->getClient());
3c39f5ac
JB
67 }
68
7ab5eb95 69 public function tearDown()
0bf99bb1 70 {
7ab5eb95 71 $databasePath = getenv('TEST_DATABASE_PATH');
72 // Remove variable environnement
73 putenv('TEST_DATABASE_PATH');
9a8a1bdf 74
7ab5eb95 75 if ($databasePath && file_exists($databasePath)) {
76 unlink($databasePath);
77 } else {
78 // Create a new client to avoid the error:
79 // Transaction commit failed because the transaction has been marked for rollback only.
80 $client = static::createClient();
81 $this->resetDatabase($client);
82 }
83
7ab5eb95 84 parent::tearDown();
0bf99bb1
J
85 }
86
87 public function testRunInstallCommand()
88 {
d5027625 89 $application = new Application($this->getClient()->getKernel());
0ee043f7 90 $application->add(new InstallCommandMock());
0bf99bb1
J
91
92 $command = $application->find('wallabag:install');
93
0bf99bb1 94 $tester = new CommandTester($command);
26650fdb
JB
95 $tester->setInputs([
96 'y', // dropping database
97 'y', // create super admin
f808b016
JB
98 'username_' . uniqid('', true), // username
99 'password_' . uniqid('', true), // password
100 'email_' . uniqid('', true) . '@wallabag.it', // email
26650fdb 101 ]);
4094ea47 102 $tester->execute([
732c2ad8 103 'command' => $command->getName(),
4094ea47 104 ]);
0bf99bb1 105
637dc4bb
JB
106 $this->assertContains('Checking system requirements.', $tester->getDisplay());
107 $this->assertContains('Setting up database.', $tester->getDisplay());
108 $this->assertContains('Administration setup.', $tester->getDisplay());
109 $this->assertContains('Config setup.', $tester->getDisplay());
0bf99bb1 110 }
c641baad
J
111
112 public function testRunInstallCommandWithReset()
113 {
d5027625 114 $application = new Application($this->getClient()->getKernel());
0ee043f7 115 $application->add(new InstallCommandMock());
c641baad
J
116
117 $command = $application->find('wallabag:install');
118
c641baad 119 $tester = new CommandTester($command);
26650fdb
JB
120 $tester->setInputs([
121 'y', // create super admin
f808b016
JB
122 'username_' . uniqid('', true), // username
123 'password_' . uniqid('', true), // password
124 'email_' . uniqid('', true) . '@wallabag.it', // email
26650fdb 125 ]);
4094ea47 126 $tester->execute([
c641baad
J
127 'command' => $command->getName(),
128 '--reset' => true,
4094ea47 129 ]);
c641baad 130
637dc4bb
JB
131 $this->assertContains('Checking system requirements.', $tester->getDisplay());
132 $this->assertContains('Setting up database.', $tester->getDisplay());
7d2d1d68 133 $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
637dc4bb
JB
134 $this->assertContains('Administration setup.', $tester->getDisplay());
135 $this->assertContains('Config setup.', $tester->getDisplay());
c641baad
J
136
137 // we force to reset everything
7d2d1d68 138 $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
c641baad
J
139 }
140
141 public function testRunInstallCommandWithDatabaseRemoved()
142 {
f62c3faf
JB
143 // skipped SQLite check when database is removed because while testing for the connection,
144 // the driver will create the file (so the database) before testing if database exist
7ab5eb95 145 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
f62c3faf
JB
146 $this->markTestSkipped('SQLite spotted: can\'t test with database removed.');
147 }
148
d5027625 149 $application = new Application($this->getClient()->getKernel());
c641baad
J
150 $application->add(new DropDatabaseDoctrineCommand());
151
152 // drop database first, so the install command won't ask to reset things
d5027625 153 $command = $application->find('doctrine:database:drop');
4094ea47 154 $command->run(new ArrayInput([
c641baad
J
155 'command' => 'doctrine:database:drop',
156 '--force' => true,
4094ea47 157 ]), new NullOutput());
c641baad 158
d5027625
JB
159 // start a new application to avoid lagging connexion to pgsql
160 $client = static::createClient();
161 $application = new Application($client->getKernel());
162 $application->add(new InstallCommand());
163
c641baad
J
164 $command = $application->find('wallabag:install');
165
c641baad 166 $tester = new CommandTester($command);
26650fdb
JB
167 $tester->setInputs([
168 'y', // create super admin
f808b016
JB
169 'username_' . uniqid('', true), // username
170 'password_' . uniqid('', true), // password
171 'email_' . uniqid('', true) . '@wallabag.it', // email
26650fdb 172 ]);
4094ea47 173 $tester->execute([
c641baad 174 'command' => $command->getName(),
4094ea47 175 ]);
c641baad 176
637dc4bb
JB
177 $this->assertContains('Checking system requirements.', $tester->getDisplay());
178 $this->assertContains('Setting up database.', $tester->getDisplay());
179 $this->assertContains('Administration setup.', $tester->getDisplay());
180 $this->assertContains('Config setup.', $tester->getDisplay());
c641baad
J
181
182 // the current database doesn't already exist
183 $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay());
184 }
185
186 public function testRunInstallCommandChooseResetSchema()
187 {
d5027625 188 $application = new Application($this->getClient()->getKernel());
0ee043f7 189 $application->add(new InstallCommandMock());
c641baad
J
190
191 $command = $application->find('wallabag:install');
192
c641baad 193 $tester = new CommandTester($command);
26650fdb
JB
194 $tester->setInputs([
195 'n', // don't want to reset the entire database
196 'y', // do want to reset the schema
197 'n', // don't want to create a new user
198 ]);
4094ea47 199 $tester->execute([
c641baad 200 'command' => $command->getName(),
4094ea47 201 ]);
c641baad 202
637dc4bb
JB
203 $this->assertContains('Checking system requirements.', $tester->getDisplay());
204 $this->assertContains('Setting up database.', $tester->getDisplay());
205 $this->assertContains('Administration setup.', $tester->getDisplay());
206 $this->assertContains('Config setup.', $tester->getDisplay());
c641baad 207
7d2d1d68 208 $this->assertContains('Dropping schema and creating schema', $tester->getDisplay());
c641baad
J
209 }
210
211 public function testRunInstallCommandChooseNothing()
212 {
d5027625 213 $application = new Application($this->getClient()->getKernel());
c641baad
J
214 $application->add(new InstallCommand());
215 $application->add(new DropDatabaseDoctrineCommand());
216 $application->add(new CreateDatabaseDoctrineCommand());
2680b0bc 217 $application->add(new MigrationsMigrateDoctrineCommand());
c641baad
J
218
219 // drop database first, so the install command won't ask to reset things
220 $command = new DropDatabaseDoctrineCommand();
221 $command->setApplication($application);
4094ea47 222 $command->run(new ArrayInput([
c641baad
J
223 'command' => 'doctrine:database:drop',
224 '--force' => true,
4094ea47 225 ]), new NullOutput());
c641baad 226
d5027625 227 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
c641baad
J
228
229 $command = new CreateDatabaseDoctrineCommand();
230 $command->setApplication($application);
4094ea47 231 $command->run(new ArrayInput([
c641baad
J
232 'command' => 'doctrine:database:create',
233 '--env' => 'test',
4094ea47 234 ]), new NullOutput());
c641baad
J
235
236 $command = $application->find('wallabag:install');
237
c641baad 238 $tester = new CommandTester($command);
26650fdb
JB
239 $tester->setInputs([
240 'n', // don't want to reset the entire database
241 'n', // don't want to create a new user
242 ]);
4094ea47 243 $tester->execute([
c641baad 244 'command' => $command->getName(),
4094ea47 245 ]);
c641baad 246
637dc4bb
JB
247 $this->assertContains('Checking system requirements.', $tester->getDisplay());
248 $this->assertContains('Setting up database.', $tester->getDisplay());
249 $this->assertContains('Administration setup.', $tester->getDisplay());
250 $this->assertContains('Config setup.', $tester->getDisplay());
c641baad
J
251
252 $this->assertContains('Creating schema', $tester->getDisplay());
253 }
254
255 public function testRunInstallCommandNoInteraction()
256 {
d5027625 257 $application = new Application($this->getClient()->getKernel());
0ee043f7 258 $application->add(new InstallCommandMock());
c641baad
J
259
260 $command = $application->find('wallabag:install');
261
c641baad 262 $tester = new CommandTester($command);
4094ea47 263 $tester->execute([
c641baad 264 'command' => $command->getName(),
26650fdb
JB
265 ], [
266 'interactive' => false,
4094ea47 267 ]);
c641baad 268
637dc4bb
JB
269 $this->assertContains('Checking system requirements.', $tester->getDisplay());
270 $this->assertContains('Setting up database.', $tester->getDisplay());
271 $this->assertContains('Administration setup.', $tester->getDisplay());
272 $this->assertContains('Config setup.', $tester->getDisplay());
c641baad 273 }
0bf99bb1 274}