]>
Commit | Line | Data |
---|---|---|
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\'t find a good way to drop current database, skipping.'); | |
32 | } | |
33 | } | |
34 | ||
35 | /** | |
36 | * Ensure next tests will have a clean database. | |
37 | */ | |
38 | public static function tearDownAfterClass() | |
39 | { | |
40 | $application = new Application(static::$kernel); | |
41 | $application->setAutoExit(false); | |
42 | ||
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([ | |
57 | 'command' => 'doctrine:fixtures:load', | |
58 | '--no-interaction' => true, | |
59 | '--env' => 'test', | |
60 | ]), new NullOutput()); | |
61 | } | |
62 | ||
63 | public function testRunInstallCommand() | |
64 | { | |
65 | $application = new Application($this->getClient()->getKernel()); | |
66 | $application->add(new InstallCommandMock()); | |
67 | ||
68 | $command = $application->find('wallabag:install'); | |
69 | ||
70 | // We mock the QuestionHelper | |
71 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
72 | ->disableOriginalConstructor() | |
73 | ->getMock(); | |
74 | $question->expects($this->any()) | |
75 | ->method('ask') | |
76 | ->will($this->returnValue('yes_'.uniqid('', true))); | |
77 | ||
78 | // We override the standard helper with our mock | |
79 | $command->getHelperSet()->set($question, 'question'); | |
80 | ||
81 | $tester = new CommandTester($command); | |
82 | $tester->execute([ | |
83 | 'command' => $command->getName(), | |
84 | ]); | |
85 | ||
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()); | |
90 | $this->assertContains('Run migrations.', $tester->getDisplay()); | |
91 | } | |
92 | ||
93 | public function testRunInstallCommandWithReset() | |
94 | { | |
95 | $application = new Application($this->getClient()->getKernel()); | |
96 | $application->add(new InstallCommandMock()); | |
97 | ||
98 | $command = $application->find('wallabag:install'); | |
99 | ||
100 | // We mock the QuestionHelper | |
101 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
102 | ->disableOriginalConstructor() | |
103 | ->getMock(); | |
104 | $question->expects($this->any()) | |
105 | ->method('ask') | |
106 | ->will($this->returnValue('yes_'.uniqid('', true))); | |
107 | ||
108 | // We override the standard helper with our mock | |
109 | $command->getHelperSet()->set($question, 'question'); | |
110 | ||
111 | $tester = new CommandTester($command); | |
112 | $tester->execute([ | |
113 | 'command' => $command->getName(), | |
114 | '--reset' => true, | |
115 | ]); | |
116 | ||
117 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); | |
118 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
119 | $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay()); | |
120 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
121 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
122 | $this->assertContains('Run migrations.', $tester->getDisplay()); | |
123 | ||
124 | // we force to reset everything | |
125 | $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay()); | |
126 | } | |
127 | ||
128 | public function testRunInstallCommandWithDatabaseRemoved() | |
129 | { | |
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 | ||
136 | $application = new Application($this->getClient()->getKernel()); | |
137 | $application->add(new DropDatabaseDoctrineCommand()); | |
138 | ||
139 | // drop database first, so the install command won't ask to reset things | |
140 | $command = $application->find('doctrine:database:drop'); | |
141 | $command->run(new ArrayInput([ | |
142 | 'command' => 'doctrine:database:drop', | |
143 | '--force' => true, | |
144 | ]), new NullOutput()); | |
145 | ||
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 | ||
151 | $command = $application->find('wallabag:install'); | |
152 | ||
153 | // We mock the QuestionHelper | |
154 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
155 | ->disableOriginalConstructor() | |
156 | ->getMock(); | |
157 | $question->expects($this->any()) | |
158 | ->method('ask') | |
159 | ->will($this->returnValue('yes_'.uniqid('', true))); | |
160 | ||
161 | // We override the standard helper with our mock | |
162 | $command->getHelperSet()->set($question, 'question'); | |
163 | ||
164 | $tester = new CommandTester($command); | |
165 | $tester->execute([ | |
166 | 'command' => $command->getName(), | |
167 | ]); | |
168 | ||
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()); | |
173 | $this->assertContains('Run migrations.', $tester->getDisplay()); | |
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 | { | |
181 | $application = new Application($this->getClient()->getKernel()); | |
182 | $application->add(new InstallCommandMock()); | |
183 | ||
184 | $command = $application->find('wallabag:install'); | |
185 | ||
186 | // We mock the QuestionHelper | |
187 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
188 | ->disableOriginalConstructor() | |
189 | ->getMock(); | |
190 | ||
191 | $question->expects($this->exactly(3)) | |
192 | ->method('ask') | |
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 | |
200 | $command->getHelperSet()->set($question, 'question'); | |
201 | ||
202 | $tester = new CommandTester($command); | |
203 | $tester->execute([ | |
204 | 'command' => $command->getName(), | |
205 | ]); | |
206 | ||
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()); | |
211 | $this->assertContains('Run migrations.', $tester->getDisplay()); | |
212 | ||
213 | $this->assertContains('Dropping schema and creating schema', $tester->getDisplay()); | |
214 | } | |
215 | ||
216 | public function testRunInstallCommandChooseNothing() | |
217 | { | |
218 | $application = new Application($this->getClient()->getKernel()); | |
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); | |
226 | $command->run(new ArrayInput([ | |
227 | 'command' => 'doctrine:database:drop', | |
228 | '--force' => true, | |
229 | ]), new NullOutput()); | |
230 | ||
231 | $this->getClient()->getContainer()->get('doctrine')->getConnection()->close(); | |
232 | ||
233 | $command = new CreateDatabaseDoctrineCommand(); | |
234 | $command->setApplication($application); | |
235 | $command->run(new ArrayInput([ | |
236 | 'command' => 'doctrine:database:create', | |
237 | '--env' => 'test', | |
238 | ]), new NullOutput()); | |
239 | ||
240 | $command = $application->find('wallabag:install'); | |
241 | ||
242 | // We mock the QuestionHelper | |
243 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
244 | ->disableOriginalConstructor() | |
245 | ->getMock(); | |
246 | ||
247 | $question->expects($this->exactly(2)) | |
248 | ->method('ask') | |
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 | |
255 | $command->getHelperSet()->set($question, 'question'); | |
256 | ||
257 | $tester = new CommandTester($command); | |
258 | $tester->execute([ | |
259 | 'command' => $command->getName(), | |
260 | ]); | |
261 | ||
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()); | |
266 | $this->assertContains('Run migrations.', $tester->getDisplay()); | |
267 | ||
268 | $this->assertContains('Creating schema', $tester->getDisplay()); | |
269 | } | |
270 | ||
271 | public function testRunInstallCommandNoInteraction() | |
272 | { | |
273 | $application = new Application($this->getClient()->getKernel()); | |
274 | $application->add(new InstallCommandMock()); | |
275 | ||
276 | $command = $application->find('wallabag:install'); | |
277 | ||
278 | // We mock the QuestionHelper | |
279 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
280 | ->disableOriginalConstructor() | |
281 | ->getMock(); | |
282 | $question->expects($this->any()) | |
283 | ->method('ask') | |
284 | ->will($this->returnValue('yes_'.uniqid('', true))); | |
285 | ||
286 | // We override the standard helper with our mock | |
287 | $command->getHelperSet()->set($question, 'question'); | |
288 | ||
289 | $tester = new CommandTester($command); | |
290 | $tester->execute([ | |
291 | 'command' => $command->getName(), | |
292 | '--no-interaction' => true, | |
293 | ]); | |
294 | ||
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()); | |
299 | $this->assertContains('Run migrations.', $tester->getDisplay()); | |
300 | } | |
301 | } |