File: StatsFactoryTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (88 lines) | stat: -rw-r--r-- 2,759 bytes parent folder | download
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php

namespace Wikimedia\Tests\Stats;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Wikimedia\Stats\Exceptions\UnsupportedFormatException;
use Wikimedia\Stats\Metrics\CounterMetric;
use Wikimedia\Stats\Metrics\GaugeMetric;
use Wikimedia\Stats\Metrics\NullMetric;
use Wikimedia\Stats\Metrics\TimingMetric;
use Wikimedia\Stats\OutputFormats;
use Wikimedia\Stats\StatsCache;
use Wikimedia\Stats\StatsFactory;
use Wikimedia\Stats\StatsUtils;

/**
 * @covers \Wikimedia\Stats\StatsFactory
 * @covers \Wikimedia\Stats\StatsUtils
 */
class StatsFactoryTest extends TestCase {

	public function testGetCounter() {
		$m = StatsFactory::newNull();
		$this->assertInstanceOf( CounterMetric::class, $m->getCounter( 'test' ) );
	}

	public function testGetGauge() {
		$m = StatsFactory::newNull();
		$this->assertInstanceOf( GaugeMetric::class, $m->getGauge( 'test' ) );
	}

	public function testGetTiming() {
		$m = StatsFactory::newNull();
		$this->assertInstanceOf( TimingMetric::class, $m->getTiming( 'test' ) );
	}

	public function testUnsupportedOutputFormat() {
		$this->expectException( UnsupportedFormatException::class );
		OutputFormats::getNewFormatter( 999 );
	}

	public function testEmptyPrefix() {
		$this->expectException( InvalidArgumentException::class );
		OutputFormats::getNewEmitter( '', new StatsCache, OutputFormats::getNewFormatter( OutputFormats::STATSD ), '' );
	}

	public function testUnsetNameConfig() {
		$m = StatsFactory::newNull();
		$this->expectException( InvalidArgumentException::class );
		$m->getCounter( '' );
	}

	public function testNormalizeString() {
		$this->assertEquals(
			'new_metric_and_things',
			StatsUtils::normalizeString( 'new metric  @#&^and *-&-*things-*&-*!@#&^%#$' )
		);
	}

	public function testNormalizeArray() {
		$this->assertEquals(
			[ 'new_test_metric', 'another_new_test_metric' ],
			StatsUtils::normalizeArray( [ 'new.test|metric', 'another$new-test_metric' ] )
		);
	}

	public function testGetNullMetricWithLabelMismatch() {
		$m = StatsFactory::newNull();
		// initialize a counter and add a sample
		$m->getCounter( 'test_metric' )->setLabel( 'a', 'a' )->increment();
		// get the same counter and attempt to add a different label key
		$metric = @$m->getCounter( 'test_metric' )->setLabel( 'b', 'b' );
		$this->assertInstanceOf( NullMetric::class, $metric );
	}

	public function testGetNullMetricOnNameCollision() {
		$m = StatsFactory::newNull();
		// define metric as counter 'test'
		$m->getCounter( 'test' );
		// redefine metric as timing 'test'
		$metric = @$m->getTiming( 'test' );
		// gauge response must be null metric
		$this->assertInstanceOf( NullMetric::class, $metric );
		// NullMetric should not throw for any method call
		$metric->increment();
	}
}