Completed
Push — master ( f4794e...1f0a28 )
by ophelie
05:23
created

testRestartWithOneComposeFileSpecified()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace DockerCompose\Tests\Manager;
4
5
use PHPUnit_Framework_TestCase;
6
use DockerCompose\ComposeFile;
7
use DockerCompose\ComposeFileCollection;
8
9
10
class ComposeManagerTest extends PHPUnit_Framework_TestCase
11
{
12
    public function setUp()
13
    {
14
        $this->manager = $this->getMockBuilder('\DockerCompose\Manager\ComposeManager')
15
            ->setMethods(['execute'])
16
            ->getMock();
17
    }
18
19
    /**
20
     * Test simple start without error
21
     */
22
    public function testStart()
23
    {
24
        $this->manager->method('execute')->with('docker-compose up -d')->willReturn(array('output' => 'ok', 'code' => 0));
25
        $this->assertEquals($this->manager->start(), 'ok');
26
    }
27
28
    /**
29
     * Test start success with one compose file
30
     */
31
    public function testStartWithOneComposeFileSpecified()
32
    {
33
        $this->manager->method('execute')->with('docker-compose -f docker-compose.test.yml up -d')->willReturn(array('output' => 'ok', 'code' => 0));
34
        $this->assertEquals($this->manager->start('docker-compose.test.yml'), 'ok');
35
    }
36
37
    /**
38
     * Test start success with two compose files
39
     */
40
    public function testStartWithTwoComposeFilesSpecified()
41
    {
42
        $this->manager->method('execute')->with('docker-compose -f docker-compose.yml -f docker-compose.test.yml up -d')->willReturn(array('output' => 'ok', 'code' => 0));
43
        $this->assertEquals($this->manager->start(['docker-compose.yml', 'docker-compose.test.yml']), 'ok');
44
    }
45
46
    /**
47
     * Test start with project option
48
     */
49
    public function testStartWithprojectOption()
50
    {
51
        $composeFiles = new ComposeFileCollection(['docker-compose.test.yml']);
52
        $composeFiles->setProjectName('unittest');
53
54
        $this->manager->method('execute')->with('docker-compose -f docker-compose.test.yml --project-name unittest up -d')->willReturn(array('output' => 'ok', 'code' => 0));
55
56
        $this->assertEquals($this->manager->start($composeFiles), 'ok');
57
58
    }
59
60
    /**
61
     * Test start with project and networking option
62
     */
63
    public function testStartWithprojectAndNetworkingOption()
64
    {
65
        $composeFiles = new ComposeFileCollection(['docker-compose.test.yml']);
66
        $composeFiles->setProjectName('unittest')->setIsNetworking(true);
67
68
        $this->manager->method('execute')->with('docker-compose -f docker-compose.test.yml --x-networking --project-name unittest up -d')->willReturn(array('output' => 'ok', 'code' => 0));
69
        $this->assertEquals($this->manager->start($composeFiles), 'ok');
70
    }
71
72
    /**
73
     * Test start with project, networking and network driver option
74
     */
75 View Code Duplication
    public function testStartWithprojectAndNetworkingWithDriverOption()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        $composeFiles = new ComposeFileCollection(['docker-compose.test.yml']);
78
        $composeFiles->setProjectName('unittest')->setIsNetworking(true)->setNetworkDriver('overlay');
79
80
        $this->manager->method('execute')->with('docker-compose -f docker-compose.test.yml --x-networking --x-network-driver overlay --project-name unittest up -d')->willReturn(array('output' => 'ok', 'code' => 0));
81
        $this->assertEquals($this->manager->start($composeFiles), 'ok');
82
    }
83
84
    /**
85
     * Test start with ComposeFileNotFoundException
86
     *
87
     * @expectedException \Exception
88
     */
89
    public function testStartThrowExceptionForDriver()
90
    {
91
        $composeFiles = new ComposeFileCollection(['docker-compose.test.yml']);
92
        $composeFiles->setProjectName('unittest')->setIsNetworking(true)->setNetworkDriver('boom');
93
        $this->manager->start();
94
    }
95
96
    /**
97
     * Test start with ComposeFileNotFoundException
98
     *
99
     * @expectedException \DockerCompose\Exception\ComposeFileNotFoundException
100
     */
