]>
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('Installing assets.', $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('Droping 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('Installing assets.', $tester->getDisplay()); | |
123 | ||
124 | // we force to reset everything | |
125 | $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay()); | |
126 | } | |
127 | ||
128 | public function testRunInstallCommandWithDatabaseRemoved() | |
129 | { | |
130 | $application = new Application($this->getClient()->getKernel()); | |
131 | $application->add(new DropDatabaseDoctrineCommand()); | |
132 | ||
133 | // drop database first, so the install command won't ask to reset things | |
134 | $command = $application->find('doctrine:database:drop'); | |
135 | $command->run(new ArrayInput([ | |
136 | 'command' => 'doctrine:database:drop', | |
137 | '--force' => true, | |
138 | ]), new NullOutput()); | |
139 | ||
140 | // start a new application to avoid lagging connexion to pgsql | |
141 | $client = static::createClient(); | |
142 | $application = new Application($client->getKernel()); | |
143 | $application->add(new InstallCommand()); | |
144 | ||
145 | $command = $application->find('wallabag:install'); | |
146 | ||
147 | // We mock the QuestionHelper | |
148 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
149 | ->disableOriginalConstructor() | |
150 | ->getMock(); | |
151 | $question->expects($this->any()) | |
152 | ->method('ask') | |
153 | ->will($this->returnValue('yes_'.uniqid('', true))); | |
154 | ||
155 | // We override the standard helper with our mock | |
156 | $command->getHelperSet()->set($question, 'question'); | |
157 | ||
158 | $tester = new CommandTester($command); | |
159 | $tester->execute([ | |
160 | 'command' => $command->getName(), | |
161 | ]); | |
162 | ||
163 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); | |
164 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
165 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
166 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
167 | $this->assertContains('Installing assets.', $tester->getDisplay()); | |
168 | ||
169 | // the current database doesn't already exist | |
170 | $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay()); | |
171 | } | |
172 | ||
173 | public function testRunInstallCommandChooseResetSchema() | |
174 | { | |
175 | $application = new Application($this->getClient()->getKernel()); | |
176 | $application->add(new InstallCommandMock()); | |
177 | ||
178 | $command = $application->find('wallabag:install'); | |
179 | ||
180 | // We mock the QuestionHelper | |
181 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
182 | ->disableOriginalConstructor() | |
183 | ->getMock(); | |
184 | ||
185 | $question->expects($this->exactly(3)) | |
186 | ->method('ask') | |
187 | ->will($this->onConsecutiveCalls( | |
188 | false, // don't want to reset the entire database | |
189 | true, // do want to reset the schema | |
190 | false // don't want to create a new user | |
191 | )); | |
192 | ||
193 | // We override the standard helper with our mock | |
194 | $command->getHelperSet()->set($question, 'question'); | |
195 | ||
196 | $tester = new CommandTester($command); | |
197 | $tester->execute([ | |
198 | 'command' => $command->getName(), | |
199 | ]); | |
200 | ||
201 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); | |
202 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
203 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
204 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
205 | $this->assertContains('Installing assets.', $tester->getDisplay()); | |
206 | ||
207 | $this->assertContains('Droping schema and creating schema', $tester->getDisplay()); | |
208 | } | |
209 | ||
210 | public function testRunInstallCommandChooseNothing() | |
211 | { | |
212 | $application = new Application($this->getClient()->getKernel()); | |
213 | $application->add(new InstallCommand()); | |
214 | $application->add(new DropDatabaseDoctrineCommand()); | |
215 | $application->add(new CreateDatabaseDoctrineCommand()); | |
216 | ||
217 | // drop database first, so the install command won't ask to reset things | |
218 | $command = new DropDatabaseDoctrineCommand(); | |
219 | $command->setApplication($application); | |
220 | $command->run(new ArrayInput([ | |
221 | 'command' => 'doctrine:database:drop', | |
222 | '--force' => true, | |
223 | ]), new NullOutput()); | |
224 | ||
225 | $this->getClient()->getContainer()->get('doctrine')->getConnection()->close(); | |
226 | ||
227 | $command = new CreateDatabaseDoctrineCommand(); | |
228 | $command->setApplication($application); | |
229 | $command->run(new ArrayInput([ | |
230 | 'command' => 'doctrine:database:create', | |
231 | '--env' => 'test', | |
232 | ]), new NullOutput()); | |
233 | ||
234 | $command = $application->find('wallabag:install'); | |
235 | ||
236 | // We mock the QuestionHelper | |
237 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
238 | ->disableOriginalConstructor() | |
239 | ->getMock(); | |
240 | ||
241 | $question->expects($this->exactly(2)) | |
242 | ->method('ask') | |
243 | ->will($this->onConsecutiveCalls( | |
244 | false, // don't want to reset the entire database | |
245 | false // don't want to create a new user | |
246 | )); | |
247 | ||
248 | // We override the standard helper with our mock | |
249 | $command->getHelperSet()->set($question, 'question'); | |
250 | ||
251 | $tester = new CommandTester($command); | |
252 | $tester->execute([ | |
253 | 'command' => $command->getName(), | |
254 | ]); | |
255 | ||
256 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); | |
257 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
258 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
259 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
260 | $this->assertContains('Installing assets.', $tester->getDisplay()); | |
261 | ||
262 | $this->assertContains('Creating schema', $tester->getDisplay()); | |
263 | } | |
264 | ||
265 | public function testRunInstallCommandNoInteraction() | |
266 | { | |
267 | $application = new Application($this->getClient()->getKernel()); | |
268 | $application->add(new InstallCommandMock()); | |
269 | ||
270 | $command = $application->find('wallabag:install'); | |
271 | ||
272 | // We mock the QuestionHelper | |
273 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
274 | ->disableOriginalConstructor() | |
275 | ->getMock(); | |
276 | $question->expects($this->any()) | |
277 | ->method('ask') | |
278 | ->will($this->returnValue('yes_'.uniqid('', true))); | |
279 | ||
280 | // We override the standard helper with our mock | |
281 | $command->getHelperSet()->set($question, 'question'); | |
282 | ||
283 | $tester = new CommandTester($command); | |
284 | $tester->execute([ | |
285 | 'command' => $command->getName(), | |
286 | '--no-interaction' => true, | |
287 | ]); | |
288 | ||
289 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); | |
290 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
291 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
292 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
293 | $this->assertContains('Installing assets.', $tester->getDisplay()); | |
294 | } | |
295 | } |