MediaWiki
REL1_24
|
00001 <?php 00002 00003 // We will use this class with getMockForAbstractClass to create a concrete mock class. 00004 // That call will die if the contructor is not public, unless we use disableOriginalConstructor(), 00005 // in which case we could not test the constructor. 00006 abstract class PoolCounterAbstractMock extends PoolCounter { 00007 public function __construct() { 00008 call_user_func_array( 'parent::__construct', func_get_args() ); 00009 } 00010 } 00011 00012 class PoolCounterTest extends MediaWikiTestCase { 00013 public function testConstruct() { 00014 $poolCounterConfig = array( 00015 'class' => 'PoolCounterMock', 00016 'timeout' => 10, 00017 'workers' => 10, 00018 'maxqueue' => 100, 00019 ); 00020 00021 $poolCounter = $this->getMockBuilder( 'PoolCounterAbstractMock' ) 00022 ->setConstructorArgs( array( $poolCounterConfig, 'testCounter', 'someKey' ) ) 00023 // don't mock anything - the proper syntax would be setMethods(null), but due 00024 // to a PHPUnit bug that does not work with getMockForAbstractClass() 00025 ->setMethods( array( 'idontexist' ) ) 00026 ->getMockForAbstractClass(); 00027 $this->assertInstanceOf( 'PoolCounter', $poolCounter ); 00028 } 00029 00030 public function testConstructWithSlots() { 00031 $poolCounterConfig = array( 00032 'class' => 'PoolCounterMock', 00033 'timeout' => 10, 00034 'workers' => 10, 00035 'slots' => 2, 00036 'maxqueue' => 100, 00037 ); 00038 00039 $poolCounter = $this->getMockBuilder( 'PoolCounterAbstractMock' ) 00040 ->setConstructorArgs( array( $poolCounterConfig, 'testCounter', 'key' ) ) 00041 ->setMethods( array( 'idontexist' ) ) // don't mock anything 00042 ->getMockForAbstractClass(); 00043 $this->assertInstanceOf( 'PoolCounter', $poolCounter ); 00044 } 00045 00046 public function testHashKeyIntoSlots() { 00047 $poolCounter = $this->getMockBuilder( 'PoolCounterAbstractMock' ) 00048 // don't mock anything - the proper syntax would be setMethods(null), but due 00049 // to a PHPUnit bug that does not work with getMockForAbstractClass() 00050 ->setMethods( array( 'idontexist' ) ) 00051 ->disableOriginalConstructor() 00052 ->getMockForAbstractClass(); 00053 00054 $hashKeyIntoSlots = new ReflectionMethod( $poolCounter, 'hashKeyIntoSlots' ); 00055 $hashKeyIntoSlots->setAccessible( true ); 00056 00057 $keysWithTwoSlots = $keysWithFiveSlots = array(); 00058 foreach ( range( 1, 100 ) as $i ) { 00059 $keysWithTwoSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'key ' . $i, 2 ); 00060 $keysWithFiveSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'key ' . $i, 5 ); 00061 } 00062 00063 $this->assertArrayEquals( range( 0, 1 ), array_unique( $keysWithTwoSlots ) ); 00064 $this->assertArrayEquals( range( 0, 4 ), array_unique( $keysWithFiveSlots ) ); 00065 00066 // make sure it is deterministic 00067 $this->assertEquals( 00068 $hashKeyIntoSlots->invoke( $poolCounter, 'asdfgh', 1000 ), 00069 $hashKeyIntoSlots->invoke( $poolCounter, 'asdfgh', 1000 ) 00070 ); 00071 } 00072 }