wiki.techinc.nl/tests/phpunit/includes/jobqueue/JobFactoryTest.php

66 lines
1.9 KiB
PHP
Raw Normal View History

<?php
use MediaWiki\JobQueue\JobFactory;
use MediaWiki\Title\Title;
/**
* @author Addshore
objectcache,resourceloader,rdbms,jobqueue: Widen @covers annotations Follows-up I4c7d826c7ec654b, I1287f3979aba1bf1. We lose useful coverage and spend valuable time keeping these accurate through refactors (or worse, forget to do so). The theoretically "bad" accidental coverage is almost never actually bad. Having said that, I'm not removing them wholesale (yet). I've audited each of these specific files to confirm it is a general test of the specified subject class, and also kept it limited to those specified classes. That's imho more than 100% of the benefit for less than 1% of the cost (more because `@covers` is more valuable than the fragile and corrosive individual private method tracking in tests that inevitably get out of date with no local incentive to keep them up to date). Cases like structure tests keep `@coversNothing` etc and we still don't count coverage of other classes. There may be a handful of large legacy classes where some methods are effectively class-like in complexity and that's why it's good for PHPUnit to offer the precision instrument but that doesn't meant we have to use that by-default for everything. I think best practice is to write good narrow unit tests, that reflect how the code should be used in practice. Not to write bad tests and hide part of its coverage within the same class or even namespace. Fortunately, that's generally what we do already it's just that we also kept these annotations still in many cases. This wastes time to keep methods in sync, time to realize (and fix) when other people inevitably didn't keep them in sync, time to find uncovered code only to realize it is already covered, time for a less experienced engineer to feel obligate to and do write a low quality test to cover the "missing" branch in an unrealistic way, time wasted in on-boarding by using such "bad" tests as example for how to use the code and then having to unlearn it months/years later, loss of telemetry in knowing what code actually isn't propertly tested due to being masked by a bad test, and lost oppertunities to find actually ununused/unreachable code and to think about how to instead structure the code such that maybe that code can be removed. ------ Especially cases like LBFactoryTest.php were getting out of hand, and in GlobalIdGeneratorTest.php we even resorted to reminding people with inline comments to keep tags in sync. Change-Id: I69b5385868cc6b451e5f2ebec9539694968bf58c
2023-04-10 21:25:34 +00:00
* @covers MediaWiki\JobQueue\JobFactory
*/
class JobFactoryTest extends MediaWikiIntegrationTestCase {
/**
* @dataProvider provideTestNewJob
*/
public function testNewJob( $handler, $expectedClass ) {
$specs = [
'testdummy' => $handler
];
$factory = new JobFactory(
$this->getServiceContainer()->getObjectFactory(),
$specs
);
$job = $factory->newJob( 'testdummy', Title::newMainPage(), [] );
$this->assertInstanceOf( $expectedClass, $job );
$job2 = $factory->newJob( 'testdummy', [] );
$this->assertInstanceOf( $expectedClass, $job2 );
$this->assertNotSame( $job, $job2, 'should not reuse instance' );
$job3 = $factory->newJob( 'testdummy', [ 'namespace' => NS_MAIN, 'title' => 'JobTestTitle' ] );
$this->assertInstanceOf( $expectedClass, $job3 );
$this->assertNotSame( $job, $job3, 'should not reuse instance' );
}
public function provideTestNewJob() {
return [
'class name, no title' => [ 'NullJob', NullJob::class ],
'class name with title' => [ DeleteLinksJob::class, DeleteLinksJob::class ],
'closure' => [ static function ( Title $title, array $params ) {
return new NullJob( $params );
}, NullJob::class ],
'function' => [ [ $this, 'newNullJob' ], NullJob::class ],
'object spec, no title' => [ [ 'class' => 'NullJob' ], NullJob::class ],
'object spec with title' => [ [ 'class' => DeleteLinksJob::class ], DeleteLinksJob::class ],
'object spec with no title and not subclass of GenericParameterJob' => [
[
'class' => ParsoidCachePrewarmJob::class,
'services' => [
'ParsoidOutputAccess',
'PageStore',
'RevisionLookup'
],
'needsPage' => false
],
ParsoidCachePrewarmJob::class
]
];
}
public function newNullJob( Title $title, array $params ) {
return new NullJob( $params );
}
}