3 namespace Tests\Wallabag\CoreBundle\Command
;
5 use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver
;
6 use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand
;
7 use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand
;
8 use Doctrine\Bundle\MigrationsBundle\Command\MigrationsMigrateDoctrineCommand
;
9 use Doctrine\DBAL\Platforms\PostgreSqlPlatform
;
10 use Doctrine\DBAL\Platforms\SqlitePlatform
;
11 use Symfony\Bundle\FrameworkBundle\Console\Application
;
12 use Symfony\Component\Console\Input\ArrayInput
;
13 use Symfony\Component\Console\Output\NullOutput
;
14 use Symfony\Component\Console\Tester\CommandTester
;
15 use Tests\Wallabag\CoreBundle\Mock\InstallCommandMock
;
16 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase
;
17 use Wallabag\CoreBundle\Command\InstallCommand
;
19 class InstallCommandTest
extends WallabagCoreTestCase
21 public static function setUpBeforeClass()
23 // disable doctrine-test-bundle
24 StaticDriver
::setKeepStaticConnections(false);
27 public static function tearDownAfterClass()
29 // enable doctrine-test-bundle
30 StaticDriver
::setKeepStaticConnections(true);
33 public function setUp()
37 /** @var \Doctrine\DBAL\Connection $connection */
38 $connection = $this->getClient()->getContainer()->get('doctrine')->getConnection();
39 if ($connection->getDatabasePlatform() instanceof PostgreSqlPlatform
) {
41 * LOG: statement: CREATE DATABASE "wallabag"
42 * ERROR: source database "template1" is being accessed by other users
43 * DETAIL: There is 1 other session using the database.
44 * STATEMENT: CREATE DATABASE "wallabag"
45 * FATAL: database "wallabag" does not exist
47 * http://stackoverflow.com/a/14374832/569101
49 $this->markTestSkipped('PostgreSQL spotted: can\'t find a good way to drop current database, skipping.');
52 if ($connection->getDatabasePlatform() instanceof SqlitePlatform
) {
53 // Environnement variable useful only for sqlite to avoid the error "attempt to write a readonly database"
54 // We can't define always this environnement variable because pdo_mysql seems to use it
55 // and we have the error:
56 // SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
57 // check the manual that corresponds to your MariaDB server version for the right syntax to use
58 // near '/tmp/wallabag_testTYj1kp' at line 1
59 $databasePath = tempnam(sys_get_temp_dir(), 'wallabag_test');
60 putenv("TEST_DATABASE_PATH=$databasePath");
62 // The environnement has been changed, recreate the client in order to update connection
66 $this->resetDatabase($this->getClient());
69 public function tearDown()
71 $databasePath = getenv('TEST_DATABASE_PATH');
72 // Remove variable environnement
73 putenv('TEST_DATABASE_PATH');
75 if ($databasePath && file_exists($databasePath)) {
76 unlink($databasePath);
78 // Create a new client to avoid the error:
79 // Transaction commit failed because the transaction has been marked for rollback only.
80 $client = static::createClient();
81 $this->resetDatabase($client);
87 public function testRunInstallCommand()
89 $application = new Application($this->getClient()->getKernel());
90 $application->add(new InstallCommandMock());
92 $command = $application->find('wallabag:install');
94 $tester = new CommandTester($command);
96 'y', // dropping database
97 'y', // create super admin
98 'username_' . uniqid('', true), // username
99 'password_' . uniqid('', true), // password
100 'email_' . uniqid('', true) . '@wallabag.it', // email
103 'command' => $command->getName(),
106 $this->assertContains('Checking system requirements.', $tester->getDisplay());
107 $this->assertContains('Setting up database.', $tester->getDisplay());
108 $this->assertContains('Administration setup.', $tester->getDisplay());
109 $this->assertContains('Config setup.', $tester->getDisplay());
112 public function testRunInstallCommandWithReset()
114 $application = new Application($this->getClient()->getKernel());
115 $application->add(new InstallCommandMock());
117 $command = $application->find('wallabag:install');
119 $tester = new CommandTester($command);
121 'y', // create super admin
122 'username_' . uniqid('', true), // username
123 'password_' . uniqid('', true), // password
124 'email_' . uniqid('', true) . '@wallabag.it', // email
127 'command' => $command->getName(),
131 $this->assertContains('Checking system requirements.', $tester->getDisplay());
132 $this->assertContains('Setting up database.', $tester->getDisplay());
133 $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
134 $this->assertContains('Administration setup.', $tester->getDisplay());
135 $this->assertContains('Config setup.', $tester->getDisplay());
137 // we force to reset everything
138 $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
141 public function testRunInstallCommandWithDatabaseRemoved()
143 // skipped SQLite check when database is removed because while testing for the connection,
144 // the driver will create the file (so the database) before testing if database exist
145 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
146 $this->markTestSkipped('SQLite spotted: can\'t test with database removed.');
149 $application = new Application($this->getClient()->getKernel());
150 $application->add(new DropDatabaseDoctrineCommand());
152 // drop database first, so the install command won't ask to reset things
153 $command = $application->find('doctrine:database:drop');
154 $command->run(new ArrayInput([
155 'command' => 'doctrine:database:drop',
157 ]), new NullOutput());
159 // start a new application to avoid lagging connexion to pgsql
160 $client = static::createClient();
161 $application = new Application($client->getKernel());
162 $application->add(new InstallCommand());
164 $command = $application->find('wallabag:install');
166 $tester = new CommandTester($command);
168 'y', // create super admin
169 'username_' . uniqid('', true), // username
170 'password_' . uniqid('', true), // password
171 'email_' . uniqid('', true) . '@wallabag.it', // email
174 'command' => $command->getName(),
177 $this->assertContains('Checking system requirements.', $tester->getDisplay());
178 $this->assertContains('Setting up database.', $tester->getDisplay());
179 $this->assertContains('Administration setup.', $tester->getDisplay());
180 $this->assertContains('Config setup.', $tester->getDisplay());
182 // the current database doesn't already exist
183 $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay());
186 public function testRunInstallCommandChooseResetSchema()
188 $application = new Application($this->getClient()->getKernel());
189 $application->add(new InstallCommandMock());
191 $command = $application->find('wallabag:install');
193 $tester = new CommandTester($command);
195 'n', // don't want to reset the entire database
196 'y', // do want to reset the schema
197 'n', // don't want to create a new user
200 'command' => $command->getName(),
203 $this->assertContains('Checking system requirements.', $tester->getDisplay());
204 $this->assertContains('Setting up database.', $tester->getDisplay());
205 $this->assertContains('Administration setup.', $tester->getDisplay());
206 $this->assertContains('Config setup.', $tester->getDisplay());
208 $this->assertContains('Dropping schema and creating schema', $tester->getDisplay());
211 public function testRunInstallCommandChooseNothing()
213 $application = new Application($this->getClient()->getKernel());
214 $application->add(new InstallCommand());
215 $application->add(new DropDatabaseDoctrineCommand());
216 $application->add(new CreateDatabaseDoctrineCommand());
217 $application->add(new MigrationsMigrateDoctrineCommand());
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 $tester = new CommandTester($command);
240 'n', // don't want to reset the entire database
241 'n', // don't want to create a new user
244 'command' => $command->getName(),
247 $this->assertContains('Checking system requirements.', $tester->getDisplay());
248 $this->assertContains('Setting up database.', $tester->getDisplay());
249 $this->assertContains('Administration setup.', $tester->getDisplay());
250 $this->assertContains('Config setup.', $tester->getDisplay());
252 $this->assertContains('Creating schema', $tester->getDisplay());
255 public function testRunInstallCommandNoInteraction()
257 $application = new Application($this->getClient()->getKernel());
258 $application->add(new InstallCommandMock());
260 $command = $application->find('wallabag:install');
262 $tester = new CommandTester($command);
264 'command' => $command->getName(),
266 'interactive' => false,
269 $this->assertContains('Checking system requirements.', $tester->getDisplay());
270 $this->assertContains('Setting up database.', $tester->getDisplay());
271 $this->assertContains('Administration setup.', $tester->getDisplay());
272 $this->assertContains('Config setup.', $tester->getDisplay());