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\DBAL\Platforms\PostgreSqlPlatform
;
9 use Doctrine\DBAL\Platforms\SqlitePlatform
;
10 use Symfony\Bundle\FrameworkBundle\Console\Application
;
11 use Symfony\Component\Console\Input\ArrayInput
;
12 use Symfony\Component\Console\Output\NullOutput
;
13 use Symfony\Component\Console\Tester\CommandTester
;
14 use Tests\Wallabag\CoreBundle\Mock\InstallCommandMock
;
15 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase
;
16 use Wallabag\CoreBundle\Command\InstallCommand
;
18 class InstallCommandTest
extends WallabagCoreTestCase
20 public function setUp()
24 /** @var \Doctrine\DBAL\Connection $connection */
25 $connection = $this->getClient()->getContainer()->get('doctrine')->getConnection();
26 if ($connection->getDatabasePlatform() instanceof PostgreSqlPlatform
) {
28 * LOG: statement: CREATE DATABASE "wallabag"
29 * ERROR: source database "template1" is being accessed by other users
30 * DETAIL: There is 1 other session using the database.
31 * STATEMENT: CREATE DATABASE "wallabag"
32 * FATAL: database "wallabag" does not exist
34 * http://stackoverflow.com/a/14374832/569101
36 $this->markTestSkipped('PostgreSQL spotted: can\'t find a good way to drop current database, skipping.');
39 if ($connection->getDatabasePlatform() instanceof SqlitePlatform
) {
40 // Environnement variable useful only for sqlite to avoid the error "attempt to write a readonly database"
41 // We can't define always this environnement variable because pdo_mysql seems to use it
42 // and we have the error:
43 // SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
44 // check the manual that corresponds to your MariaDB server version for the right syntax to use
45 // near '/tmp/wallabag_testTYj1kp' at line 1
46 $databasePath = tempnam(sys_get_temp_dir(), 'wallabag_test');
47 putenv("TEST_DATABASE_PATH=$databasePath");
49 // The environnement has been changed, recreate the client in order to update connection
53 // disable doctrine-test-bundle
54 StaticDriver::setKeepStaticConnections(false);
56 $this->resetDatabase($this->getClient());
59 public function tearDown()
61 $databasePath = getenv('TEST_DATABASE_PATH');
62 // Remove variable environnement
63 putenv('TEST_DATABASE_PATH');
64 if ($databasePath && file_exists($databasePath)) {
65 unlink($databasePath);
67 // Create a new client to avoid the error:
68 // Transaction commit failed because the transaction has been marked for rollback only.
69 $client = static::createClient();
70 $this->resetDatabase($client);
73 // enable doctrine-test-bundle
74 StaticDriver::setKeepStaticConnections(true);
78 public function testRunInstallCommand()
80 $application = new Application($this->getClient()->getKernel());
81 $application->add(new InstallCommandMock());
83 $command = $application->find('wallabag:install');
85 $tester = new CommandTester($command);
87 'y', // dropping database
88 'y', // create super admin
89 'username_' . uniqid('', true), // username
90 'password_' . uniqid('', true), // password
91 'email_' . uniqid('', true) . '@wallabag.it', // email
94 'command' => $command->getName(),
97 $this->assertContains('Checking system requirements.', $tester->getDisplay());
98 $this->assertContains('Setting up database.', $tester->getDisplay());
99 $this->assertContains('Administration setup.', $tester->getDisplay());
100 $this->assertContains('Config setup.', $tester->getDisplay());
101 $this->assertContains('Run migrations.', $tester->getDisplay());
104 public function testRunInstallCommandWithReset()
106 $application = new Application($this->getClient()->getKernel());
107 $application->add(new InstallCommandMock());
109 $command = $application->find('wallabag:install');
111 $tester = new CommandTester($command);
113 'y', // create super admin
114 'username_' . uniqid('', true), // username
115 'password_' . uniqid('', true), // password
116 'email_' . uniqid('', true) . '@wallabag.it', // email
119 'command' => $command->getName(),
123 $this->assertContains('Checking system requirements.', $tester->getDisplay());
124 $this->assertContains('Setting up database.', $tester->getDisplay());
125 $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
126 $this->assertContains('Administration setup.', $tester->getDisplay());
127 $this->assertContains('Config setup.', $tester->getDisplay());
128 $this->assertContains('Run migrations.', $tester->getDisplay());
130 // we force to reset everything
131 $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
134 public function testRunInstallCommandWithDatabaseRemoved()
136 // skipped SQLite check when database is removed because while testing for the connection,
137 // the driver will create the file (so the database) before testing if database exist
138 if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
139 $this->markTestSkipped('SQLite spotted: can\'t test with database removed.');
142 $application = new Application($this->getClient()->getKernel());
143 $application->add(new DropDatabaseDoctrineCommand());
145 // drop database first, so the install command won't ask to reset things
146 $command = $application->find('doctrine:database:drop');
147 $command->run(new ArrayInput([
148 'command' => 'doctrine:database:drop',
150 ]), new NullOutput());
152 // start a new application to avoid lagging connexion to pgsql
153 $client = static::createClient();
154 $application = new Application($client->getKernel());
155 $application->add(new InstallCommand());
157 $command = $application->find('wallabag:install');
159 $tester = new CommandTester($command);
161 'y', // create super admin
162 'username_' . uniqid('', true), // username
163 'password_' . uniqid('', true), // password
164 'email_' . uniqid('', true) . '@wallabag.it', // email
167 'command' => $command->getName(),
170 $this->assertContains('Checking system requirements.', $tester->getDisplay());
171 $this->assertContains('Setting up database.', $tester->getDisplay());
172 $this->assertContains('Administration setup.', $tester->getDisplay());
173 $this->assertContains('Config setup.', $tester->getDisplay());
174 $this->assertContains('Run migrations.', $tester->getDisplay());
176 // the current database doesn't already exist
177 $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay());
180 public function testRunInstallCommandChooseResetSchema()
182 $application = new Application($this->getClient()->getKernel());
183 $application->add(new InstallCommandMock());
185 $command = $application->find('wallabag:install');
187 $tester = new CommandTester($command);
189 'n', // don't want to reset the entire database
190 'y', // do want to reset the schema
191 'n', // don't want to create a new user
194 'command' => $command->getName(),
197 $this->assertContains('Checking system requirements.', $tester->getDisplay());
198 $this->assertContains('Setting up database.', $tester->getDisplay());
199 $this->assertContains('Administration setup.', $tester->getDisplay());
200 $this->assertContains('Config setup.', $tester->getDisplay());
201 $this->assertContains('Run migrations.', $tester->getDisplay());
203 $this->assertContains('Dropping schema and creating schema', $tester->getDisplay());
206 public function testRunInstallCommandChooseNothing()
208 $application = new Application($this->getClient()->getKernel());
209 $application->add(new InstallCommand());
210 $application->add(new DropDatabaseDoctrineCommand());
211 $application->add(new CreateDatabaseDoctrineCommand());
213 // drop database first, so the install command won't ask to reset things
214 $command = new DropDatabaseDoctrineCommand();
215 $command->setApplication($application);
216 $command->run(new ArrayInput([
217 'command' => 'doctrine:database:drop',
219 ]), new NullOutput());
221 $this->getClient()->getContainer()->get('doctrine')->getConnection()->close();
223 $command = new CreateDatabaseDoctrineCommand();
224 $command->setApplication($application);
225 $command->run(new ArrayInput([
226 'command' => 'doctrine:database:create',
228 ]), new NullOutput());
230 $command = $application->find('wallabag:install');
232 $tester = new CommandTester($command);
234 'n', // don't want to reset the entire database
235 'n', // don't want to create a new user
238 'command' => $command->getName(),
241 $this->assertContains('Checking system requirements.', $tester->getDisplay());
242 $this->assertContains('Setting up database.', $tester->getDisplay());
243 $this->assertContains('Administration setup.', $tester->getDisplay());
244 $this->assertContains('Config setup.', $tester->getDisplay());
245 $this->assertContains('Run migrations.', $tester->getDisplay());
247 $this->assertContains('Creating schema', $tester->getDisplay());
250 public function testRunInstallCommandNoInteraction()
252 $application = new Application($this->getClient()->getKernel());
253 $application->add(new InstallCommandMock());
255 $command = $application->find('wallabag:install');
257 $tester = new CommandTester($command);
259 'command' => $command->getName(),
261 'interactive' => false,
264 $this->assertContains('Checking system requirements.', $tester->getDisplay());
265 $this->assertContains('Setting up database.', $tester->getDisplay());
266 $this->assertContains('Administration setup.', $tester->getDisplay());
267 $this->assertContains('Config setup.', $tester->getDisplay());
268 $this->assertContains('Run migrations.', $tester->getDisplay());