ConfigurationTest::testHost()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of graze/dog-statsd
5
 *
6
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://github.com/graze/dog-statsd/blob/master/LICENSE.md
12
 * @link    https://github.com/graze/dog-statsd
13
 */
14
15
namespace Graze\DogStatsD\Test\Unit;
16
17
use Graze\DogStatsD\Exception\ConfigurationException;
18
use Graze\DogStatsD\Test\TestCase;
19
20
class ConfigurationTest extends TestCase
21
{
22
    public function testHost()
23
    {
24
        $this->client->configure([
25
            'host' => '127.0.0.1',
26
        ]);
27
        $this->assertEquals('127.0.0.1', $this->client->getHost());
28
    }
29
30
    public function testHostInvalidTypeWillThrowAnException()
31
    {
32
        $this->expectException(ConfigurationException::class);
33
        $this->expectExceptionMessage("Option: host is expected to be: 'string', was: 'integer'");
34
35
        $this->client->configure([
36
            'host' => 12434,
37
        ]);
38
    }
39
40
    public function testLargePortWillThrowAnException()
41
    {
42
        $this->expectException(ConfigurationException::class);
43
        $this->expectExceptionMessage("Option: Port is invalid or is out of range");
44
45
        $this->client->configure([
46
            'port' => 65536,
47
        ]);
48
    }
49
50
    public function testStringPortWillThrowAnException()
51
    {
52
        $this->expectException(ConfigurationException::class);
53
        $this->expectExceptionMessage("Option: Port is invalid or is out of range");
54
55
        $this->client->configure([
56
            'port' => 'not-integer',
57
        ]);
58
    }
59
60
    public function testValidStringPort()
61
    {
62
        $this->client->configure([
63
            'port' => '1234',
64
        ]);
65
        $this->assertEquals(1234, $this->client->getPort());
66
    }
67
68
    public function testDefaultPort()
69
    {
70
        $this->assertEquals(8125, $this->client->getPort());
71
    }
72
73
    public function testValidPort()
74
    {
75
        $this->client->configure([
76
            'port' => 1234,
77
        ]);
78
        $this->assertEquals(1234, $this->client->getPort());
79
    }
80
81
    public function testInvalidNamespace()
82
    {
83
        $this->expectException(ConfigurationException::class);
84
        $this->expectExceptionMessage("Option: namespace is expected to be: 'string', was: 'integer'");
85
86
        $this->client->configure([
87
            'namespace' => 12345,
88
        ]);
89
    }
90
91
    public function testInvalidDataDogThrowAnException()
92
    {
93
        $this->expectException(ConfigurationException::class);
94
        $this->expectExceptionMessage("Option: dataDog is expected to be: 'boolean', was: 'string'");
95
96
        $this->client->configure([
97
            'dataDog' => 'invalid',
98
        ]);
99
    }
100
101
    public function testInvalidTagsThrowsAnException()
102
    {
103
        $this->expectException(ConfigurationException::class);
104
        $this->expectExceptionMessage("Option: tags is expected to be: 'array', was: 'string'");
105
106
        $this->client->configure([
107
            'tags' => 'tag,tag2',
108
        ]);
109
    }
110
111
    public function testInvalidOnErrorThrowsAnException()
112
    {
113
        $this->expectException(ConfigurationException::class);
114
        $this->expectExceptionMessage("Option: onError 'somethingelse' is not one of: [error,exception,ignore]");
115
116
        $this->client->configure([
117
            'onError' => 'somethingelse',
118
        ]);
119
    }
120
121
    public function testOnErrorConfiguration()
122
    {
123
        // exception is default
124
        $this->assertEquals('exception', $this->client->getConfig()['onError']);
125
126
        $this->client->configure(['onError' => 'error']);
127
        $this->assertEquals('error', $this->client->getConfig()['onError']);
128
129
        $this->client->configure(['onError' => 'exception']);
130
        $this->assertEquals('exception', $this->client->getConfig()['onError']);
131
132
        $this->client->configure(['onError' => 'ignore']);
133
        $this->assertEquals('ignore', $this->client->getConfig()['onError']);
134
    }
135
136
    public function testTagsProcessorAcceptsCallable()
137
    {
138
        $processor = function (array $tags) {
139
            return $tags;
140
        };
141
        $this->client->configure([
142
            'tagProcessors' => [$processor],
143
        ]);
144
        $this->assertEquals([$processor], $this->client->getConfig()['tagProcessors']);
145
    }
146
147
    public function testTagsProcessorDoesNotAcceptOtherThings()
148
    {
149
        $this->expectException(ConfigurationException::class);
150
        $this->expectExceptionMessage("supplied tag processor is not a callable");
151
152
        $this->client->configure([
153
            'tagProcessors' => ['a string']
154
        ]);
155
    }
156
}
157