Passed
Pull Request — master (#18)
by David
03:04
created

ServiceTest::testLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
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",
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": {
23
                          "value": "fooo",
24
                          "type": "containerEnvVariable"
25
                        }
26
                      },
27
    "labels"        : {
28
                        "foo": {"value": "fooo"},
29
                        "bar": {"value": "baar"}
30
                      },               
31
    "volumes"       : [
32
                        {
33
                          "type"      : "volume",
34
                          "source"    : "foo",
35
                          "target"    : "bar",
36
                          "readOnly"  : true
37
                        }
38
                      ]
39
  }
40
}
41
JSON;
42
43
    private const MISSING_SERVICE_NAME_PAYLOAD = <<< 'JSON'
44
{
45
  "service": {
46
    "internalPorts": [80]
47
  }
48
}
49
JSON;
50
51
    private const UNKNOWN_ENV_VARIABLE_TYPE_PAYLOAD = <<< 'JSON'
52
{
53
  "serviceName": "foo",
54
  "service": {
55
    "environment": {
56
      "FOO": {
57
        "value": "fooo",
58
        "type": "YIKES_THATS_SOME_BAD_TYPE_HERE"
59
      }
60
    }
61
  }
62
}
63
JSON;
64
65
    private const UNKNOWN_VOLUME_TYPE_PAYLOAD = <<< 'JSON'
66
{
67
  "serviceName": "foo",
68
  "service": {
69
    "volumes": [
70
      {
71
        "type": "AGAIN?WTF",
72
        "source": "foo"
73
      }
74
    ]
75
  }
76
}
77
JSON;
78
79
    public function testValidPayload() : void
80
    {
81
        $array = json_decode(self::VALID_PAYLOAD, true);
82
        $service = Service::parsePayload($array);
83
        $out = $service->jsonSerialize();
84
        $this->assertEquals($array, $out);
85
    }
86
87
    public function testMissingServiceNamePayload() : void
88
    {
89
        $this->expectException(ServiceException::class);
90
        $array = json_decode(self::MISSING_SERVICE_NAME_PAYLOAD, true);
91
        Service::parsePayload($array)->jsonSerialize();
92
    }
93
94
    public function testUnknownEnvVariableTypePayload() : void
95
    {
96
        $this->expectException(ServiceException::class);
97
        $array = json_decode(self::UNKNOWN_ENV_VARIABLE_TYPE_PAYLOAD, true);
98
        Service::parsePayload($array)->jsonSerialize();
99
    }
100
101
    public function testUnknownVolumeTypePayload() : void
102
    {
103
        $this->expectException(ServiceException::class);
104
        $array = json_decode(self::UNKNOWN_VOLUME_TYPE_PAYLOAD, true);
105
        Service::parsePayload($array)->jsonSerialize();
106
    }
107
108
    public function testLabels(): void
109
    {
110
        $service = new Service();
111
        $service->setServiceName('foobar');
112
        $service->addLabel('traefik.backend', 'foobar');
113
        $array = $service->jsonSerialize();
114
        $this->assertEquals(['traefik.backend' => ['value'=>'foobar']], $array['service']['labels'][0]);
115
    }
116
}
117