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 * @see testRunInstallCommandWithDatabaseRemoved
190 $this->markTestSkipped('PostgreSQL spotted: can find a good way to drop current database, skipping.');
193 $application = new Application($this->getClient()->getKernel());
194 $application->add(new InstallCommand());
195 $application->add(new DropDatabaseDoctrineCommand());
196 $application->add(new CreateDatabaseDoctrineCommand());
198 // drop database first, so the install command won't ask to reset things
199 $command = new DropDatabaseDoctrineCommand();
200 $command->setApplication($application);
201 $command->run(new ArrayInput(array(
202 'command' => 'doctrine:database:drop',
204 )), new NullOutput());
206 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
208 $command = new CreateDatabaseDoctrineCommand();
209 $command->setApplication($application);
210 $command->run(new ArrayInput(array(
211 'command' => 'doctrine:database:create',
213 )), new NullOutput());
215 $command = $application->find('wallabag:install');
217 // We mock the QuestionHelper
218 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
219 ->disableOriginalConstructor()
222 $question->expects($this->exactly(2))
224 ->will($this->onConsecutiveCalls(
225 false, // don't want to reset the entire database
226 false // don't want to create a new user
229 // We override the standard helper with our mock
230 $command->getHelperSet()->set($question, 'question');
232 $tester = new CommandTester($command);
233 $tester->execute(array(
234 'command' => $command->getName(),
237 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
238 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
239 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
240 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
242 $this->assertContains('Creating schema', $tester->getDisplay());
245 public function testRunInstallCommandNoInteraction()
247 $application = new Application($this->getClient()->getKernel());
248 $application->add(new InstallCommandMock());
250 $command = $application->find('wallabag:install');
252 // We mock the QuestionHelper
253 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
254 ->disableOriginalConstructor()
256 $question->expects($this->any())
258 ->will($this->returnValue('yes_'.uniqid('', true)));
260 // We override the standard helper with our mock
261 $command->getHelperSet()->set($question, 'question');
263 $tester = new CommandTester($command);
264 $tester->execute(array(
265 'command' => $command->getName(),
266 '--no-interaction' => true,
269 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
270 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
271 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
272 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());