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() );
12 class PoolCounterTest
extends MediaWikiTestCase
{
13 public function testConstruct() {
14 $poolCounterConfig = [
15 'class' => 'PoolCounterMock',
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 );
30 public function testConstructWithSlots() {
31 $poolCounterConfig = [
32 'class' => 'PoolCounterMock',
39 $poolCounter = $this->getMockBuilder( 'PoolCounterAbstractMock' )
40 ->setConstructorArgs( [ $poolCounterConfig, 'testCounter', 'key' ] )
41 ->setMethods( [ 'idontexist' ] ) // don't mock anything
42 ->getMockForAbstractClass();
43 $this->assertInstanceOf( 'PoolCounter', $poolCounter );
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();
54 $hashKeyIntoSlots = new ReflectionMethod( $poolCounter, 'hashKeyIntoSlots' );
55 $hashKeyIntoSlots->setAccessible( true );
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 );
64 for ( $i = 0; $i <= 1; $i++
) {
65 $twoSlotKeys[] = "test:$i";
68 for ( $i = 0; $i <= 4; $i++
) {
69 $fiveSlotKeys[] = "test:$i";
72 $this->assertArrayEquals( $twoSlotKeys, array_unique( $keysWithTwoSlots ) );
73 $this->assertArrayEquals( $fiveSlotKeys, array_unique( $keysWithFiveSlots ) );
75 // make sure it is deterministic
77 $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 ),
78 $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 )