The default will remain PHPUnit 4.x due to PHP 5.5 support. But, we should allow developers to run tests with newer PHPUnit versions which are noticably faster (especially for code coverage reports). * <https://github.com/sebastianbergmann/phpunit/wiki/Release-Announcement-for-PHPUnit-5.4.0> PHPUnit 5 deprecates the getMock() shortcut for getMockBuilder()->getMock(). It instead introduces the shortcut createMock() which has better defaults than getMockBuilder(). For example, it sets 'disableArgumentCloning' and other things by default. Going forward, code should either use getMockBuilder directly and configure it using the setter methods (instead of the confusing variadic arguments of getMock) or simply use the new minimalistic createMock method. This patch backports the createMock method to MediaWikiTestCase so that we can start using it. Change-Id: I091c0289b21d2b1c876adba89529dc3e72b99af2
67 lines
2 KiB
PHP
67 lines
2 KiB
PHP
<?php
|
|
|
|
use Liuggio\StatsdClient\Entity\StatsdData;
|
|
use Liuggio\StatsdClient\Sender\SenderInterface;
|
|
|
|
class SamplingStatsdClientTest extends PHPUnit_Framework_TestCase {
|
|
/**
|
|
* @dataProvider samplingDataProvider
|
|
*/
|
|
public function testSampling( $data, $sampleRate, $seed, $expectWrite ) {
|
|
$sender = $this->getMockBuilder( SenderInterface::class )->getMock();
|
|
$sender->expects( $this->any() )->method( 'open' )->will( $this->returnValue( true ) );
|
|
if ( $expectWrite ) {
|
|
$sender->expects( $this->once() )->method( 'write' )
|
|
->with( $this->anything(), $this->equalTo( $data ) );
|
|
} else {
|
|
$sender->expects( $this->never() )->method( 'write' );
|
|
}
|
|
mt_srand( $seed );
|
|
$client = new SamplingStatsdClient( $sender );
|
|
$client->send( $data, $sampleRate );
|
|
}
|
|
|
|
public function samplingDataProvider() {
|
|
$unsampled = new StatsdData();
|
|
$unsampled->setKey( 'foo' );
|
|
$unsampled->setValue( 1 );
|
|
|
|
$sampled = new StatsdData();
|
|
$sampled->setKey( 'foo' );
|
|
$sampled->setValue( 1 );
|
|
$sampled->setSampleRate( '0.1' );
|
|
|
|
return [
|
|
// $data, $sampleRate, $seed, $expectWrite
|
|
[ $unsampled, 1, 0 /*0.44*/, true ],
|
|
[ $sampled, 1, 0 /*0.44*/, false ],
|
|
[ $sampled, 1, 4 /*0.03*/, true ],
|
|
[ $unsampled, 0.1, 0 /*0.44*/, false ],
|
|
[ $sampled, 0.5, 0 /*0.44*/, false ],
|
|
[ $sampled, 0.5, 4 /*0.03*/, false ],
|
|
];
|
|
}
|
|
|
|
public function testSetSamplingRates() {
|
|
$matching = new StatsdData();
|
|
$matching->setKey( 'foo.bar' );
|
|
$matching->setValue( 1 );
|
|
|
|
$nonMatching = new StatsdData();
|
|
$nonMatching->setKey( 'oof.bar' );
|
|
$nonMatching->setValue( 1 );
|
|
|
|
$sender = $this->getMockBuilder( SenderInterface::class )->getMock();
|
|
$sender->expects( $this->any() )->method( 'open' )->will( $this->returnValue( true ) );
|
|
$sender->expects( $this->once() )->method( 'write' )->with( $this->anything(),
|
|
$this->equalTo( $nonMatching ) );
|
|
|
|
$client = new SamplingStatsdClient( $sender );
|
|
$client->setSamplingRates( [ 'foo.*' => 0.2 ] );
|
|
|
|
mt_srand( 0 ); // next random is 0.44
|
|
$client->send( $matching );
|
|
mt_srand( 0 );
|
|
$client->send( $nonMatching );
|
|
}
|
|
}
|