]>
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 function setUp() | |
22 | { | |
23 | parent::setUp(); | |
24 | ||
25 | /** @var \Doctrine\DBAL\Connection $connection */ | |
26 | $connection = $this->getClient()->getContainer()->get('doctrine')->getConnection(); | |
27 | if ($connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { | |
28 | /* | |
29 | * LOG: statement: CREATE DATABASE "wallabag" | |
30 | * ERROR: source database "template1" is being accessed by other users | |
31 | * DETAIL: There is 1 other session using the database. | |
32 | * STATEMENT: CREATE DATABASE "wallabag" | |
33 | * FATAL: database "wallabag" does not exist | |
34 | * | |
35 | * http://stackoverflow.com/a/14374832/569101 | |
36 | */ | |
37 | $this->markTestSkipped('PostgreSQL spotted: can\'t find a good way to drop current database, skipping.'); | |
38 | } | |
39 | ||
40 | if ($connection->getDatabasePlatform() instanceof SqlitePlatform) { | |
41 | // Environnement variable useful only for sqlite to avoid the error "attempt to write a readonly database" | |
42 | // We can't define always this environnement variable because pdo_mysql seems to use it | |
43 | // and we have the error: | |
44 | // SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; | |
45 | // check the manual that corresponds to your MariaDB server version for the right syntax to use | |
46 | // near '/tmp/wallabag_testTYj1kp' at line 1 | |
47 | $databasePath = tempnam(sys_get_temp_dir(), 'wallabag_test'); | |
48 | putenv("TEST_DATABASE_PATH=$databasePath"); | |
49 | ||
50 | // The environnement has been changed, recreate the client in order to update connection | |
51 | parent::setUp(); | |
52 | } | |
53 | ||
54 | // disable doctrine-test-bundle | |
55 | StaticDriver::setKeepStaticConnections(false); | |
56 | ||
57 | $this->resetDatabase($this->getClient()); | |
58 | } | |
59 | ||
60 | public function tearDown() | |
61 | { | |
62 | $databasePath = getenv('TEST_DATABASE_PATH'); | |
63 | // Remove variable environnement | |
64 | putenv('TEST_DATABASE_PATH'); | |
65 | ||
66 | if ($databasePath && file_exists($databasePath)) { | |
67 | unlink($databasePath); | |
68 | } else { | |
69 | // Create a new client to avoid the error: | |
70 | // Transaction commit failed because the transaction has been marked for rollback only. | |
71 | $client = static::createClient(); | |
72 | $this->resetDatabase($client); | |
73 | } | |
74 | ||
75 | // enable doctrine-test-bundle | |
76 | StaticDriver::setKeepStaticConnections(true); | |
77 | parent::tearDown(); | |
78 | } | |
79 | ||
80 | public function testRunInstallCommand() | |
81 | { | |
82 | $application = new Application($this->getClient()->getKernel()); | |
83 | $application->add(new InstallCommandMock()); | |
84 | ||
85 | $command = $application->find('wallabag:install'); | |
86 | ||
87 | $tester = new CommandTester($command); | |
88 | $tester->setInputs([ | |
89 | 'y', // dropping database | |
90 | 'y', // create super admin | |
91 | 'username_' . uniqid('', true), // username | |
92 | 'password_' . uniqid('', true), // password | |
93 | 'email_' . uniqid('', true) . '@wallabag.it', // email | |
94 | ]); | |
95 | $tester->execute([ | |
96 | 'command' => $command->getName(), | |
97 | ]); | |
98 | ||
99 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); | |
100 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
101 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
102 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
103 | } | |
104 | ||
105 | public function testRunInstallCommandWithReset() | |
106 | { | |
107 | $application = new Application($this->getClient()->getKernel()); | |
108 | $application->add(new InstallCommandMock()); | |
109 | ||
110 | $command = $application->find('wallabag:install'); | |
111 | ||
112 | $tester = new CommandTester($command); | |
113 | $tester->setInputs([ | |
114 | 'y', // create super admin | |
115 | 'username_' . uniqid('', true), // username | |
116 | 'password_' . uniqid('', true), // password | |
117 | 'email_' . uniqid('', true) . '@wallabag.it', // email | |
118 | ]); | |
119 | $tester->execute([ | |
120 | 'command' => $command->getName(), | |
121 | '--reset' => true, | |
122 | ]); | |
123 | ||
124 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); | |
125 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
126 | $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay()); | |
127 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
128 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
129 | ||
130 | // we force to reset everything | |
131 | $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay()); | |
132 | } | |
133 | ||
134 | public function testRunInstallCommandWithDatabaseRemoved() | |
135 | { | |
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.'); | |
140 | } | |
141 | ||
142 | $application = new Application($this->getClient()->getKernel()); | |
143 | $application->add(new DropDatabaseDoctrineCommand()); | |
144 | ||
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', | |
149 | '--force' => true, | |
150 | ]), new NullOutput()); | |
151 | ||
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()); | |
156 | ||
157 | $command = $application->find('wallabag:install'); | |
158 | ||
159 | $tester = new CommandTester($command); | |
160 | $tester->setInputs([ | |
161 | 'y', // create super admin | |
162 | 'username_' . uniqid('', true), // username | |
163 | 'password_' . uniqid('', true), // password | |
164 | 'email_' . uniqid('', true) . '@wallabag.it', // email | |
165 | ]); | |
166 | $tester->execute([ | |
167 | 'command' => $command->getName(), | |
168 | ]); | |
169 | ||
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 | ||
175 | // the current database doesn't already exist | |
176 | $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay()); | |
177 | } | |
178 | ||
179 | public function testRunInstallCommandChooseResetSchema() | |
180 | { | |
181 | $application = new Application($this->getClient()->getKernel()); | |
182 | $application->add(new InstallCommandMock()); | |
183 | ||
184 | $command = $application->find('wallabag:install'); | |
185 | ||
186 | $tester = new CommandTester($command); | |
187 | $tester->setInputs([ | |
188 | 'n', // don't want to reset the entire database | |
189 | 'y', // do want to reset the schema | |
190 | 'n', // don't want to create a new user | |
191 | ]); | |
192 | $tester->execute([ | |
193 | 'command' => $command->getName(), | |
194 | ]); | |
195 | ||
196 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); | |
197 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
198 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
199 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
200 | ||
201 | $this->assertContains('Dropping schema and creating schema', $tester->getDisplay()); | |
202 | } | |
203 | ||
204 | public function testRunInstallCommandChooseNothing() | |
205 | { | |
206 | $application = new Application($this->getClient()->getKernel()); | |
207 | $application->add(new InstallCommand()); | |
208 | $application->add(new DropDatabaseDoctrineCommand()); | |
209 | $application->add(new CreateDatabaseDoctrineCommand()); | |
210 | $application->add(new MigrationsMigrateDoctrineCommand()); | |
211 | ||
212 | // drop database first, so the install command won't ask to reset things | |
213 | $command = new DropDatabaseDoctrineCommand(); | |
214 | $command->setApplication($application); | |
215 | $command->run(new ArrayInput([ | |
216 | 'command' => 'doctrine:database:drop', | |
217 | '--force' => true, | |
218 | ]), new NullOutput()); | |
219 | ||
220 | $this->getClient()->getContainer()->get('doctrine')->getConnection()->close(); | |
221 | ||
222 | $command = new CreateDatabaseDoctrineCommand(); | |
223 | $command->setApplication($application); | |
224 | $command->run(new ArrayInput([ | |
225 | 'command' => 'doctrine:database:create', | |
226 | '--env' => 'test', | |
227 | ]), new NullOutput()); | |
228 | ||
229 | $command = $application->find('wallabag:install'); | |
230 | ||
231 | $tester = new CommandTester($command); | |
232 | $tester->setInputs([ | |
233 | 'n', // don't want to reset the entire database | |
234 | 'n', // don't want to create a new user | |
235 | ]); | |
236 | $tester->execute([ | |
237 | 'command' => $command->getName(), | |
238 | ]); | |
239 | ||
240 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); | |
241 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
242 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
243 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
244 | ||
245 | $this->assertContains('Creating schema', $tester->getDisplay()); | |
246 | } | |
247 | ||
248 | public function testRunInstallCommandNoInteraction() | |
249 | { | |
250 | $application = new Application($this->getClient()->getKernel()); | |
251 | $application->add(new InstallCommandMock()); | |
252 | ||
253 | $command = $application->find('wallabag:install'); | |
254 | ||
255 | $tester = new CommandTester($command); | |
256 | $tester->execute([ | |
257 | 'command' => $command->getName(), | |
258 | ], [ | |
259 | 'interactive' => false, | |
260 | ]); | |
261 | ||
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 | } | |
267 | } |