Merge "Replace extract() with explicit variable definitions in DjVuImage"
[lhc/web/wiklou.git] / tests / phpunit / includes / poolcounter / PoolCounterTest.php
1 <?php
2
3 // We will use this class with getMockForAbstractClass to create a concrete mock class.
4 // That call will die if the contructor is not public, unless we use disableOriginalConstructor(),
5 // in which case we could not test the constructor.
6 abstract class PoolCounterAbstractMock extends PoolCounter {
7 public function __construct() {
8 call_user_func_array( 'parent::__construct', func_get_args() );
9 }
10 }
11
12 class PoolCounterTest extends MediaWikiTestCase {
13 public function testConstruct() {
14 $poolCounterConfig = [
15 'class' => 'PoolCounterMock',
16 'timeout' => 10,
17 'workers' => 10,
18 'maxqueue' => 100,
19 ];
20
21 $poolCounter = $this->getMockBuilder( 'PoolCounterAbstractMock' )
22 ->setConstructorArgs( [ $poolCounterConfig, 'testCounter', 'someKey' ] )
23 // don't mock anything - the proper syntax would be setMethods(null), but due
24 // to a PHPUnit bug that does not work with getMockForAbstractClass()
25 ->setMethods( [ 'idontexist' ] )
26 ->getMockForAbstractClass();
27 $this->assertInstanceOf( 'PoolCounter', $poolCounter );
28 }
29
30 public function testConstructWithSlots() {
31 $poolCounterConfig = [
32 'class' => 'PoolCounterMock',
33 'timeout' => 10,
34 'workers' => 10,
35 'slots' => 2,
36 'maxqueue' => 100,
37 ];
38
39 $poolCounter = $this->getMockBuilder( 'PoolCounterAbstractMock' )
40 ->setConstructorArgs( [ $poolCounterConfig, 'testCounter', 'key' ] )
41 ->setMethods( [ 'idontexist' ] ) // don't mock anything
42 ->getMockForAbstractClass();
43 $this->assertInstanceOf( 'PoolCounter', $poolCounter );
44 }
45
46 public function testHashKeyIntoSlots() {
47 $poolCounter = $this->getMockBuilder( 'PoolCounterAbstractMock' )
48 // don't mock anything - the proper syntax would be setMethods(null), but due
49 // to a PHPUnit bug that does not work with getMockForAbstractClass()
50 ->setMethods( [ 'idontexist' ] )
51 ->disableOriginalConstructor()
52 ->getMockForAbstractClass();
53
54 $hashKeyIntoSlots = new ReflectionMethod( $poolCounter, 'hashKeyIntoSlots' );
55 $hashKeyIntoSlots->setAccessible( true );
56
57 $keysWithTwoSlots = $keysWithFiveSlots = [];
58 foreach ( range( 1, 100 ) as $i ) {
59 $keysWithTwoSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 2 );
60 $keysWithFiveSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 5 );
61 }
62
63 $twoSlotKeys = [];
64 for ( $i = 0; $i <= 1; $i++ ) {
65 $twoSlotKeys[] = "test:$i";
66 }
67 $fiveSlotKeys = [];
68 for ( $i = 0; $i <= 4; $i++ ) {
69 $fiveSlotKeys[] = "test:$i";
70 }
71
72 $this->assertArrayEquals( $twoSlotKeys, array_unique( $keysWithTwoSlots ) );
73 $this->assertArrayEquals( $fiveSlotKeys, array_unique( $keysWithFiveSlots ) );
74
75 // make sure it is deterministic
76 $this->assertEquals(
77 $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 ),
78 $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 )
79 );
80 }
81 }