Debian Codegraph @

extensions/CategoryTree/tests/phpunit/CategoryCacheTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Extension\CategoryTree\Tests;
22
23 use MediaWiki\Category\Category;
24 use MediaWiki\Extension\CategoryTree\CategoryCache;
25 use MediaWiki\Title\TitleValue;
26 use MediaWikiIntegrationTestCase;
27 use Wikimedia\Rdbms\IConnectionProvider;
28 use Wikimedia\TestingAccessWrapper;
29
30 /**
31 * @group Database
32 * @covers \MediaWiki\Extension\CategoryTree\CategoryCache
33 */
34 class CategoryCacheTest extends MediaWikiIntegrationTestCase {
35
36 public function testConstruct() {
37 new CategoryCache(
38 $this->createMock( IConnectionProvider::class )
39 );
40 $this->addToAssertionCount( 1 );
41 }
42
43 public function testDoQuery() {
44 // Create a row in the category table
45 $this->editPage(
46 new TitleValue( NS_MAIN, 'CategoryTreeCategoryCacheTest' ),
47 '[[Category:Exists]]'
48 );
49
50 $categoryCache = TestingAccessWrapper::newFromObject(
51 $this->getServiceContainer()->get( 'CategoryTree.CategoryCache' )
52 );
53
54 $categoryCache->doQuery( [
55 new TitleValue( NS_CATEGORY, 'Exists' ),
56 new TitleValue( NS_CATEGORY, 'Missed' ),
57 ] );
58
59 $this->assertCount( 2, $categoryCache->cache );
60 $this->assertArrayHasKey( 'Exists', $categoryCache->cache );
61 $this->assertInstanceOf( Category::class, $categoryCache->cache['Exists'] );
62 $this->assertArrayHasKey( 'Missed', $categoryCache->cache );
63 $this->assertNull( $categoryCache->cache['Missed'] );
64 }
65
66 public function testGetCategory() {
67 // Create a row in the category table
68 $this->editPage(
69 new TitleValue( NS_MAIN, 'CategoryTreeCategoryCacheTest' ),
70 '[[Category:Exists]]'
71 );
72
73 $categoryCache = TestingAccessWrapper::newFromObject(
74 $this->getServiceContainer()->get( 'CategoryTree.CategoryCache' )
75 );
76 $title = new TitleValue( NS_CATEGORY, 'Exists' );
77
78 // Test for cache miss
79 $this->assertCount( 0, $categoryCache->cache );
80 $this->assertInstanceOf( Category::class, $categoryCache->getCategory( $title ) );
81 $this->assertCount( 1, $categoryCache->cache );
82
83 // Test normal access
84 $this->assertInstanceOf( Category::class, $categoryCache->getCategory( $title ) );
85 }
86 }