Completed
Push — master ( 8d3377...5d9cd9 )
by mw
232:45 queued 197:48
created

BufferedStatsdCollectorTest::testCalcMedian()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Tests\Utils;
4
5
use SMW\Utils\BufferedStatsdCollector;
6
use Onoi\BlobStore\BlobStore;
7
use Onoi\BlobStore\Container;
8
9
/**
10
 * @covers \SMW\Utils\BufferedStatsdCollector
11
 * @group semantic-mediawiki
12
 *
13
 * @license GNU GPL v2+
14
 * @since 2.5
15
 *
16
 * @author mwjames
17
 */
18
class BufferedStatsdCollectorTest extends \PHPUnit_Framework_TestCase {
19
20
	private $blobStore;
21
	private $container;
22
23
	protected function setUp() {
24
25
		$this->container = $this->getMockBuilder( Container::class )
26
			->disableOriginalConstructor()
27
			->getMock();
28
29
		$this->blobStore = $this->getMockBuilder( BlobStore::class )
30
			->disableOriginalConstructor()
31
			->getMock();
32
33
		$this->blobStore->expects( $this->any() )
34
			->method( 'read' )
35
			->will( $this->returnValue( $this->container ) );
36
	}
37
38
	public function testCanConstruct() {
39
40
		$this->assertInstanceOf(
41
			BufferedStatsdCollector::class,
42
			new BufferedStatsdCollector( $this->blobStore, 42 )
43
		);
44
	}
45
46
	public function testIncr() {
47
48
		$this->container->expects( $this->once() )
49
			->method( 'has' )
50
			->will( $this->returnValue( true ) );
51
52
		$this->container->expects( $this->once() )
53
			->method( 'get' )
54
			->will( $this->returnValue( 10 ) );
55
56
		$this->container->expects( $this->once() )
57
			->method( 'set' )
58
			->with(
59
				$this->equalTo( 'Foo.bar' ),
60
				$this->equalTo( 11 ) );
61
62
		$this->blobStore->expects( $this->once() )
63
			->method( 'save' );
64
65
		$instance = new BufferedStatsdCollector(
66
			$this->blobStore,
67
			42
68
		);
69
70
		$instance->incr( 'Foo.bar' );
71
		$instance->saveStats();
72
	}
73
74
	public function testSet() {
75
76
		$this->container->expects( $this->once() )
77
			->method( 'has' )
78
			->will( $this->returnValue( true ) );
79
80
		$this->container->expects( $this->once() )
81
			->method( 'get' )
82
			->will( $this->returnValue( 10 ) );
83
84
		$this->container->expects( $this->once() )
85
			->method( 'set' )
86
			->with(
87
				$this->equalTo( 'Foo.bar' ),
88
				$this->equalTo( 10 ) );
89
90
		$this->blobStore->expects( $this->once() )
91
			->method( 'save' );
92
93
		$instance = new BufferedStatsdCollector(
94
			$this->blobStore,
95
			42
96
		);
97
98
		$instance->set( 'Foo.bar', 10 );
99
		$instance->saveStats();
100
	}
101
102
	public function testCalcMedian() {
103
104
		$this->container->expects( $this->once() )
105
			->method( 'has' )
106
			->will( $this->returnValue( true ) );
107
108
		$this->container->expects( $this->once() )
109
			->method( 'get' )
110
			->will( $this->returnValue( 10 ) );
111
112
		$this->container->expects( $this->once() )
113
			->method( 'set' )
114
			->with(
115
				$this->equalTo( 'Foo.bar' ),
116
				$this->equalTo( 7.5 ) );
117
118
		$this->blobStore->expects( $this->once() )
119
			->method( 'save' );
120
121
		$instance = new BufferedStatsdCollector(
122
			$this->blobStore,
123
			42
124
		);
125
126
		$instance->calcMedian( 'Foo.bar', 5 );
127
		$instance->saveStats();
128
	}
129
130
	public function testStats_Simple() {
131
132
		$this->container->expects( $this->once() )
133
			->method( 'getData' )
134
			->will( $this->returnValue( array( 'Foo' => 1, 'Bar' => 1 ) ) );
135
136
		$expected = array(
137
			'Foo' => 1,
138
			'Bar' => 1
139
		);
140
141
		$instance = new BufferedStatsdCollector(
142
			$this->blobStore,
143
			42
144
		);
145
146
		$this->assertEquals(
147
			$expected,
148
			$instance->getStats()
149
		);
150
	}
151
152
	public function testStats_SimpleHierarchy() {
153
154
		$this->container->expects( $this->once() )
155
			->method( 'getData' )
156
			->will( $this->returnValue( array( 'Foo.foobar' => 1, 'Bar' => 1 ) ) );
157
158
		$expected = array(
159
			'Foo' => array( 'foobar' => 1 ),
160
			'Bar' => 1
161
		);
162
163
		$instance = new BufferedStatsdCollector(
164
			$this->blobStore,
165
			42
166
		);
167
168
		$this->assertEquals(
169
			$expected,
170
			$instance->getStats()
171
		);
172
	}
173
174
	public function testStats_ExtendedHierarchy() {
175
176
		$this->container->expects( $this->once() )
177
			->method( 'getData' )
178
			->will( $this->returnValue( array( 'Foo.foobar' => 5, 'Bar' => 1, 'Foo.foobar.baz' => 1 ) ) );
179
180
		$expected = array(
181
			'Foo' => array( 'foobar' => array( 5, 'baz' => 1 ) ),
182
			'Bar' => 1
183
		);
184
185
		$instance = new BufferedStatsdCollector(
186
			$this->blobStore,
187
			42
188
		);
189
190
		$this->assertEquals(
191
			$expected,
192
			$instance->getStats()
193
		);
194
	}
195
196
}
197