]>
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 | */ | |
31 | $this->markTestSkipped('PostgreSQL spotted: can find a good way to drop current database, skipping.'); | |
32 | } | |
33 | } | |
34 | ||
c641baad | 35 | public static function tearDownAfterClass() |
0bf99bb1 | 36 | { |
0bf99bb1 J |
37 | $application = new Application(static::$kernel); |
38 | $application->setAutoExit(false); | |
39 | ||
4094ea47 | 40 | $code = $application->run(new ArrayInput([ |
0bf99bb1 J |
41 | 'command' => 'doctrine:fixtures:load', |
42 | '--no-interaction' => true, | |
43 | '--env' => 'test', | |
4094ea47 | 44 | ]), new NullOutput()); |
0bf99bb1 J |
45 | } |
46 | ||
47 | public function testRunInstallCommand() | |
48 | { | |
d5027625 | 49 | $application = new Application($this->getClient()->getKernel()); |
0ee043f7 | 50 | $application->add(new InstallCommandMock()); |
0bf99bb1 J |
51 | |
52 | $command = $application->find('wallabag:install'); | |
53 | ||
78507d28 JB |
54 | // We mock the QuestionHelper |
55 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
0bf99bb1 J |
56 | ->disableOriginalConstructor() |
57 | ->getMock(); | |
78507d28 | 58 | $question->expects($this->any()) |
0bf99bb1 | 59 | ->method('ask') |
78507d28 | 60 | ->will($this->returnValue('yes_'.uniqid('', true))); |
0bf99bb1 J |
61 | |
62 | // We override the standard helper with our mock | |
78507d28 | 63 | $command->getHelperSet()->set($question, 'question'); |
0bf99bb1 J |
64 | |
65 | $tester = new CommandTester($command); | |
4094ea47 | 66 | $tester->execute([ |
732c2ad8 | 67 | 'command' => $command->getName(), |
4094ea47 | 68 | ]); |
0bf99bb1 | 69 | |
637dc4bb JB |
70 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
71 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
72 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
73 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
74 | $this->assertContains('Installing assets.', $tester->getDisplay()); | |
0bf99bb1 | 75 | } |
c641baad J |
76 | |
77 | public function testRunInstallCommandWithReset() | |
78 | { | |
d5027625 | 79 | $application = new Application($this->getClient()->getKernel()); |
0ee043f7 | 80 | $application->add(new InstallCommandMock()); |
c641baad J |
81 | |
82 | $command = $application->find('wallabag:install'); | |
83 | ||
78507d28 JB |
84 | // We mock the QuestionHelper |
85 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
c641baad J |
86 | ->disableOriginalConstructor() |
87 | ->getMock(); | |
78507d28 | 88 | $question->expects($this->any()) |
c641baad | 89 | ->method('ask') |
78507d28 | 90 | ->will($this->returnValue('yes_'.uniqid('', true))); |
c641baad J |
91 | |
92 | // We override the standard helper with our mock | |
78507d28 | 93 | $command->getHelperSet()->set($question, 'question'); |
c641baad J |
94 | |
95 | $tester = new CommandTester($command); | |
4094ea47 | 96 | $tester->execute([ |
c641baad J |
97 | 'command' => $command->getName(), |
98 | '--reset' => true, | |
4094ea47 | 99 | ]); |
c641baad | 100 | |
637dc4bb JB |
101 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
102 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
2a586069 | 103 | $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay()); |
637dc4bb JB |
104 | $this->assertContains('Administration setup.', $tester->getDisplay()); |
105 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
106 | $this->assertContains('Installing assets.', $tester->getDisplay()); | |
c641baad J |
107 | |
108 | // we force to reset everything | |
d5027625 | 109 | $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay()); |
c641baad J |
110 | } |
111 | ||
112 | public function testRunInstallCommandWithDatabaseRemoved() | |
113 | { | |
d5027625 | 114 | $application = new Application($this->getClient()->getKernel()); |
c641baad J |
115 | $application->add(new DropDatabaseDoctrineCommand()); |
116 | ||
117 | // drop database first, so the install command won't ask to reset things | |
d5027625 | 118 | $command = $application->find('doctrine:database:drop'); |
4094ea47 | 119 | $command->run(new ArrayInput([ |
c641baad J |
120 | 'command' => 'doctrine:database:drop', |
121 | '--force' => true, | |
4094ea47 | 122 | ]), new NullOutput()); |
c641baad | 123 | |
d5027625 JB |
124 | // start a new application to avoid lagging connexion to pgsql |
125 | $client = static::createClient(); | |
126 | $application = new Application($client->getKernel()); | |
127 | $application->add(new InstallCommand()); | |
128 | ||
c641baad J |
129 | $command = $application->find('wallabag:install'); |
130 | ||
78507d28 JB |
131 | // We mock the QuestionHelper |
132 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
c641baad J |
133 | ->disableOriginalConstructor() |
134 | ->getMock(); | |
78507d28 | 135 | $question->expects($this->any()) |
c641baad | 136 | ->method('ask') |
78507d28 | 137 | ->will($this->returnValue('yes_'.uniqid('', true))); |
c641baad J |
138 | |
139 | // We override the standard helper with our mock | |
78507d28 | 140 | $command->getHelperSet()->set($question, 'question'); |
c641baad J |
141 | |
142 | $tester = new CommandTester($command); | |
4094ea47 | 143 | $tester->execute([ |
c641baad | 144 | 'command' => $command->getName(), |
4094ea47 | 145 | ]); |
c641baad | 146 | |
637dc4bb JB |
147 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
148 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
149 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
150 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
151 | $this->assertContains('Installing assets.', $tester->getDisplay()); | |
c641baad J |
152 | |
153 | // the current database doesn't already exist | |
154 | $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay()); | |
155 | } | |
156 | ||
157 | public function testRunInstallCommandChooseResetSchema() | |
158 | { | |
d5027625 | 159 | $application = new Application($this->getClient()->getKernel()); |
0ee043f7 | 160 | $application->add(new InstallCommandMock()); |
c641baad J |
161 | |
162 | $command = $application->find('wallabag:install'); | |
163 | ||
78507d28 JB |
164 | // We mock the QuestionHelper |
165 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
c641baad J |
166 | ->disableOriginalConstructor() |
167 | ->getMock(); | |
168 | ||
78507d28 JB |
169 | $question->expects($this->exactly(3)) |
170 | ->method('ask') | |
c641baad J |
171 | ->will($this->onConsecutiveCalls( |
172 | false, // don't want to reset the entire database | |
173 | true, // do want to reset the schema | |
174 | false // don't want to create a new user | |
175 | )); | |
176 | ||
177 | // We override the standard helper with our mock | |
78507d28 | 178 | $command->getHelperSet()->set($question, 'question'); |
c641baad J |
179 | |
180 | $tester = new CommandTester($command); | |
4094ea47 | 181 | $tester->execute([ |
c641baad | 182 | 'command' => $command->getName(), |
4094ea47 | 183 | ]); |
c641baad | 184 | |
637dc4bb JB |
185 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
186 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
187 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
188 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
189 | $this->assertContains('Installing assets.', $tester->getDisplay()); | |
c641baad J |
190 | |
191 | $this->assertContains('Droping schema and creating schema', $tester->getDisplay()); | |
192 | } | |
193 | ||
194 | public function testRunInstallCommandChooseNothing() | |
195 | { | |
d5027625 | 196 | $application = new Application($this->getClient()->getKernel()); |
c641baad J |
197 | $application->add(new InstallCommand()); |
198 | $application->add(new DropDatabaseDoctrineCommand()); | |
199 | $application->add(new CreateDatabaseDoctrineCommand()); | |
200 | ||
201 | // drop database first, so the install command won't ask to reset things | |
202 | $command = new DropDatabaseDoctrineCommand(); | |
203 | $command->setApplication($application); | |
4094ea47 | 204 | $command->run(new ArrayInput([ |
c641baad J |
205 | 'command' => 'doctrine:database:drop', |
206 | '--force' => true, | |
4094ea47 | 207 | ]), new NullOutput()); |
c641baad | 208 | |
d5027625 | 209 | $this->getClient()->getContainer()->get('doctrine')->getConnection()->close(); |
c641baad J |
210 | |
211 | $command = new CreateDatabaseDoctrineCommand(); | |
212 | $command->setApplication($application); | |
4094ea47 | 213 | $command->run(new ArrayInput([ |
c641baad J |
214 | 'command' => 'doctrine:database:create', |
215 | '--env' => 'test', | |
4094ea47 | 216 | ]), new NullOutput()); |
c641baad J |
217 | |
218 | $command = $application->find('wallabag:install'); | |
219 | ||
78507d28 JB |
220 | // We mock the QuestionHelper |
221 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
c641baad J |
222 | ->disableOriginalConstructor() |
223 | ->getMock(); | |
224 | ||
78507d28 JB |
225 | $question->expects($this->exactly(2)) |
226 | ->method('ask') | |
c641baad J |
227 | ->will($this->onConsecutiveCalls( |
228 | false, // don't want to reset the entire database | |
229 | false // don't want to create a new user | |
230 | )); | |
231 | ||
232 | // We override the standard helper with our mock | |
78507d28 | 233 | $command->getHelperSet()->set($question, 'question'); |
c641baad J |
234 | |
235 | $tester = new CommandTester($command); | |
4094ea47 | 236 | $tester->execute([ |
c641baad | 237 | 'command' => $command->getName(), |
4094ea47 | 238 | ]); |
c641baad | 239 | |
637dc4bb JB |
240 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
241 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
242 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
243 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
244 | $this->assertContains('Installing assets.', $tester->getDisplay()); | |
c641baad J |
245 | |
246 | $this->assertContains('Creating schema', $tester->getDisplay()); | |
247 | } | |
248 | ||
249 | public function testRunInstallCommandNoInteraction() | |
250 | { | |
d5027625 | 251 | $application = new Application($this->getClient()->getKernel()); |
0ee043f7 | 252 | $application->add(new InstallCommandMock()); |
c641baad J |
253 | |
254 | $command = $application->find('wallabag:install'); | |
255 | ||
78507d28 JB |
256 | // We mock the QuestionHelper |
257 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
c641baad J |
258 | ->disableOriginalConstructor() |
259 | ->getMock(); | |
78507d28 | 260 | $question->expects($this->any()) |
c641baad | 261 | ->method('ask') |
78507d28 | 262 | ->will($this->returnValue('yes_'.uniqid('', true))); |
c641baad J |
263 | |
264 | // We override the standard helper with our mock | |
78507d28 | 265 | $command->getHelperSet()->set($question, 'question'); |
c641baad J |
266 | |
267 | $tester = new CommandTester($command); | |
4094ea47 | 268 | $tester->execute([ |
c641baad J |
269 | 'command' => $command->getName(), |
270 | '--no-interaction' => true, | |
4094ea47 | 271 | ]); |
c641baad | 272 | |
637dc4bb JB |
273 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |
274 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
275 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
276 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
277 | $this->assertContains('Installing assets.', $tester->getDisplay()); | |
c641baad | 278 | } |
0bf99bb1 | 279 | } |