101 View Code Duplication
    public function testStartThrowComposeFileNotFound()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103
        $error = 'Can\'t find a suitable configuration file in this directory or any parent. Are you in the right directory?\n';
104
        $error .= 'Supported filenames: docker-compose.yml, docker-compose.yaml, fig.yml, fig.yaml';
105
        $this->manager->method('execute')->with('docker-compose up -d')->willReturn(array('output' => $error, 'code' => 1));
106
        $this->manager->start();
107
    }
108
109
    /**
110
     * Test start with DockerHostConnexionErrorException
111
     *
112
     * @expectedException \DockerCompose\Exception\DockerHostConnexionErrorException
113
     */
114 View Code Duplication
    public function testStartThrowDockerHostConnexionErrorException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    {
116
        $error = 'Couldn\'t connect to Docker daemon at http+docker://localunixsocket - is it running?\n';
117
        $error .= 'If it\'s at a non-standard location, specify the URL with the DOCKER_HOST environment variable.';
118
        $this->manager->method('execute')->with('docker-compose up -d')->willReturn(array('output' => $error, 'code' => 1));
119
        $this->manager->start();
120
    }
121
122
    /**
123
     * Test start with DockerInstallationMissingException
124
     *
125
     * @expectedException \DockerCompose\Exception\DockerInstallationMissingException
126
     */
127
    public function testStartThrowDockerInstallationMissingException()
128
    {
129
        $this->manager->method('execute')->with('docker-compose up -d')->willReturn(array('output' => '', 'code' => 127));
130
        $this->manager->start();
131
    }
132
133
    /**
134
     * Test start with DockerInstallationMissingException
135
     *
136
     * @expectedException \Exception
137
     */
138
    public function testStartThrowUnknownException()
139
    {
140
        $this->manager->method('execute')->with('docker-compose up -d')->willReturn(array('output' => '', 'code' => 1));
141
        $this->manager->start();
142
    }
143
144
    /**
145
     *  Test stop whithout error
146
     */
147
    public function testStop()
148
    {
149
        $this->manager->method('execute')->with('docker-compose stop')->willReturn(array('output' => 'ok', 'code' => 0));
150
        $this->assertEquals($this->manager->stop(), 'ok');
151
    }
152
153
    /**
154
     * Test stop success with one compose file
155
     */
156
    public function testStopWithOneComposeFileSpecified()
157
    {
158
159
        $this->manager->method('execute')->with('docker-compose -f docker-compose.test.yml stop')->willReturn(array('output' => 'ok', 'code' => 0));
160
        $this->assertEquals($this->manager->stop('docker-compose.test.yml'), 'ok');
161
    }
162
163
    /**
164
     * Test stop success with two compose files
165
     */
166
    public function testStopWithTwoComposeFilesSpecified()
167
    {
168
169
        $this->manager->method('execute')->with('docker-compose -f docker-compose.yml -f docker-compose.test.yml stop')->willReturn(array('output' => 'ok', 'code' => 0));
170
        $this->assertEquals($this->manager->stop(['docker-compose.yml', 'docker-compose.test.yml']), 'ok');
171
    }
172
173
    /**
174
     * Test stop with ComposeFileNotFoundException
175
     *
176
     * @expectedException \DockerCompose\Exception\ComposeFileNotFoundException
177
     */
178 View Code Duplication
    public function testStopThrowComposeFileNotFound()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
    {
180
        $error = 'Can\'t find a suitable configuration file in this directory or any parent. Are you in the right directory?\n';
181
        $error .= 'Supported filenames: docker-compose.yml, docker-compose.yaml, fig.yml, fig.yaml';
182
        $this->manager->method('execute')->with('docker-compose stop')->willReturn(array('output' => $error, 'code' => 1));
183
        $this->manager->stop();
184
    }
185
186
    /**
187
     * Test stop with DockerHostConnexionErrorException
188
     *
189
     * @expectedException \DockerCompose\Exception\DockerHostConnexionErrorException
190
     */
