3 namespace Wallabag\CoreBundle\Tests\Command
;
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 Wallabag\CoreBundle\Tests\Mock\InstallCommandMock
;
13 use Wallabag\CoreBundle\Tests\WallabagCoreTestCase
;
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('Droping database, creating database and schema, clearing the cache', $tester->getDisplay());
85 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
86 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
88 // we force to reset everything
89 $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay());
92 public function testRunInstallCommandWithDatabaseRemoved()
94 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver
) {
96 * LOG: statement: CREATE DATABASE "wallabag"
97 * ERROR: source database "template1" is being accessed by other users
98 * DETAIL: There is 1 other session using the database.
99 * STATEMENT: CREATE DATABASE "wallabag"
100 * FATAL: database "wallabag" does not exist
102 * http://stackoverflow.com/a/14374832/569101
104 $this->markTestSkipped('PostgreSQL spotted: can find a good way to drop current database, skipping.');
107 $application = new Application($this->getClient()->getKernel());
108 $application->add(new DropDatabaseDoctrineCommand());
110 // drop database first, so the install command won't ask to reset things
111 $command = $application->find('doctrine:database:drop');
112 $command->run(new ArrayInput(array(
113 'command' => 'doctrine:database:drop',
115 )), new NullOutput());
117 // start a new application to avoid lagging connexion to pgsql
118 $client = static::createClient();
119 $application = new Application($client->getKernel());
120 $application->add(new InstallCommand());
122 $command = $application->find('wallabag:install');
124 // We mock the QuestionHelper
125 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
126 ->disableOriginalConstructor()
128 $question->expects($this->any())
130 ->will($this->returnValue('yes_'.uniqid('', true)));
132 // We override the standard helper with our mock
133 $command->getHelperSet()->set($question, 'question');
135 $tester = new CommandTester($command);
136 $tester->execute(array(
137 'command' => $command->getName(),
140 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
141 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
142 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
143 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
145 // the current database doesn't already exist
146 $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay());
149 public function testRunInstallCommandChooseResetSchema()
151 $application = new Application($this->getClient()->getKernel());
152 $application->add(new InstallCommandMock());
154 $command = $application->find('wallabag:install');
156 // We mock the QuestionHelper
157 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
158 ->disableOriginalConstructor()
161 $question->expects($this->exactly(3))
163 ->will($this->onConsecutiveCalls(
164 false, // don't want to reset the entire database
165 true, // do want to reset the schema
166 false // don't want to create a new user
169 // We override the standard helper with our mock
170 $command->getHelperSet()->set($question, 'question');
172 $tester = new CommandTester($command);
173 $tester->execute(array(
174 'command' => $command->getName(),
177 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
178 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
179 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
180 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
182 $this->assertContains('Droping schema and creating schema', $tester->getDisplay());
185 public function testRunInstallCommandChooseNothing()
187 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver
) {
189 * @see testRunInstallCommandWithDatabaseRemoved
191 $this->markTestSkipped('PostgreSQL spotted: can find a good way to drop current database, skipping.');
194 $application = new Application($this->getClient()->getKernel());
195 $application->add(new InstallCommand());
196 $application->add(new DropDatabaseDoctrineCommand());
197 $application->add(new CreateDatabaseDoctrineCommand());
199 // drop database first, so the install command won't ask to reset things
200 $command = new DropDatabaseDoctrineCommand();
201 $command->setApplication($application);
202 $command->run(new ArrayInput(array(
203 'command' => 'doctrine:database:drop',
205 )), new NullOutput());
207 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
209 $command = new CreateDatabaseDoctrineCommand();
210 $command->setApplication($application);
211 $command->run(new ArrayInput(array(
212 'command' => 'doctrine:database:create',
214 )), new NullOutput());
216 $command = $application->find('wallabag:install');
218 // We mock the QuestionHelper
219 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
220 ->disableOriginalConstructor()
223 $question->expects($this->exactly(2))
225 ->will($this->onConsecutiveCalls(
226 false, // don't want to reset the entire database
227 false // don't want to create a new user
230 // We override the standard helper with our mock
231 $command->getHelperSet()->set($question, 'question');
233 $tester = new CommandTester($command);
234 $tester->execute(array(
235 'command' => $command->getName(),
238 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
239 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
240 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
241 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());
243 $this->assertContains('Creating schema', $tester->getDisplay());
246 public function testRunInstallCommandNoInteraction()
248 $application = new Application($this->getClient()->getKernel());
249 $application->add(new InstallCommandMock());
251 $command = $application->find('wallabag:install');
253 // We mock the QuestionHelper
254 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
255 ->disableOriginalConstructor()
257 $question->expects($this->any())
259 ->will($this->returnValue('yes_'.uniqid('', true)));
261 // We override the standard helper with our mock
262 $command->getHelperSet()->set($question, 'question');
264 $tester = new CommandTester($command);
265 $tester->execute(array(
266 'command' => $command->getName(),
267 '--no-interaction' => true,
270 $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay());
271 $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay());
272 $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay());
273 $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay());