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