191 View Code Duplication
    public function testStopThrowDockerHostConnexionErrorException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
    {
193
        $error = 'Couldn\'t connect to Docker daemon at http+docker://localunixsocket - is it running?\n';
194
        $error .= 'If it\'s at a non-standard location, specify the URL with the DOCKER_HOST environment variable.';
195
        $this->manager->method('execute')->with('docker-compose stop')->willReturn(array('output' => $error, 'code' => 1));
196
        $this->manager->stop();
197
    }
198
199
    /**
200
     * Test stop with DockerInstallationMissingException
201
     *
202
     * @expectedException \DockerCompose\Exception\DockerInstallationMissingException
203
     */
204
    public function testStopThrowDockerInstallationMissingException()
205
    {
206
        $this->manager->method('execute')->with('docker-compose stop')->willReturn(array('output' => '', 'code' => 127));
207
        $this->manager->stop();
208
    }
209
210
    /**
211
     *  Test remove whithout error
212
     */
213
    public function testRemove()
214
    {
215
        $this->manager->method('execute')->with('docker-compose rm')->willReturn(array('output' => 'ok', 'code' => 0));
216
        $this->assertEquals($this->manager->remove(), 'ok');
217
    }
218
219
    /**
220
     *  Test remove force
221
     */
222
    public function testRemoveForce()
223
    {
224
        $this->manager->method('execute')->with('docker-compose rm --force')->willReturn(array('output' => 'ok', 'code' => 0));
225
        $this->assertEquals($this->manager->remove([], true), 'ok');
226
    }
227
228
    /**
229
     *  Test remove volumes
230
     */
231
    public function testRemoveVolumes()
232
    {
233
        $this->manager->method('execute')->with('docker-compose rm -v')->willReturn(array('output' => 'ok', 'code' => 0));
234
        $this->assertEquals($this->manager->remove([], false, true), 'ok');
235
    }
236
237
    /**
238
     *  Test remove force and volumes
239
     */
240
    public function testRemoveForceAndVolumes()
241
    {
242
        $this->manager->method('execute')->with('docker-compose rm --force -v')->willReturn(array('output' => 'ok', 'code' => 0));
243
        $this->assertEquals($this->manager->remove([], true, true), 'ok');
244
    }
245
246
    /**
247
     * Test remove success with one compose file
248
     */
249
    public function testRemoveWithOneComposeFileSpecified()
250
    {
251
252
        $this->manager->method('execute')->with('docker-compose -f docker-compose.test.yml rm')->willReturn(array('output' => 'ok', 'code' => 0));
253
        $this->assertEquals($this->manager->remove('docker-compose.test.yml'), 'ok');
254
    }
255
256
    /**
257
     * Test remove success with two compose files
258
     */
259
    public function testRemoveWithTwoComposeFilesSpecified()
260
    {
261
262
        $this->manager->method('execute')->with('docker-compose -f docker-compose.yml -f docker-compose.test.yml rm')->willReturn(array('output' => 'ok', 'code' => 0));
263
        $this->assertEquals($this->manager->remove(['docker-compose.yml', 'docker-compose.test.yml']), 'ok');
264
    }
265
266
    /**
267
     * Test remove with ComposeFileNotFoundException
268
     *
269
     * @expectedException \DockerCompose\Exception\ComposeFileNotFoundException
270
     */
271 View Code Duplication
    public function testRemoveThrowComposeFileNotFound()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
272
    {
273
        $error = 'Can\'t find a suitable configuration file in this directory or any parent. Are you in the right directory?\n';
274
        $error .= 'Supported filenames: docker-compose.yml, docker-compose.yaml, fig.yml, fig.yaml';
275
        $this->manager->method('execute')->with('docker-compose rm')->willReturn(array('output' => $error, 'code' => 1));
276
        $this->manager->remove();
277
    }
278
279
    /**
280
     * Test remove with DockerHostConnexionErrorException
281
     *
282
     * @expectedException \DockerCompose\Exception\DockerHostConnexionErrorException
283
     */
284 View Code Duplication
    public function testRemoveThrowDockerHostConnexionErrorException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
285
    {
286
        $error = 'Couldn\'t connect to Docker daemon at http+docker://localunixsocket - is it running?\n';
287
        $error .= 'If it\'s at a non-standard location, specify the URL with the DOCKER_HOST environment variable.';
288
        $this->manager->method('execute')->with('docker-compose rm')->willReturn(array('output' => $error, 'code' => 1));
289
        $this->manager->remove();
290
    }
