| 1 |
<?php |
| 2 |
|
| 3 |
namespace Liuggio\StatsdClient\Entity; |
| 4 |
|
| 5 |
use Liuggio\StatsdClient\Service\StatsdService; |
| 6 |
|
| 7 |
class StatsdServiceTest extends \PHPUnit_Framework_TestCase |
| 8 |
{ |
| 9 |
private $clientMock; |
| 10 |
private $factoryMock; |
| 11 |
|
| 12 |
public function setUp() |
| 13 |
{ |
| 14 |
$this->clientMock = $this->getMockBuilder('Liuggio\StatsdClient\StatsdClient') |
| 15 |
->disableOriginalConstructor() |
| 16 |
->getMock(); |
| 17 |
$this->factoryMock = $this->getMockBuilder('Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface') |
| 18 |
->disableOriginalConstructor() |
| 19 |
->getMock(); |
| 20 |
} |
| 21 |
|
| 22 |
public function testConstructorWithoutFactory() |
| 23 |
{ |
| 24 |
$dut = new StatsdService($this->clientMock); |
| 25 |
$dut->timing('foo.bar', 123); |
| 26 |
} |
| 27 |
|
| 28 |
public function testFactoryImplementation() |
| 29 |
{ |
| 30 |
$data = new StatsdData(); |
| 31 |
|
| 32 |
// Configure the client mock. |
| 33 |
$this->factoryMock->expects($this->once())->method('timing')->willReturn($data); |
| 34 |
$this->factoryMock->expects($this->once())->method('gauge')->willReturn($data); |
| 35 |
$this->factoryMock->expects($this->once())->method('set')->willReturn($data); |
| 36 |
$this->factoryMock->expects($this->once())->method('increment')->willReturn($data); |
| 37 |
$this->factoryMock->expects($this->once())->method('decrement')->willReturn($data); |
| 38 |
$this->factoryMock->expects($this->once())->method('updateCount')->willReturn($data); |
| 39 |
|
| 40 |
// Actual test |
| 41 |
$dut = new StatsdService($this->clientMock, $this->factoryMock); |
| 42 |
$dut->timing('foo.bar', 123); |
| 43 |
$dut->gauge('foo.bar', 123); |
| 44 |
$dut->set('foo.bar', 123); |
| 45 |
$dut->increment('foo.bar'); |
| 46 |
$dut->decrement('foo.bar'); |
| 47 |
$dut->updateCount('foo.bar', 123); |
| 48 |
} |
| 49 |
|
| 50 |
public function testFlush() |
| 51 |
{ |
| 52 |
$data = new StatsdData(); |
| 53 |
$this->factoryMock->expects($this->once())->method('timing')->willReturn($data); |
| 54 |
$this->clientMock->expects($this->once())->method('send') |
| 55 |
->with($this->equalTo(array($data))); |
| 56 |
|
| 57 |
// Actual test |
| 58 |
$dut = new StatsdService($this->clientMock, $this->factoryMock); |
| 59 |
$dut->timing('foobar', 123); |
| 60 |
$dut->flush(); |
| 61 |
} |
| 62 |
|
| 63 |
public function testSampling() |
| 64 |
{ |
| 65 |
$tries = false; |
| 66 |
$closure = function($a, $b) use (&$tries) { |
| 67 |
$tries = !$tries; |
| 68 |
return $tries ? 1 : 0; |
| 69 |
}; |
| 70 |
|
| 71 |
$data = new StatsdData(); |
| 72 |
$this->factoryMock->expects($this->exactly(2))->method('timing')->willReturn($data); |
| 73 |
$this->clientMock->expects($this->once())->method('send') |
| 74 |
->with($this->equalTo(array($data))); |
| 75 |
|
| 76 |
// Actual test |
| 77 |
$dut = new StatsdService($this->clientMock, $this->factoryMock); |
| 78 |
$dut->setSamplingRate(0.1); |
| 79 |
$dut->setSamplingFunction($closure); |
| 80 |
$dut->timing('foo', 123); |
| 81 |
$dut->timing('bar', 123); |
| 82 |
$dut->flush(); |
| 83 |
|
| 84 |
$this->assertSame(0.1, $data->getSampleRate()); |
| 85 |
} |
| 86 |
} |