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