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