| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\Stats; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use PHPUnit\Framework\TestCase; |
| 7 |
use Wikimedia\Stats\Exceptions\UnsupportedFormatException; |
| 8 |
use Wikimedia\Stats\Metrics\CounterMetric; |
| 9 |
use Wikimedia\Stats\Metrics\GaugeMetric; |
| 10 |
use Wikimedia\Stats\Metrics\NullMetric; |
| 11 |
use Wikimedia\Stats\Metrics\TimingMetric; |
| 12 |
use Wikimedia\Stats\OutputFormats; |
| 13 |
use Wikimedia\Stats\StatsCache; |
| 14 |
use Wikimedia\Stats\StatsFactory; |
| 15 |
use Wikimedia\Stats\StatsUtils; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class StatsFactoryTest extends TestCase { |
| 22 |
|
| 23 |
public function testGetCounter() { |
| 24 |
$m = StatsFactory::newNull(); |
| 25 |
$this->assertInstanceOf( CounterMetric::class, $m->getCounter( 'test' ) ); |
| 26 |
} |
| 27 |
|
| 28 |
public function testGetGauge() { |
| 29 |
$m = StatsFactory::newNull(); |
| 30 |
$this->assertInstanceOf( GaugeMetric::class, $m->getGauge( 'test' ) ); |
| 31 |
} |
| 32 |
|
| 33 |
public function testGetTiming() { |
| 34 |
$m = StatsFactory::newNull(); |
| 35 |
$this->assertInstanceOf( TimingMetric::class, $m->getTiming( 'test' ) ); |
| 36 |
} |
| 37 |
|
| 38 |
public function testUnsupportedOutputFormat() { |
| 39 |
$this->expectException( UnsupportedFormatException::class ); |
| 40 |
OutputFormats::getNewFormatter( 999 ); |
| 41 |
} |
| 42 |
|
| 43 |
public function testEmptyPrefix() { |
| 44 |
$this->expectException( InvalidArgumentException::class ); |
| 45 |
OutputFormats::getNewEmitter( '', new StatsCache, OutputFormats::getNewFormatter( OutputFormats::STATSD ), '' ); |
| 46 |
} |
| 47 |
|
| 48 |
public function testUnsetNameConfig() { |
| 49 |
$m = StatsFactory::newNull(); |
| 50 |
$this->expectException( InvalidArgumentException::class ); |
| 51 |
$m->getCounter( '' ); |
| 52 |
} |
| 53 |
|
| 54 |
public function testNormalizeString() { |
| 55 |
$this->assertEquals( |
| 56 |
'new_metric_and_things', |
| 57 |
StatsUtils::normalizeString( 'new metric @#&^and *-&-*things-*&-*!@#&^%#$' ) |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
public function testNormalizeArray() { |
| 62 |
$this->assertEquals( |
| 63 |
[ 'new_test_metric', 'another_new_test_metric' ], |
| 64 |
StatsUtils::normalizeArray( [ 'new.test|metric', 'another$new-test_metric' ] ) |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
public function testGetNullMetricWithLabelMismatch() { |
| 69 |
$m = StatsFactory::newNull(); |
| 70 |
|
| 71 |
$m->getCounter( 'test_metric' )->setLabel( 'a', 'a' )->increment(); |
| 72 |
|
| 73 |
$metric = @$m->getCounter( 'test_metric' )->setLabel( 'b', 'b' ); |
| 74 |
$this->assertInstanceOf( NullMetric::class, $metric ); |
| 75 |
} |
| 76 |
|
| 77 |
public function testGetNullMetricOnNameCollision() { |
| 78 |
$m = StatsFactory::newNull(); |
| 79 |
|
| 80 |
$m->getCounter( 'test' ); |
| 81 |
|
| 82 |
$metric = @$m->getTiming( 'test' ); |
| 83 |
|
| 84 |
$this->assertInstanceOf( NullMetric::class, $metric ); |
| 85 |
|
| 86 |
$metric->increment(); |
| 87 |
} |
| 88 |
} |