]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Command/InstallCommandTest.php
Fix 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
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
JB
35 /**
36 * Ensure next tests will have a clean database
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
78507d28
JB
70 // We mock the QuestionHelper
71 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
0bf99bb1
J
72 ->disableOriginalConstructor()
73 ->getMock();
78507d28 74 $question->expects($this->any())
0bf99bb1 75 ->method('ask')
78507d28 76 ->will($this->returnValue('yes_'.uniqid('', true)));
0bf99bb1
J
77
78 // We override the standard helper with our mock
78507d28 79 $command->getHelperSet()->set($question, 'question');
0bf99bb1
J
80
81 $tester = new CommandTester($command);
4094ea47 82 $tester->execute([
732c2ad8 83 'command' => $command->getName(),
4094ea47 84 ]);
0bf99bb1 85
637dc4bb
JB
86 $this->assertContains('Checking system requirements.', $tester->getDisplay());
87 $this->assertContains('Setting up database.', $tester->getDisplay());
88 $this->assertContains('Administration setup.', $tester->getDisplay());
89 $this->assertContains('Config setup.', $tester->getDisplay());
90 $this->assertContains('Installing assets.', $tester->getDisplay());
0bf99bb1 91 }
c641baad
J
92
93 public function testRunInstallCommandWithReset()
94 {
d5027625 95 $application = new Application($this->getClient()->getKernel());
0ee043f7 96 $application->add(new InstallCommandMock());
c641baad
J
97
98 $command = $application->find('wallabag:install');
99
78507d28
JB
100 // We mock the QuestionHelper
101 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
c641baad
J
102 ->disableOriginalConstructor()
103 ->getMock();
78507d28 104 $question->expects($this->any())
c641baad 105 ->method('ask')
78507d28 106 ->will($this->returnValue('yes_'.uniqid('', true)));
c641baad
J
107
108 // We override the standard helper with our mock
78507d28 109 $command->getHelperSet()->set($question, 'question');
c641baad
J
110
111 $tester = new CommandTester($command);
4094ea47 112 $tester->execute([
c641baad
J
113 'command' => $command->getName(),
114 '--reset' => true,
4094ea47 115 ]);
c641baad 116
637dc4bb
JB
117 $this->assertContains('Checking system requirements.', $tester->getDisplay());
118 $this->assertContains('Setting up database.', $tester->getDisplay());
2a586069 119 $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay());
637dc4bb
JB
120 $this->assertContains('Administration setup.', $tester->getDisplay());
121 $this->assertContains('Config setup.', $tester->getDisplay());
122 $this->assertContains('Installing assets.', $tester->getDisplay());
c641baad
J
123
124 // we force to reset everything
d5027625 125 $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay());
c641baad
J
126 }
127
128 public function testRunInstallCommandWithDatabaseRemoved()
129 {
d5027625 130 $application = new Application($this->getClient()->getKernel());
c641baad
J
131 $application->add(new DropDatabaseDoctrineCommand());
132
133 // drop database first, so the install command won't ask to reset things
d5027625 134 $command = $application->find('doctrine:database:drop');
4094ea47 135 $command->run(new ArrayInput([
c641baad
J
136 'command' => 'doctrine:database:drop',
137 '--force' => true,
4094ea47 138 ]), new NullOutput());
c641baad 139
d5027625
JB
140 // start a new application to avoid lagging connexion to pgsql
141 $client = static::createClient();
142 $application = new Application($client->getKernel());
143 $application->add(new InstallCommand());
144
c641baad
J
145 $command = $application->find('wallabag:install');
146
78507d28
JB
147 // We mock the QuestionHelper
148 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
c641baad
J
149 ->disableOriginalConstructor()
150 ->getMock();
78507d28 151 $question->expects($this->any())
c641baad 152 ->method('ask')
78507d28 153 ->will($this->returnValue('yes_'.uniqid('', true)));
c641baad
J
154
155 // We override the standard helper with our mock
78507d28 156 $command->getHelperSet()->set($question, 'question');
c641baad
J
157
158 $tester = new CommandTester($command);
4094ea47 159 $tester->execute([
c641baad 160 'command' => $command->getName(),
4094ea47 161 ]);
c641baad 162
637dc4bb
JB
163 $this->assertContains('Checking system requirements.', $tester->getDisplay());
164 $this->assertContains('Setting up database.', $tester->getDisplay());
165 $this->assertContains('Administration setup.', $tester->getDisplay());
166 $this->assertContains('Config setup.', $tester->getDisplay());
167 $this->assertContains('Installing assets.', $tester->getDisplay());
c641baad
J
168
169 // the current database doesn't already exist
170 $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay());
171 }
172
173 public function testRunInstallCommandChooseResetSchema()
174 {
d5027625 175 $application = new Application($this->getClient()->getKernel());
0ee043f7 176 $application->add(new InstallCommandMock());
c641baad
J
177
178 $command = $application->find('wallabag:install');
179
78507d28
JB
180 // We mock the QuestionHelper
181 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
c641baad
J
182 ->disableOriginalConstructor()
183 ->getMock();
184
78507d28
JB
185 $question->expects($this->exactly(3))
186 ->method('ask')
c641baad
J
187 ->will($this->onConsecutiveCalls(
188 false, // don't want to reset the entire database
189 true, // do want to reset the schema
190 false // don't want to create a new user
191 ));
192
193 // We override the standard helper with our mock
78507d28 194 $command->getHelperSet()->set($question, 'question');
c641baad
J
195
196 $tester = new CommandTester($command);
4094ea47 197 $tester->execute([
c641baad 198 'command' => $command->getName(),
4094ea47 199 ]);
c641baad 200
637dc4bb
JB
201 $this->assertContains('Checking system requirements.', $tester->getDisplay());
202 $this->assertContains('Setting up database.', $tester->getDisplay());
203 $this->assertContains('Administration setup.', $tester->getDisplay());
204 $this->assertContains('Config setup.', $tester->getDisplay());
205 $this->assertContains('Installing assets.', $tester->getDisplay());
c641baad
J
206
207 $this->assertContains('Droping schema and creating schema', $tester->getDisplay());
208 }
209
210 public function testRunInstallCommandChooseNothing()
211 {
d5027625 212 $application = new Application($this->getClient()->getKernel());
c641baad
J
213 $application->add(new InstallCommand());
214 $application->add(new DropDatabaseDoctrineCommand());
215 $application->add(new CreateDatabaseDoctrineCommand());
216
217 // drop database first, so the install command won't ask to reset things
218 $command = new DropDatabaseDoctrineCommand();
219 $command->setApplication($application);
4094ea47 220 $command->run(new ArrayInput([
c641baad
J
221 'command' => 'doctrine:database:drop',
222 '--force' => true,
4094ea47 223 ]), new NullOutput());
c641baad 224
d5027625 225 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
c641baad
J
226
227 $command = new CreateDatabaseDoctrineCommand();
228 $command->setApplication($application);
4094ea47 229 $command->run(new ArrayInput([
c641baad
J
230 'command' => 'doctrine:database:create',
231 '--env' => 'test',
4094ea47 232 ]), new NullOutput());
c641baad
J
233
234 $command = $application->find('wallabag:install');
235
78507d28
JB
236 // We mock the QuestionHelper
237 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
c641baad
J
238 ->disableOriginalConstructor()
239 ->getMock();
240
78507d28
JB
241 $question->expects($this->exactly(2))
242 ->method('ask')
c641baad
J
243 ->will($this->onConsecutiveCalls(
244 false, // don't want to reset the entire database
245 false // don't want to create a new user
246 ));
247
248 // We override the standard helper with our mock
78507d28 249 $command->getHelperSet()->set($question, 'question');
c641baad
J
250
251 $tester = new CommandTester($command);
4094ea47 252 $tester->execute([
c641baad 253 'command' => $command->getName(),
4094ea47 254 ]);
c641baad 255
637dc4bb
JB
256 $this->assertContains('Checking system requirements.', $tester->getDisplay());
257 $this->assertContains('Setting up database.', $tester->getDisplay());
258 $this->assertContains('Administration setup.', $tester->getDisplay());
259 $this->assertContains('Config setup.', $tester->getDisplay());
260 $this->assertContains('Installing assets.', $tester->getDisplay());
c641baad
J
261
262 $this->assertContains('Creating schema', $tester->getDisplay());
263 }
264
265 public function testRunInstallCommandNoInteraction()
266 {
d5027625 267 $application = new Application($this->getClient()->getKernel());
0ee043f7 268 $application->add(new InstallCommandMock());
c641baad
J
269
270 $command = $application->find('wallabag:install');
271
78507d28
JB
272 // We mock the QuestionHelper
273 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
c641baad
J
274 ->disableOriginalConstructor()
275 ->getMock();
78507d28 276 $question->expects($this->any())
c641baad 277 ->method('ask')
78507d28 278 ->will($this->returnValue('yes_'.uniqid('', true)));
c641baad
J
279
280 // We override the standard helper with our mock
78507d28 281 $command->getHelperSet()->set($question, 'question');
c641baad
J
282
283 $tester = new CommandTester($command);
4094ea47 284 $tester->execute([
c641baad
J
285 'command' => $command->getName(),
286 '--no-interaction' => true,
4094ea47 287 ]);
c641baad 288
637dc4bb
JB
289 $this->assertContains('Checking system requirements.', $tester->getDisplay());
290 $this->assertContains('Setting up database.', $tester->getDisplay());
291 $this->assertContains('Administration setup.', $tester->getDisplay());
292 $this->assertContains('Config setup.', $tester->getDisplay());
293 $this->assertContains('Installing assets.', $tester->getDisplay());
c641baad 294 }
0bf99bb1 295}