]>
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()); | |
7d2d1d68 | 90 | $this->assertContains('Run migrations.', $tester->getDisplay()); |
0bf99bb1 | 91 | } |
c641baad J |
92 | |
93 | public function testRunInstallCommandWithReset() | |
94 | { | |
d5027625 | 95 | $application = new Application($this->getClient()->getKernel()); |
0ee043f7 | 96 | $application->add(new InstallCommandMock()); |
c641baad J |
97 | |
98 | $command = $application->find('wallabag:install'); | |
99 | ||
78507d28 JB |
100 | // We mock the QuestionHelper |
101 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
c641baad J |
102 | ->disableOriginalConstructor() |
103 | ->getMock(); | |
78507d28 | 104 | $question->expects($this->any()) |
c641baad | 105 | ->method('ask') |
78507d28 | 106 | ->will($this->returnValue('yes_'.uniqid('', true))); |
c641baad J |
107 | |
108 | // We override the standard helper with our mock | |
78507d28 | 109 | $command->getHelperSet()->set($question, 'question'); |
c641baad J |
110 | |
111 | $tester = new CommandTester($command); | |
4094ea47 | 112 | $tester->execute([ |
c641baad J |
113 | 'command' => $command->getName(), |
114 | '--reset' => true, | |
4094ea47 | 115 | ]); |
c641baad | 116 | |
637dc4bb JB |
117 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
118 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
7d2d1d68 | 119 | $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay()); |
637dc4bb JB |
120 | $this->assertContains('Administration setup.', $tester->getDisplay()); |
121 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
7d2d1d68 | 122 | $this->assertContains('Run migrations.', $tester->getDisplay()); |
c641baad J |
123 | |
124 | // we force to reset everything | |
7d2d1d68 | 125 | $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay()); |
c641baad J |
126 | } |
127 | ||
128 | public function testRunInstallCommandWithDatabaseRemoved() | |
129 | { | |
f62c3faf JB |
130 | // skipped SQLite check when database is removed because while testing for the connection, |
131 | // the driver will create the file (so the database) before testing if database exist | |
132 | if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) { | |
133 | $this->markTestSkipped('SQLite spotted: can\'t test with database removed.'); | |
134 | } | |
135 | ||
d5027625 | 136 | $application = new Application($this->getClient()->getKernel()); |
c641baad J |
137 | $application->add(new DropDatabaseDoctrineCommand()); |
138 | ||
139 | // drop database first, so the install command won't ask to reset things | |
d5027625 | 140 | $command = $application->find('doctrine:database:drop'); |
4094ea47 | 141 | $command->run(new ArrayInput([ |
c641baad J |
142 | 'command' => 'doctrine:database:drop', |
143 | '--force' => true, | |
4094ea47 | 144 | ]), new NullOutput()); |
c641baad | 145 | |
d5027625 JB |
146 | // start a new application to avoid lagging connexion to pgsql |
147 | $client = static::createClient(); | |
148 | $application = new Application($client->getKernel()); | |
149 | $application->add(new InstallCommand()); | |
150 | ||
c641baad J |
151 | $command = $application->find('wallabag:install'); |
152 | ||
78507d28 JB |
153 | // We mock the QuestionHelper |
154 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
c641baad J |
155 | ->disableOriginalConstructor() |
156 | ->getMock(); | |
78507d28 | 157 | $question->expects($this->any()) |
c641baad | 158 | ->method('ask') |
78507d28 | 159 | ->will($this->returnValue('yes_'.uniqid('', true))); |
c641baad J |
160 | |
161 | // We override the standard helper with our mock | |
78507d28 | 162 | $command->getHelperSet()->set($question, 'question'); |
c641baad J |
163 | |
164 | $tester = new CommandTester($command); | |
4094ea47 | 165 | $tester->execute([ |
c641baad | 166 | 'command' => $command->getName(), |
4094ea47 | 167 | ]); |
c641baad | 168 | |
637dc4bb JB |
169 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
170 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
171 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
172 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
7d2d1d68 | 173 | $this->assertContains('Run migrations.', $tester->getDisplay()); |
c641baad J |
174 | |
175 | // the current database doesn't already exist | |
176 | $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay()); | |
177 | } | |
178 | ||
179 | public function testRunInstallCommandChooseResetSchema() | |
180 | { | |
d5027625 | 181 | $application = new Application($this->getClient()->getKernel()); |
0ee043f7 | 182 | $application->add(new InstallCommandMock()); |
c641baad J |
183 | |
184 | $command = $application->find('wallabag:install'); | |
185 | ||
78507d28 JB |
186 | // We mock the QuestionHelper |
187 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
c641baad J |
188 | ->disableOriginalConstructor() |
189 | ->getMock(); | |
190 | ||
78507d28 JB |
191 | $question->expects($this->exactly(3)) |
192 | ->method('ask') | |
c641baad J |
193 | ->will($this->onConsecutiveCalls( |
194 | false, // don't want to reset the entire database | |
195 | true, // do want to reset the schema | |
196 | false // don't want to create a new user | |
197 | )); | |
198 | ||
199 | // We override the standard helper with our mock | |
78507d28 | 200 | $command->getHelperSet()->set($question, 'question'); |
c641baad J |
201 | |
202 | $tester = new CommandTester($command); | |
4094ea47 | 203 | $tester->execute([ |
c641baad | 204 | 'command' => $command->getName(), |
4094ea47 | 205 | ]); |
c641baad | 206 | |
637dc4bb JB |
207 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
208 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
209 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
210 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
7d2d1d68 | 211 | $this->assertContains('Run migrations.', $tester->getDisplay()); |
c641baad | 212 | |
7d2d1d68 | 213 | $this->assertContains('Dropping schema and creating schema', $tester->getDisplay()); |
c641baad J |
214 | } |
215 | ||
216 | public function testRunInstallCommandChooseNothing() | |
217 | { | |
d5027625 | 218 | $application = new Application($this->getClient()->getKernel()); |
c641baad J |
219 | $application->add(new InstallCommand()); |
220 | $application->add(new DropDatabaseDoctrineCommand()); | |
221 | $application->add(new CreateDatabaseDoctrineCommand()); | |
222 | ||
223 | // drop database first, so the install command won't ask to reset things | |
224 | $command = new DropDatabaseDoctrineCommand(); | |
225 | $command->setApplication($application); | |
4094ea47 | 226 | $command->run(new ArrayInput([ |
c641baad J |
227 | 'command' => 'doctrine:database:drop', |
228 | '--force' => true, | |
4094ea47 | 229 | ]), new NullOutput()); |
c641baad | 230 | |
d5027625 | 231 | $this->getClient()->getContainer()->get('doctrine')->getConnection()->close(); |
c641baad J |
232 | |
233 | $command = new CreateDatabaseDoctrineCommand(); | |
234 | $command->setApplication($application); | |
4094ea47 | 235 | $command->run(new ArrayInput([ |
c641baad J |
236 | 'command' => 'doctrine:database:create', |
237 | '--env' => 'test', | |
4094ea47 | 238 | ]), new NullOutput()); |
c641baad J |
239 | |
240 | $command = $application->find('wallabag:install'); | |
241 | ||
78507d28 JB |
242 | // We mock the QuestionHelper |
243 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
c641baad J |
244 | ->disableOriginalConstructor() |
245 | ->getMock(); | |
246 | ||
78507d28 JB |
247 | $question->expects($this->exactly(2)) |
248 | ->method('ask') | |
c641baad J |
249 | ->will($this->onConsecutiveCalls( |
250 | false, // don't want to reset the entire database | |
251 | false // don't want to create a new user | |
252 | )); | |
253 | ||
254 | // We override the standard helper with our mock | |
78507d28 | 255 | $command->getHelperSet()->set($question, 'question'); |
c641baad J |
256 | |
257 | $tester = new CommandTester($command); | |
4094ea47 | 258 | $tester->execute([ |
c641baad | 259 | 'command' => $command->getName(), |
4094ea47 | 260 | ]); |
c641baad | 261 | |
637dc4bb JB |
262 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
263 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
264 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
265 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
7d2d1d68 | 266 | $this->assertContains('Run migrations.', $tester->getDisplay()); |
c641baad J |
267 | |
268 | $this->assertContains('Creating schema', $tester->getDisplay()); | |
269 | } | |
270 | ||
271 | public function testRunInstallCommandNoInteraction() | |
272 | { | |
d5027625 | 273 | $application = new Application($this->getClient()->getKernel()); |
0ee043f7 | 274 | $application->add(new InstallCommandMock()); |
c641baad J |
275 | |
276 | $command = $application->find('wallabag:install'); | |
277 | ||
78507d28 JB |
278 | // We mock the QuestionHelper |
279 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
c641baad J |
280 | ->disableOriginalConstructor() |
281 | ->getMock(); | |
78507d28 | 282 | $question->expects($this->any()) |
c641baad | 283 | ->method('ask') |
78507d28 | 284 | ->will($this->returnValue('yes_'.uniqid('', true))); |
c641baad J |
285 | |
286 | // We override the standard helper with our mock | |
78507d28 | 287 | $command->getHelperSet()->set($question, 'question'); |
c641baad J |
288 | |
289 | $tester = new CommandTester($command); | |
4094ea47 | 290 | $tester->execute([ |
c641baad J |
291 | 'command' => $command->getName(), |
292 | '--no-interaction' => true, | |
4094ea47 | 293 | ]); |
c641baad | 294 | |
637dc4bb JB |
295 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
296 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
297 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
298 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
7d2d1d68 | 299 | $this->assertContains('Run migrations.', $tester->getDisplay()); |
c641baad | 300 | } |
0bf99bb1 | 301 | } |