summaryrefslogtreecommitdiff
path: root/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/Service/StatsdServiceTest.php
blob: dcf2710dda8dc85e1ae4faf1380ae9fa25b26e00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

namespace Liuggio\StatsdClient\Entity;

use Liuggio\StatsdClient\Service\StatsdService;

class StatsdServiceTest extends \PHPUnit_Framework_TestCase
{
    private $clientMock;
    private $factoryMock;

    public function setUp()
    {
        $this->clientMock = $this->getMockBuilder('Liuggio\StatsdClient\StatsdClient')
            ->disableOriginalConstructor()
            ->getMock();
        $this->factoryMock = $this->getMockBuilder('Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface')
            ->disableOriginalConstructor()
            ->getMock();
    }

    public function testConstructorWithoutFactory()
    {
        $dut = new StatsdService($this->clientMock);
        $dut->timing('foo.bar', 123);
    }

    public function testFactoryImplementation()
    {
        $data = new StatsdData();

        // Configure the client mock.
        $this->factoryMock->expects($this->once())->method('timing')->willReturn($data);
        $this->factoryMock->expects($this->once())->method('gauge')->willReturn($data);
        $this->factoryMock->expects($this->once())->method('set')->willReturn($data);
        $this->factoryMock->expects($this->once())->method('increment')->willReturn($data);
        $this->factoryMock->expects($this->once())->method('decrement')->willReturn($data);
        $this->factoryMock->expects($this->once())->method('updateCount')->willReturn($data);

        // Actual test
        $dut = new StatsdService($this->clientMock, $this->factoryMock);
        $dut->timing('foo.bar', 123);
        $dut->gauge('foo.bar', 123);
        $dut->set('foo.bar', 123);
        $dut->increment('foo.bar');
        $dut->decrement('foo.bar');
        $dut->updateCount('foo.bar', 123);
    }

    public function testFlush()
    {
        $data = new StatsdData();
        $this->factoryMock->expects($this->once())->method('timing')->willReturn($data);
        $this->clientMock->expects($this->once())->method('send')
            ->with($this->equalTo(array($data)));

        // Actual test
        $dut = new StatsdService($this->clientMock, $this->factoryMock);
        $dut->timing('foobar', 123);
        $dut->flush();
    }

    public function testSampling()
    {
        $tries = false;
        $closure = function($a, $b) use (&$tries) {
            $tries = !$tries;
            return $tries ? 1 : 0;
        };

        $data = new StatsdData();
        $this->factoryMock->expects($this->exactly(2))->method('timing')->willReturn($data);
        $this->clientMock->expects($this->once())->method('send')
            ->with($this->equalTo(array($data)));

        // Actual test
        $dut = new StatsdService($this->clientMock, $this->factoryMock);
        $dut->setSamplingRate(0.1);
        $dut->setSamplingFunction($closure);
        $dut->timing('foo', 123);
        $dut->timing('bar', 123);
        $dut->flush();

        $this->assertSame(0.1, $data->getSampleRate());
    }
}