-
Open the test file at tests/PluginTest.php and read its current state before adding anything. Verify PluginTest already has protected function setUp(): void initializing $this->reflection = new ReflectionClass(Plugin::class);.
-
Choose the right assertion pattern based on what you are testing:
| What to test | Pattern to use |
|---|
| Static property value | $this->assertSame('expected', Plugin::$prop); |
| Static property existence & visibility | $this->reflection->hasProperty('prop') + isStatic() + isPublic() |
| Method exists, public, static | $this->reflection->hasMethod('name') + isPublic() + isStatic() |
| Method parameter type | $method->getParameters()[0]->getType()->getName() |
| Code calls a function | Extract source via file() + array_slice() on start/end lines, then assertStringContainsString() |
| File/dir on disk | assertFileExists(dirname(__DIR__) . '/path') |
| Hook registration | assertSame([Plugin::class, 'methodName'], $hooks['vps.event']) |
-
Source-analysis pattern (for verifying internal behavior without running side-effectful code):
$method = $this->reflection->getMethod('getQueue');
$startLine = $method->getStartLine();
$endLine = $method->getEndLine();
$filename = $method->getFileName();
$this->assertNotFalse($filename);
$lines = file($filename);
$this->assertNotFalse($lines);
$methodSource = implode('', array_slice($lines, $startLine - 1, $endLine - $startLine + 1));
$this->assertStringContainsString('stopPropagation', $methodSource);
Use this pattern whenever the method under test calls globals (myadmin_log, get_service_define, \TFSmarty, $GLOBALS['tf']).
-
Add @dataProvider when checking multiple similar values (e.g., template files, settings keys, method names). Provider methods return array<string, array{string}> with descriptive string keys:
public function testTemplateFilesExist(string $template): void
{
$this->assertFileExists(dirname(__DIR__) . '/templates/' . $template);
}
public function templateFileProvider(): array
{
return [
'create' => ['create.sh.tpl'],
'start' => ['start.sh.tpl'],
];
}
-
New template test: when a new templates/foo.sh.tpl is added, add 'foo' => ['foo.sh.tpl'] to templateFileProvider() AND 'backup_foo' => ['backup/foo.sh.tpl'] if a backup variant exists.
-
Docblock format — every test method needs a docblock:
DataProvider tests also need @dataProvider providerMethodName and @param type $paramName.
-
Verify the new test passes before finishing:
vendor/bin/phpunit tests/ -v
All tests must be green. If failOnWarning triggers, check for undefined variables or wrong types.