]>
Commit | Line | Data |
---|---|---|
0bf99bb1 J |
1 | <?php |
2 | ||
23634d5d | 3 | namespace Tests\Wallabag\CoreBundle\Command; |
0bf99bb1 | 4 | |
619cc453 JB |
5 | use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand; |
6 | use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand; | |
0bf99bb1 | 7 | use Symfony\Bundle\FrameworkBundle\Console\Application; |
0bf99bb1 J |
8 | use Symfony\Component\Console\Input\ArrayInput; |
9 | use Symfony\Component\Console\Output\NullOutput; | |
619cc453 JB |
10 | use Symfony\Component\Console\Tester\CommandTester; |
11 | use Wallabag\CoreBundle\Command\InstallCommand; | |
23634d5d JB |
12 | use Tests\Wallabag\CoreBundle\Mock\InstallCommandMock; |
13 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; | |
0bf99bb1 | 14 | |
769e19dc | 15 | class InstallCommandTest extends WallabagCoreTestCase |
0bf99bb1 | 16 | { |
3c39f5ac JB |
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 | */ | |
6dfac457 | 31 | $this->markTestSkipped('PostgreSQL spotted: can\'t find a good way to drop current database, skipping.'); |
3c39f5ac JB |
32 | } |
33 | } | |
34 | ||
6dfac457 | 35 | /** |
8541b3c4 | 36 | * Ensure next tests will have a clean database. |
6dfac457 | 37 | */ |
c641baad | 38 | public static function tearDownAfterClass() |
0bf99bb1 | 39 | { |
0bf99bb1 J |
40 | $application = new Application(static::$kernel); |
41 | $application->setAutoExit(false); | |
42 | ||
6dfac457 JB |
43 | $application->run(new ArrayInput([ |
44 | 'command' => 'doctrine:schema:drop', | |
45 | '--no-interaction' => true, | |
46 | '--force' => true, | |
47 | '--env' => 'test', | |
48 | ]), new NullOutput()); | |
49 | ||
50 | $application->run(new ArrayInput([ | |
51 | 'command' => 'doctrine:schema:create', | |
52 | '--no-interaction' => true, | |
53 | '--env' => 'test', | |
54 | ]), new NullOutput()); | |
55 | ||
56 | $application->run(new ArrayInput([ | |
0bf99bb1 J |
57 | 'command' => 'doctrine:fixtures:load', |
58 | '--no-interaction' => true, | |
59 | '--env' => 'test', | |
4094ea47 | 60 | ]), new NullOutput()); |
0bf99bb1 J |
61 | } |
62 | ||
63 | public function testRunInstallCommand() | |
64 | { | |
d5027625 | 65 | $application = new Application($this->getClient()->getKernel()); |
0ee043f7 | 66 | $application->add(new InstallCommandMock()); |
0bf99bb1 J |
67 | |
68 | $command = $application->find('wallabag:install'); | |
69 | ||
78507d28 JB |
70 | // We mock the QuestionHelper |
71 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
0bf99bb1 J |
72 | ->disableOriginalConstructor() |
73 | ->getMock(); | |
78507d28 | 74 | $question->expects($this->any()) |
0bf99bb1 | 75 | ->method('ask') |
78507d28 | 76 | ->will($this->returnValue('yes_'.uniqid('', true))); |
0bf99bb1 J |
77 | |
78 | // We override the standard helper with our mock | |
78507d28 | 79 | $command->getHelperSet()->set($question, 'question'); |
0bf99bb1 J |
80 | |
81 | $tester = new CommandTester($command); | |
4094ea47 | 82 | $tester->execute([ |
732c2ad8 | 83 | 'command' => $command->getName(), |
4094ea47 | 84 | ]); |
0bf99bb1 | 85 | |
637dc4bb JB |
86 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
87 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
88 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
89 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
0bf99bb1 | 90 | } |
c641baad J |
91 | |
92 | public function testRunInstallCommandWithReset() | |
93 | { | |
d5027625 | 94 | $application = new Application($this->getClient()->getKernel()); |
0ee043f7 | 95 | $application->add(new InstallCommandMock()); |
c641baad J |
96 | |
97 | $command = $application->find('wallabag:install'); | |
98 | ||
78507d28 JB |
99 | // We mock the QuestionHelper |
100 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
c641baad J |
101 | ->disableOriginalConstructor() |
102 | ->getMock(); | |
78507d28 | 103 | $question->expects($this->any()) |
c641baad | 104 | ->method('ask') |
78507d28 | 105 | ->will($this->returnValue('yes_'.uniqid('', true))); |
c641baad J |
106 | |
107 | // We override the standard helper with our mock | |
78507d28 | 108 | $command->getHelperSet()->set($question, 'question'); |
c641baad J |
109 | |
110 | $tester = new CommandTester($command); | |
4094ea47 | 111 | $tester->execute([ |
c641baad J |
112 | 'command' => $command->getName(), |
113 | '--reset' => true, | |
4094ea47 | 114 | ]); |
c641baad | 115 | |
637dc4bb JB |
116 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
117 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
2a586069 | 118 | $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay()); |
637dc4bb JB |
119 | $this->assertContains('Administration setup.', $tester->getDisplay()); |
120 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
c641baad J |
121 | |
122 | // we force to reset everything | |
d5027625 | 123 | $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay()); |
c641baad J |
124 | } |
125 | ||
126 | public function testRunInstallCommandWithDatabaseRemoved() | |
127 | { | |
f62c3faf JB |
128 | // skipped SQLite check when database is removed because while testing for the connection, |
129 | // the driver will create the file (so the database) before testing if database exist | |
130 | if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) { | |
131 | $this->markTestSkipped('SQLite spotted: can\'t test with database removed.'); | |
132 | } | |
133 | ||
d5027625 | 134 | $application = new Application($this->getClient()->getKernel()); |
c641baad J |
135 | $application->add(new DropDatabaseDoctrineCommand()); |
136 | ||
137 | // drop database first, so the install command won't ask to reset things | |
d5027625 | 138 | $command = $application->find('doctrine:database:drop'); |
4094ea47 | 139 | $command->run(new ArrayInput([ |
c641baad J |
140 | 'command' => 'doctrine:database:drop', |
141 | '--force' => true, | |
4094ea47 | 142 | ]), new NullOutput()); |
c641baad | 143 | |
d5027625 JB |
144 | // start a new application to avoid lagging connexion to pgsql |
145 | $client = static::createClient(); | |
146 | $application = new Application($client->getKernel()); | |
147 | $application->add(new InstallCommand()); | |
148 | ||
c641baad J |
149 | $command = $application->find('wallabag:install'); |
150 | ||
78507d28 JB |
151 | // We mock the QuestionHelper |
152 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
c641baad J |
153 | ->disableOriginalConstructor() |
154 | ->getMock(); | |
78507d28 | 155 | $question->expects($this->any()) |
c641baad | 156 | ->method('ask') |
78507d28 | 157 | ->will($this->returnValue('yes_'.uniqid('', true))); |
c641baad J |
158 | |
159 | // We override the standard helper with our mock | |
78507d28 | 160 | $command->getHelperSet()->set($question, 'question'); |
c641baad J |
161 | |
162 | $tester = new CommandTester($command); | |
4094ea47 | 163 | $tester->execute([ |
c641baad | 164 | 'command' => $command->getName(), |
4094ea47 | 165 | ]); |
c641baad | 166 | |
637dc4bb JB |
167 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
168 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
169 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
170 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
c641baad J |
171 | |
172 | // the current database doesn't already exist | |
173 | $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay()); | |
174 | } | |
175 | ||
176 | public function testRunInstallCommandChooseResetSchema() | |
177 | { | |
d5027625 | 178 | $application = new Application($this->getClient()->getKernel()); |
0ee043f7 | 179 | $application->add(new InstallCommandMock()); |
c641baad J |
180 | |
181 | $command = $application->find('wallabag:install'); | |
182 | ||
78507d28 JB |
183 | // We mock the QuestionHelper |
184 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
c641baad J |
185 | ->disableOriginalConstructor() |
186 | ->getMock(); | |
187 | ||
78507d28 JB |
188 | $question->expects($this->exactly(3)) |
189 | ->method('ask') | |
c641baad J |
190 | ->will($this->onConsecutiveCalls( |
191 | false, // don't want to reset the entire database | |
192 | true, // do want to reset the schema | |
193 | false // don't want to create a new user | |
194 | )); | |
195 | ||
196 | // We override the standard helper with our mock | |
78507d28 | 197 | $command->getHelperSet()->set($question, 'question'); |
c641baad J |
198 | |
199 | $tester = new CommandTester($command); | |
4094ea47 | 200 | $tester->execute([ |
c641baad | 201 | 'command' => $command->getName(), |
4094ea47 | 202 | ]); |
c641baad | 203 | |
637dc4bb JB |
204 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
205 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
206 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
207 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
c641baad J |
208 | |
209 | $this->assertContains('Droping schema and creating schema', $tester->getDisplay()); | |
210 | } | |
211 | ||
212 | public function testRunInstallCommandChooseNothing() | |
213 | { | |
d5027625 | 214 | $application = new Application($this->getClient()->getKernel()); |
c641baad J |
215 | $application->add(new InstallCommand()); |
216 | $application->add(new DropDatabaseDoctrineCommand()); | |
217 | $application->add(new CreateDatabaseDoctrineCommand()); | |
218 | ||
219 | // drop database first, so the install command won't ask to reset things | |
220 | $command = new DropDatabaseDoctrineCommand(); | |
221 | $command->setApplication($application); | |
4094ea47 | 222 | $command->run(new ArrayInput([ |
c641baad J |
223 | 'command' => 'doctrine:database:drop', |
224 | '--force' => true, | |
4094ea47 | 225 | ]), new NullOutput()); |
c641baad | 226 | |
d5027625 | 227 | $this->getClient()->getContainer()->get('doctrine')->getConnection()->close(); |
c641baad J |
228 | |
229 | $command = new CreateDatabaseDoctrineCommand(); | |
230 | $command->setApplication($application); | |
4094ea47 | 231 | $command->run(new ArrayInput([ |
c641baad J |
232 | 'command' => 'doctrine:database:create', |
233 | '--env' => 'test', | |
4094ea47 | 234 | ]), new NullOutput()); |
c641baad J |
235 | |
236 | $command = $application->find('wallabag:install'); | |
237 | ||
78507d28 JB |
238 | // We mock the QuestionHelper |
239 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
c641baad J |
240 | ->disableOriginalConstructor() |
241 | ->getMock(); | |
242 | ||
78507d28 JB |
243 | $question->expects($this->exactly(2)) |
244 | ->method('ask') | |
c641baad J |
245 | ->will($this->onConsecutiveCalls( |
246 | false, // don't want to reset the entire database | |
247 | false // don't want to create a new user | |
248 | )); | |
249 | ||
250 | // We override the standard helper with our mock | |
78507d28 | 251 | $command->getHelperSet()->set($question, 'question'); |
c641baad J |
252 | |
253 | $tester = new CommandTester($command); | |
4094ea47 | 254 | $tester->execute([ |
c641baad | 255 | 'command' => $command->getName(), |
4094ea47 | 256 | ]); |
c641baad | 257 | |
637dc4bb JB |
258 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
259 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
260 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
261 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
c641baad J |
262 | |
263 | $this->assertContains('Creating schema', $tester->getDisplay()); | |
264 | } | |
265 | ||
266 | public function testRunInstallCommandNoInteraction() | |
267 | { | |
d5027625 | 268 | $application = new Application($this->getClient()->getKernel()); |
0ee043f7 | 269 | $application->add(new InstallCommandMock()); |
c641baad J |
270 | |
271 | $command = $application->find('wallabag:install'); | |
272 | ||
78507d28 JB |
273 | // We mock the QuestionHelper |
274 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
c641baad J |
275 | ->disableOriginalConstructor() |
276 | ->getMock(); | |
78507d28 | 277 | $question->expects($this->any()) |
c641baad | 278 | ->method('ask') |
78507d28 | 279 | ->will($this->returnValue('yes_'.uniqid('', true))); |
c641baad J |
280 | |
281 | // We override the standard helper with our mock | |
78507d28 | 282 | $command->getHelperSet()->set($question, 'question'); |
c641baad J |
283 | |
284 | $tester = new CommandTester($command); | |
4094ea47 | 285 | $tester->execute([ |
c641baad J |
286 | 'command' => $command->getName(), |
287 | '--no-interaction' => true, | |
4094ea47 | 288 | ]); |
c641baad | 289 | |
637dc4bb JB |
290 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
291 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
292 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
293 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
c641baad | 294 | } |
0bf99bb1 | 295 | } |