]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Command; | |
4 | ||
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; | |
18 | ||
19 | class InstallCommandTest extends WallabagCoreTestCase | |
20 | { | |
21 | public static function setUpBeforeClass() | |
22 | { | |
23 | // disable doctrine-test-bundle | |
24 | StaticDriver::setKeepStaticConnections(false); | |
25 | } | |
26 | ||
27 | public static function tearDownAfterClass() | |
28 | { | |
29 | // enable doctrine-test-bundle | |
30 | StaticDriver::setKeepStaticConnections(true); | |
31 | } | |
32 | ||
33 | public function setUp() | |
34 | { | |
35 | parent::setUp(); | |
36 | ||
37 | /** @var \Doctrine\DBAL\Connection $connection */ | |
38 | $connection = $this->getClient()->getContainer()->get('doctrine')->getConnection(); | |
39 | if ($connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { | |
40 | /* | |
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 | |
46 | * | |
47 | * http://stackoverflow.com/a/14374832/569101 | |
48 | */ | |
49 | $this->markTestSkipped('PostgreSQL spotted: can\'t find a good way to drop current database, skipping.'); | |
50 | } | |
51 | ||
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"); | |
61 | ||
62 | // The environnement has been changed, recreate the client in order to update connection | |
63 | parent::setUp(); | |
64 | } | |
65 | ||
66 | $this->resetDatabase($this->getClient()); | |
67 | } | |
68 | ||
69 | public function tearDown() | |
70 | { | |
71 | $databasePath = getenv('TEST_DATABASE_PATH'); | |
72 | // Remove variable environnement | |
73 | putenv('TEST_DATABASE_PATH'); | |
74 | ||
75 | if ($databasePath && file_exists($databasePath)) { | |
76 | unlink($databasePath); | |
77 | } else { | |
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); | |
82 | } | |
83 | ||
84 | parent::tearDown(); | |
85 | } | |
86 | ||
87 | public function testRunInstallCommand() | |
88 | { | |
89 | $application = new Application($this->getClient()->getKernel()); | |
90 | $application->add(new InstallCommandMock()); | |
91 | ||
92 | $command = $application->find('wallabag:install'); | |
93 | ||
94 | $tester = new CommandTester($command); | |
95 | $tester->setInputs([ | |
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 | |
101 | ]); | |
102 | $tester->execute([ | |
103 | 'command' => $command->getName(), | |
104 | ]); | |
105 | ||
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()); | |
110 | } | |
111 | ||
112 | public function testRunInstallCommandWithReset() | |
113 | { | |
114 | $application = new Application($this->getClient()->getKernel()); | |
115 | $application->add(new InstallCommandMock()); | |
116 | ||
117 | $command = $application->find('wallabag:install'); | |
118 | ||
119 | $tester = new CommandTester($command); | |
120 | $tester->setInputs([ | |
121 | 'y', // create super admin | |
122 | 'username_' . uniqid('', true), // username | |
123 | 'password_' . uniqid('', true), // password | |
124 | 'email_' . uniqid('', true) . '@wallabag.it', // email | |
125 | ]); | |
126 | $tester->execute([ | |
127 | 'command' => $command->getName(), | |
128 | '--reset' => true, | |
129 | ]); | |
130 | ||
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()); | |
136 | ||
137 | // we force to reset everything | |
138 | $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay()); | |
139 | } | |
140 | ||
141 | public function testRunInstallCommandWithDatabaseRemoved() | |
142 | { | |
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.'); | |
147 | } | |
148 | ||
149 | $application = new Application($this->getClient()->getKernel()); | |
150 | $application->add(new DropDatabaseDoctrineCommand()); | |
151 | ||
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', | |
156 | '--force' => true, | |
157 | ]), new NullOutput()); | |
158 | ||
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()); | |
163 | ||
164 | $command = $application->find('wallabag:install'); | |
165 | ||
166 | $tester = new CommandTester($command); | |
167 | $tester->setInputs([ | |
168 | 'y', // create super admin | |
169 | 'username_' . uniqid('', true), // username | |
170 | 'password_' . uniqid('', true), // password | |
171 | 'email_' . uniqid('', true) . '@wallabag.it', // email | |
172 | ]); | |
173 | $tester->execute([ | |
174 | 'command' => $command->getName(), | |
175 | ]); | |
176 | ||
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()); | |
181 | ||
182 | // the current database doesn't already exist | |
183 | $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay()); | |
184 | } | |
185 | ||
186 | public function testRunInstallCommandChooseResetSchema() | |
187 | { | |
188 | $application = new Application($this->getClient()->getKernel()); | |
189 | $application->add(new InstallCommandMock()); | |
190 | ||
191 | $command = $application->find('wallabag:install'); | |
192 | ||
193 | $tester = new CommandTester($command); | |
194 | $tester->setInputs([ | |
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 | |
198 | ]); | |
199 | $tester->execute([ | |
200 | 'command' => $command->getName(), | |
201 | ]); | |
202 | ||
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()); | |
207 | ||
208 | $this->assertContains('Dropping schema and creating schema', $tester->getDisplay()); | |
209 | } | |
210 | ||
211 | public function testRunInstallCommandChooseNothing() | |
212 | { | |
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()); | |
218 | ||
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', | |
224 | '--force' => true, | |
225 | ]), new NullOutput()); | |
226 | ||
227 | $this->getClient()->getContainer()->get('doctrine')->getConnection()->close(); | |
228 | ||
229 | $command = new CreateDatabaseDoctrineCommand(); | |
230 | $command->setApplication($application); | |
231 | $command->run(new ArrayInput([ | |
232 | 'command' => 'doctrine:database:create', | |
233 | '--env' => 'test', | |
234 | ]), new NullOutput()); | |
235 | ||
236 | $command = $application->find('wallabag:install'); | |
237 | ||
238 | $tester = new CommandTester($command); | |
239 | $tester->setInputs([ | |
240 | 'n', // don't want to reset the entire database | |
241 | 'n', // don't want to create a new user | |
242 | ]); | |
243 | $tester->execute([ | |
244 | 'command' => $command->getName(), | |
245 | ]); | |
246 | ||
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()); | |
251 | ||
252 | $this->assertContains('Creating schema', $tester->getDisplay()); | |
253 | } | |
254 | ||
255 | public function testRunInstallCommandNoInteraction() | |
256 | { | |
257 | $application = new Application($this->getClient()->getKernel()); | |
258 | $application->add(new InstallCommandMock()); | |
259 | ||
260 | $command = $application->find('wallabag:install'); | |
261 | ||
262 | $tester = new CommandTester($command); | |
263 | $tester->execute([ | |
264 | 'command' => $command->getName(), | |
265 | ], [ | |
266 | 'interactive' => false, | |
267 | ]); | |
268 | ||
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()); | |
273 | } | |
274 | } |