summaryrefslogtreecommitdiff
path: root/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/ReadmeTest.php
blob: 4608a601636177fc87fb2a4e6c848c14c88fb63d (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
<?php

namespace Liuggio\StatsdClient;

use Liuggio\StatsdClient\StatsdClient;
use Liuggio\StatsdClient\Factory\StatsdDataFactory;
//use Liuggio\StatsdClient\Sender\SocketSender;


class ReadmeTest extends \PHPUnit_Framework_TestCase
{
    public function testFullUsageWithObject() {

        $sender = $this->mockSender();
        // $sender = new Sender();

        // StatsdClient(SenderInterface $sender, $host = 'udp://localhost', $port = 8126, $reducePacket = true, $fail_silently = true)
        $client = new StatsdClient($sender);
        $factory = new StatsdDataFactory('\Liuggio\StatsdClient\Entity\StatsdData');

        // create the data with the factory
        $data[] = $factory->timing('usageTime', 100);
        $data[] = $factory->increment('visitor');
        $data[] = $factory->decrement('click');
        $data[] = $factory->gauge('gaugor', 333);
        $data[] = $factory->set('uniques', 765);

        // send the data as array or directly as object
        $client->send($data);
    }



    public function testFullUsageArray() {
        
        $sender = $this->mockSender();
        // $sender = new Sender();

        // StatsdClient(SenderInterface $sender, $host = 'localhost', $port = 8126, $protocol='udp', $reducePacket = true, $fail_silently = true)
        $client = new StatsdClient($sender, $host = 'localhost', $port = 8126, 'udp', $reducePacket = true, $fail_silently = true);
 
        $data[] ="increment:1|c";
        $data[] ="set:value|s";
        $data[] ="gauge:value|g";
        $data[] = "timing:10|ms";
        $data[] = "decrement:-1|c";
        $data[] ="key:1|c";         

        // send the data as array or directly as object
        $client->send($data);
    }


    private function mockSender() {
        $sender =  $this->getMock('\Liuggio\StatsdClient\Sender\SenderInterface', array('open', 'write', 'close'));
        $sender->expects($this->once())
            ->method('open')
            ->will($this->returnValue(true));

        $sender->expects($this->any())  //If you set the reduce = true into the StatsdClient the write will be called once
            ->method('write')
            ->will($this->returnCallBack(function($fp, $message) {
             //  echo PHP_EOL . "- " . $message;
        }));

        $sender->expects($this->once())
            ->method('close')
            ->will($this->returnValue(true));

        return $sender;
    }
}