testLargePortWillThrowAnException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
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\Test\TestCase;
18
use Graze\DogStatsD\Exception\ConfigurationException;
19
20
class EnvConfigurationTest extends TestCase
21
{
22
    public function tearDown(): void
23
    {
24
        putenv('DD_AGENT_HOST=');
25
        putenv('DD_DOGSTATSD_PORT=');
26
        putenv('DD_ENTITY_ID=');
27
    }
28
29
    public function testHost()
30
    {
31
        putenv('DD_AGENT_HOST=127.0.0.1');
32
        $this->client->configure();
33
34
        $this->assertEquals('127.0.0.1', $this->client->getHost());
35
    }
36
37
    public function testPort()
38
    {
39
        putenv('DD_DOGSTATSD_PORT=12434');
40
        $this->client->configure();
41
42
        $this->assertEquals(12434, $this->client->getPort());
43
    }
44
45
    public function testLargePortWillThrowAnException()
46
    {
47
        $this->expectException(ConfigurationException::class);
48
        $this->expectExceptionMessage("Option: Port is invalid or is out of range");
49
50
        putenv('DD_DOGSTATSD_PORT=65536');
51
        $this->client->configure();
52
    }
53
54
    public function testStringPortWillThrowAnException()
55
    {
56
        $this->expectException(ConfigurationException::class);
57
        $this->expectExceptionMessage("Option: Port is invalid or is out of range");
58
59
        putenv('DD_DOGSTATSD_PORT=not-integer');
60
        $this->client->configure();
61
    }
62
63
    public function testTags()
64
    {
65
        putenv('DD_ENTITY_ID=f87dsf7dsf9s7d9f8');
66
        $this->client->configure();
67
68
        $this->client->gauge('test_metric', 456);
69
        $this->assertEquals('test_metric:456|g|#dd.internal.entity_id:f87dsf7dsf9s7d9f8', $this->client->getLastMessage());
70
    }
71
72
    public function testTagsAppended()
73
    {
74
        putenv('DD_ENTITY_ID=f87dsf7dsf9s7d9f8');
75
        $this->client->configure([
76
            'tags' => ['key' => 'value'],
77
        ]);
78
79
        $this->client->gauge('test_metric', 456);
80
        $this->assertEquals('test_metric:456|g|#key:value,dd.internal.entity_id:f87dsf7dsf9s7d9f8', $this->client->getLastMessage());
81
    }
82
}
83