291
292
    /**
293
     * Test remove with DockerInstallationMissingException
294
     *
295
     * @expectedException \DockerCompose\Exception\DockerInstallationMissingException
296
     */
297
    public function testRemoveThrowDockerInstallationMissingException()
298
    {
299
        $this->manager->method('execute')->with('docker-compose rm')->willReturn(array('output' => '', 'code' => 127));
300
        $this->manager->remove();
301
    }
302
303
    /**
304
     * Test run
305
     */
306
    public function testrun()
307
    {
308
        $this->manager->method('execute')->with('docker-compose run --rm test mycommand')->willReturn(array('output' => 'ok', 'code' => 0));
309
        $this->assertEquals($this->manager->run('test', 'mycommand'), 'ok');
310
    }
311
312
    /**
313
     * Test run
314
     * @expectedException \DockerCompose\Exception\NoSuchServiceException
315
     */
316
    public function testrunThrowNoSuchServiceException()
317
    {
318
        $this->manager->method('execute')->with('docker-compose run --rm test mycommand')->willReturn(array('output' => 'ERROR: No such service: test', 'code' => 1));
319
        $this->manager->run('test', 'mycommand');
320
    }
321
322
    /**
323
     * Test run with project, networking and network driver option
324
     */
325 View Code Duplication
    public function testRuntWithprojectAndNetworkingWithDriverOption()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
326
    {
327
        $composeFiles = new ComposeFileCollection(['docker-compose.test.yml']);
328
        $composeFiles->setProjectName('unittest')->setIsNetworking(true)->setNetworkDriver('overlay');
329
330
        $this->manager->method('execute')->with('docker-compose -f docker-compose.test.yml --x-networking --x-network-driver overlay --project-name unittest run --rm test mycommand')->willReturn(array('output' => 'ok', 'code' => 0));
331
        $this->assertEquals($this->manager->run('test', 'mycommand', $composeFiles), 'ok');
332
    }
333
334
    /**
335
     * Test simple build without error
336
     */
337
    public function testBuild()
338
    {
339
        $this->manager->method('execute')->with('docker-compose build --pull')->willReturn(array('output' => 'ok', 'code' => 0));
340
        $this->assertEquals($this->manager->build(), 'ok');
341
    }
342
343
    /**
344
     * Test build success with one compose file
345
     */
346
    public function testBuildWithOneComposeFileSpecified()
347
    {
348
        $this->manager->method('execute')->with('docker-compose -f docker-compose.test.yml build --pull')->willReturn(array('output' => 'ok', 'code' => 0));
349
        $this->assertEquals($this->manager->build('docker-compose.test.yml'), 'ok');
350
    }
351
352
    /**
353
     * Test build success with two compose files
354
     */
355
    public function testBuildWithTwoComposeFilesSpecified()
356
    {
357
        $this->manager->method('execute')->with('docker-compose -f docker-compose.yml -f docker-compose.test.yml build --pull')->willReturn(array('output' => 'ok', 'code' => 0));
358
        $this->assertEquals($this->manager->build(['docker-compose.yml', 'docker-compose.test.yml']), 'ok');
359
    }
360
361
    /**
362
     * Test simple build without pull
363
     */
364
    public function testBuildWithoutPull()
365
    {
366
        $this->manager->method('execute')->with('docker-compose build')->willReturn(array('output' => 'ok', 'code' => 0));
367
        $this->assertEquals($this->manager->build([], false), 'ok');
368
    }
369
370
    /**
371
     * Test simple build with --force-rm
372
     */
373
    public function testBuildWithForceRm()
374
    {
375
        $this->manager->method('execute')->with('docker-compose build --force-rm')->willReturn(array('output' => 'ok', 'code' => 0));
376
        $this->assertEquals($this->manager->build([], false, true), 'ok');
377
    }
378
379
    /**
380
     * Test simple build with --no-cache
381
     */
382
    public function testBuildWithNoCache()
383
    {
384
        $this->manager->method('execute')->with('docker-compose build --no-cache')->willReturn(array('output' => 'ok', 'code' => 0));
385
        $this->assertEquals($this->manager->build([], false, false, false), 'ok');
386
    }
