]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Command/InstallCommandTest.php
Add a real configuration for CS-Fixer
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Command / InstallCommandTest.php
1 <?php
2
3 namespace Tests\Wallabag\CoreBundle\Command;
4
5 use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver;
6 use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
7 use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
8 use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
9 use Doctrine\DBAL\Platforms\SqlitePlatform;
10 use Symfony\Bundle\FrameworkBundle\Console\Application;
11 use Symfony\Component\Console\Input\ArrayInput;
12 use Symfony\Component\Console\Output\NullOutput;
13 use Symfony\Component\Console\Tester\CommandTester;
14 use Tests\Wallabag\CoreBundle\Mock\InstallCommandMock;
15 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
16 use Wallabag\CoreBundle\Command\InstallCommand;
17
18 class InstallCommandTest extends WallabagCoreTestCase
19 {
20 public function setUp()
21 {
22 parent::setUp();
23
24 /** @var \Doctrine\DBAL\Connection $connection */
25 $connection = $this->getClient()->getContainer()->get('doctrine')->getConnection();
26 if ($connection->getDatabasePlatform() instanceof PostgreSqlPlatform) {
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 */
36 $this->markTestSkipped('PostgreSQL spotted: can\'t find a good way to drop current database, skipping.');
37 }
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());
57 }
58
59 public function tearDown()
60 {
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();
76 }
77
78 public function testRunInstallCommand()
79 {
80 $application = new Application($this->getClient()->getKernel());
81 $application->add(new InstallCommandMock());
82
83 $command = $application->find('wallabag:install');
84
85 $tester = new CommandTester($command);
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 ]);
93 $tester->execute([
94 'command' => $command->getName(),
95 ]);
96
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());
101 $this->assertContains('Run migrations.', $tester->getDisplay());
102 }
103
104 public function testRunInstallCommandWithReset()
105 {
106 $application = new Application($this->getClient()->getKernel());
107 $application->add(new InstallCommandMock());
108
109 $command = $application->find('wallabag:install');
110
111 $tester = new CommandTester($command);
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 ]);
118 $tester->execute([
119 'command' => $command->getName(),
120 '--reset' => true,
121 ]);
122
123 $this->assertContains('Checking system requirements.', $tester->getDisplay());
124 $this->assertContains('Setting up database.', $tester->getDisplay());
125 $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
126 $this->assertContains('Administration setup.', $tester->getDisplay());
127 $this->assertContains('Config setup.', $tester->getDisplay());
128 $this->assertContains('Run migrations.', $tester->getDisplay());
129
130 // we force to reset everything
131 $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
132 }
133
134 public function testRunInstallCommandWithDatabaseRemoved()
135 {
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
138 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
139 $this->markTestSkipped('SQLite spotted: can\'t test with database removed.');
140 }
141
142 $application = new Application($this->getClient()->getKernel());
143 $application->add(new DropDatabaseDoctrineCommand());
144
145 // drop database first, so the install command won't ask to reset things
146 $command = $application->find('doctrine:database:drop');
147 $command->run(new ArrayInput([
148 'command' => 'doctrine:database:drop',
149 '--force' => true,
150 ]), new NullOutput());
151
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
157 $command = $application->find('wallabag:install');
158
159 $tester = new CommandTester($command);
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 ]);
166 $tester->execute([
167 'command' => $command->getName(),
168 ]);
169
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());
174 $this->assertContains('Run migrations.', $tester->getDisplay());
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 {
182 $application = new Application($this->getClient()->getKernel());
183 $application->add(new InstallCommandMock());
184
185 $command = $application->find('wallabag:install');
186
187 $tester = new CommandTester($command);
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 ]);
193 $tester->execute([
194 'command' => $command->getName(),
195 ]);
196
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());
201 $this->assertContains('Run migrations.', $tester->getDisplay());
202
203 $this->assertContains('Dropping schema and creating schema', $tester->getDisplay());
204 }
205
206 public function testRunInstallCommandChooseNothing()
207 {
208 $application = new Application($this->getClient()->getKernel());
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);
216 $command->run(new ArrayInput([
217 'command' => 'doctrine:database:drop',
218 '--force' => true,
219 ]), new NullOutput());
220
221 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
222
223 $command = new CreateDatabaseDoctrineCommand();
224 $command->setApplication($application);
225 $command->run(new ArrayInput([
226 'command' => 'doctrine:database:create',
227 '--env' => 'test',
228 ]), new NullOutput());
229
230 $command = $application->find('wallabag:install');
231
232 $tester = new CommandTester($command);
233 $tester->setInputs([
234 'n', // don't want to reset the entire database
235 'n', // don't want to create a new user
236 ]);
237 $tester->execute([
238 'command' => $command->getName(),
239 ]);
240
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());
245 $this->assertContains('Run migrations.', $tester->getDisplay());
246
247 $this->assertContains('Creating schema', $tester->getDisplay());
248 }
249
250 public function testRunInstallCommandNoInteraction()
251 {
252 $application = new Application($this->getClient()->getKernel());
253 $application->add(new InstallCommandMock());
254
255 $command = $application->find('wallabag:install');
256
257 $tester = new CommandTester($command);
258 $tester->execute([
259 'command' => $command->getName(),
260 ], [
261 'interactive' => false,
262 ]);
263
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());
268 $this->assertContains('Run migrations.', $tester->getDisplay());
269 }
270 }