diff options
Diffstat (limited to 'tests/Wallabag/CoreBundle/Command/InstallCommandTest.php')
-rw-r--r-- | tests/Wallabag/CoreBundle/Command/InstallCommandTest.php | 173 |
1 files changed, 71 insertions, 102 deletions
diff --git a/tests/Wallabag/CoreBundle/Command/InstallCommandTest.php b/tests/Wallabag/CoreBundle/Command/InstallCommandTest.php index 122a87d4..94fc0b94 100644 --- a/tests/Wallabag/CoreBundle/Command/InstallCommandTest.php +++ b/tests/Wallabag/CoreBundle/Command/InstallCommandTest.php | |||
@@ -2,8 +2,11 @@ | |||
2 | 2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Command; | 3 | namespace Tests\Wallabag\CoreBundle\Command; |
4 | 4 | ||
5 | use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver; | ||
5 | use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand; | 6 | use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand; |
6 | use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand; | 7 | use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand; |
8 | use Doctrine\DBAL\Platforms\PostgreSqlPlatform; | ||
9 | use Doctrine\DBAL\Platforms\SqlitePlatform; | ||
7 | use Symfony\Bundle\FrameworkBundle\Console\Application; | 10 | use Symfony\Bundle\FrameworkBundle\Console\Application; |
8 | use Symfony\Component\Console\Input\ArrayInput; | 11 | use Symfony\Component\Console\Input\ArrayInput; |
9 | use Symfony\Component\Console\Output\NullOutput; | 12 | use Symfony\Component\Console\Output\NullOutput; |
@@ -18,7 +21,9 @@ class InstallCommandTest extends WallabagCoreTestCase | |||
18 | { | 21 | { |
19 | parent::setUp(); | 22 | parent::setUp(); |
20 | 23 | ||
21 | if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver) { | 24 | /** @var \Doctrine\DBAL\Connection $connection */ |
25 | $connection = $this->getClient()->getContainer()->get('doctrine')->getConnection(); | ||
26 | if ($connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { | ||
22 | /* | 27 | /* |
23 | * LOG: statement: CREATE DATABASE "wallabag" | 28 | * LOG: statement: CREATE DATABASE "wallabag" |
24 | * ERROR: source database "template1" is being accessed by other users | 29 | * ERROR: source database "template1" is being accessed by other users |
@@ -30,34 +35,44 @@ class InstallCommandTest extends WallabagCoreTestCase | |||
30 | */ | 35 | */ |
31 | $this->markTestSkipped('PostgreSQL spotted: can\'t find a good way to drop current database, skipping.'); | 36 | $this->markTestSkipped('PostgreSQL spotted: can\'t find a good way to drop current database, skipping.'); |
32 | } | 37 | } |
33 | } | ||
34 | 38 | ||
35 | /** | 39 | if ($connection->getDatabasePlatform() instanceof SqlitePlatform) { |
36 | * Ensure next tests will have a clean database. | 40 | // Environnement variable useful only for sqlite to avoid the error "attempt to write a readonly database" |
37 | */ | 41 | // We can't define always this environnement variable because pdo_mysql seems to use it |
38 | public static function tearDownAfterClass() | 42 | // and we have the error: |
39 | { | 43 | // SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; |
40 | $application = new Application(static::$kernel); | 44 | // check the manual that corresponds to your MariaDB server version for the right syntax to use |
41 | $application->setAutoExit(false); | 45 | // near '/tmp/wallabag_testTYj1kp' at line 1 |
46 | $databasePath = tempnam(sys_get_temp_dir(), 'wallabag_test'); | ||
47 | putenv("TEST_DATABASE_PATH=$databasePath"); | ||
48 | |||
49 | // The environnement has been changed, recreate the client in order to update connection | ||
50 | parent::setUp(); | ||
51 | } | ||
42 | 52 | ||
43 | $application->run(new ArrayInput([ | 53 | // disable doctrine-test-bundle |
44 | 'command' => 'doctrine:schema:drop', | 54 | StaticDriver::setKeepStaticConnections(false); |
45 | '--no-interaction' => true, | ||
46 | '--force' => true, | ||
47 | '--env' => 'test', | ||
48 | ]), new NullOutput()); | ||
49 | 55 | ||
50 | $application->run(new ArrayInput([ | 56 | $this->resetDatabase($this->getClient()); |
51 | 'command' => 'doctrine:schema:create', | 57 | } |
52 | '--no-interaction' => true, | ||
53 | '--env' => 'test', | ||
54 | ]), new NullOutput()); | ||
55 | 58 | ||
56 | $application->run(new ArrayInput([ | 59 | public function tearDown() |
57 | 'command' => 'doctrine:fixtures:load', | 60 | { |
58 | '--no-interaction' => true, | 61 | $databasePath = getenv('TEST_DATABASE_PATH'); |
59 | '--env' => 'test', | 62 | // Remove variable environnement |
60 | ]), new NullOutput()); | 63 | putenv('TEST_DATABASE_PATH'); |
64 | if ($databasePath && file_exists($databasePath)) { | ||
65 | unlink($databasePath); | ||
66 | } else { | ||
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); | ||
71 | } | ||
72 | |||
73 | // enable doctrine-test-bundle | ||
74 | StaticDriver::setKeepStaticConnections(true); | ||
75 | parent::tearDown(); | ||
61 | } | 76 | } |
62 | 77 | ||
63 | public function testRunInstallCommand() | 78 | public function testRunInstallCommand() |
@@ -67,18 +82,14 @@ class InstallCommandTest extends WallabagCoreTestCase | |||
67 | 82 | ||
68 | $command = $application->find('wallabag:install'); | 83 | $command = $application->find('wallabag:install'); |
69 | 84 | ||
70 | // We mock the QuestionHelper | ||
71 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | ||
72 | ->disableOriginalConstructor() | ||
73 | ->getMock(); | ||
74 | $question->expects($this->any()) | ||
75 | ->method('ask') | ||
76 | ->will($this->returnValue('yes_'.uniqid('', true))); | ||
77 | |||
78 | // We override the standard helper with our mock | ||
79 | $command->getHelperSet()->set($question, 'question'); | ||
80 | |||
81 | $tester = new CommandTester($command); | 85 | $tester = new CommandTester($command); |
86 | $tester->setInputs([ | ||
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 | ||
92 | ]); | ||
82 | $tester->execute([ | 93 | $tester->execute([ |
83 | 'command' => $command->getName(), | 94 | 'command' => $command->getName(), |
84 | ]); | 95 | ]); |
@@ -97,18 +108,13 @@ class InstallCommandTest extends WallabagCoreTestCase | |||
97 | 108 | ||
98 | $command = $application->find('wallabag:install'); | 109 | $command = $application->find('wallabag:install'); |
99 | 110 | ||
100 | // We mock the QuestionHelper | ||
101 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | ||
102 | ->disableOriginalConstructor() | ||
103 | ->getMock(); | ||
104 | $question->expects($this->any()) | ||
105 | ->method('ask') | ||
106 | ->will($this->returnValue('yes_'.uniqid('', true))); | ||
107 | |||
108 | // We override the standard helper with our mock | ||
109 | $command->getHelperSet()->set($question, 'question'); | ||
110 | |||
111 | $tester = new CommandTester($command); | 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 | ]); | ||
112 | $tester->execute([ | 118 | $tester->execute([ |
113 | 'command' => $command->getName(), | 119 | 'command' => $command->getName(), |
114 | '--reset' => true, | 120 | '--reset' => true, |
@@ -129,7 +135,7 @@ class InstallCommandTest extends WallabagCoreTestCase | |||
129 | { | 135 | { |
130 | // skipped SQLite check when database is removed because while testing for the connection, | 136 | // skipped SQLite check when database is removed because while testing for the connection, |
131 | // the driver will create the file (so the database) before testing if database exist | 137 | // the driver will create the file (so the database) before testing if database exist |
132 | if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) { | 138 | if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) { |
133 | $this->markTestSkipped('SQLite spotted: can\'t test with database removed.'); | 139 | $this->markTestSkipped('SQLite spotted: can\'t test with database removed.'); |
134 | } | 140 | } |
135 | 141 | ||
@@ -150,18 +156,13 @@ class InstallCommandTest extends WallabagCoreTestCase | |||
150 | 156 | ||
151 | $command = $application->find('wallabag:install'); | 157 | $command = $application->find('wallabag:install'); |
152 | 158 | ||
153 | // We mock the QuestionHelper | ||
154 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | ||
155 | ->disableOriginalConstructor() | ||
156 | ->getMock(); | ||
157 | $question->expects($this->any()) | ||
158 | ->method('ask') | ||
159 | ->will($this->returnValue('yes_'.uniqid('', true))); | ||
160 | |||
161 | // We override the standard helper with our mock | ||
162 | $command->getHelperSet()->set($question, 'question'); | ||
163 | |||
164 | $tester = new CommandTester($command); | 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 | ]); | ||
165 | $tester->execute([ | 166 | $tester->execute([ |
166 | 'command' => $command->getName(), | 167 | 'command' => $command->getName(), |
167 | ]); | 168 | ]); |
@@ -183,23 +184,12 @@ class InstallCommandTest extends WallabagCoreTestCase | |||
183 | 184 | ||
184 | $command = $application->find('wallabag:install'); | 185 | $command = $application->find('wallabag:install'); |
185 | 186 | ||
186 | // We mock the QuestionHelper | ||
187 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | ||
188 | ->disableOriginalConstructor() | ||
189 | ->getMock(); | ||
190 | |||
191 | $question->expects($this->exactly(3)) | ||
192 | ->method('ask') | ||
193 | ->will($this->onConsecutiveCalls( | ||
194 | false, // don't want to reset the entire database | ||
195 | true, // do want to reset the schema | ||
196 | false // don't want to create a new user | ||
197 | )); | ||
198 | |||
199 | // We override the standard helper with our mock | ||
200 | $command->getHelperSet()->set($question, 'question'); | ||
201 | |||
202 | $tester = new CommandTester($command); | 187 | $tester = new CommandTester($command); |
188 | $tester->setInputs([ | ||
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 | ||
192 | ]); | ||
203 | $tester->execute([ | 193 | $tester->execute([ |
204 | 'command' => $command->getName(), | 194 | 'command' => $command->getName(), |
205 | ]); | 195 | ]); |
@@ -239,22 +229,11 @@ class InstallCommandTest extends WallabagCoreTestCase | |||
239 | 229 | ||
240 | $command = $application->find('wallabag:install'); | 230 | $command = $application->find('wallabag:install'); |
241 | 231 | ||
242 | // We mock the QuestionHelper | ||
243 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | ||
244 | ->disableOriginalConstructor() | ||
245 | ->getMock(); | ||
246 | |||
247 | $question->expects($this->exactly(2)) | ||
248 | ->method('ask') | ||
249 | ->will($this->onConsecutiveCalls( | ||
250 | false, // don't want to reset the entire database | ||
251 | false // don't want to create a new user | ||
252 | )); | ||
253 | |||
254 | // We override the standard helper with our mock | ||
255 | $command->getHelperSet()->set($question, 'question'); | ||
256 | |||
257 | $tester = new CommandTester($command); | 232 | $tester = new CommandTester($command); |
233 | $tester->setInputs([ | ||
234 | 'n', // don't want to reset the entire database | ||
235 | 'n', // don't want to create a new user | ||
236 | ]); | ||
258 | $tester->execute([ | 237 | $tester->execute([ |
259 | 'command' => $command->getName(), | 238 | 'command' => $command->getName(), |
260 | ]); | 239 | ]); |
@@ -275,21 +254,11 @@ class InstallCommandTest extends WallabagCoreTestCase | |||
275 | 254 | ||
276 | $command = $application->find('wallabag:install'); | 255 | $command = $application->find('wallabag:install'); |
277 | 256 | ||
278 | // We mock the QuestionHelper | ||
279 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | ||
280 | ->disableOriginalConstructor() | ||
281 | ->getMock(); | ||
282 | $question->expects($this->any()) | ||
283 | ->method('ask') | ||
284 | ->will($this->returnValue('yes_'.uniqid('', true))); | ||
285 | |||
286 | // We override the standard helper with our mock | ||
287 | $command->getHelperSet()->set($question, 'question'); | ||
288 | |||
289 | $tester = new CommandTester($command); | 257 | $tester = new CommandTester($command); |
290 | $tester->execute([ | 258 | $tester->execute([ |
291 | 'command' => $command->getName(), | 259 | 'command' => $command->getName(), |
292 | '--no-interaction' => true, | 260 | ], [ |
261 | 'interactive' => false, | ||
293 | ]); | 262 | ]); |
294 | 263 | ||
295 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); | 264 | $this->assertContains('Checking system requirements.', $tester->getDisplay()); |