HHVM does not support variadic arguments with type hints. This is mostly not a big problem, because we can just drop the type hint, but for some reason PHPUnit adds a type hint of "array" when it creates mocks, so a class with a variadic method can't be mocked (at least in some cases). As such, I left alone all the classes that seem like someone might like to mock them, like Title and User. If anyone wants to mock them in the future, they'll have to switch back to func_get_args(). Some of the changes are definitely safe, like functions and test classes. In most cases, func_get_args() (and/or func_get_arg(), func_num_args() ) were only present because the code was written before we required PHP 5.6, and writing them as variadic functions is strictly superior. In some cases I left them alone, aside from HHVM compatibility: * Forwarding all arguments to another function. It's useful to keep func_get_args() here where we want to keep the list of expected arguments and their meanings in the function signature line for documentation purposes, but don't want to copy-paste a long line of argument names. * Handling deprecated calling conventions. * One or two miscellaneous cases where we're basically using the arguments individually but want to use them as an array as well for some reason. Change-Id: I066ec95a7beb7c0665146195a08e7cce1222c788
84 lines
2.8 KiB
PHP
84 lines
2.8 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @covers PoolCounter
|
|
*/
|
|
class PoolCounterTest extends MediaWikiTestCase {
|
|
public function testConstruct() {
|
|
$poolCounterConfig = [
|
|
'class' => 'PoolCounterMock',
|
|
'timeout' => 10,
|
|
'workers' => 10,
|
|
'maxqueue' => 100,
|
|
];
|
|
|
|
$poolCounter = $this->getMockBuilder( PoolCounterAbstractMock::class )
|
|
->setConstructorArgs( [ $poolCounterConfig, 'testCounter', 'someKey' ] )
|
|
// don't mock anything - the proper syntax would be setMethods(null), but due
|
|
// to a PHPUnit bug that does not work with getMockForAbstractClass()
|
|
->setMethods( [ 'idontexist' ] )
|
|
->getMockForAbstractClass();
|
|
$this->assertInstanceOf( PoolCounter::class, $poolCounter );
|
|
}
|
|
|
|
public function testConstructWithSlots() {
|
|
$poolCounterConfig = [
|
|
'class' => 'PoolCounterMock',
|
|
'timeout' => 10,
|
|
'workers' => 10,
|
|
'slots' => 2,
|
|
'maxqueue' => 100,
|
|
];
|
|
|
|
$poolCounter = $this->getMockBuilder( PoolCounterAbstractMock::class )
|
|
->setConstructorArgs( [ $poolCounterConfig, 'testCounter', 'key' ] )
|
|
->setMethods( [ 'idontexist' ] ) // don't mock anything
|
|
->getMockForAbstractClass();
|
|
$this->assertInstanceOf( PoolCounter::class, $poolCounter );
|
|
}
|
|
|
|
public function testHashKeyIntoSlots() {
|
|
$poolCounter = $this->getMockBuilder( PoolCounterAbstractMock::class )
|
|
// don't mock anything - the proper syntax would be setMethods(null), but due
|
|
// to a PHPUnit bug that does not work with getMockForAbstractClass()
|
|
->setMethods( [ 'idontexist' ] )
|
|
->disableOriginalConstructor()
|
|
->getMockForAbstractClass();
|
|
|
|
$hashKeyIntoSlots = new ReflectionMethod( $poolCounter, 'hashKeyIntoSlots' );
|
|
$hashKeyIntoSlots->setAccessible( true );
|
|
|
|
$keysWithTwoSlots = $keysWithFiveSlots = [];
|
|
foreach ( range( 1, 100 ) as $i ) {
|
|
$keysWithTwoSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 2 );
|
|
$keysWithFiveSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 5 );
|
|
}
|
|
|
|
$twoSlotKeys = [];
|
|
for ( $i = 0; $i <= 1; $i++ ) {
|
|
$twoSlotKeys[] = "test:$i";
|
|
}
|
|
$fiveSlotKeys = [];
|
|
for ( $i = 0; $i <= 4; $i++ ) {
|
|
$fiveSlotKeys[] = "test:$i";
|
|
}
|
|
|
|
$this->assertArrayEquals( $twoSlotKeys, array_unique( $keysWithTwoSlots ) );
|
|
$this->assertArrayEquals( $fiveSlotKeys, array_unique( $keysWithFiveSlots ) );
|
|
|
|
// make sure it is deterministic
|
|
$this->assertEquals(
|
|
$hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 ),
|
|
$hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 )
|
|
);
|
|
}
|
|
}
|
|
|
|
// We will use this class with getMockForAbstractClass to create a concrete mock class.
|
|
// That call will die if the contructor is not public, unless we use disableOriginalConstructor(),
|
|
// in which case we could not test the constructor.
|
|
abstract class PoolCounterAbstractMock extends PoolCounter {
|
|
public function __construct( ...$args ) {
|
|
call_user_func_array( 'parent::__construct', $args );
|
|
}
|
|
}
|