]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Tests/Command/InstallCommandTest.php
Defining Github token is now useless
[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());
2a586069 84 $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay());
c641baad
J
85 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
86 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
87
88 // we force to reset everything
d5027625 89 $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay());
c641baad
J
90 }
91
92 public function testRunInstallCommandWithDatabaseRemoved()
93 {
75c48e3a
JB
94 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver) {
95 /*
d5027625
JB
96 * LOG: statement: CREATE DATABASE "wallabag"
97 * ERROR: source database "template1" is being accessed by other users
98 * DETAIL: There is 1 other session using the database.
99 * STATEMENT: CREATE DATABASE "wallabag"
100 * FATAL: database "wallabag" does not exist
101 *
102 * http://stackoverflow.com/a/14374832/569101
103 */
104 $this->markTestSkipped('PostgreSQL spotted: can find a good way to drop current database, skipping.');
105 }
106
107 $application = new Application($this->getClient()->getKernel());
c641baad
J
108 $application->add(new DropDatabaseDoctrineCommand());
109
110 // drop database first, so the install command won't ask to reset things
d5027625 111 $command = $application->find('doctrine:database:drop');
c641baad
J
112 $command->run(new ArrayInput(array(
113 'command' => 'doctrine:database:drop',
114 '--force' => true,
115 )), new NullOutput());
116
d5027625
JB
117 // start a new application to avoid lagging connexion to pgsql
118 $client = static::createClient();
119 $application = new Application($client->getKernel());
120 $application->add(new InstallCommand());
121
c641baad
J
122 $command = $application->find('wallabag:install');
123
78507d28
JB
124 // We mock the QuestionHelper
125 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
c641baad
J
126 ->disableOriginalConstructor()
127 ->getMock();
78507d28 128 $question->expects($this->any())
c641baad 129 ->method('ask')
78507d28 130 ->will($this->returnValue('yes_'.uniqid('', true)));
c641baad
J
131
132 // We override the standard helper with our mock
78507d28 133 $command->getHelperSet()->set($question, 'question');
c641baad
J
134
135 $tester = new CommandTester($command);
136 $tester->execute(array(
137 'command' => $command->getName(),
138 ));
139
140 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
141 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
142 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
143 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
144
145 // the current database doesn't already exist
146 $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay());
147 }
148
149 public function testRunInstallCommandChooseResetSchema()
150 {
d5027625 151 $application = new Application($this->getClient()->getKernel());
0ee043f7 152 $application->add(new InstallCommandMock());
c641baad
J
153
154 $command = $application->find('wallabag:install');
155
78507d28
JB
156 // We mock the QuestionHelper
157 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
c641baad
J
158 ->disableOriginalConstructor()
159 ->getMock();
160
78507d28
JB
161 $question->expects($this->exactly(3))
162 ->method('ask')
c641baad
J
163 ->will($this->onConsecutiveCalls(
164 false, // don't want to reset the entire database
165 true, // do want to reset the schema
166 false // don't want to create a new user
167 ));
168
169 // We override the standard helper with our mock
78507d28 170 $command->getHelperSet()->set($question, 'question');
c641baad
J
171
172 $tester = new CommandTester($command);
173 $tester->execute(array(
174 'command' => $command->getName(),
175 ));
176
177 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
178 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
179 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
180 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
181
182 $this->assertContains('Droping schema and creating schema', $tester->getDisplay());
183 }
184
185 public function testRunInstallCommandChooseNothing()
186 {
75c48e3a
JB
187 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver) {
188 /*
970e0e99 189 * @see testRunInstallCommandWithDatabaseRemoved
d5027625
JB
190 */
191 $this->markTestSkipped('PostgreSQL spotted: can find a good way to drop current database, skipping.');
192 }
193
194 $application = new Application($this->getClient()->getKernel());
c641baad
J
195 $application->add(new InstallCommand());
196 $application->add(new DropDatabaseDoctrineCommand());
197 $application->add(new CreateDatabaseDoctrineCommand());
198
199 // drop database first, so the install command won't ask to reset things
200 $command = new DropDatabaseDoctrineCommand();
201 $command->setApplication($application);
202 $command->run(new ArrayInput(array(
203 'command' => 'doctrine:database:drop',
204 '--force' => true,
205 )), new NullOutput());
206
d5027625 207 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
c641baad
J
208
209 $command = new CreateDatabaseDoctrineCommand();
210 $command->setApplication($application);
211 $command->run(new ArrayInput(array(
212 'command' => 'doctrine:database:create',
213 '--env' => 'test',
214 )), new NullOutput());
215
216 $command = $application->find('wallabag:install');
217
78507d28
JB
218 // We mock the QuestionHelper
219 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
c641baad
J
220 ->disableOriginalConstructor()
221 ->getMock();
222
78507d28
JB
223 $question->expects($this->exactly(2))
224 ->method('ask')
c641baad
J
225 ->will($this->onConsecutiveCalls(
226 false, // don't want to reset the entire database
227 false // don't want to create a new user
228 ));
229
230 // We override the standard helper with our mock
78507d28 231 $command->getHelperSet()->set($question, 'question');
c641baad
J
232
233 $tester = new CommandTester($command);
234 $tester->execute(array(
235 'command' => $command->getName(),
236 ));
237
238 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
239 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
240 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
241 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
242
243 $this->assertContains('Creating schema', $tester->getDisplay());
244 }
245
246 public function testRunInstallCommandNoInteraction()
247 {
d5027625 248 $application = new Application($this->getClient()->getKernel());
0ee043f7 249 $application->add(new InstallCommandMock());
c641baad
J
250
251 $command = $application->find('wallabag:install');
252
78507d28
JB
253 // We mock the QuestionHelper
254 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
c641baad
J
255 ->disableOriginalConstructor()
256 ->getMock();
78507d28 257 $question->expects($this->any())
c641baad 258 ->method('ask')
78507d28 259 ->will($this->returnValue('yes_'.uniqid('', true)));
c641baad
J
260
261 // We override the standard helper with our mock
78507d28 262 $command->getHelperSet()->set($question, 'question');
c641baad
J
263
264 $tester = new CommandTester($command);
265 $tester->execute(array(
266 'command' => $command->getName(),
267 '--no-interaction' => true,
268 ));
269
270 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
271 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
272 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
273 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
274 }
0bf99bb1 275}