]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Tests\Command; | |
4 | ||
5 | use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand; | |
6 | use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand; | |
7 | use Symfony\Bundle\FrameworkBundle\Console\Application; | |
8 | use Symfony\Component\Console\Input\ArrayInput; | |
9 | use Symfony\Component\Console\Output\NullOutput; | |
10 | use Symfony\Component\Console\Tester\CommandTester; | |
11 | use Wallabag\CoreBundle\Command\InstallCommand; | |
12 | use Wallabag\CoreBundle\Tests\Mock\InstallCommandMock; | |
13 | use Wallabag\CoreBundle\Tests\WallabagCoreTestCase; | |
14 | ||
15 | class InstallCommandTest extends WallabagCoreTestCase | |
16 | { | |
17 | public function setUp() | |
18 | { | |
19 | parent::setUp(); | |
20 | ||
21 | if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver) { | |
22 | /* | |
23 | * LOG: statement: CREATE DATABASE "wallabag" | |
24 | * ERROR: source database "template1" is being accessed by other users | |
25 | * DETAIL: There is 1 other session using the database. | |
26 | * STATEMENT: CREATE DATABASE "wallabag" | |
27 | * FATAL: database "wallabag" does not exist | |
28 | * | |
29 | * http://stackoverflow.com/a/14374832/569101 | |
30 | */ | |
31 | $this->markTestSkipped('PostgreSQL spotted: can find a good way to drop current database, skipping.'); | |
32 | } | |
33 | } | |
34 | ||
35 | public static function tearDownAfterClass() | |
36 | { | |
37 | $application = new Application(static::$kernel); | |
38 | $application->setAutoExit(false); | |
39 | ||
40 | $code = $application->run(new ArrayInput(array( | |
41 | 'command' => 'doctrine:fixtures:load', | |
42 | '--no-interaction' => true, | |
43 | '--env' => 'test', | |
44 | )), new NullOutput()); | |
45 | } | |
46 | ||
47 | public function testRunInstallCommand() | |
48 | { | |
49 | $application = new Application($this->getClient()->getKernel()); | |
50 | $application->add(new InstallCommandMock()); | |
51 | ||
52 | $command = $application->find('wallabag:install'); | |
53 | ||
54 | // We mock the QuestionHelper | |
55 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
56 | ->disableOriginalConstructor() | |
57 | ->getMock(); | |
58 | $question->expects($this->any()) | |
59 | ->method('ask') | |
60 | ->will($this->returnValue('yes_'.uniqid('', true))); | |
61 | ||
62 | // We override the standard helper with our mock | |
63 | $command->getHelperSet()->set($question, 'question'); | |
64 | ||
65 | $tester = new CommandTester($command); | |
66 | $tester->execute(array( | |
67 | 'command' => $command->getName(), | |
68 | )); | |
69 | ||
70 | $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay()); | |
71 | $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay()); | |
72 | $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay()); | |
73 | $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay()); | |
74 | } | |
75 | ||
76 | public function testRunInstallCommandWithReset() | |
77 | { | |
78 | $application = new Application($this->getClient()->getKernel()); | |
79 | $application->add(new InstallCommandMock()); | |
80 | ||
81 | $command = $application->find('wallabag:install'); | |
82 | ||
83 | // We mock the QuestionHelper | |
84 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
85 | ->disableOriginalConstructor() | |
86 | ->getMock(); | |
87 | $question->expects($this->any()) | |
88 | ->method('ask') | |
89 | ->will($this->returnValue('yes_'.uniqid('', true))); | |
90 | ||
91 | // We override the standard helper with our mock | |
92 | $command->getHelperSet()->set($question, 'question'); | |
93 | ||
94 | $tester = new CommandTester($command); | |
95 | $tester->execute(array( | |
96 | 'command' => $command->getName(), | |
97 | '--reset' => true, | |
98 | )); | |
99 | ||
100 | $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay()); | |
101 | $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay()); | |
102 | $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay()); | |
103 | $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay()); | |
104 | $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay()); | |
105 | ||
106 | // we force to reset everything | |
107 | $this->assertContains('Droping database, creating database and schema, clearing the cache', $tester->getDisplay()); | |
108 | } | |
109 | ||
110 | public function testRunInstallCommandWithDatabaseRemoved() | |
111 | { | |
112 | $application = new Application($this->getClient()->getKernel()); | |
113 | $application->add(new DropDatabaseDoctrineCommand()); | |
114 | ||
115 | // drop database first, so the install command won't ask to reset things | |
116 | $command = $application->find('doctrine:database:drop'); | |
117 | $command->run(new ArrayInput(array( | |
118 | 'command' => 'doctrine:database:drop', | |
119 | '--force' => true, | |
120 | )), new NullOutput()); | |
121 | ||
122 | // start a new application to avoid lagging connexion to pgsql | |
123 | $client = static::createClient(); | |
124 | $application = new Application($client->getKernel()); | |
125 | $application->add(new InstallCommand()); | |
126 | ||
127 | $command = $application->find('wallabag:install'); | |
128 | ||
129 | // We mock the QuestionHelper | |
130 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
131 | ->disableOriginalConstructor() | |
132 | ->getMock(); | |
133 | $question->expects($this->any()) | |
134 | ->method('ask') | |
135 | ->will($this->returnValue('yes_'.uniqid('', true))); | |
136 | ||
137 | // We override the standard helper with our mock | |
138 | $command->getHelperSet()->set($question, 'question'); | |
139 | ||
140 | $tester = new CommandTester($command); | |
141 | $tester->execute(array( | |
142 | 'command' => $command->getName(), | |
143 | )); | |
144 | ||
145 | $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay()); | |
146 | $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay()); | |
147 | $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay()); | |
148 | $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay()); | |
149 | ||
150 | // the current database doesn't already exist | |
151 | $this->assertContains('Creating database and schema, clearing the cache', $tester->getDisplay()); | |
152 | } | |
153 | ||
154 | public function testRunInstallCommandChooseResetSchema() | |
155 | { | |
156 | $application = new Application($this->getClient()->getKernel()); | |
157 | $application->add(new InstallCommandMock()); | |
158 | ||
159 | $command = $application->find('wallabag:install'); | |
160 | ||
161 | // We mock the QuestionHelper | |
162 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
163 | ->disableOriginalConstructor() | |
164 | ->getMock(); | |
165 | ||
166 | $question->expects($this->exactly(3)) | |
167 | ->method('ask') | |
168 | ->will($this->onConsecutiveCalls( | |
169 | false, // don't want to reset the entire database | |
170 | true, // do want to reset the schema | |
171 | false // don't want to create a new user | |
172 | )); | |
173 | ||
174 | // We override the standard helper with our mock | |
175 | $command->getHelperSet()->set($question, 'question'); | |
176 | ||
177 | $tester = new CommandTester($command); | |
178 | $tester->execute(array( | |
179 | 'command' => $command->getName(), | |
180 | )); | |
181 | ||
182 | $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay()); | |
183 | $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay()); | |
184 | $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay()); | |
185 | $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay()); | |
186 | ||
187 | $this->assertContains('Droping schema and creating schema', $tester->getDisplay()); | |
188 | } | |
189 | ||
190 | public function testRunInstallCommandChooseNothing() | |
191 | { | |
192 | $application = new Application($this->getClient()->getKernel()); | |
193 | $application->add(new InstallCommand()); | |
194 | $application->add(new DropDatabaseDoctrineCommand()); | |
195 | $application->add(new CreateDatabaseDoctrineCommand()); | |
196 | ||
197 | // drop database first, so the install command won't ask to reset things | |
198 | $command = new DropDatabaseDoctrineCommand(); | |
199 | $command->setApplication($application); | |
200 | $command->run(new ArrayInput(array( | |
201 | 'command' => 'doctrine:database:drop', | |
202 | '--force' => true, | |
203 | )), new NullOutput()); | |
204 | ||
205 | $this->getClient()->getContainer()->get('doctrine')->getConnection()->close(); | |
206 | ||
207 | $command = new CreateDatabaseDoctrineCommand(); | |
208 | $command->setApplication($application); | |
209 | $command->run(new ArrayInput(array( | |
210 | 'command' => 'doctrine:database:create', | |
211 | '--env' => 'test', | |
212 | )), new NullOutput()); | |
213 | ||
214 | $command = $application->find('wallabag:install'); | |
215 | ||
216 | // We mock the QuestionHelper | |
217 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
218 | ->disableOriginalConstructor() | |
219 | ->getMock(); | |
220 | ||
221 | $question->expects($this->exactly(2)) | |
222 | ->method('ask') | |
223 | ->will($this->onConsecutiveCalls( | |
224 | false, // don't want to reset the entire database | |
225 | false // don't want to create a new user | |
226 | )); | |
227 | ||
228 | // We override the standard helper with our mock | |
229 | $command->getHelperSet()->set($question, 'question'); | |
230 | ||
231 | $tester = new CommandTester($command); | |
232 | $tester->execute(array( | |
233 | 'command' => $command->getName(), | |
234 | )); | |
235 | ||
236 | $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay()); | |
237 | $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay()); | |
238 | $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay()); | |
239 | $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay()); | |
240 | ||
241 | $this->assertContains('Creating schema', $tester->getDisplay()); | |
242 | } | |
243 | ||
244 | public function testRunInstallCommandNoInteraction() | |
245 | { | |
246 | $application = new Application($this->getClient()->getKernel()); | |
247 | $application->add(new InstallCommandMock()); | |
248 | ||
249 | $command = $application->find('wallabag:install'); | |
250 | ||
251 | // We mock the QuestionHelper | |
252 | $question = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') | |
253 | ->disableOriginalConstructor() | |
254 | ->getMock(); | |
255 | $question->expects($this->any()) | |
256 | ->method('ask') | |
257 | ->will($this->returnValue('yes_'.uniqid('', true))); | |
258 | ||
259 | // We override the standard helper with our mock | |
260 | $command->getHelperSet()->set($question, 'question'); | |
261 | ||
262 | $tester = new CommandTester($command); | |
263 | $tester->execute(array( | |
264 | 'command' => $command->getName(), | |
265 | '--no-interaction' => true, | |
266 | )); | |
267 | ||
268 | $this->assertContains('Step 1 of 4. Checking system requirements.', $tester->getDisplay()); | |
269 | $this->assertContains('Step 2 of 4. Setting up database.', $tester->getDisplay()); | |
270 | $this->assertContains('Step 3 of 4. Administration setup.', $tester->getDisplay()); | |
271 | $this->assertContains('Step 4 of 4. Installing assets.', $tester->getDisplay()); | |
272 | } | |
273 | } |