387
388
    /**
389
     * Test simple build with --pull --force-rm --no-cache
390
     */
391
    public function testBuildWithPullAndForceRmAndNoCache()
392
    {
393
        $this->manager->method('execute')->with('docker-compose build --pull --force-rm --no-cache')->willReturn(array('output' => 'ok', 'code' => 0));
394
        $this->assertEquals($this->manager->build([], true, true, false), 'ok');
395
396
    }
397
398
    /**
399
     * Test simple ps without error
400
     */
401
    public function testPs()
402
    {
403
        $this->manager->method('execute')->with('docker-compose ps')->willReturn(array('output' => 'ok', 'code' => 0));
404
        $this->assertEquals($this->manager->ps(), 'ok');
405
    }
406
407
    /**
408
     * Test ps success with one compose file
409
     */
410
    public function testPsWithOneComposeFileSpecified()
411
    {
412
        $this->manager->method('execute')->with('docker-compose -f docker-compose.test.yml ps')->willReturn(array('output' => 'ok', 'code' => 0));
413
        $this->assertEquals($this->manager->ps('docker-compose.test.yml'), 'ok');
414
    }
415
416
    /**
417
     * Test ps success with two compose files
418
     */
419
    public function testPsWithTwoComposeFilesSpecified()
420
    {
421
        $this->manager->method('execute')->with('docker-compose -f docker-compose.yml -f docker-compose.test.yml ps')->willReturn(array('output' => 'ok', 'code' => 0));
422
        $this->assertEquals($this->manager->ps(['docker-compose.yml', 'docker-compose.test.yml']), 'ok');
423
    }
424
425
    /**
426
     * Test ps with project option
427
     */
428
    public function testPsWithprojectOption()
429
    {
430
        $composeFiles = new ComposeFileCollection(['docker-compose.test.yml']);
431
        $composeFiles->setProjectName('unittest');
432
433
        $this->manager->method('execute')->with('docker-compose -f docker-compose.test.yml --project-name unittest ps')->willReturn(array('output' => 'ok', 'code' => 0));
434
435
        $this->assertEquals($this->manager->ps($composeFiles), 'ok');
436
    }
437
438
    /**
439
     * Test simple restart without error
440
     */
441
    public function testRestart()
442
    {
443
        $this->manager->method('execute')->with('docker-compose restart')->willReturn(array('output' => 'ok', 'code' => 0));
444
        $this->assertEquals($this->manager->restart(), 'ok');
445
    }
446
447
    /**
448
     * Test simple restart with timeout
449
     */
450
    public function testRestartWithTimeout()
451
    {
452
        $this->manager->method('execute')->with('docker-compose restart --timeout=30')->willReturn(array('output' => 'ok', 'code' => 0));
453
        $this->assertEquals($this->manager->restart([], 30), 'ok');
454
    }
455
456
    /**
457
     * Test restart success with one compose file
458
     */
459
    public function testRestartWithOneComposeFileSpecified()
460
    {
461
        $this->manager->method('execute')->with('docker-compose -f docker-compose.test.yml restart')->willReturn(array('output' => 'ok', 'code' => 0));
462
        $this->assertEquals($this->manager->restart('docker-compose.test.yml'), 'ok');
463
    }
464
465
    /**
466
     * Test restart success with two compose files
467
     */
468
    public function testRestartWithTwoComposeFilesSpecified()
469
    {
470
        $this->manager->method('execute')->with('docker-compose -f docker-compose.yml -f docker-compose.test.yml restart')->willReturn(array('output' => 'ok', 'code' => 0));
471
        $this->assertEquals($this->manager->restart(['docker-compose.yml', 'docker-compose.test.yml']), 'ok');
472
    }
473
474
    /**
475
     * Test restart with project option
476
     */
477
    public function testRestartWithprojectOption()
478
    {
479
        $composeFiles = new ComposeFileCollection(['docker-compose.test.yml']);
480
        $composeFiles->setProjectName('unittest');
481
482
        $this->manager->method('execute')->with('docker-compose -f docker-compose.test.yml --project-name unittest restart')->willReturn(array('output' => 'ok', 'code' => 0));
483
484
        $this->assertEquals($this->manager->restart($composeFiles), 'ok');
485
486
    }
487
}
488