]>
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 | } | |
91 | ||
92 | public function testRunInstallCommandWithReset() | |
93 | { | |
94 | $application = new Application($this->getClient()->getKernel()); | |
95 | $application->add(new InstallCommandMock()); | |
96 | ||
97 | $command = $application->find('wallabag:install'); | |
98 | ||
99 | // We mock the QuestionHelper | |
100 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
101 | ->disableOriginalConstructor() | |
102 | ->getMock(); | |
103 | $question->expects($this->any()) | |
104 | ->method('ask') | |
105 | ->will($this->returnValue('yes_'.uniqid('', true))); | |
106 | ||
107 | // We override the standard helper with our mock | |
108 | $command->getHelperSet()->set($question, 'question'); | |
109 | ||
110 | $tester = new CommandTester($command); | |
111 | $tester->execute([ | |
112 | 'command' => $command->getName(), | |
113 | '--reset' => true, | |
114 | ]); | |
115 | ||
116 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); | |
117 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
118 | $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay()); | |
119 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
120 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
121 | ||
122 | // we force to reset everything | |
123 | $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay()); | |
124 | } | |
125 | ||
126 | public function testRunInstallCommandWithDatabaseRemoved() | |
127 | { | |
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 | ||
134 | $application = new Application($this->getClient()->getKernel()); | |
135 | $application->add(new DropDatabaseDoctrineCommand()); | |
136 | ||
137 | // drop database first, so the install command won't ask to reset things | |
138 | $command = $application->find('doctrine:database:drop'); | |
139 | $command->run(new ArrayInput([ | |
140 | 'command' => 'doctrine:database:drop', | |
141 | '--force' => true, | |
142 | ]), new NullOutput()); | |
143 | ||
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 | ||
149 | $command = $application->find('wallabag:install'); | |
150 | ||
151 | // We mock the QuestionHelper | |
152 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
153 | ->disableOriginalConstructor() | |
154 | ->getMock(); | |
155 | $question->expects($this->any()) | |
156 | ->method('ask') | |
157 | ->will($this->returnValue('yes_'.uniqid('', true))); | |
158 | ||
159 | // We override the standard helper with our mock | |
160 | $command->getHelperSet()->set($question, 'question'); | |
161 | ||
162 | $tester = new CommandTester($command); | |
163 | $tester->execute([ | |
164 | 'command' => $command->getName(), | |
165 | ]); | |
166 | ||
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()); | |
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 | { | |
178 | $application = new Application($this->getClient()->getKernel()); | |
179 | $application->add(new InstallCommandMock()); | |
180 | ||
181 | $command = $application->find('wallabag:install'); | |
182 | ||
183 | // We mock the QuestionHelper | |
184 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
185 | ->disableOriginalConstructor() | |
186 | ->getMock(); | |
187 | ||
188 | $question->expects($this->exactly(3)) | |
189 | ->method('ask') | |
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 | |
197 | $command->getHelperSet()->set($question, 'question'); | |
198 | ||
199 | $tester = new CommandTester($command); | |
200 | $tester->execute([ | |
201 | 'command' => $command->getName(), | |
202 | ]); | |
203 | ||
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()); | |
208 | ||
209 | $this->assertContains('Droping schema and creating schema', $tester->getDisplay()); | |
210 | } | |
211 | ||
212 | public function testRunInstallCommandChooseNothing() | |
213 | { | |
214 | $application = new Application($this->getClient()->getKernel()); | |
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); | |
222 | $command->run(new ArrayInput([ | |
223 | 'command' => 'doctrine:database:drop', | |
224 | '--force' => true, | |
225 | ]), new NullOutput()); | |
226 | ||
227 | $this->getClient()->getContainer()->get('doctrine')->getConnection()->close(); | |
228 | ||
229 | $command = new CreateDatabaseDoctrineCommand(); | |
230 | $command->setApplication($application); | |
231 | $command->run(new ArrayInput([ | |
232 | 'command' => 'doctrine:database:create', | |
233 | '--env' => 'test', | |
234 | ]), new NullOutput()); | |
235 | ||
236 | $command = $application->find('wallabag:install'); | |
237 | ||
238 | // We mock the QuestionHelper | |
239 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
240 | ->disableOriginalConstructor() | |
241 | ->getMock(); | |
242 | ||
243 | $question->expects($this->exactly(2)) | |
244 | ->method('ask') | |
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 | |
251 | $command->getHelperSet()->set($question, 'question'); | |
252 | ||
253 | $tester = new CommandTester($command); | |
254 | $tester->execute([ | |
255 | 'command' => $command->getName(), | |
256 | ]); | |
257 | ||
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()); | |
262 | ||
263 | $this->assertContains('Creating schema', $tester->getDisplay()); | |
264 | } | |
265 | ||
266 | public function testRunInstallCommandNoInteraction() | |
267 | { | |
268 | $application = new Application($this->getClient()->getKernel()); | |
269 | $application->add(new InstallCommandMock()); | |
270 | ||
271 | $command = $application->find('wallabag:install'); | |
272 | ||
273 | // We mock the QuestionHelper | |
274 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
275 | ->disableOriginalConstructor() | |
276 | ->getMock(); | |
277 | $question->expects($this->any()) | |
278 | ->method('ask') | |
279 | ->will($this->returnValue('yes_'.uniqid('', true))); | |
280 | ||
281 | // We override the standard helper with our mock | |
282 | $command->getHelperSet()->set($question, 'question'); | |
283 | ||
284 | $tester = new CommandTester($command); | |
285 | $tester->execute([ | |
286 | 'command' => $command->getName(), | |
287 | '--no-interaction' => true, | |
288 | ]); | |
289 | ||
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()); | |
294 | } | |
295 | } |