]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Tests/Command/InstallCommandTest.php
Skipping PostgreSQL test that drop database
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Tests / Command / InstallCommandTest.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Tests\Command;
4
5 use Wallabag\CoreBundle\Tests\WallabagCoreTestCase;
6 use Wallabag\CoreBundle\Command\InstallCommand;
7 use Wallabag\CoreBundle\Tests\Mock\InstallCommandMock;
8 use Symfony\Bundle\FrameworkBundle\Console\Application;
9 use Symfony\Component\Console\Tester\CommandTester;
10 use Symfony\Component\Console\Input\ArrayInput;
11 use Symfony\Component\Console\Output\NullOutput;
12 use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
13 use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
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 * LOG: statement: CREATE DATABASE "wallabag"
189 * ERROR: source database "template1" is being accessed by other users
190 * DETAIL: There is 1 other session using the database.
191 * STATEMENT: CREATE DATABASE "wallabag"
192 * FATAL: database "wallabag" does not exist
193 *
194 * http://stackoverflow.com/a/14374832/569101
195 */
196 $this->markTestSkipped('PostgreSQL spotted: can find a good way to drop current database, skipping.');
197 }
198
199 $application = new Application($this->getClient()->getKernel());
200 $application->add(new InstallCommand());
201 $application->add(new DropDatabaseDoctrineCommand());
202 $application->add(new CreateDatabaseDoctrineCommand());
203
204 // drop database first, so the install command won't ask to reset things
205 $command = new DropDatabaseDoctrineCommand();
206 $command->setApplication($application);
207 $command->run(new ArrayInput(array(
208 'command' => 'doctrine:database:drop',
209 '--force' => true,
210 )), new NullOutput());
211
212 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
213
214 $command = new CreateDatabaseDoctrineCommand();
215 $command->setApplication($application);
216 $command->run(new ArrayInput(array(
217 'command' => 'doctrine:database:create',
218 '--env' => 'test',
219 )), new NullOutput());
220
221 $command = $application->find('wallabag:install');
222
223 // We mock the QuestionHelper
224 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
225 ->disableOriginalConstructor()
226 ->getMock();
227
228 $question->expects($this->exactly(2))
229 ->method('ask')
230 ->will($this->onConsecutiveCalls(
231 false, // don't want to reset the entire database
232 false // don't want to create a new user
233 ));
234
235 // We override the standard helper with our mock
236 $command->getHelperSet()->set($question, 'question');
237
238 $tester = new CommandTester($command);
239 $tester->execute(array(
240 'command' => $command->getName(),
241 ));
242
243 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
244 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
245 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
246 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
247
248 $this->assertContains('Creating schema', $tester->getDisplay());
249 }
250
251 public function testRunInstallCommandNoInteraction()
252 {
253 $application = new Application($this->getClient()->getKernel());
254 $application->add(new InstallCommandMock());
255
256 $command = $application->find('wallabag:install');
257
258 // We mock the QuestionHelper
259 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
260 ->disableOriginalConstructor()
261 ->getMock();
262 $question->expects($this->any())
263 ->method('ask')
264 ->will($this->returnValue('yes_'.uniqid('', true)));
265
266 // We override the standard helper with our mock
267 $command->getHelperSet()->set($question, 'question');
268
269 $tester = new CommandTester($command);
270 $tester->execute(array(
271 'command' => $command->getName(),
272 '--no-interaction' => true,
273 ));
274
275 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
276 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
277 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
278 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
279 }
280 }