3 namespace Tests\Wallabag\CoreBundle\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 Tests\Wallabag\CoreBundle\Mock\InstallCommandMock
;
13 use Tests\Wallabag\CoreBundle\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\'t find a good way to drop current database, skipping.');
36 * Ensure next tests will have a clean database.
38 public static function tearDownAfterClass()
40 $application = new Application(static::$kernel);
41 $application->setAutoExit(false);
43 $application->run(new ArrayInput([
44 'command' => 'doctrine:schema:drop',
45 '--no-interaction' => true,
48 ]), new NullOutput());
50 $application->run(new ArrayInput([
51 'command' => 'doctrine:schema:create',
52 '--no-interaction' => true,
54 ]), new NullOutput());
56 $application->run(new ArrayInput([
57 'command' => 'doctrine:fixtures:load',
58 '--no-interaction' => true,
60 ]), new NullOutput());
63 public function testRunInstallCommand()
65 $application = new Application($this->getClient()->getKernel());
66 $application->add(new InstallCommandMock());
68 $command = $application->find('wallabag:install');
70 // We mock the QuestionHelper
71 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
72 ->disableOriginalConstructor()
74 $question->expects($this->any())
76 ->will($this->returnValue('yes_'.uniqid('', true)));
78 // We override the standard helper with our mock
79 $command->getHelperSet()->set($question, 'question');
81 $tester = new CommandTester($command);
83 'command' => $command->getName(),
86 $this->assertContains('Checking system requirements.', $tester->getDisplay());
87 $this->assertContains('Setting up database.', $tester->getDisplay());
88 $this->assertContains('Administration setup.', $tester->getDisplay());
89 $this->assertContains('Config setup.', $tester->getDisplay());
92 public function testRunInstallCommandWithReset()
94 $application = new Application($this->getClient()->getKernel());
95 $application->add(new InstallCommandMock());
97 $command = $application->find('wallabag:install');
99 // We mock the QuestionHelper
100 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
101 ->disableOriginalConstructor()
103 $question->expects($this->any())
105 ->will($this->returnValue('yes_'.uniqid('', true)));
107 // We override the standard helper with our mock
108 $command->getHelperSet()->set($question, 'question');
110 $tester = new CommandTester($command);
112 'command' => $command->getName(),
116 $this->assertContains('Checking system requirements.', $tester->getDisplay());
117 $this->assertContains('Setting up database.', $tester->getDisplay());
118 $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay());
119 $this->assertContains('Administration setup.', $tester->getDisplay());
120 $this->assertContains('Config setup.', $tester->getDisplay());
122 // we force to reset everything
123 $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay());
126 public function testRunInstallCommandWithDatabaseRemoved()
128 // skipped SQLite check when database is removed because while testing for the connection,
129 // the driver will create the file (so the database) before testing if database exist
130 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver
) {
131 $this->markTestSkipped('SQLite spotted: can\'t test with database removed.');
134 $application = new Application($this->getClient()->getKernel());
135 $application->add(new DropDatabaseDoctrineCommand());
137 // drop database first, so the install command won't ask to reset things
138 $command = $application->find('doctrine:database:drop');
139 $command->run(new ArrayInput([
140 'command' => 'doctrine:database:drop',
142 ]), new NullOutput());
144 // start a new application to avoid lagging connexion to pgsql
145 $client = static::createClient();
146 $application = new Application($client->getKernel());
147 $application->add(new InstallCommand());
149 $command = $application->find('wallabag:install');
151 // We mock the QuestionHelper
152 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
153 ->disableOriginalConstructor()
155 $question->expects($this->any())
157 ->will($this->returnValue('yes_'.uniqid('', true)));
159 // We override the standard helper with our mock
160 $command->getHelperSet()->set($question, 'question');
162 $tester = new CommandTester($command);
164 'command' => $command->getName(),
167 $this->assertContains('Checking system requirements.', $tester->getDisplay());
168 $this->assertContains('Setting up database.', $tester->getDisplay());
169 $this->assertContains('Administration setup.', $tester->getDisplay());
170 $this->assertContains('Config setup.', $tester->getDisplay());
172 // the current database doesn't already exist
173 $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay());
176 public function testRunInstallCommandChooseResetSchema()
178 $application = new Application($this->getClient()->getKernel());
179 $application->add(new InstallCommandMock());
181 $command = $application->find('wallabag:install');
183 // We mock the QuestionHelper
184 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
185 ->disableOriginalConstructor()
188 $question->expects($this->exactly(3))
190 ->will($this->onConsecutiveCalls(
191 false, // don't want to reset the entire database
192 true, // do want to reset the schema
193 false // don't want to create a new user
196 // We override the standard helper with our mock
197 $command->getHelperSet()->set($question, 'question');
199 $tester = new CommandTester($command);
201 'command' => $command->getName(),
204 $this->assertContains('Checking system requirements.', $tester->getDisplay());
205 $this->assertContains('Setting up database.', $tester->getDisplay());
206 $this->assertContains('Administration setup.', $tester->getDisplay());
207 $this->assertContains('Config setup.', $tester->getDisplay());
209 $this->assertContains('Droping schema and creating schema', $tester->getDisplay());
212 public function testRunInstallCommandChooseNothing()
214 $application = new Application($this->getClient()->getKernel());
215 $application->add(new InstallCommand());
216 $application->add(new DropDatabaseDoctrineCommand());
217 $application->add(new CreateDatabaseDoctrineCommand());
219 // drop database first, so the install command won't ask to reset things
220 $command = new DropDatabaseDoctrineCommand();
221 $command->setApplication($application);
222 $command->run(new ArrayInput([
223 'command' => 'doctrine:database:drop',
225 ]), new NullOutput());
227 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
229 $command = new CreateDatabaseDoctrineCommand();
230 $command->setApplication($application);
231 $command->run(new ArrayInput([
232 'command' => 'doctrine:database:create',
234 ]), new NullOutput());
236 $command = $application->find('wallabag:install');
238 // We mock the QuestionHelper
239 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
240 ->disableOriginalConstructor()
243 $question->expects($this->exactly(2))
245 ->will($this->onConsecutiveCalls(
246 false, // don't want to reset the entire database
247 false // don't want to create a new user
250 // We override the standard helper with our mock
251 $command->getHelperSet()->set($question, 'question');
253 $tester = new CommandTester($command);
255 'command' => $command->getName(),
258 $this->assertContains('Checking system requirements.', $tester->getDisplay());
259 $this->assertContains('Setting up database.', $tester->getDisplay());
260 $this->assertContains('Administration setup.', $tester->getDisplay());
261 $this->assertContains('Config setup.', $tester->getDisplay());
263 $this->assertContains('Creating schema', $tester->getDisplay());
266 public function testRunInstallCommandNoInteraction()
268 $application = new Application($this->getClient()->getKernel());
269 $application->add(new InstallCommandMock());
271 $command = $application->find('wallabag:install');
273 // We mock the QuestionHelper
274 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
275 ->disableOriginalConstructor()
277 $question->expects($this->any())
279 ->will($this->returnValue('yes_'.uniqid('', true)));
281 // We override the standard helper with our mock
282 $command->getHelperSet()->set($question, 'question');
284 $tester = new CommandTester($command);
286 'command' => $command->getName(),
287 '--no-interaction' => true,
290 $this->assertContains('Checking system requirements.', $tester->getDisplay());
291 $this->assertContains('Setting up database.', $tester->getDisplay());
292 $this->assertContains('Administration setup.', $tester->getDisplay());
293 $this->assertContains('Config setup.', $tester->getDisplay());