]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Tests/Command/InstallCommandTest.php
Symfony Upgrade Fixer FTW
[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('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
88 $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay());
89 }
90
91 public function testRunInstallCommandWithDatabaseRemoved()
92 {
93 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver) {
94 /*
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());
107 $application->add(new DropDatabaseDoctrineCommand());
108
109 // drop database first, so the install command won't ask to reset things
110 $command = $application->find('doctrine:database:drop');
111 $command->run(new ArrayInput(array(
112 'command' => 'doctrine:database:drop',
113 '--force' => true,
114 )), new NullOutput());
115
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
121 $command = $application->find('wallabag:install');
122
123 // We mock the QuestionHelper
124 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
125 ->disableOriginalConstructor()
126 ->getMock();
127 $question->expects($this->any())
128 ->method('ask')
129 ->will($this->returnValue('yes_'.uniqid('', true)));
130
131 // We override the standard helper with our mock
132 $command->getHelperSet()->set($question, 'question');
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 {
150 $application = new Application($this->getClient()->getKernel());
151 $application->add(new InstallCommandMock());
152
153 $command = $application->find('wallabag:install');
154
155 // We mock the QuestionHelper
156 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
157 ->disableOriginalConstructor()
158 ->getMock();
159
160 $question->expects($this->exactly(3))
161 ->method('ask')
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
169 $command->getHelperSet()->set($question, 'question');
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 {
186 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver) {
187 /*
188 * @see testRunInstallCommandWithDatabaseRemoved
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());
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
206 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
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
217 // We mock the QuestionHelper
218 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
219 ->disableOriginalConstructor()
220 ->getMock();
221
222 $question->expects($this->exactly(2))
223 ->method('ask')
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
230 $command->getHelperSet()->set($question, 'question');
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 {
247 $application = new Application($this->getClient()->getKernel());
248 $application->add(new InstallCommandMock());
249
250 $command = $application->find('wallabag:install');
251
252 // We mock the QuestionHelper
253 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
254 ->disableOriginalConstructor()
255 ->getMock();
256 $question->expects($this->any())
257 ->method('ask')
258 ->will($this->returnValue('yes_'.uniqid('', true)));
259
260 // We override the standard helper with our mock
261 $command->getHelperSet()->set($question, 'question');
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 }
274 }