]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Tests/Command/InstallCommandTest.php
Merge pull request #1524 from wallabag/sf2.8
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Tests / Command / InstallCommandTest.php
CommitLineData
0bf99bb1
J
1<?php
2
3namespace Wallabag\CoreBundle\Tests\Command;
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;
12use Wallabag\CoreBundle\Tests\Mock\InstallCommandMock;
13use Wallabag\CoreBundle\Tests\WallabagCoreTestCase;
0bf99bb1 14
769e19dc 15class InstallCommandTest extends WallabagCoreTestCase
0bf99bb1 16{
c641baad 17 public static function tearDownAfterClass()
0bf99bb1 18 {
0bf99bb1
J
19 $application = new Application(static::$kernel);
20 $application->setAutoExit(false);
21
22 $code = $application->run(new ArrayInput(array(
23 'command' => 'doctrine:fixtures:load',
24 '--no-interaction' => true,
25 '--env' => 'test',
26 )), new NullOutput());
27 }
28
29 public function testRunInstallCommand()
30 {
d5027625 31 $application = new Application($this->getClient()->getKernel());
0ee043f7 32 $application->add(new InstallCommandMock());
0bf99bb1
J
33
34 $command = $application->find('wallabag:install');
35
78507d28
JB
36 // We mock the QuestionHelper
37 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
0bf99bb1
J
38 ->disableOriginalConstructor()
39 ->getMock();
78507d28 40 $question->expects($this->any())
0bf99bb1 41 ->method('ask')
78507d28 42 ->will($this->returnValue('yes_'.uniqid('', true)));
0bf99bb1
J
43
44 // We override the standard helper with our mock
78507d28 45 $command->getHelperSet()->set($question, 'question');
0bf99bb1
J
46
47 $tester = new CommandTester($command);
48 $tester->execute(array(
732c2ad8 49 'command' => $command->getName(),
0bf99bb1
J
50 ));
51
52 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
53 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
54 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
55 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
56 }
c641baad
J
57
58 public function testRunInstallCommandWithReset()
59 {
d5027625 60 $application = new Application($this->getClient()->getKernel());
0ee043f7 61 $application->add(new InstallCommandMock());
c641baad
J
62
63 $command = $application->find('wallabag:install');
64
78507d28
JB
65 // We mock the QuestionHelper
66 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
c641baad
J
67 ->disableOriginalConstructor()
68 ->getMock();
78507d28 69 $question->expects($this->any())
c641baad 70 ->method('ask')
78507d28 71 ->will($this->returnValue('yes_'.uniqid('', true)));
c641baad
J
72
73 // We override the standard helper with our mock
78507d28 74 $command->getHelperSet()->set($question, 'question');
c641baad
J
75
76 $tester = new CommandTester($command);
77 $tester->execute(array(
78 'command' => $command->getName(),
79 '--reset' => true,
80 ));
81
82 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
83 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
84 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
85 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
86
87 // we force to reset everything
d5027625 88 $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay());
c641baad
J
89 }
90
91 public function testRunInstallCommandWithDatabaseRemoved()
92 {
75c48e3a
JB
93 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver) {
94 /*
d5027625
JB
95 * LOG: statement: CREATE DATABASE "wallabag"
96 * ERROR: source database "template1" is being accessed by other users
97 * DETAIL: There is 1 other session using the database.
98 * STATEMENT: CREATE DATABASE "wallabag"
99 * FATAL: database "wallabag" does not exist
100 *
101 * http://stackoverflow.com/a/14374832/569101
102 */
103 $this->markTestSkipped('PostgreSQL spotted: can find a good way to drop current database, skipping.');
104 }
105
106 $application = new Application($this->getClient()->getKernel());
c641baad
J
107 $application->add(new DropDatabaseDoctrineCommand());
108
109 // drop database first, so the install command won't ask to reset things
d5027625 110 $command = $application->find('doctrine:database:drop');
c641baad
J
111 $command->run(new ArrayInput(array(
112 'command' => 'doctrine:database:drop',
113 '--force' => true,
114 )), new NullOutput());
115
d5027625
JB
116 // start a new application to avoid lagging connexion to pgsql
117 $client = static::createClient();
118 $application = new Application($client->getKernel());
119 $application->add(new InstallCommand());
120
c641baad
J
121 $command = $application->find('wallabag:install');
122
78507d28
JB
123 // We mock the QuestionHelper
124 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
c641baad
J
125 ->disableOriginalConstructor()
126 ->getMock();
78507d28 127 $question->expects($this->any())
c641baad 128 ->method('ask')
78507d28 129 ->will($this->returnValue('yes_'.uniqid('', true)));
c641baad
J
130
131 // We override the standard helper with our mock
78507d28 132 $command->getHelperSet()->set($question, 'question');
c641baad
J
133
134 $tester = new CommandTester($command);
135 $tester->execute(array(
136 'command' => $command->getName(),
137 ));
138
139 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
140 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
141 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
142 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
143
144 // the current database doesn't already exist
145 $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay());
146 }
147
148 public function testRunInstallCommandChooseResetSchema()
149 {
d5027625 150 $application = new Application($this->getClient()->getKernel());
0ee043f7 151 $application->add(new InstallCommandMock());
c641baad
J
152
153 $command = $application->find('wallabag:install');
154
78507d28
JB
155 // We mock the QuestionHelper
156 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
c641baad
J
157 ->disableOriginalConstructor()
158 ->getMock();
159
78507d28
JB
160 $question->expects($this->exactly(3))
161 ->method('ask')
c641baad
J
162 ->will($this->onConsecutiveCalls(
163 false, // don't want to reset the entire database
164 true, // do want to reset the schema
165 false // don't want to create a new user
166 ));
167
168 // We override the standard helper with our mock
78507d28 169 $command->getHelperSet()->set($question, 'question');
c641baad
J
170
171 $tester = new CommandTester($command);
172 $tester->execute(array(
173 'command' => $command->getName(),
174 ));
175
176 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
177 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
178 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
179 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
180
181 $this->assertContains('Droping schema and creating schema', $tester->getDisplay());
182 }
183
184 public function testRunInstallCommandChooseNothing()
185 {
75c48e3a
JB
186 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver) {
187 /*
970e0e99 188 * @see testRunInstallCommandWithDatabaseRemoved
d5027625
JB
189 */
190 $this->markTestSkipped('PostgreSQL spotted: can find a good way to drop current database, skipping.');
191 }
192
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);
201 $command->run(new ArrayInput(array(
202 'command' => 'doctrine:database:drop',
203 '--force' => true,
204 )), new NullOutput());
205
d5027625 206 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
c641baad
J
207
208 $command = new CreateDatabaseDoctrineCommand();
209 $command->setApplication($application);
210 $command->run(new ArrayInput(array(
211 'command' => 'doctrine:database:create',
212 '--env' => 'test',
213 )), new NullOutput());
214
215 $command = $application->find('wallabag:install');
216
78507d28
JB
217 // We mock the QuestionHelper
218 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
c641baad
J
219 ->disableOriginalConstructor()
220 ->getMock();
221
78507d28
JB
222 $question->expects($this->exactly(2))
223 ->method('ask')
c641baad
J
224 ->will($this->onConsecutiveCalls(
225 false, // don't want to reset the entire database
226 false // don't want to create a new user
227 ));
228
229 // We override the standard helper with our mock
78507d28 230 $command->getHelperSet()->set($question, 'question');
c641baad
J
231
232 $tester = new CommandTester($command);
233 $tester->execute(array(
234 'command' => $command->getName(),
235 ));
236
237 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
238 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
239 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
240 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
241
242 $this->assertContains('Creating schema', $tester->getDisplay());
243 }
244
245 public function testRunInstallCommandNoInteraction()
246 {
d5027625 247 $application = new Application($this->getClient()->getKernel());
0ee043f7 248 $application->add(new InstallCommandMock());
c641baad
J
249
250 $command = $application->find('wallabag:install');
251
78507d28
JB
252 // We mock the QuestionHelper
253 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
c641baad
J
254 ->disableOriginalConstructor()
255 ->getMock();
78507d28 256 $question->expects($this->any())
c641baad 257 ->method('ask')
78507d28 258 ->will($this->returnValue('yes_'.uniqid('', true)));
c641baad
J
259
260 // We override the standard helper with our mock
78507d28 261 $command->getHelperSet()->set($question, 'question');
c641baad
J
262
263 $tester = new CommandTester($command);
264 $tester->execute(array(
265 'command' => $command->getName(),
266 '--no-interaction' => true,
267 ));
268
269 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
270 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
271 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
272 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
273 }
0bf99bb1 274}