]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Command/InstallCommandTest.php
71c2ffc633917c74a5d00016cd30d704932c1f5c
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Command / InstallCommandTest.php
1 <?php
2
3 namespace Tests\Wallabag\CoreBundle\Command;
4
5 use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
6 use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
7 use Symfony\Bundle\FrameworkBundle\Console\Application;
8 use Symfony\Component\Console\Input\ArrayInput;
9 use Symfony\Component\Console\Output\NullOutput;
10 use Symfony\Component\Console\Tester\CommandTester;
11 use Wallabag\CoreBundle\Command\InstallCommand;
12 use Tests\Wallabag\CoreBundle\Mock\InstallCommandMock;
13 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
14
15 class InstallCommandTest extends WallabagCoreTestCase
16 {
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 */
31 $this->markTestSkipped('PostgreSQL spotted: can\'t find a good way to drop current database, skipping.');
32 }
33 }
34
35 /**
36 * Ensure next tests will have a clean database.
37 */
38 public static function tearDownAfterClass()
39 {
40 $application = new Application(static::$kernel);
41 $application->setAutoExit(false);
42
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([
57 'command' => 'doctrine:fixtures:load',
58 '--no-interaction' => true,
59 '--env' => 'test',
60 ]), new NullOutput());
61 }
62
63 public function testRunInstallCommand()
64 {
65 $application = new Application($this->getClient()->getKernel());
66 $application->add(new InstallCommandMock());
67
68 $command = $application->find('wallabag:install');
69
70 $tester = new CommandTester($command);
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 ]);
78 $tester->execute([
79 'command' => $command->getName(),
80 ]);
81
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());
86 $this->assertContains('Run migrations.', $tester->getDisplay());
87 }
88
89 public function testRunInstallCommandWithReset()
90 {
91 $application = new Application($this->getClient()->getKernel());
92 $application->add(new InstallCommandMock());
93
94 $command = $application->find('wallabag:install');
95
96 $tester = new CommandTester($command);
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 ]);
103 $tester->execute([
104 'command' => $command->getName(),
105 '--reset' => true,
106 ]);
107
108 $this->assertContains('Checking system requirements.', $tester->getDisplay());
109 $this->assertContains('Setting up database.', $tester->getDisplay());
110 $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
111 $this->assertContains('Administration setup.', $tester->getDisplay());
112 $this->assertContains('Config setup.', $tester->getDisplay());
113 $this->assertContains('Run migrations.', $tester->getDisplay());
114
115 // we force to reset everything
116 $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
117 }
118
119 public function testRunInstallCommandWithDatabaseRemoved()
120 {
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
127 $application = new Application($this->getClient()->getKernel());
128 $application->add(new DropDatabaseDoctrineCommand());
129
130 // drop database first, so the install command won't ask to reset things
131 $command = $application->find('doctrine:database:drop');
132 $command->run(new ArrayInput([
133 'command' => 'doctrine:database:drop',
134 '--force' => true,
135 ]), new NullOutput());
136
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
142 $command = $application->find('wallabag:install');
143
144 $tester = new CommandTester($command);
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 ]);
151 $tester->execute([
152 'command' => $command->getName(),
153 ]);
154
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());
159 $this->assertContains('Run migrations.', $tester->getDisplay());
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 {
167 $application = new Application($this->getClient()->getKernel());
168 $application->add(new InstallCommandMock());
169
170 $command = $application->find('wallabag:install');
171
172 $tester = new CommandTester($command);
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 ]);
178 $tester->execute([
179 'command' => $command->getName(),
180 ]);
181
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());
186 $this->assertContains('Run migrations.', $tester->getDisplay());
187
188 $this->assertContains('Dropping schema and creating schema', $tester->getDisplay());
189 }
190
191 public function testRunInstallCommandChooseNothing()
192 {
193 $application = new Application($this->getClient()->getKernel());
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);
201 $command->run(new ArrayInput([
202 'command' => 'doctrine:database:drop',
203 '--force' => true,
204 ]), new NullOutput());
205
206 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
207
208 $command = new CreateDatabaseDoctrineCommand();
209 $command->setApplication($application);
210 $command->run(new ArrayInput([
211 'command' => 'doctrine:database:create',
212 '--env' => 'test',
213 ]), new NullOutput());
214
215 $command = $application->find('wallabag:install');
216
217 $tester = new CommandTester($command);
218 $tester->setInputs([
219 'n', // don't want to reset the entire database
220 'n', // don't want to create a new user
221 ]);
222 $tester->execute([
223 'command' => $command->getName(),
224 ]);
225
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());
230 $this->assertContains('Run migrations.', $tester->getDisplay());
231
232 $this->assertContains('Creating schema', $tester->getDisplay());
233 }
234
235 public function testRunInstallCommandNoInteraction()
236 {
237 $application = new Application($this->getClient()->getKernel());
238 $application->add(new InstallCommandMock());
239
240 $command = $application->find('wallabag:install');
241
242 $tester = new CommandTester($command);
243 $tester->execute([
244 'command' => $command->getName(),
245 ], [
246 'interactive' => false,
247 ]);
248
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());
253 $this->assertContains('Run migrations.', $tester->getDisplay());
254 }
255 }