wiki.techinc.nl/tests/phpunit/includes/CategoryTest.php
Timo Tijhof 5c20049e2d Category: Widen @covers annotations in unit tests
Follows-up I69b5385868, I4c7d826c7e, I1287f3979ab, which similarly
widened the `@covers` annotations of other test suites:

> We lose useful coverage and spend valuable time keeping these tags
> accurate through refactors (or worse, forget to do so).
>
> I've audited each test to confirm it is a general test of the
> subject class, where tagging missing methods would be an accepted
> change, thus widening it is merely a no-op that clarifies intent
> and reduces maintenance. I am not disabling the "only track coverage
> of specified subject" mechanism, nor am I claiming coverage in
> in classes outside the subject under test.
>
> Tracking this narrow detail wastes time to keep methods in sync during
> refactors, time to realize (and fix) when other people inevitably
> dodn't keep them in sync, time lost in finding uncovered code to
> write tests for only to realize it is already covered but not tagged.

Follows-up I6c602290a (4d1da6f7f8) which updated the `@covers`
annotations in this test to the fully qualified class name.

Change-Id: I6d845bdfbb80d622f6ed099a29551dea90c860fc
2023-05-30 15:59:25 +01:00

158 lines
4.1 KiB
PHP

<?php
use MediaWiki\Category\Category;
use MediaWiki\MainConfigNames;
use MediaWiki\Title\Title;
/**
* @covers MediaWiki\Category\Category
* @group Database
* @group Category
*/
class CategoryTest extends MediaWikiIntegrationTestCase {
protected function setUp(): void {
parent::setUp();
$this->setUserLang( 'en' );
$this->overrideConfigValues( [
MainConfigNames::AllowUserJs => false,
MainConfigNames::DefaultLanguageVariant => false,
MainConfigNames::MetaNamespace => 'Project',
MainConfigNames::LanguageCode => 'en',
] );
$this->tablesUsed[] = 'category';
}
public function addDBData() {
$this->db->insert(
'category',
[
[
'cat_id' => 1,
'cat_title' => 'Example',
'cat_pages' => 3,
'cat_subcats' => 4,
'cat_files' => 5
]
],
__METHOD__,
[ 'IGNORE' ]
);
}
public function testInitialize_idNotExist() {
$category = Category::newFromID( -1 );
$this->assertFalse( $category->getName() );
}
public static function provideInitializeVariants() {
return [
// Existing title
[ 'newFromName', 'Example', 'getID', 1 ],
[ 'newFromName', 'Example', 'getName', 'Example' ],
[ 'newFromName', 'Example', 'getMemberCount', 3 ],
[ 'newFromName', 'Example', 'getSubcatCount', 4 ],
[ 'newFromName', 'Example', 'getFileCount', 5 ],
// Non-existing title
[ 'newFromName', 'NoExample', 'getID', 0 ],
[ 'newFromName', 'NoExample', 'getName', 'NoExample' ],
[ 'newFromName', 'NoExample', 'getMemberCount', 0 ],
[ 'newFromName', 'NoExample', 'getSubcatCount', 0 ],
[ 'newFromName', 'NoExample', 'getFileCount', 0 ],
// Existing ID
[ 'newFromID', 1, 'getID', 1 ],
[ 'newFromID', 1, 'getName', 'Example' ],
[ 'newFromID', 1, 'getMemberCount', 3 ],
[ 'newFromID', 1, 'getSubcatCount', 4 ],
[ 'newFromID', 1, 'getFileCount', 5 ]
];
}
/**
* @dataProvider provideInitializeVariants
*/
public function testInitialize( $createFunction, $createParam, $testFunction, $expected ) {
$category = Category::{$createFunction}( $createParam );
$this->assertEquals( $expected, $category->{$testFunction}() );
}
public function testNewFromName_validTitle() {
$category = Category::newFromName( 'Example' );
$this->assertSame( 'Example', $category->getName() );
}
public function testNewFromName_invalidTitle() {
$this->assertFalse( Category::newFromName( '#' ) );
}
public function testNewFromTitle() {
$title = Title::makeTitle( NS_CATEGORY, 'Example' );
$category = Category::newFromTitle( $title );
$this->assertSame( 'Example', $category->getName() );
$this->assertTrue( $title->isSamePageAs( $category->getPage() ) );
$this->assertTrue( $title->isSamePageAs( $category->getTitle() ) );
}
public function testNewFromID() {
$category = Category::newFromID( 5 );
$this->assertSame( 5, $category->getID() );
}
public function testNewFromRow_found() {
$dbw = wfGetDB( DB_PRIMARY );
$category = Category::newFromRow( $dbw->selectRow(
'category',
[ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
[ 'cat_id' => 1 ],
__METHOD__
) );
$this->assertSame( '1', $category->getID() );
}
public function testNewFromRow_notFoundWithoutTitle() {
$dbw = wfGetDB( DB_PRIMARY );
$row = $dbw->selectRow(
'category',
[ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
[ 'cat_id' => 1 ],
__METHOD__
);
$row->cat_title = null;
$this->assertFalse( Category::newFromRow( $row ) );
}
public function testNewFromRow_notFoundWithTitle() {
$dbw = wfGetDB( DB_PRIMARY );
$row = $dbw->selectRow(
'category',
[ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
[ 'cat_id' => 1 ],
__METHOD__
);
$row->cat_title = null;
$category = Category::newFromRow(
$row,
Title::makeTitle( NS_CATEGORY, 'Example' )
);
$this->assertFalse( $category->getID() );
}
public function testGetCounts() {
// Defined via addDBDataOnce
$category = Category::newFromID( 1 );
$this->assertEquals( 3, $category->getMemberCount() );
$this->assertEquals( 4, $category->getSubcatCount() );
$this->assertEquals( 5, $category->getFileCount() );
}
}