| 1 |
<?php |
| 2 |
|
| 3 |
namespace Liuggio\StatsdClient; |
| 4 |
|
| 5 |
use Liuggio\StatsdClient\StatsdClient; |
| 6 |
use Liuggio\StatsdClient\Factory\StatsdDataFactory; |
| 7 |
//use Liuggio\StatsdClient\Sender\SocketSender; |
| 8 |
|
| 9 |
|
| 10 |
class ReadmeTest extends \PHPUnit_Framework_TestCase |
| 11 |
{ |
| 12 |
public function testFullUsageWithObject() { |
| 13 |
|
| 14 |
$sender = $this->mockSender(); |
| 15 |
// $sender = new Sender(); |
| 16 |
|
| 17 |
// StatsdClient(SenderInterface $sender, $host = 'udp://localhost', $port = 8126, $reducePacket = true, $fail_silently = true) |
| 18 |
$client = new StatsdClient($sender); |
| 19 |
$factory = new StatsdDataFactory('\Liuggio\StatsdClient\Entity\StatsdData'); |
| 20 |
|
| 21 |
// create the data with the factory |
| 22 |
$data[] = $factory->timing('usageTime', 100); |
| 23 |
$data[] = $factory->increment('visitor'); |
| 24 |
$data[] = $factory->decrement('click'); |
| 25 |
$data[] = $factory->gauge('gaugor', 333); |
| 26 |
$data[] = $factory->set('uniques', 765); |
| 27 |
|
| 28 |
// send the data as array or directly as object |
| 29 |
$client->send($data); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function testFullUsageArray() { |
| 35 |
|
| 36 |
$sender = $this->mockSender(); |
| 37 |
// $sender = new Sender(); |
| 38 |
|
| 39 |
// StatsdClient(SenderInterface $sender, $host = 'localhost', $port = 8126, $protocol='udp', $reducePacket = true, $fail_silently = true) |
| 40 |
$client = new StatsdClient($sender, $host = 'localhost', $port = 8126, 'udp', $reducePacket = true, $fail_silently = true); |
| 41 |
|
| 42 |
$data[] ="increment:1|c"; |
| 43 |
$data[] ="set:value|s"; |
| 44 |
$data[] ="gauge:value|g"; |
| 45 |
$data[] = "timing:10|ms"; |
| 46 |
$data[] = "decrement:-1|c"; |
| 47 |
$data[] ="key:1|c"; |
| 48 |
|
| 49 |
// send the data as array or directly as object |
| 50 |
$client->send($data); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
private function mockSender() { |
| 55 |
$sender = $this->getMock('\Liuggio\StatsdClient\Sender\SenderInterface', array('open', 'write', 'close')); |
| 56 |
$sender->expects($this->once()) |
| 57 |
->method('open') |
| 58 |
->will($this->returnValue(true)); |
| 59 |
|
| 60 |
$sender->expects($this->any()) //If you set the reduce = true into the StatsdClient the write will be called once |
| 61 |
->method('write') |
| 62 |
->will($this->returnCallBack(function($fp, $message) { |
| 63 |
// echo PHP_EOL . "- " . $message; |
| 64 |
})); |
| 65 |
|
| 66 |
$sender->expects($this->once()) |
| 67 |
->method('close') |
| 68 |
->will($this->returnValue(true)); |
| 69 |
|
| 70 |
return $sender; |
| 71 |
} |
| 72 |
} |