Skip to content

Commit a0115a7

Browse files
committed
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
1 parent baa02a8 commit a0115a7

File tree

1 file changed

+4
-29
lines changed

1 file changed

+4
-29
lines changed

tests/phpunit/integration/includes/libs/uuid/GlobalIdGeneratorTest.php

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
use Wikimedia\Timestamp\ConvertibleTimestamp;
44
use Wikimedia\UUID\GlobalIdGenerator;
55

6+
/**
7+
* @covers \Wikimedia\UUID\GlobalIdGenerator
8+
*/
69
class GlobalIdGeneratorTest extends PHPUnit\Framework\TestCase {
710

811
use MediaWikiCoversValidator;
@@ -12,13 +15,7 @@ class GlobalIdGeneratorTest extends PHPUnit\Framework\TestCase {
1215
private $globalIdGenerator;
1316

1417
/**
15-
* Test that generated UIDs have the expected properties
16-
*
1718
* @dataProvider provider_testTimestampedUID
18-
* @covers \Wikimedia\UUID\GlobalIdGenerator::newTimestampedUID88
19-
* @covers \Wikimedia\UUID\GlobalIdGenerator::getTimestampedID88
20-
* @covers \Wikimedia\UUID\GlobalIdGenerator::newTimestampedUID128
21-
* @covers \Wikimedia\UUID\GlobalIdGenerator::getTimestampedID128
2219
*/
2320
public function testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits ) {
2421
$id = $this->globalIdGenerator->$method();
@@ -67,22 +64,15 @@ public function testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits
6764
}
6865
}
6966

70-
/**
71-
* [ method, length, bits, hostbits ]
72-
* NOTE: When adding a new method name here please update the covers tags for the tests!
73-
*/
7467
public static function provider_testTimestampedUID() {
68+
// [ method name, expected length, bits, hostbits ]
7569
return [
7670
[ 'newTimestampedUID128', 39, 128, 46, 48 ],
7771
[ 'newTimestampedUID128', 39, 128, 46, 48 ],
7872
[ 'newTimestampedUID88', 27, 88, 46, 32 ],
7973
];
8074
}
8175

82-
/**
83-
* @covers \Wikimedia\UUID\GlobalIdGenerator::newUUIDv1
84-
* @covers \Wikimedia\UUID\GlobalIdGenerator::getUUIDv1
85-
*/
8676
public function testUUIDv1() {
8777
$ids = [];
8878
for ( $i = 0; $i < 100; $i++ ) {
@@ -112,9 +102,6 @@ public function testUUIDv1() {
112102
$this->assertEquals( array_unique( $ids ), $ids, "All generated IDs are unique." );
113103
}
114104

115-
/**
116-
* @covers \Wikimedia\UUID\GlobalIdGenerator::newUUIDv4
117-
*/
118105
public function testUUIDv4() {
119106
$ids = [];
120107
for ( $i = 0; $i < 100; $i++ ) {
@@ -130,9 +117,6 @@ public function testUUIDv4() {
130117
$this->assertEquals( array_unique( $ids ), $ids, 'All generated IDs are unique.' );
131118
}
132119

133-
/**
134-
* @covers \Wikimedia\UUID\GlobalIdGenerator::newRawUUIDv4
135-
*/
136120
public function testRawUUIDv4() {
137121
for ( $i = 0; $i < 100; $i++ ) {
138122
$id = $this->globalIdGenerator->newRawUUIDv4();
@@ -153,9 +137,6 @@ public function testRawUUIDv4() {
153137
}
154138
}
155139

156-
/**
157-
* @covers \Wikimedia\UUID\GlobalIdGenerator::newSequentialPerNodeID
158-
*/
159140
public function testNewSequentialID() {
160141
$id1 = $this->globalIdGenerator->newSequentialPerNodeID( 'test', 32 );
161142
$id2 = $this->globalIdGenerator->newSequentialPerNodeID( 'test', 32 );
@@ -166,10 +147,6 @@ public function testNewSequentialID() {
166147
$this->assertGreaterThan( $id1, $id2, "IDs increasing in value" );
167148
}
168149

169-
/**
170-
* @covers \Wikimedia\UUID\GlobalIdGenerator::newSequentialPerNodeIDs
171-
* @covers \Wikimedia\UUID\GlobalIdGenerator::getSequentialPerNodeIDs
172-
*/
173150
public function testNewSequentialIDs() {
174151
$ids = $this->globalIdGenerator->newSequentialPerNodeIDs( 'test', 32, 5 );
175152
$lastId = null;
@@ -191,7 +168,6 @@ public static function provideGetTimestampFromUUIDv1() {
191168
* @param string $uuid
192169
* @param string $ts
193170
* @dataProvider provideGetTimestampFromUUIDv1
194-
* @covers \Wikimedia\UUID\GlobalIdGenerator::getTimestampFromUUIDv1
195171
*/
196172
public function testGetTimestampFromUUIDv1( string $uuid, string $ts ) {
197173
$this->assertEquals( $ts, $this->globalIdGenerator->getTimestampFromUUIDv1( $uuid ) );
@@ -209,7 +185,6 @@ public static function provideGetTimestampFromUUIDv1InvalidUUIDv1() {
209185
/**
210186
* @param string $uuid
211187
* @dataProvider provideGetTimestampFromUUIDv1InvalidUUIDv1
212-
* @covers \Wikimedia\UUID\GlobalIdGenerator::getTimestampFromUUIDv1
213188
*/
214189
public function testGetTimestampFromUUIDv1InvalidUUIDv1( string $uuid ) {
215190
$this->expectException( InvalidArgumentException::class );

0 commit comments

Comments
 (0)