3 namespace Wallabag\CoreBundle\Tests\Command
;
5 use Wallabag\CoreBundle\Tests\WallabagCoreTestCase
;
6 use Wallabag\CoreBundle\Command\InstallCommand
;
7 use Wallabag\CoreBundle\Tests\Mock\InstallCommandMock
;
8 use Symfony\Bundle\FrameworkBundle\Console\Application
;
9 use Symfony\Component\Console\Tester\CommandTester
;
10 use Symfony\Component\Console\Input\ArrayInput
;
11 use Symfony\Component\Console\Output\NullOutput
;
12 use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand
;
13 use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand
;
15 class InstallCommandTest
extends WallabagCoreTestCase
17 public static function tearDownAfterClass()
19 $application = new Application(static::$kernel);
20 $application->setAutoExit(false);
22 $code = $application->run(new ArrayInput(array(
23 'command' => 'doctrine:fixtures:load',
24 '--no-interaction' => true,
26 )), new NullOutput());
29 public function testRunInstallCommand()
31 $application = new Application($this->getClient()->getKernel());
32 $application->add(new InstallCommandMock());
34 $command = $application->find('wallabag:install');
36 // We mock the QuestionHelper
37 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
38 ->disableOriginalConstructor()
40 $question->expects($this->any())
42 ->will($this->returnValue('yes_'.uniqid('', true)));
44 // We override the standard helper with our mock
45 $command->getHelperSet()->set($question, 'question');
47 $tester = new CommandTester($command);
48 $tester->execute(array(
49 'command' => $command->getName(),
52 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
53 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
54 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
55 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
58 public function testRunInstallCommandWithReset()
60 $application = new Application($this->getClient()->getKernel());
61 $application->add(new InstallCommandMock());
63 $command = $application->find('wallabag:install');
65 // We mock the QuestionHelper
66 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
67 ->disableOriginalConstructor()
69 $question->expects($this->any())
71 ->will($this->returnValue('yes_'.uniqid('', true)));
73 // We override the standard helper with our mock
74 $command->getHelperSet()->set($question, 'question');
76 $tester = new CommandTester($command);
77 $tester->execute(array(
78 'command' => $command->getName(),
82 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
83 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
84 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
85 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
87 // we force to reset everything
88 $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay());
91 public function testRunInstallCommandWithDatabaseRemoved()
93 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver
) {
95 * LOG: statement: CREATE DATABASE "wallabag"
96 * ERROR: source database "template1" is being accessed by other users
97 * DETAIL: There is 1 other session using the database.
98 * STATEMENT: CREATE DATABASE "wallabag"
99 * FATAL: database "wallabag" does not exist
101 * http://stackoverflow.com/a/14374832/569101
103 $this->markTestSkipped('PostgreSQL spotted: can find a good way to drop current database, skipping.');
106 $application = new Application($this->getClient()->getKernel());
107 $application->add(new DropDatabaseDoctrineCommand());
109 // drop database first, so the install command won't ask to reset things
110 $command = $application->find('doctrine:database:drop');
111 $command->run(new ArrayInput(array(
112 'command' => 'doctrine:database:drop',
114 )), new NullOutput());
116 // start a new application to avoid lagging connexion to pgsql
117 $client = static::createClient();
118 $application = new Application($client->getKernel());
119 $application->add(new InstallCommand());
121 $command = $application->find('wallabag:install');
123 // We mock the QuestionHelper
124 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
125 ->disableOriginalConstructor()
127 $question->expects($this->any())
129 ->will($this->returnValue('yes_'.uniqid('', true)));
131 // We override the standard helper with our mock
132 $command->getHelperSet()->set($question, 'question');
134 $tester = new CommandTester($command);
135 $tester->execute(array(
136 'command' => $command->getName(),
139 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
140 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
141 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
142 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
144 // the current database doesn't already exist
145 $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay());
148 public function testRunInstallCommandChooseResetSchema()
150 $application = new Application($this->getClient()->getKernel());
151 $application->add(new InstallCommandMock());
153 $command = $application->find('wallabag:install');
155 // We mock the QuestionHelper
156 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
157 ->disableOriginalConstructor()
160 $question->expects($this->exactly(3))
162 ->will($this->onConsecutiveCalls(
163 false, // don't want to reset the entire database
164 true, // do want to reset the schema
165 false // don't want to create a new user
168 // We override the standard helper with our mock
169 $command->getHelperSet()->set($question, 'question');
171 $tester = new CommandTester($command);
172 $tester->execute(array(
173 'command' => $command->getName(),
176 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
177 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
178 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
179 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
181 $this->assertContains('Droping schema and creating schema', $tester->getDisplay());
184 public function testRunInstallCommandChooseNothing()
186 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver
) {
188 * LOG: statement: CREATE DATABASE "wallabag"
189 * ERROR: source database "template1" is being accessed by other users
190 * DETAIL: There is 1 other session using the database.
191 * STATEMENT: CREATE DATABASE "wallabag"
192 * FATAL: database "wallabag" does not exist
194 * http://stackoverflow.com/a/14374832/569101
196 $this->markTestSkipped('PostgreSQL spotted: can find a good way to drop current database, skipping.');
199 $application = new Application($this->getClient()->getKernel());
200 $application->add(new InstallCommand());
201 $application->add(new DropDatabaseDoctrineCommand());
202 $application->add(new CreateDatabaseDoctrineCommand());
204 // drop database first, so the install command won't ask to reset things
205 $command = new DropDatabaseDoctrineCommand();
206 $command->setApplication($application);
207 $command->run(new ArrayInput(array(
208 'command' => 'doctrine:database:drop',
210 )), new NullOutput());
212 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
214 $command = new CreateDatabaseDoctrineCommand();
215 $command->setApplication($application);
216 $command->run(new ArrayInput(array(
217 'command' => 'doctrine:database:create',
219 )), new NullOutput());
221 $command = $application->find('wallabag:install');
223 // We mock the QuestionHelper
224 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
225 ->disableOriginalConstructor()
228 $question->expects($this->exactly(2))
230 ->will($this->onConsecutiveCalls(
231 false, // don't want to reset the entire database
232 false // don't want to create a new user
235 // We override the standard helper with our mock
236 $command->getHelperSet()->set($question, 'question');
238 $tester = new CommandTester($command);
239 $tester->execute(array(
240 'command' => $command->getName(),
243 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
244 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
245 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
246 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
248 $this->assertContains('Creating schema', $tester->getDisplay());
251 public function testRunInstallCommandNoInteraction()
253 $application = new Application($this->getClient()->getKernel());
254 $application->add(new InstallCommandMock());
256 $command = $application->find('wallabag:install');
258 // We mock the QuestionHelper
259 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
260 ->disableOriginalConstructor()
262 $question->expects($this->any())
264 ->will($this->returnValue('yes_'.uniqid('', true)));
266 // We override the standard helper with our mock
267 $command->getHelperSet()->set($question, 'question');
269 $tester = new CommandTester($command);
270 $tester->execute(array(
271 'command' => $command->getName(),
272 '--no-interaction' => true,
275 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
276 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
277 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
278 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());