Passed
Push — master ( eb31f8...a04d78 )
by David
02:26
created

ServiceTest::testSettersAndAdders()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 0
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\Registry;
4
5
6
use PHPUnit\Framework\TestCase;
7
use TheAentMachine\Service\Exception\ServiceException;
8
use TheAentMachine\Service\Service;
9
10
class ServiceTest extends TestCase
11
{
12
    private const VALID_PAYLOAD = <<< 'JSON'
13
{
14
  "serviceName" : "foo",
15
  "service": {
16
    "image"         : "foo/bar:baz",
17
    "command"       : ["foo", "-bar", "-baz", "--qux"],
18
    "internalPorts" : [1, 2, 3],
19
    "dependsOn"     : ["foo", "bar"],
20
    "ports"         : [{"source": 80, "target": 8080}],
21
    "environment"   : {
22
                        "FOO": {"value": "foo", "type": "sharedEnvVariable"},
23
                        "BAR": {"value": "bar", "type": "sharedSecret"},
24
                        "BAZ": {"value": "baz", "type": "imageEnvVariable"},
25
                        "QUX": {"value": "qux", "type": "containerEnvVariable"}
26
                      },
27
    "labels"        : {
28
                        "foo": {"value": "fooo"},
29
                        "bar": {"value": "baar"}
30
                      },               
31
    "volumes"       : [
32
                        {"type": "volume", "source": "foo", "target": "/foo", "readOnly": true},
33
                        {"type": "bind", "source": "/bar", "target": "/bar", "readOnly": false},
34
                        {"type": "tmpfs", "source": "baz"}
35
                      ]
36
  }
37
}
38
JSON;
39
40
    private const MISSING_SERVICE_NAME_PAYLOAD = <<< 'JSON'
41
{
42
  "service": {
43
    "internalPorts": [80]
44
  }
45
}
46
JSON;
47
48
    private const UNKNOWN_ENV_VARIABLE_TYPE_PAYLOAD = <<< 'JSON'
49
{
50
  "serviceName": "foo",
51
  "service": {
52
    "environment": {
53
      "FOO": {
54
        "value": "fooo",
55
        "type": "YIKES_THATS_SOME_BAD_TYPE_HERE"
56
      }
57
    }
58
  }
59
}
60
JSON;
61
62
    private const UNKNOWN_VOLUME_TYPE_PAYLOAD = <<< 'JSON'
63
{
64
  "serviceName": "foo",
65
  "service": {
66
    "volumes": [
67
      {
68
        "type": "AGAIN?WTF",
69
        "source": "foo"
70
      }
71
    ]
72
  }
73
}
74
JSON;
75
76
    public function testValidPayload(): void
77
    {
78
        $array = json_decode(self::VALID_PAYLOAD, true);
79
        $service = Service::parsePayload($array);
80
        $out = $service->jsonSerialize();
81
        $this->assertEquals($array, $out);
82
    }
83
84
    public function testMissingServiceNamePayload(): void
85
    {
86
        $this->expectException(ServiceException::class);
87
        $array = json_decode(self::MISSING_SERVICE_NAME_PAYLOAD, true);
88
        Service::parsePayload($array)->jsonSerialize();
89
    }
90
91
    public function testUnknownEnvVariableTypePayload(): void
92
    {
93
        $this->expectException(ServiceException::class);
94
        $array = json_decode(self::UNKNOWN_ENV_VARIABLE_TYPE_PAYLOAD, true);
95
        Service::parsePayload($array)->jsonSerialize();
96
    }
97
98
    public function testUnknownVolumeTypePayload(): void
99
    {
100
        $this->expectException(ServiceException::class);
101
        $array = json_decode(self::UNKNOWN_VOLUME_TYPE_PAYLOAD, true);
102
        Service::parsePayload($array)->jsonSerialize();
103
    }
104
105
    public function testSettersAndAdders(): void
106
    {
107
        $s = new Service();
108
        $s->setServiceName('foo');
109
        $s->setImage('foo/bar:baz');
110
        $s->setCommand(['foo', '-bar', '-baz']);
111
        $s->addCommand('--qux');
112
        $s->setInternalPorts([1, 2]);
113
        $s->addInternalPort(3);
114
        $s->setDependsOn(['foo']);
115
        $s->addDependsOn('bar');
116
        $s->addPort(80, 8080);
117
        $s->addLabel('foo', 'fooo');
118
        $s->addLabel('bar', 'baar');
119
        $s->addSharedEnvVariable('FOO', 'foo');
120
        $s->addSharedSecret('BAR', 'bar');
121
        $s->addImageEnvVariable('BAZ', 'baz');
122
        $s->addContainerEnvVariable('QUX', 'qux');
123
        $s->addNamedVolume('foo', '/foo', true);
124
        $s->addBindVolume('/bar', '/bar', false);
125
        $s->addTmpfsVolume('baz');
126
        $outArray = $s->jsonSerialize();
127
        $expectedArray = json_decode(self::VALID_PAYLOAD, true);
128
        $this->assertEquals($outArray, $expectedArray);
129
    }
130
}
131