]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Command/InstallCommandTest.php
Merge pull request #3168 from wallabag/instapaper-tags-import
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Command / InstallCommandTest.php
CommitLineData
0bf99bb1
J
1<?php
2
23634d5d 3namespace Tests\Wallabag\CoreBundle\Command;
0bf99bb1 4
619cc453
JB
5use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
6use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
0bf99bb1 7use Symfony\Bundle\FrameworkBundle\Console\Application;
0bf99bb1
J
8use Symfony\Component\Console\Input\ArrayInput;
9use Symfony\Component\Console\Output\NullOutput;
619cc453
JB
10use Symfony\Component\Console\Tester\CommandTester;
11use Wallabag\CoreBundle\Command\InstallCommand;
23634d5d
JB
12use Tests\Wallabag\CoreBundle\Mock\InstallCommandMock;
13use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
0bf99bb1 14
769e19dc 15class InstallCommandTest extends WallabagCoreTestCase
0bf99bb1 16{
3c39f5ac
JB
17 public function setUp()
18 {
19 parent::setUp();
20
21 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver) {
22 /*
23 * LOG: statement: CREATE DATABASE "wallabag"
24 * ERROR: source database "template1" is being accessed by other users
25 * DETAIL: There is 1 other session using the database.
26 * STATEMENT: CREATE DATABASE "wallabag"
27 * FATAL: database "wallabag" does not exist
28 *
29 * http://stackoverflow.com/a/14374832/569101
30 */
6dfac457 31 $this->markTestSkipped('PostgreSQL spotted: can\'t find a good way to drop current database, skipping.');
3c39f5ac
JB
32 }
33 }
34
6dfac457 35 /**
8541b3c4 36 * Ensure next tests will have a clean database.
6dfac457 37 */
c641baad 38 public static function tearDownAfterClass()
0bf99bb1 39 {
0bf99bb1
J
40 $application = new Application(static::$kernel);
41 $application->setAutoExit(false);
42
6dfac457
JB
43 $application->run(new ArrayInput([
44 'command' => 'doctrine:schema:drop',
45 '--no-interaction' => true,
46 '--force' => true,
47 '--env' => 'test',
48 ]), new NullOutput());
49
50 $application->run(new ArrayInput([
51 'command' => 'doctrine:schema:create',
52 '--no-interaction' => true,
53 '--env' => 'test',
54 ]), new NullOutput());
55
56 $application->run(new ArrayInput([
0bf99bb1
J
57 'command' => 'doctrine:fixtures:load',
58 '--no-interaction' => true,
59 '--env' => 'test',
4094ea47 60 ]), new NullOutput());
0bf99bb1
J
61 }
62
63 public function testRunInstallCommand()
64 {
d5027625 65 $application = new Application($this->getClient()->getKernel());
0ee043f7 66 $application->add(new InstallCommandMock());
0bf99bb1
J
67
68 $command = $application->find('wallabag:install');
69
0bf99bb1 70 $tester = new CommandTester($command);
26650fdb
JB
71 $tester->setInputs([
72 'y', // dropping database
73 'y', // create super admin
74 'username_'.uniqid('', true), // username
75 'password_'.uniqid('', true), // password
76 'email_'.uniqid('', true).'@wallabag.it', // email
77 ]);
4094ea47 78 $tester->execute([
732c2ad8 79 'command' => $command->getName(),
4094ea47 80 ]);
0bf99bb1 81
637dc4bb
JB
82 $this->assertContains('Checking system requirements.', $tester->getDisplay());
83 $this->assertContains('Setting up database.', $tester->getDisplay());
84 $this->assertContains('Administration setup.', $tester->getDisplay());
85 $this->assertContains('Config setup.', $tester->getDisplay());
7d2d1d68 86 $this->assertContains('Run migrations.', $tester->getDisplay());
0bf99bb1 87 }
c641baad
J
88
89 public function testRunInstallCommandWithReset()
90 {
d5027625 91 $application = new Application($this->getClient()->getKernel());
0ee043f7 92 $application->add(new InstallCommandMock());
c641baad
J
93
94 $command = $application->find('wallabag:install');
95
c641baad 96 $tester = new CommandTester($command);
26650fdb
JB
97 $tester->setInputs([
98 'y', // create super admin
99 'username_'.uniqid('', true), // username
100 'password_'.uniqid('', true), // password
101 'email_'.uniqid('', true).'@wallabag.it', // email
102 ]);
4094ea47 103 $tester->execute([
c641baad
J
104 'command' => $command->getName(),
105 '--reset' => true,
4094ea47 106 ]);
c641baad 107
637dc4bb
JB
108 $this->assertContains('Checking system requirements.', $tester->getDisplay());
109 $this->assertContains('Setting up database.', $tester->getDisplay());
7d2d1d68 110 $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
637dc4bb
JB
111 $this->assertContains('Administration setup.', $tester->getDisplay());
112 $this->assertContains('Config setup.', $tester->getDisplay());
7d2d1d68 113 $this->assertContains('Run migrations.', $tester->getDisplay());
c641baad
J
114
115 // we force to reset everything
7d2d1d68 116 $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
c641baad
J
117 }
118
119 public function testRunInstallCommandWithDatabaseRemoved()
120 {
f62c3faf
JB
121 // skipped SQLite check when database is removed because while testing for the connection,
122 // the driver will create the file (so the database) before testing if database exist
123 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) {
124 $this->markTestSkipped('SQLite spotted: can\'t test with database removed.');
125 }
126
d5027625 127 $application = new Application($this->getClient()->getKernel());
c641baad
J
128 $application->add(new DropDatabaseDoctrineCommand());
129
130 // drop database first, so the install command won't ask to reset things
d5027625 131 $command = $application->find('doctrine:database:drop');
4094ea47 132 $command->run(new ArrayInput([
c641baad
J
133 'command' => 'doctrine:database:drop',
134 '--force' => true,
4094ea47 135 ]), new NullOutput());
c641baad 136
d5027625
JB
137 // start a new application to avoid lagging connexion to pgsql
138 $client = static::createClient();
139 $application = new Application($client->getKernel());
140 $application->add(new InstallCommand());
141
c641baad
J
142 $command = $application->find('wallabag:install');
143
c641baad 144 $tester = new CommandTester($command);
26650fdb
JB
145 $tester->setInputs([
146 'y', // create super admin
147 'username_'.uniqid('', true), // username
148 'password_'.uniqid('', true), // password
149 'email_'.uniqid('', true).'@wallabag.it', // email
150 ]);
4094ea47 151 $tester->execute([
c641baad 152 'command' => $command->getName(),
4094ea47 153 ]);
c641baad 154
637dc4bb
JB
155 $this->assertContains('Checking system requirements.', $tester->getDisplay());
156 $this->assertContains('Setting up database.', $tester->getDisplay());
157 $this->assertContains('Administration setup.', $tester->getDisplay());
158 $this->assertContains('Config setup.', $tester->getDisplay());
7d2d1d68 159 $this->assertContains('Run migrations.', $tester->getDisplay());
c641baad
J
160
161 // the current database doesn't already exist
162 $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay());
163 }
164
165 public function testRunInstallCommandChooseResetSchema()
166 {
d5027625 167 $application = new Application($this->getClient()->getKernel());
0ee043f7 168 $application->add(new InstallCommandMock());
c641baad
J
169
170 $command = $application->find('wallabag:install');
171
c641baad 172 $tester = new CommandTester($command);
26650fdb
JB
173 $tester->setInputs([
174 'n', // don't want to reset the entire database
175 'y', // do want to reset the schema
176 'n', // don't want to create a new user
177 ]);
4094ea47 178 $tester->execute([
c641baad 179 'command' => $command->getName(),
4094ea47 180 ]);
c641baad 181
637dc4bb
JB
182 $this->assertContains('Checking system requirements.', $tester->getDisplay());
183 $this->assertContains('Setting up database.', $tester->getDisplay());
184 $this->assertContains('Administration setup.', $tester->getDisplay());
185 $this->assertContains('Config setup.', $tester->getDisplay());
7d2d1d68 186 $this->assertContains('Run migrations.', $tester->getDisplay());
c641baad 187
7d2d1d68 188 $this->assertContains('Dropping schema and creating schema', $tester->getDisplay());
c641baad
J
189 }
190
191 public function testRunInstallCommandChooseNothing()
192 {
d5027625 193 $application = new Application($this->getClient()->getKernel());
c641baad
J
194 $application->add(new InstallCommand());
195 $application->add(new DropDatabaseDoctrineCommand());
196 $application->add(new CreateDatabaseDoctrineCommand());
197
198 // drop database first, so the install command won't ask to reset things
199 $command = new DropDatabaseDoctrineCommand();
200 $command->setApplication($application);
4094ea47 201 $command->run(new ArrayInput([
c641baad
J
202 'command' => 'doctrine:database:drop',
203 '--force' => true,
4094ea47 204 ]), new NullOutput());
c641baad 205
d5027625 206 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
c641baad
J
207
208 $command = new CreateDatabaseDoctrineCommand();
209 $command->setApplication($application);
4094ea47 210 $command->run(new ArrayInput([
c641baad
J
211 'command' => 'doctrine:database:create',
212 '--env' => 'test',
4094ea47 213 ]), new NullOutput());
c641baad
J
214
215 $command = $application->find('wallabag:install');
216
c641baad 217 $tester = new CommandTester($command);
26650fdb
JB
218 $tester->setInputs([
219 'n', // don't want to reset the entire database
220 'n', // don't want to create a new user
221 ]);
4094ea47 222 $tester->execute([
c641baad 223 'command' => $command->getName(),
4094ea47 224 ]);
c641baad 225
637dc4bb
JB
226 $this->assertContains('Checking system requirements.', $tester->getDisplay());
227 $this->assertContains('Setting up database.', $tester->getDisplay());
228 $this->assertContains('Administration setup.', $tester->getDisplay());
229 $this->assertContains('Config setup.', $tester->getDisplay());
7d2d1d68 230 $this->assertContains('Run migrations.', $tester->getDisplay());
c641baad
J
231
232 $this->assertContains('Creating schema', $tester->getDisplay());
233 }
234
235 public function testRunInstallCommandNoInteraction()
236 {
d5027625 237 $application = new Application($this->getClient()->getKernel());
0ee043f7 238 $application->add(new InstallCommandMock());
c641baad
J
239
240 $command = $application->find('wallabag:install');
241
c641baad 242 $tester = new CommandTester($command);
4094ea47 243 $tester->execute([
c641baad 244 'command' => $command->getName(),
26650fdb
JB
245 ], [
246 'interactive' => false,
4094ea47 247 ]);
c641baad 248
637dc4bb
JB
249 $this->assertContains('Checking system requirements.', $tester->getDisplay());
250 $this->assertContains('Setting up database.', $tester->getDisplay());
251 $this->assertContains('Administration setup.', $tester->getDisplay());
252 $this->assertContains('Config setup.', $tester->getDisplay());
7d2d1d68 253 $this->assertContains('Run migrations.', $tester->getDisplay());
c641baad 254 }
0bf99bb1 255}