build: Updating mediawiki/mediawiki-codesniffer to 16.0.0
[lhc/web/wiklou.git] / tests / phpunit / maintenance / BenchmarkerTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Maintenance;
4
5 use Benchmarker;
6 use MediaWikiCoversValidator;
7 use Wikimedia\TestingAccessWrapper;
8
9 /**
10 * @covers Benchmarker
11 */
12 class BenchmarkerTest extends \PHPUnit\Framework\TestCase {
13
14 use MediaWikiCoversValidator;
15
16 public function testBenchSimple() {
17 $bench = $this->getMockBuilder( Benchmarker::class )
18 ->setMethods( [ 'execute', 'output' ] )
19 ->getMock();
20 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
21 $benchProxy->defaultCount = 3;
22
23 $count = 0;
24 $bench->bench( [
25 'test' => function () use ( &$count ) {
26 $count++;
27 }
28 ] );
29
30 $this->assertSame( 3, $count );
31 }
32
33 public function testBenchSetup() {
34 $bench = $this->getMockBuilder( Benchmarker::class )
35 ->setMethods( [ 'execute', 'output' ] )
36 ->getMock();
37 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
38 $benchProxy->defaultCount = 2;
39
40 $buffer = [];
41 $bench->bench( [
42 'test' => [
43 'setup' => function () use ( &$buffer ) {
44 $buffer[] = 'setup';
45 },
46 'function' => function () use ( &$buffer ) {
47 $buffer[] = 'run';
48 }
49 ]
50 ] );
51
52 $this->assertSame( [ 'setup', 'run', 'run' ], $buffer );
53 }
54
55 public function testBenchVerbose() {
56 $bench = $this->getMockBuilder( Benchmarker::class )
57 ->setMethods( [ 'execute', 'output', 'hasOption', 'verboseRun' ] )
58 ->getMock();
59 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
60 $benchProxy->defaultCount = 1;
61
62 $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
63 ->will( $this->returnValueMap( [
64 [ 'verbose', true ],
65 [ 'count', false ],
66 ] ) );
67
68 $bench->expects( $this->once() )->method( 'verboseRun' )
69 ->with( 0 )
70 ->willReturn( null );
71
72 $bench->bench( [
73 'test' => function () {
74 }
75 ] );
76 }
77
78 public function noop() {
79 }
80
81 public function testBenchName_method() {
82 $bench = $this->getMockBuilder( Benchmarker::class )
83 ->setMethods( [ 'execute', 'output', 'addResult' ] )
84 ->getMock();
85 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
86 $benchProxy->defaultCount = 1;
87
88 $bench->expects( $this->once() )->method( 'addResult' )
89 ->with( $this->callback( function ( $res ) {
90 return isset( $res['name'] ) && $res['name'] === __CLASS__ . '::noop()';
91 } ) );
92
93 $bench->bench( [
94 [ 'function' => [ $this, 'noop' ] ]
95 ] );
96 }
97
98 public function testBenchName_string() {
99 $bench = $this->getMockBuilder( Benchmarker::class )
100 ->setMethods( [ 'execute', 'output', 'addResult' ] )
101 ->getMock();
102 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
103 $benchProxy->defaultCount = 1;
104
105 $bench->expects( $this->once() )->method( 'addResult' )
106 ->with( $this->callback( function ( $res ) {
107 return 'strtolower(A)';
108 } ) );
109
110 $bench->bench( [ [
111 'function' => 'strtolower',
112 'args' => [ 'A' ],
113 ] ] );
114 }
115
116 /**
117 * @covers Benchmarker::verboseRun
118 */
119 public function testVerboseRun() {
120 $bench = $this->getMockBuilder( Benchmarker::class )
121 ->setMethods( [ 'execute', 'output', 'hasOption', 'startBench', 'addResult' ] )
122 ->getMock();
123 $benchProxy = TestingAccessWrapper::newFromObject( $bench );
124 $benchProxy->defaultCount = 1;
125
126 $bench->expects( $this->exactly( 2 ) )->method( 'hasOption' )
127 ->will( $this->returnValueMap( [
128 [ 'verbose', true ],
129 [ 'count', false ],
130 ] ) );
131
132 $bench->expects( $this->once() )->method( 'output' )
133 ->with( $this->callback( function ( $out ) {
134 return preg_match( '/memory.+ peak/', $out ) === 1;
135 } ) );
136
137 $bench->bench( [
138 'test' => function () {
139 }
140 ] );
141 }
142 }