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());
90 $this->assertContains('Run migrations.', $tester->getDisplay());
93 public function testRunInstallCommandWithReset()
95 $application = new Application($this->getClient()->getKernel());
96 $application->add(new InstallCommandMock());
98 $command = $application->find('wallabag:install');
100 // We mock the QuestionHelper
101 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
102 ->disableOriginalConstructor()
104 $question->expects($this->any())
106 ->will($this->returnValue('yes_'.uniqid('', true)));
108 // We override the standard helper with our mock
109 $command->getHelperSet()->set($question, 'question');
111 $tester = new CommandTester($command);
113 'command' => $command->getName(),
117 $this->assertContains('Checking system requirements.', $tester->getDisplay());
118 $this->assertContains('Setting up database.', $tester->getDisplay());
119 $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
120 $this->assertContains('Administration setup.', $tester->getDisplay());
121 $this->assertContains('Config setup.', $tester->getDisplay());
122 $this->assertContains('Run migrations.', $tester->getDisplay());
124 // we force to reset everything
125 $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
128 public function testRunInstallCommandWithDatabaseRemoved()
130 // skipped SQLite check when database is removed because while testing for the connection,
131 // the driver will create the file (so the database) before testing if database exist
132 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver
) {
133 $this->markTestSkipped('SQLite spotted: can\'t test with database removed.');
136 $application = new Application($this->getClient()->getKernel());
137 $application->add(new DropDatabaseDoctrineCommand());
139 // drop database first, so the install command won't ask to reset things
140 $command = $application->find('doctrine:database:drop');
141 $command->run(new ArrayInput([
142 'command' => 'doctrine:database:drop',
144 ]), new NullOutput());
146 // start a new application to avoid lagging connexion to pgsql
147 $client = static::createClient();
148 $application = new Application($client->getKernel());
149 $application->add(new InstallCommand());
151 $command = $application->find('wallabag:install');
153 // We mock the QuestionHelper
154 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
155 ->disableOriginalConstructor()
157 $question->expects($this->any())
159 ->will($this->returnValue('yes_'.uniqid('', true)));
161 // We override the standard helper with our mock
162 $command->getHelperSet()->set($question, 'question');
164 $tester = new CommandTester($command);
166 'command' => $command->getName(),
169 $this->assertContains('Checking system requirements.', $tester->getDisplay());
170 $this->assertContains('Setting up database.', $tester->getDisplay());
171 $this->assertContains('Administration setup.', $tester->getDisplay());
172 $this->assertContains('Config setup.', $tester->getDisplay());
173 $this->assertContains('Run migrations.', $tester->getDisplay());
175 // the current database doesn't already exist
176 $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay());
179 public function testRunInstallCommandChooseResetSchema()
181 $application = new Application($this->getClient()->getKernel());
182 $application->add(new InstallCommandMock());
184 $command = $application->find('wallabag:install');
186 // We mock the QuestionHelper
187 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
188 ->disableOriginalConstructor()
191 $question->expects($this->exactly(3))
193 ->will($this->onConsecutiveCalls(
194 false, // don't want to reset the entire database
195 true, // do want to reset the schema
196 false // don't want to create a new user
199 // We override the standard helper with our mock
200 $command->getHelperSet()->set($question, 'question');
202 $tester = new CommandTester($command);
204 'command' => $command->getName(),
207 $this->assertContains('Checking system requirements.', $tester->getDisplay());
208 $this->assertContains('Setting up database.', $tester->getDisplay());
209 $this->assertContains('Administration setup.', $tester->getDisplay());
210 $this->assertContains('Config setup.', $tester->getDisplay());
211 $this->assertContains('Run migrations.', $tester->getDisplay());
213 $this->assertContains('Dropping schema and creating schema', $tester->getDisplay());
216 public function testRunInstallCommandChooseNothing()
218 $application = new Application($this->getClient()->getKernel());
219 $application->add(new InstallCommand());
220 $application->add(new DropDatabaseDoctrineCommand());
221 $application->add(new CreateDatabaseDoctrineCommand());
223 // drop database first, so the install command won't ask to reset things
224 $command = new DropDatabaseDoctrineCommand();
225 $command->setApplication($application);
226 $command->run(new ArrayInput([
227 'command' => 'doctrine:database:drop',
229 ]), new NullOutput());
231 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
233 $command = new CreateDatabaseDoctrineCommand();
234 $command->setApplication($application);
235 $command->run(new ArrayInput([
236 'command' => 'doctrine:database:create',
238 ]), new NullOutput());
240 $command = $application->find('wallabag:install');
242 // We mock the QuestionHelper
243 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
244 ->disableOriginalConstructor()
247 $question->expects($this->exactly(2))
249 ->will($this->onConsecutiveCalls(
250 false, // don't want to reset the entire database
251 false // don't want to create a new user
254 // We override the standard helper with our mock
255 $command->getHelperSet()->set($question, 'question');
257 $tester = new CommandTester($command);
259 'command' => $command->getName(),
262 $this->assertContains('Checking system requirements.', $tester->getDisplay());
263 $this->assertContains('Setting up database.', $tester->getDisplay());
264 $this->assertContains('Administration setup.', $tester->getDisplay());
265 $this->assertContains('Config setup.', $tester->getDisplay());
266 $this->assertContains('Run migrations.', $tester->getDisplay());
268 $this->assertContains('Creating schema', $tester->getDisplay());
271 public function testRunInstallCommandNoInteraction()
273 $application = new Application($this->getClient()->getKernel());
274 $application->add(new InstallCommandMock());
276 $command = $application->find('wallabag:install');
278 // We mock the QuestionHelper
279 $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
280 ->disableOriginalConstructor()
282 $question->expects($this->any())
284 ->will($this->returnValue('yes_'.uniqid('', true)));
286 // We override the standard helper with our mock
287 $command->getHelperSet()->set($question, 'question');
289 $tester = new CommandTester($command);
291 'command' => $command->getName(),
292 '--no-interaction' => true,
295 $this->assertContains('Checking system requirements.', $tester->getDisplay());
296 $this->assertContains('Setting up database.', $tester->getDisplay());
297 $this->assertContains('Administration setup.', $tester->getDisplay());
298 $this->assertContains('Config setup.', $tester->getDisplay());
299 $this->assertContains('Run migrations.', $tester->getDisplay());