Completed
Pull Request — master (#13)
by Harry
02:43
created

testCanBeConfiguredToNotThrowOnError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of graze/dog-statsd
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/dog-statsd/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/dog-statsd
12
 */
13
14
namespace Graze\DogStatsD\Test\Unit;
15
16
use Graze\DogStatsD\Test\TestCase;
17
18
class ConnectionTest extends TestCase
19
{
20
    /**
21
     * Non-integer ports are not acceptable
22
     *
23
     * @expectedException \Graze\DogStatsD\Exception\ConnectionException
24
     */
25
    public function testInvalidHost()
26
    {
27
        $this->client->configure([
28
            'host' => 'hostdoesnotexiststalleverlol.stupidtld',
29
        ]);
30
        $this->client->increment('test');
31
    }
32
33
    public function testTimeoutSettingIsUsedWhenCreatingSocketIfProvided()
34
    {
35
        $this->client->configure([
36
            'host'    => 'localhost',
37
            'timeout' => 123.425,
38
        ]);
39
        $this->assertAttributeSame(123.425, 'timeout', $this->client);
40
    }
41
42
    public function testCanBeConfiguredToThrowErrors()
43
    {
44
        $this->client->configure([
45
            'host'    => 'hostdoesnotexiststalleverlol.stupidtld',
46
            'onError' => 'error',
47
        ]);
48
        $handlerInvoked = false;
49
50
        $testCase = $this;
51
52
        set_error_handler(
53
            function ($errno, $errstr, $errfile) use ($testCase, &$handlerInvoked) {
0 ignored issues
show
Unused Code introduced by
The parameter $errfile is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
                $handlerInvoked = true;
55
56
                $testCase->assertSame(E_USER_WARNING, $errno);
57
                $testCase->assertSame(
58
                    'StatsD server connection failed (udp://hostdoesnotexiststalleverlol.stupidtld:8125)',
59
                    $errstr
60
                );
61
            },
62
            E_USER_WARNING
63
        );
64
65
        $this->client->increment('test');
66
        restore_error_handler();
67
68
        $this->assertTrue($handlerInvoked);
69
    }
70
71
    public function testCanBeConfiguredToNotThrowOnError()
72
    {
73
        $this->client->configure([
74
            'host'    => 'hostdoesnotexiststalleverlol.stupidtld',
75
            'onError' => 'ignore',
76
        ]);
77
78
        $this->client->increment('test');
79
        $this->assertFalse($this->client->wasSuccessful());
80
    }
81
82
    public function testTimeoutDefaultsToPhpIniDefaultSocketTimeout()
83
    {
84
        $this->assertAttributeEquals(ini_get('default_socket_timeout'), 'timeout', $this->client);
85
    }
86
}
87