]>
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 | if ($databasePath && file_exists($databasePath)) { | |
66 | unlink($databasePath); | |
67 | } else { | |
68 | // Create a new client to avoid the error: | |
69 | // Transaction commit failed because the transaction has been marked for rollback only. | |
70 | $client = static::createClient(); | |
71 | $this->resetDatabase($client); | |
72 | } | |
73 | ||
74 | // enable doctrine-test-bundle | |
75 | StaticDriver::setKeepStaticConnections(true); | |
76 | parent::tearDown(); | |
77 | } | |
78 | ||
79 | public function testRunInstallCommand() | |
80 | { | |
81 | $application = new Application($this->getClient()->getKernel()); | |
82 | $application->add(new InstallCommandMock()); | |
83 | ||
84 | $command = $application->find('wallabag:install'); | |
85 | ||
86 | $tester = new CommandTester($command); | |
87 | $tester->setInputs([ | |
88 | 'y', // dropping database | |
89 | 'y', // create super admin | |
90 | 'username_' . uniqid('', true), // username | |
91 | 'password_' . uniqid('', true), // password | |
92 | 'email_' . uniqid('', true) . '@wallabag.it', // email | |
93 | ]); | |
94 | $tester->execute([ | |
95 | 'command' => $command->getName(), | |
96 | ]); | |
97 | ||
98 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); | |
99 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
100 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
101 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
102 | } | |
103 | ||
104 | public function testRunInstallCommandWithReset() | |
105 | { | |
106 | $application = new Application($this->getClient()->getKernel()); | |
107 | $application->add(new InstallCommandMock()); | |
108 | ||
109 | $command = $application->find('wallabag:install'); | |
110 | ||
111 | $tester = new CommandTester($command); | |
112 | $tester->setInputs([ | |
113 | 'y', // create super admin | |
114 | 'username_' . uniqid('', true), // username | |
115 | 'password_' . uniqid('', true), // password | |
116 | 'email_' . uniqid('', true) . '@wallabag.it', // email | |
117 | ]); | |
118 | $tester->execute([ | |
119 | 'command' => $command->getName(), | |
120 | '--reset' => true, | |
121 | ]); | |
122 | ||
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 | ||
129 | // we force to reset everything | |
130 | $this->assertContains('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay()); | |
131 | } | |
132 | ||
133 | public function testRunInstallCommandWithDatabaseRemoved() | |
134 | { | |
135 | // skipped SQLite check when database is removed because while testing for the connection, | |
136 | // the driver will create the file (so the database) before testing if database exist | |
137 | if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) { | |
138 | $this->markTestSkipped('SQLite spotted: can\'t test with database removed.'); | |
139 | } | |
140 | ||
141 | $application = new Application($this->getClient()->getKernel()); | |
142 | $application->add(new DropDatabaseDoctrineCommand()); | |
143 | ||
144 | // drop database first, so the install command won't ask to reset things | |
145 | $command = $application->find('doctrine:database:drop'); | |
146 | $command->run(new ArrayInput([ | |
147 | 'command' => 'doctrine:database:drop', | |
148 | '--force' => true, | |
149 | ]), new NullOutput()); | |
150 | ||
151 | // start a new application to avoid lagging connexion to pgsql | |
152 | $client = static::createClient(); | |
153 | $application = new Application($client->getKernel()); | |
154 | $application->add(new InstallCommand()); | |
155 | ||
156 | $command = $application->find('wallabag:install'); | |
157 | ||
158 | $tester = new CommandTester($command); | |
159 | $tester->setInputs([ | |
160 | 'y', // create super admin | |
161 | 'username_' . uniqid('', true), // username | |
162 | 'password_' . uniqid('', true), // password | |
163 | 'email_' . uniqid('', true) . '@wallabag.it', // email | |
164 | ]); | |
165 | $tester->execute([ | |
166 | 'command' => $command->getName(), | |
167 | ]); | |
168 | ||
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 | ||
174 | // the current database doesn't already exist | |
175 | $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay()); | |
176 | } | |
177 | ||
178 | public function testRunInstallCommandChooseResetSchema() | |
179 | { | |
180 | $application = new Application($this->getClient()->getKernel()); | |
181 | $application->add(new InstallCommandMock()); | |
182 | ||
183 | $command = $application->find('wallabag:install'); | |
184 | ||
185 | $tester = new CommandTester($command); | |
186 | $tester->setInputs([ | |
187 | 'n', // don't want to reset the entire database | |
188 | 'y', // do want to reset the schema | |
189 | 'n', // don't want to create a new user | |
190 | ]); | |
191 | $tester->execute([ | |
192 | 'command' => $command->getName(), | |
193 | ]); | |
194 | ||
195 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); | |
196 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
197 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
198 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
199 | ||
200 | $this->assertContains('Dropping schema and creating schema', $tester->getDisplay()); | |
201 | } | |
202 | ||
203 | public function testRunInstallCommandChooseNothing() | |
204 | { | |
205 | $application = new Application($this->getClient()->getKernel()); | |
206 | $application->add(new InstallCommand()); | |
207 | $application->add(new DropDatabaseDoctrineCommand()); | |
208 | $application->add(new CreateDatabaseDoctrineCommand()); | |
209 | $application->add(new MigrationsMigrateDoctrineCommand()); | |
210 | ||
211 | // drop database first, so the install command won't ask to reset things | |
212 | $command = new DropDatabaseDoctrineCommand(); | |
213 | $command->setApplication($application); | |
214 | $command->run(new ArrayInput([ | |
215 | 'command' => 'doctrine:database:drop', | |
216 | '--force' => true, | |
217 | ]), new NullOutput()); | |
218 | ||
219 | $this->getClient()->getContainer()->get('doctrine')->getConnection()->close(); | |
220 | ||
221 | $command = new CreateDatabaseDoctrineCommand(); | |
222 | $command->setApplication($application); | |
223 | $command->run(new ArrayInput([ | |
224 | 'command' => 'doctrine:database:create', | |
225 | '--env' => 'test', | |
226 | ]), new NullOutput()); | |
227 | ||
228 | $command = $application->find('wallabag:install'); | |
229 | ||
230 | $tester = new CommandTester($command); | |
231 | $tester->setInputs([ | |
232 | 'n', // don't want to reset the entire database | |
233 | 'n', // don't want to create a new user | |
234 | ]); | |
235 | $tester->execute([ | |
236 | 'command' => $command->getName(), | |
237 | ]); | |
238 | ||
239 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); | |
240 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
241 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
242 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
243 | ||
244 | $this->assertContains('Creating schema', $tester->getDisplay()); | |
245 | } | |
246 | ||
247 | public function testRunInstallCommandNoInteraction() | |
248 | { | |
249 | $application = new Application($this->getClient()->getKernel()); | |
250 | $application->add(new InstallCommandMock()); | |
251 | ||
252 | $command = $application->find('wallabag:install'); | |
253 | ||
254 | $tester = new CommandTester($command); | |
255 | $tester->execute([ | |
256 | 'command' => $command->getName(), | |
257 | ], [ | |
258 | 'interactive' => false, | |
259 | ]); | |
260 | ||
261 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); | |
262 | $this->assertContains('Setting up database.', $tester->getDisplay()); | |
263 | $this->assertContains('Administration setup.', $tester->getDisplay()); | |
264 | $this->assertContains('Config setup.', $tester->getDisplay()); | |
265 | } | |
266 | } |