ClientTest::testDestruction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 22
rs 9.7998
c 0
b 0
f 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\Client;
17
use Graze\DogStatsD\Stream\StreamWriter;
18
use Graze\DogStatsD\Test\TestCase;
19
use ReflectionProperty;
20
21
class ClientTest extends TestCase
22
{
23
    public function testNewInstance()
24
    {
25
        $client = new Client();
26
        $this->assertTrue($client instanceof Client);
27
        $this->assertRegExp('/^DogStatsD\\\Client::\[[a-zA-Z0-9]+\]$/', (String) $client);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertRegExp() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/4086 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

27
        /** @scrutinizer ignore-deprecated */ $this->assertRegExp('/^DogStatsD\\\Client::\[[a-zA-Z0-9]+\]$/', (String) $client);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
28
    }
29
30
    public function testStaticInstance()
31
    {
32
        $client1 = Client::instance('instance1');
33
        $this->assertTrue($client1 instanceof Client);
34
        $client2 = Client::instance('instance2');
35
        $client3 = Client::instance('instance1');
36
        $this->assertEquals('DogStatsD\Client::[instance2]', (String) $client2);
37
        $this->assertFalse((String) $client1 === (String) $client2);
38
        $this->assertTrue((String) $client1 === (String) $client3);
39
    }
40
41
    public function testDestruction()
42
    {
43
        $client = new Client();
44
        $client->configure([]);
45
        $client->increment('test', 1);
46
47
        // get the stream
48
        $reflector = new ReflectionProperty(Client::class, 'stream');
49
        $reflector->setAccessible(true);
50
        $stream = $reflector->getValue($client);
51
52
        // get the socket
53
        $reflector = new ReflectionProperty(StreamWriter::class, 'socket');
54
        $reflector->setAccessible(true);
55
        $socket = $reflector->getValue($stream);
56
57
        $stream = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $stream is dead and can be removed.
Loading history...
58
        $this->assertTrue(is_resource($socket));
59
        $client = null;
60
61
        $this->assertNull($client);
62
        $this->assertFalse(is_resource($socket));
63
    }
64
65
    public function testRemovalOfStaticInstance()
66
    {
67
        $client = Client::instance('first');
68
        $client->configure([]);
69
        $client->increment('test', 1);
70
71
        // get the stream
72
        $reflector = new ReflectionProperty(Client::class, 'stream');
73
        $reflector->setAccessible(true);
74
        $stream = $reflector->getValue($client);
75
76
        // get the socket
77
        $reflector = new ReflectionProperty(StreamWriter::class, 'socket');
78
        $reflector->setAccessible(true);
79
        $socket = $reflector->getValue($stream);
80
81
        $stream = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $stream is dead and can be removed.
Loading history...
82
        $this->assertTrue(is_resource($socket));
83
        $client = null;
84
85
        $this->assertNull($client);
86
        $this->assertTrue(is_resource($socket));
87
88
        $this->assertTrue(Client::deleteInstance('first'));
89
        $this->assertFalse(is_resource($socket));
90
    }
91
92
    public function testDeleteInstanceOfNonExistantInstanceReturnsFalse()
93
    {
94
        $this->assertFalse(Client::deleteInstance('nope'));
95
    }
96
97
    public function testDefaultInstances()
98
    {
99
        $client1 = Client::instance();
100
        $this->assertTrue(Client::deleteInstance());
101
        $this->assertFalse(Client::deleteInstance());
102
        $client2 = Client::instance();
103
        $this->assertNotSame($client1, $client2);
104
    }
105
}
106