Passed
Push — test ( d838cf...cef4a5 )
by Tom
03:29
created

ServiceOptionsTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testOptionMissingArgument() 0 8 1
A testNoOption() 0 5 1
A testOptionWithMissingService() 0 8 1
A getTestBindings() 0 12 1
A testOptionWithService() 0 13 1
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Utility;
6
7
use Ktomk\Pipelines\Cli\Args;
8
use Ktomk\Pipelines\Cli\ExecTester;
9
use Ktomk\Pipelines\Cli\Streams;
10
use Ktomk\Pipelines\File\File;
11
use Ktomk\Pipelines\Runner\Env;
12
use Ktomk\Pipelines\Runner\RunOpts;
13
use Ktomk\Pipelines\TestCase;
14
15
/**
16
 * Class ServiceOptionsTest
17
 *
18
 * @package Ktomk\Pipelines\Utility
19
 * @covers \Ktomk\Pipelines\Utility\ServiceOptions
20
 */
21
class ServiceOptionsTest extends TestCase
22
{
23
    /**
24
     * @throws StatusException
25
     * @throws \Ktomk\Pipelines\Cli\ArgsException
26
     */
27
    public function testNoOption()
28
    {
29
        $file = File::createFromFile(__DIR__ . '/../../data/yml/service-definitions.yml');
30
        $this->getTestBindings(array('command'), $file)->run();
31
        $this->addToAssertionCount(1);
32
    }
33
34
    /**
35
     * @throws StatusException
36
     * @throws \Ktomk\Pipelines\Cli\ArgsException
37
     */
38
    public function testOptionMissingArgument()
39
    {
40
        $file = File::createFromFile(__DIR__ . '/../../data/yml/service-definitions.yml');
41
42
        $this->expectException('Ktomk\Pipelines\Cli\ArgsException');
43
        $this->expectExceptionMessage('option --service requires an argument');
44
45
        $this->getTestBindings(array('command', '--service'), $file)->run();
46
    }
47
48
    /**
49
     * @throws StatusException
50
     * @throws \Ktomk\Pipelines\Cli\ArgsException
51
     */
52
    public function testOptionWithMissingService()
53
    {
54
        $file = File::createFromFile(__DIR__ . '/../../data/yml/service-definitions.yml');
55
56
        $this->expectException('Ktomk\Pipelines\Cli\ArgsException');
57
        $this->expectExceptionMessage('undefined service: foobar');
58
59
        $this->getTestBindings(array('command', '--service', 'foobar'), $file)->run();
60
    }
61
62
    /**
63
     * @throws StatusException
64
     * @throws \Ktomk\Pipelines\Cli\ArgsException
65
     */
66
    public function testOptionWithService()
67
    {
68
        $file = File::createFromFile(__DIR__ . '/../../data/yml/service-definitions.yml');
69
70
        $this->expectException('Ktomk\Pipelines\Utility\StatusException');
71
        $this->expectExceptionMessage('');
72
        $this->expectExceptionCode(0);
73
74
        $options = $this->getTestBindings(array('command', '--service', 'redis'), $file, $execTester);
75
76
        /** @var ExecTester $execTester */
77
        $execTester->expect('pass', 'docker', 0, 'docker run');
78
        $options->run();
79
    }
80
81
    /**
82
     * @param array $args
83
     * @param File $file
84
     * @param null|ExecTester $execTester
85
     *
86
     * @return ServiceOptions
87
     */
88
    private function getTestBindings(array $args, File $file, ExecTester &$execTester = null)
89
    {
90
        $execTester = new ExecTester($this);
91
92
        return ServiceOptions::bind(
93
            Args::create($args),
94
            new Streams(),
95
            $file,
96
            $execTester,
97
            Env::create(),
98
            RunOpts::create('prefix'),
99
            $this->createMock('Ktomk\Pipelines\Runner\Directories')
100
        );
101
    }
102
}
103