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

ImageTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 67
dl 0
loc 157
rs 10
c 0
b 0
f 0
wmc 15

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testJsonSerialize() 0 10 1
A testCreateFromArrayWithSuperfluousProperties() 0 11 1
A testNameAsString() 0 5 1
A provideImageArrays() 0 10 1
A testGetProperties() 0 7 1
A testCreateFromString() 0 4 1
A testCreateFromArray() 0 10 1
A testCreateFromInvalidName() 0 6 1
A testCreateMissingName() 0 6 1
A testValidation() 0 10 2
A testValidateImageSectionInvalidName() 0 9 1
A testCreateFromObject() 0 6 1
A testGetName() 0 5 1
A testValidateImageSectionValidName() 0 7 1
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\File;
6
7
use Ktomk\Pipelines\TestCase;
8
9
/**
10
 * @covers \Ktomk\Pipelines\File\Image
11
 */
12
class ImageTest extends TestCase
13
{
14
    public function testCreateFromString()
15
    {
16
        $image = new Image('account-name/java:8u66');
17
        self::assertInstanceOf('Ktomk\Pipelines\File\Image', $image);
18
    }
19
20
    /**
21
     * @return void
22
     */
23
    public function testCreateFromInvalidName()
24
    {
25
        $this->expectException('Ktomk\Pipelines\File\ParseException');
26
        $this->expectExceptionMessage('\'image\' invalid Docker image name: \'/\'');
27
28
        new Image('/');
29
    }
30
31
    public function testCreateFromArray()
32
    {
33
        $array = array(
34
            'name' => 'account-name/java:8u66',
35
            'username' => '$DOCKER_HUB_USERNAME',
36
            'password' => '$DOCKER_HUB_PASSWORD',
37
            'email' => '$DOCKER_HUB_EMAIL',
38
        );
39
        $image = new Image($array);
40
        self::assertInstanceOf('Ktomk\Pipelines\File\Image', $image);
41
    }
42
43
    /**
44
     */
45
    public function testCreateFromArrayWithSuperfluousProperties()
46
    {
47
        $this->expectException('Ktomk\Pipelines\File\ParseException');
48
        $this->expectExceptionMessage('unknown \'image\' property \'superfluous\'');
49
50
        $array = array(
51
            'superfluous' => null,
52
            'name' => 'account-name/java:8u66',
53
        );
54
        $image = new Image($array);
55
        self::assertInstanceOf('Ktomk\Pipelines\File\Image', $image);
56
    }
57
58
    /**
59
     */
60
    public function testCreateFromObject()
61
    {
62
        $this->expectException('Ktomk\Pipelines\File\ParseException');
63
        $this->expectExceptionMessage('\'image\' expects either \'a string\' or \'a section\'');
64
65
        new Image((object)array());
0 ignored issues
show
Bug introduced by
(object)array() of type object is incompatible with the type array|string expected by parameter $image of Ktomk\Pipelines\File\Image::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        new Image(/** @scrutinizer ignore-type */ (object)array());
Loading history...
66
    }
67
68
    /**
69
     */
70
    public function testCreateMissingName()
71
    {
72
        $this->expectException('Ktomk\Pipelines\File\ParseException');
73
        $this->expectExceptionMessage('\'image\' needs a name');
74
75
        new Image(array());
76
    }
77
78
    public function testGetName()
79
    {
80
        $expected = 'foo/bar:tag';
81
        $image = new Image($expected);
82
        self::assertSame($expected, (string)$image->getName());
83
    }
84
85
    public function testNameAsString()
86
    {
87
        $expected = 'foo/bar:tag';
88
        $image = new Image(array('name' => $expected));
89
        self::assertSame($expected, $image->__toString());
90
    }
91
92
    public function testGetProperties()
93
    {
94
        $expected = 'foo/bar:tag';
95
        $image = new Image(array('name' => $expected));
96
        $actual = $image->getProperties();
97
        $class = 'Ktomk\Pipelines\Value\Properties';
98
        self::assertInstanceOf($class, $actual);
99
    }
100
101
    public function testJsonSerialize()
102
    {
103
        $expected = array(
104
            'name' => 'peace',
105
        );
106
        $image = new Image($expected);
107
        $actual = $image->jsonSerialize();
108
        self::assertSame(
109
            $expected,
110
            $actual
111
        );
112
    }
113
114
    public function provideImageArrays()
115
    {
116
        return array(
117
            array(array(), false), # no image is not invalid
118
            array(array('image' => null), true), # null image is invalid
119
            array(array('image' => ''), true), # empty string image is invalid
120
            array(array('image' => 'foo'), false), # string image is valid
121
            array(array('image' => array()), true), # empty array image is invalid
122
            array(array('image' => array('name' => '')), true), # array image is invalid if it has an empty name
123
            array(array('image' => array('name' => 'foo')), false), # array image is valid if it has a name
124
        );
125
    }
126
127
    /**
128
     * @dataProvider provideImageArrays
129
     *
130
     * @param array $array
131
     * @param $expected
132
     */
133
    public function testValidation(array $array, $expected)
134
    {
135
        $thrown = false;
136
137
        try {
138
            Image::validate($array);
139
        } catch (ParseException $exception) {
140
            $thrown = true;
141
        }
142
        self::assertSame($expected, $thrown);
143
    }
144
145
    /**
146
     * @return void
147
     */
148
    public function testValidateImageSectionInvalidName()
149
    {
150
        $this->expectException('Ktomk\Pipelines\File\ParseException');
151
        $this->expectExceptionMessage('\'image\' invalid Docker image name: \'/\'');
152
153
        $image = array(
154
            'image' => array('name' => '/'),
155
        );
156
        Image::validate($image);
157
    }
158
159
    /**
160
     * @return void
161
     */
162
    public function testValidateImageSectionValidName()
163
    {
164
        $image = array(
165
            'image' => array('name' => 'php/5.6:latest'),
166
        );
167
        Image::validate($image);
168
        $this->addToAssertionCount(1);
169
    }
170
}
171