testTimeoutDefaultsToPhpIniDefaultSocketTimeout()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\ConnectionException;
18
use Graze\DogStatsD\Test\TestCase;
19
20
class ConnectionTest extends TestCase
21
{
22
    /**
23
     * Non-integer ports are not acceptable
24
     */
25
    public function testInvalidHost()
26
    {
27
        $this->expectException(ConnectionException::class);
28
29
        $this->client->configure([
30
            'host' => 'hostdoesnotexiststalleverlol.stupidtld',
31
        ]);
32
        $this->client->increment('test');
33
    }
34
35
    public function testTimeoutSettingIsUsedWhenCreatingSocketIfProvided()
36
    {
37
        $this->client->configure([
38
            'host'    => 'localhost',
39
            'timeout' => 123.425,
40
        ]);
41
        $this->assertSame(123.425, $this->client->getConfig()['timeout']);
42
    }
43
44
    public function testCanBeConfiguredToThrowErrors()
45
    {
46
        $this->client->configure([
47
            'host'    => 'hostdoesnotexiststalleverlol.stupidtld',
48
            'onError' => 'error',
49
        ]);
50
        $handlerInvoked = false;
51
52
        $testCase = $this;
53
54
        set_error_handler(
55
            function ($errno, $errstr) use ($testCase, &$handlerInvoked) {
56
                $handlerInvoked = true;
57
58
                $testCase->assertSame(E_USER_WARNING, $errno);
59
                $testCase->assertSame(
60
                    'StatsD server connection failed (udp://hostdoesnotexiststalleverlol.stupidtld:8125)',
61
                    $errstr
62
                );
63
            },
64
            E_USER_WARNING
65
        );
66
67
        $this->client->increment('test');
68
        restore_error_handler();
69
70
        $this->assertTrue($handlerInvoked);
71
    }
72
73
    public function testCanBeConfiguredToNotThrowOnError()
74
    {
75
        $this->client->configure([
76
            'host'    => 'hostdoesnotexiststalleverlol.stupidtld',
77
            'onError' => 'ignore',
78
        ]);
79
80
        $this->client->increment('test');
81
        $this->assertFalse($this->client->wasSuccessful());
82
    }
83
84
    public function testTimeoutDefaultsToPhpIniDefaultSocketTimeout()
85
    {
86
        $this->assertEquals(ini_get('default_socket_timeout'), $this->client->getConfig()['timeout']);
87
    }
88
}
89