Completed
Pull Request — master (#1)
by Harry
03:02
created

testCanBeConfiguredNotToThrowConnectionExceptions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
namespace Graze\DogStatsD\Test\Unit;
4
5
use Graze\DogStatsD\Test\TestCase;
6
7
class ConnectionTest extends TestCase
8
{
9
    /**
10
     * Non-integer ports are not acceptable
11
     *
12
     * @expectedException \Graze\DogStatsD\Exception\ConnectionException
13
     */
14
    public function testInvalidHost()
15
    {
16
        $this->client->configure(array(
17
            'host' => 'hostdoesnotexiststalleverlol.stupidtld',
18
        ));
19
        $this->client->increment('test');
20
    }
21
22
    public function testTimeoutSettingIsUsedWhenCreatingSocketIfProvided()
23
    {
24
        $this->client->configure(array(
25
            'host'    => 'localhost',
26
            'timeout' => 123,
27
        ));
28
        $this->assertAttributeSame(123, 'timeout', $this->client);
29
    }
30
31
    public function testCanBeConfiguredNotToThrowConnectionExceptions()
32
    {
33
        $this->client->configure(array(
34
            'host'            => 'hostdoesnotexiststalleverlol.stupidtld',
35
            'throwExceptions' => false,
36
        ));
37
        $handlerInvoked = false;
38
39
        $testCase = $this;
40
41
        set_error_handler(
42
            function ($errno, $errstr, $errfile, $errline, $errcontext) use ($testCase, &$handlerInvoked) {
0 ignored issues
show
Unused Code introduced by
The parameter $errline 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...
Unused Code introduced by
The parameter $errcontext 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...
43
                $handlerInvoked = true;
44
45
                $testCase->assertSame(E_USER_WARNING, $errno);
46
                $testCase->assertSame(
47
                    'StatsD server connection failed (udp://hostdoesnotexiststalleverlol.stupidtld:8125)',
48
                    $errstr
49
                );
50
                $testCase->assertSame(realpath(__DIR__ . '/../../src/Client.php'), $errfile);
51
            },
52
            E_USER_WARNING
53
        );
54
55
        $this->client->increment('test');
56
        restore_error_handler();
57
58
        $this->assertTrue($handlerInvoked);
59
    }
60
61
    public function testTimeoutDefaultsToPhpIniDefaultSocketTimeout()
62
    {
63
        $this->assertAttributeEquals(ini_get('default_socket_timeout'), 'timeout', $this->client);
64
    }
65
}
66