]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Tests/Command/InstallCommandTest.php
Fix tests
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Tests / Command / InstallCommandTest.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Tests\Command;
4
5 use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
6 use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
7 use Symfony\Bundle\FrameworkBundle\Console\Application;
8 use Symfony\Component\Console\Input\ArrayInput;
9 use Symfony\Component\Console\Output\NullOutput;
10 use Symfony\Component\Console\Tester\CommandTester;
11 use Wallabag\CoreBundle\Command\InstallCommand;
12 use Wallabag\CoreBundle\Tests\Mock\InstallCommandMock;
13 use Wallabag\CoreBundle\Tests\WallabagCoreTestCase;
14
15 class InstallCommandTest extends WallabagCoreTestCase
16 {
17 public static function tearDownAfterClass()
18 {
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 {
31 $application = new Application($this->getClient()->getKernel());
32 $application->add(new InstallCommandMock());
33
34 $command = $application->find('wallabag:install');
35
36 // We mock the QuestionHelper
37 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
38 ->disableOriginalConstructor()
39 ->getMock();
40 $question->expects($this->any())
41 ->method('ask')
42 ->will($this->returnValue('yes_'.uniqid('', true)));
43
44 // We override the standard helper with our mock
45 $command->getHelperSet()->set($question, 'question');
46
47 $tester = new CommandTester($command);
48 $tester->execute(array(
49 'command' => $command->getName(),
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 }
57
58 public function testRunInstallCommandWithReset()
59 {
60 $application = new Application($this->getClient()->getKernel());
61 $application->add(new InstallCommandMock());
62
63 $command = $application->find('wallabag:install');
64
65 // We mock the QuestionHelper
66 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
67 ->disableOriginalConstructor()
68 ->getMock();
69 $question->expects($this->any())
70 ->method('ask')
71 ->will($this->returnValue('yes_'.uniqid('', true)));
72
73 // We override the standard helper with our mock
74 $command->getHelperSet()->set($question, 'question');
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('Droping database, creating database and schema, clearing the cache', $tester->getDisplay());
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
89 $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay());
90 }
91
92 public function testRunInstallCommandWithDatabaseRemoved()
93 {
94 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver) {
95 /*
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());
108 $application->add(new DropDatabaseDoctrineCommand());
109
110 // drop database first, so the install command won't ask to reset things
111 $command = $application->find('doctrine:database:drop');
112 $command->run(new ArrayInput(array(
113 'command' => 'doctrine:database:drop',
114 '--force' => true,
115 )), new NullOutput());
116
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
122 $command = $application->find('wallabag:install');
123
124 // We mock the QuestionHelper
125 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
126 ->disableOriginalConstructor()
127 ->getMock();
128 $question->expects($this->any())
129 ->method('ask')
130 ->will($this->returnValue('yes_'.uniqid('', true)));
131
132 // We override the standard helper with our mock
133 $command->getHelperSet()->set($question, 'question');
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 {
151 $application = new Application($this->getClient()->getKernel());
152 $application->add(new InstallCommandMock());
153
154 $command = $application->find('wallabag:install');
155
156 // We mock the QuestionHelper
157 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
158 ->disableOriginalConstructor()
159 ->getMock();
160
161 $question->expects($this->exactly(3))
162 ->method('ask')
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
170 $command->getHelperSet()->set($question, 'question');
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 {
187 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver) {
188 /*
189 * @see testRunInstallCommandWithDatabaseRemoved
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());
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
207 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
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
218 // We mock the QuestionHelper
219 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
220 ->disableOriginalConstructor()
221 ->getMock();
222
223 $question->expects($this->exactly(2))
224 ->method('ask')
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
231 $command->getHelperSet()->set($question, 'question');
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 {
248 $application = new Application($this->getClient()->getKernel());
249 $application->add(new InstallCommandMock());
250
251 $command = $application->find('wallabag:install');
252
253 // We mock the QuestionHelper
254 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
255 ->disableOriginalConstructor()
256 ->getMock();
257 $question->expects($this->any())
258 ->method('ask')
259 ->will($this->returnValue('yes_'.uniqid('', true)));
260
261 // We override the standard helper with our mock
262 $command->getHelperSet()->set($question, 'question');
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 }
275 }