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