mediawiki-extensions-Echo/tests/phpunit/includes/cache/TitleLocalCacheTest.php

79 lines
2.4 KiB
PHP
Raw Normal View History

<?php
class EchoTitleLocalCacheTest extends MediaWikiTestCase {
public function testCreate() {
$cache = EchoTitleLocalCache::create();
$this->assertInstanceOf( 'EchoTitleLocalCache', $cache );
return $cache;
}
/**
* @depends testCreate
*/
public function testAdd( $cache ) {
$cache->add( 1 );
$this->assertEquals( count( $cache->getLookups() ), 1 );
$this->assertArrayHasKey( 1, $cache->getLookups() );
}
/**
* @depends testCreate
*/
public function testGet( $cache ) {
$object = new \ReflectionObject( $cache );
$targets = $object->getProperty( 'targets' );
$targets->setAccessible( true );
$lruMap = new MapCacheLRU( EchoLocalCache::TARGET_MAX_NUM );
$lruMap->set( 1, $this->mockTitle() );
$targets->setValue( $cache, $lruMap );
$lookups = $object->getProperty( 'lookups' );
$lookups->setAccessible( true );
$lookups->setValue( $cache, array( '1' => '1', '2' => '2' ) );
# A second page in addition to MediaWikiTestPage 'UTPage' since
# TitleLocalCache internally invokes Title::newFromIDs()
$this->insertPage('EchoTitleLocalCacheTest_testGet');
$titles = Title::newFromIDs( array( '1', '2' ) );
$this->assertEquals( 2, count( $titles ), "Must have at least two pages" );
$this->assertEquals( array(1 => '1', 2 => '2' ), $cache->getLookups() );
// MapCacheLRU should treat key 1 same as '1'
$this->assertInstanceOf( 'Title', $cache->get( '1' ) );
$this->assertTrue( count( $cache->getLookups() ) > 0 );
$this->assertInstanceOf( 'Title', $cache->get( 2 ) );
$this->assertTrue( count( $cache->getLookups() ) == 0 );
}
/**
* @depends testCreate
*/
public function testClearAll( $cache ) {
$object = new \ReflectionObject( $cache );
$targets = $object->getProperty( 'targets' );
$targets->setAccessible( true );
$lruMap = new MapCacheLRU( EchoLocalCache::TARGET_MAX_NUM );
$lruMap->set( 1, $this->mockTitle() );
$targets->setValue( $cache, $lruMap );
$lookups = $object->getProperty( 'lookups' );
$lookups->setAccessible( true );
$lookups->setValue( $cache, array( '1' => '1', '2' => '2' ) );
$cache->clearAll();
$this->assertTrue( count( $cache->getLookups() ) == 0 );
$this->assertNull( $cache->getTargets()->get( 1 ) );
$this->assertNull( $cache->getTargets()->get( '1' ) );
}
/**
* Mock object of Title
*/
protected function mockTitle() {
$title = $this->getMockBuilder( 'Title' )
->disableOriginalConstructor()
->getMock();
return $title;
}
}