public function testGetDataItemForCachedId()
 {
     $connection = $this->getMockBuilder('\\SMW\\MediaWiki\\Database')->disableOriginalConstructor()->getMock();
     $connection->expects($this->never())->method('selectRow');
     $cache = new FixedInMemoryCache();
     $cache->save(42, 'Foo#0##');
     $instance = new DataItemByIdFinder($connection, 'foo', $cache);
     $this->assertInstanceOf('\\SMW\\DIWikiPage', $instance->getDataItemForId(42));
     $stats = $cache->getStats();
     $this->assertEquals(0, $stats['misses']);
     $this->assertEquals(1, $stats['hits']);
 }
 public function testFindRedirectIdForNonCachedRedirect()
 {
     $row = new \stdClass();
     $row->o_id = 42;
     $connection = $this->getMockBuilder('\\SMW\\MediaWiki\\Database')->disableOriginalConstructor()->getMock();
     $connection->expects($this->once())->method('selectRow')->with($this->anything(), $this->anything(), $this->equalTo(array('s_title' => 'Foo', 's_namespace' => 0)))->will($this->returnValue($row));
     $cache = new FixedInMemoryCache();
     $instance = new RedirectInfoStore($connection, $cache);
     $this->assertEquals(42, $instance->findRedirectIdFor('Foo', 0));
     $stats = $cache->getStats();
     $this->assertEquals(0, $stats['hits']);
     $instance->findRedirectIdFor('Foo', 0);
     $stats = $cache->getStats();
     $this->assertEquals(1, $stats['hits']);
 }
 public function testLeastRecentlyUsedShiftForLimitedCacheSize()
 {
     $instance = new FixedInMemoryCache(5);
     $instance->save('berlin', array('berlin'));
     $this->assertEquals(array('berlin'), $instance->fetch('berlin'));
     foreach (array('paris', 'london', '東京', '北京', 'new york') as $city) {
         $instance->save($city, array($city));
     }
     // 'paris' was added and removes 'berlin' from the cache
     $this->assertFalse($instance->fetch('berlin'));
     $stats = $instance->getStats();
     $this->assertEquals(5, $stats['count']);
     // 'paris' moves to the top (last postion as most recently used) and
     // 'london' becomes the next LRU candidate
     $this->assertEquals(array('paris'), $instance->fetch('paris'));
     $instance->save('rio', 'rio');
     $this->assertFalse($instance->fetch('london'));
     // 東京 would be the next LRU slot but setting it again will move it to MRU
     // and push 北京 into the next LRU position
     $instance->save('東京', '東京');
     $instance->save('sidney', 'sidney');
     $this->assertFalse($instance->fetch('北京'));
     $stats = $instance->getStats();
     $this->assertEquals(5, $stats['count']);
 }