19baf5a82f6bb2065049f7c4df12bfed2bacb117
6 class PoolCounterTest
extends MediaWikiTestCase
{
7 public function testConstruct() {
9 'class' => 'PoolCounterMock',
15 $poolCounter = $this->getMockBuilder( PoolCounterAbstractMock
::class )
16 ->setConstructorArgs( [ $poolCounterConfig, 'testCounter', 'someKey' ] )
17 // don't mock anything - the proper syntax would be setMethods(null), but due
18 // to a PHPUnit bug that does not work with getMockForAbstractClass()
19 ->setMethods( [ 'idontexist' ] )
20 ->getMockForAbstractClass();
21 $this->assertInstanceOf( PoolCounter
::class, $poolCounter );
24 public function testConstructWithSlots() {
25 $poolCounterConfig = [
26 'class' => 'PoolCounterMock',
33 $poolCounter = $this->getMockBuilder( PoolCounterAbstractMock
::class )
34 ->setConstructorArgs( [ $poolCounterConfig, 'testCounter', 'key' ] )
35 ->setMethods( [ 'idontexist' ] ) // don't mock anything
36 ->getMockForAbstractClass();
37 $this->assertInstanceOf( PoolCounter
::class, $poolCounter );
40 public function testHashKeyIntoSlots() {
41 $poolCounter = $this->getMockBuilder( PoolCounterAbstractMock
::class )
42 // don't mock anything - the proper syntax would be setMethods(null), but due
43 // to a PHPUnit bug that does not work with getMockForAbstractClass()
44 ->setMethods( [ 'idontexist' ] )
45 ->disableOriginalConstructor()
46 ->getMockForAbstractClass();
48 $hashKeyIntoSlots = new ReflectionMethod( $poolCounter, 'hashKeyIntoSlots' );
49 $hashKeyIntoSlots->setAccessible( true );
51 $keysWithTwoSlots = $keysWithFiveSlots = [];
52 foreach ( range( 1, 100 ) as $i ) {
53 $keysWithTwoSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 2 );
54 $keysWithFiveSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 5 );
58 for ( $i = 0; $i <= 1; $i++
) {
59 $twoSlotKeys[] = "test:$i";
62 for ( $i = 0; $i <= 4; $i++
) {
63 $fiveSlotKeys[] = "test:$i";
66 $this->assertArrayEquals( $twoSlotKeys, array_unique( $keysWithTwoSlots ) );
67 $this->assertArrayEquals( $fiveSlotKeys, array_unique( $keysWithFiveSlots ) );
69 // make sure it is deterministic
71 $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 ),
72 $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 )
77 // We will use this class with getMockForAbstractClass to create a concrete mock class.
78 // That call will die if the contructor is not public, unless we use disableOriginalConstructor(),
79 // in which case we could not test the constructor.
80 abstract class PoolCounterAbstractMock
extends PoolCounter
{
81 public function __construct() {
82 call_user_func_array( 'parent::__construct', func_get_args() );