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 function setUp()
21 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver
) {
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
29 * http://stackoverflow.com/a/14374832/569101
31 $this->markTestSkipped('PostgreSQL spotted: can find a good way to drop current database, skipping.');
35 public static function tearDownAfterClass()
37 $application = new Application(static::$kernel);
38 $application->setAutoExit(false);
40 $code = $application->run(new ArrayInput([
41 'command' => 'doctrine:fixtures:load',
42 '--no-interaction' => true,
44 ]), new NullOutput());
47 public function testRunInstallCommand()
49 $application = new Application($this->getClient()->getKernel());
50 $application->add(new InstallCommandMock());
52 $command = $application->find('wallabag:install');
54 // We mock the QuestionHelper
55 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
56 ->disableOriginalConstructor()
58 $question->expects($this->any())
60 ->will($this->returnValue('yes_'.uniqid('', true)));
62 // We override the standard helper with our mock
63 $command->getHelperSet()->set($question, 'question');
65 $tester = new CommandTester($command);
67 'command' => $command->getName(),
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());
76 public function testRunInstallCommandWithReset()
78 $application = new Application($this->getClient()->getKernel());
79 $application->add(new InstallCommandMock());
81 $command = $application->find('wallabag:install');
83 // We mock the QuestionHelper
84 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
85 ->disableOriginalConstructor()
87 $question->expects($this->any())
89 ->will($this->returnValue('yes_'.uniqid('', true)));
91 // We override the standard helper with our mock
92 $command->getHelperSet()->set($question, 'question');
94 $tester = new CommandTester($command);
96 'command' => $command->getName(),
100 $this->assertContains('Checking system requirements.', $tester->getDisplay());
101 $this->assertContains('Setting up database.', $tester->getDisplay());
102 $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay());
103 $this->assertContains('Administration setup.', $tester->getDisplay());
104 $this->assertContains('Config setup.', $tester->getDisplay());
106 // we force to reset everything
107 $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay());
110 public function testRunInstallCommandWithDatabaseRemoved()
112 $application = new Application($this->getClient()->getKernel());
113 $application->add(new DropDatabaseDoctrineCommand());
115 // drop database first, so the install command won't ask to reset things
116 $command = $application->find('doctrine:database:drop');
117 $command->run(new ArrayInput([
118 'command' => 'doctrine:database:drop',
120 ]), new NullOutput());
122 // start a new application to avoid lagging connexion to pgsql
123 $client = static::createClient();
124 $application = new Application($client->getKernel());
125 $application->add(new InstallCommand());
127 $command = $application->find('wallabag:install');
129 // We mock the QuestionHelper
130 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
131 ->disableOriginalConstructor()
133 $question->expects($this->any())
135 ->will($this->returnValue('yes_'.uniqid('', true)));
137 // We override the standard helper with our mock
138 $command->getHelperSet()->set($question, 'question');
140 $tester = new CommandTester($command);
142 'command' => $command->getName(),
145 $this->assertContains('Checking system requirements.', $tester->getDisplay());
146 $this->assertContains('Setting up database.', $tester->getDisplay());
147 $this->assertContains('Administration setup.', $tester->getDisplay());
148 $this->assertContains('Config setup.', $tester->getDisplay());
150 // the current database doesn't already exist
151 $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay());
154 public function testRunInstallCommandChooseResetSchema()
156 $application = new Application($this->getClient()->getKernel());
157 $application->add(new InstallCommandMock());
159 $command = $application->find('wallabag:install');
161 // We mock the QuestionHelper
162 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
163 ->disableOriginalConstructor()
166 $question->expects($this->exactly(3))
168 ->will($this->onConsecutiveCalls(
169 false, // don't want to reset the entire database
170 true, // do want to reset the schema
171 false // don't want to create a new user
174 // We override the standard helper with our mock
175 $command->getHelperSet()->set($question, 'question');
177 $tester = new CommandTester($command);
179 'command' => $command->getName(),
182 $this->assertContains('Checking system requirements.', $tester->getDisplay());
183 $this->assertContains('Setting up database.', $tester->getDisplay());
184 $this->assertContains('Administration setup.', $tester->getDisplay());
185 $this->assertContains('Config setup.', $tester->getDisplay());
187 $this->assertContains('Droping schema and creating schema', $tester->getDisplay());
190 public function testRunInstallCommandChooseNothing()
192 $application = new Application($this->getClient()->getKernel());
193 $application->add(new InstallCommand());
194 $application->add(new DropDatabaseDoctrineCommand());
195 $application->add(new CreateDatabaseDoctrineCommand());
197 // drop database first, so the install command won't ask to reset things
198 $command = new DropDatabaseDoctrineCommand();
199 $command->setApplication($application);
200 $command->run(new ArrayInput([
201 'command' => 'doctrine:database:drop',
203 ]), new NullOutput());
205 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
207 $command = new CreateDatabaseDoctrineCommand();
208 $command->setApplication($application);
209 $command->run(new ArrayInput([
210 'command' => 'doctrine:database:create',
212 ]), new NullOutput());
214 $command = $application->find('wallabag:install');
216 // We mock the QuestionHelper
217 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
218 ->disableOriginalConstructor()
221 $question->expects($this->exactly(2))
223 ->will($this->onConsecutiveCalls(
224 false, // don't want to reset the entire database
225 false // don't want to create a new user
228 // We override the standard helper with our mock
229 $command->getHelperSet()->set($question, 'question');
231 $tester = new CommandTester($command);
233 'command' => $command->getName(),
236 $this->assertContains('Checking system requirements.', $tester->getDisplay());
237 $this->assertContains('Setting up database.', $tester->getDisplay());
238 $this->assertContains('Administration setup.', $tester->getDisplay());
239 $this->assertContains('Config setup.', $tester->getDisplay());
241 $this->assertContains('Creating schema', $tester->getDisplay());
244 public function testRunInstallCommandNoInteraction()
246 $application = new Application($this->getClient()->getKernel());
247 $application->add(new InstallCommandMock());
249 $command = $application->find('wallabag:install');
251 // We mock the QuestionHelper
252 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
253 ->disableOriginalConstructor()
255 $question->expects($this->any())
257 ->will($this->returnValue('yes_'.uniqid('', true)));
259 // We override the standard helper with our mock
260 $command->getHelperSet()->set($question, 'question');
262 $tester = new CommandTester($command);
264 'command' => $command->getName(),
265 '--no-interaction' => true,
268 $this->assertContains('Checking system requirements.', $tester->getDisplay());
269 $this->assertContains('Setting up database.', $tester->getDisplay());
270 $this->assertContains('Administration setup.', $tester->getDisplay());
271 $this->assertContains('Config setup.', $tester->getDisplay());