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() );
15 class PoolCounterTest
extends MediaWikiTestCase
{
16 public function testConstruct() {
17 $poolCounterConfig = [
18 'class' => 'PoolCounterMock',
24 $poolCounter = $this->getMockBuilder( 'PoolCounterAbstractMock' )
25 ->setConstructorArgs( [ $poolCounterConfig, 'testCounter', 'someKey' ] )
26 // don't mock anything - the proper syntax would be setMethods(null), but due
27 // to a PHPUnit bug that does not work with getMockForAbstractClass()
28 ->setMethods( [ 'idontexist' ] )
29 ->getMockForAbstractClass();
30 $this->assertInstanceOf( 'PoolCounter', $poolCounter );
33 public function testConstructWithSlots() {
34 $poolCounterConfig = [
35 'class' => 'PoolCounterMock',
42 $poolCounter = $this->getMockBuilder( 'PoolCounterAbstractMock' )
43 ->setConstructorArgs( [ $poolCounterConfig, 'testCounter', 'key' ] )
44 ->setMethods( [ 'idontexist' ] ) // don't mock anything
45 ->getMockForAbstractClass();
46 $this->assertInstanceOf( 'PoolCounter', $poolCounter );
49 public function testHashKeyIntoSlots() {
50 $poolCounter = $this->getMockBuilder( 'PoolCounterAbstractMock' )
51 // don't mock anything - the proper syntax would be setMethods(null), but due
52 // to a PHPUnit bug that does not work with getMockForAbstractClass()
53 ->setMethods( [ 'idontexist' ] )
54 ->disableOriginalConstructor()
55 ->getMockForAbstractClass();
57 $hashKeyIntoSlots = new ReflectionMethod( $poolCounter, 'hashKeyIntoSlots' );
58 $hashKeyIntoSlots->setAccessible( true );
60 $keysWithTwoSlots = $keysWithFiveSlots = [];
61 foreach ( range( 1, 100 ) as $i ) {
62 $keysWithTwoSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 2 );
63 $keysWithFiveSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 5 );
67 for ( $i = 0; $i <= 1; $i++
) {
68 $twoSlotKeys[] = "test:$i";
71 for ( $i = 0; $i <= 4; $i++
) {
72 $fiveSlotKeys[] = "test:$i";
75 $this->assertArrayEquals( $twoSlotKeys, array_unique( $keysWithTwoSlots ) );
76 $this->assertArrayEquals( $fiveSlotKeys, array_unique( $keysWithFiveSlots ) );
78 // make sure it is deterministic
80 $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 ),
81 $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 )