Search by

friendsofmago / mago-phpunit

amateescualfredbez

Mago extension for PHPUnit and Prophecy: types for mocks and assertions, test class checks and PHPUnit linter rules.

Package info

github.com/FriendsOfMago/mago-phpunit

pkg:composer/friendsofmago/mago-phpunit

Statistics

Installs: 432

Dependents: 1

Suggesters: 0

Stars: 4

Open Issues: 5

0.3.5 2026-12-01 11:06 UTC

README

CI Packagist version Downloads PHP Mago PHPUnit License

friendsofmago/mago-phpunit is a Mago extension for PHPUnit and Prophecy, and the Mago counterpart of phpstan-phpunit.

  • Fewer false reports in tests. The extension adds the types of mocks, asserted values and Prophecy calls that are missing from PHPUnit's docblocks, so test code that works isn't reported.
  • Mistakes found before the tests run. Data providers with the wrong values, mocked methods that don't exist, a missing parent::setUp() and similar problems are reported by the analyzer.
  • Assertions that can't fail. An assertion that always passes for the value's type is reported.
  • PHPUnit linter rules. The PHPUnit rules of Mago's linter, such as strict assertions and one assertion style, come with the extension, with their fixes.
$mailer = $this->createMock(Mailer::class);
$mailer->method('sendLater');
// phpunit/mock-undefined-method: The method `sendLater()` doesn't exist on `Mailer`.

PHPUnit 10.5, 11.5, 12 and 13 are supported, and so is Prophecy. The extension needs PHP 8.1 or later and Mago 1.62.0 or a later 1.x release. The PHPUnit version is detected from the PHPUnit classes in vendor. Checks that only apply to some versions, such as the annotation checks, are turned on or off to match.

Setup

composer require --dev friendsofmago/mago-phpunit

This also installs Mago through Composer, as vendor/bin/mago. If the project doesn't have a mago.toml yet, run vendor/bin/mago init first.

Mago runs an extension in separate PHP processes, called workers. The command that starts a worker is set in an [extension-hosts.*] table in mago.toml. Add this block to mago.toml:

[extension-hosts.phpunit]
command = ["php", "-d", "xdebug.mode=off", "-d", "pcov.enabled=0", "vendor/friendsofmago/mago-phpunit/resources/worker.php"]

Check these settings too:

  • The tests must be in [source] paths, and vendor must be in [source] includes, as set by mago init. The PHPUnit and Prophecy classes are read from vendor.
  • If [linter] has integrations = ["phpunit"], remove "phpunit" from that list. Otherwise the same code is reported twice, once by Mago's built-in rules and once by the extension's linter rules.
  • If the project uses a framework's Mago extension that already includes these checks, leave out the [extension-hosts.phpunit] block. Mago stops with an error when the same checks are registered by two extensions.

Run vendor/bin/mago extension list from the project root to check that the extension is loaded. Then run vendor/bin/mago analyze and vendor/bin/mago lint as usual.

The worker path is relative to the project root. The -d options turn off Xdebug and pcov in the worker, since both slow it down. PHP ignores each option when Xdebug or pcov isn't installed.

Fewer false reports

Mago tracks the type of every value through the code. After assertNotNull($user), for example, $user has a type without null, so a later $user->getName() isn't reported. This is called narrowing. PHPUnit 10 and later describe most of their types in docblocks already used by Mago: createMock(Foo::class) returns MockObject&Foo, and assertions such as assertNotNull(), assertInstanceOf() and assertIsString() narrow their value. The types that can't be written in a docblock are added by the extension.

Assertions

These assertions narrow their value in a way that depends on another argument:

Assertion The value afterwards
assertArrayHasKey('key', $array) has the key, when the key is a literal
assertArrayNotHasKey('key', $array) doesn't have the key, when the key is a literal
assertObjectHasProperty('name', $object) has the property, when the value is typed object or mixed
assertCount(2, $list) has two items, when the count is an int literal
assertNotCount(2, $list) doesn't have two items, when the count is an int literal
assertNotEmpty($value) isn't empty
assertNotSame(null, $value) isn't that value, when it's null, a bool, int or string literal, or an enum case
assertGreaterThan(0, $n), assertLessThan() and their OrEqual forms is in that range, when the bound is an int literal, so assertGreaterThan(0, $n) narrows $n to positive-int
assertContains($needle, $array) the needle has one of the array's value types
assertNotContains($needle, $array) the needle isn't any literal value always held by the array, such as the values of ['a', 'b']
assertContainsOnly('string', $values) (PHPUnit 10 to 12) holds only values of the named type. On PHPUnit 10 the name can also be a class; later versions have assertContainsOnlyInstancesOf() for that.
assertEmpty($value) is empty, unless it may be a Countable object, a GMP or a BcMath\Number
assertStringContainsString('x', $text), assertStringContainsStringIgnoringCase(), assertStringStartsWith() and assertStringEndsWith() is a non-empty string, when the needle is a non-empty string and the value is typed as a string, mixed, null or false
assertJson($text) is a non-empty string, when the value is typed as a string, mixed, null or false

A value of another type, such as int|string, isn't narrowed, since PHP converts it to a string in a file without strict types. A mixed value is narrowed, which assumes the file has strict types: the extension can't see whether it does.

On PHPUnit 10, assertContainsOnlyInstancesOf(Foo::class, $values) and assertIsList($value) narrow too. Later versions have their own @phpstan-assert tags for them, and Mago reads those.

All of these narrow through $this->, self::, static::, Assert:: and PHPUnit's global functions.

$result = $this->build();                    // array{primary?: string}
$this->assertArrayHasKey('primary', $result);
return $result['primary'];
// Without the extension: possibly-undefined-string-array-index

Mocks

Mocks and stubs are called doubles in PHPUnit. The extension types them in these cases.

Intersection mocks. PHPUnit declares createMockForIntersectionOfInterfaces() as returning a plain MockObject, since a docblock can't build a type from a list of class names. The methods of the interfaces then look missing. With the extension, createMockForIntersectionOfInterfaces([A::class, B::class]) returns MockObject&A&B, and createStubForIntersectionOfInterfaces() returns Stub&A&B, when the interfaces are listed in a literal array.

Trait mocks. On PHPUnit 10 and 11, getMockForTrait() and getObjectForTrait() are declared as returning a plain MockObject or object, since a trait can't be a type in PHP. The trait's methods then look missing. With the extension, getMockForTrait(Greets::class) returns MockObject&Greets, and getObjectForTrait(Greets::class) returns Greets.

Mocks typed as a union. Older tests often type a mock as Foo|MockObject, from before PHP had intersection types:

/** @var Mailer|MockObject */
private $mailer;

$this->mailer->expects($this->once())->method('send');
// Without the extension: non-existent-method: Method `expects` does not exist on type `Mailer`.

Mago checks each type in a union on its own, and Mailer doesn't have expects(). With the extension, the expectation isn't reported, and the mock is accepted where a Mailer is expected. A call to a Mailer method, such as $this->mailer->send(), is still reported, as non-existent-method on MockObject. An extension can't change how Mago parses the docblock. Typing the mock as Mailer&MockObject fixes both.

Mocks typed as the class alone. A mock stored in a Mailer property loses its mock methods:

private Mailer $mailer;   // set to $this->createMock(Mailer::class)

$this->mailer->expects($this->once())->method('send')->willReturn(true);
// Without the extension: non-existent-method on expects(), then mixed-method-access on
// method() and on willReturn().
// With it: phpunit/mock-call-on-plain-type on expects().

With the extension, the chain is reported once, on expects(). The fix, shown in the help text, is to type the property as Mailer&MockObject. A real Mailer object gets the error too, because a mock and a real object have the same type here. That error is correct. expects() fails on a real object when the test runs.

Prophecy

$mailer = $this->prophesize(Mailer::class);
$mailer->send('a@example.com')->willReturn(true);   // not reported
$mailer->sendLater('a@example.com');                // still reported, Mailer doesn't have it

Prophecy sets up these calls through __call(), so without the extension Mago reports each one as non-documented-method. With it, a call to a method of the prophesized class returns a MethodProphecy. After willImplement(Foo::class), the prophecy also has the methods of Foo. A chained willExtend(Foo::class) gives a prophecy with the methods of Foo instead of the prophesized class's, and keeps the interfaces. On its own line, as in $prophecy->willExtend(Foo::class);, the prophecy keeps the old class next to Foo, as declared by Prophecy's @phpstan-this-out static<T&U> tag. A double of a Traversable interface has the Iterator methods added by Prophecy, unless the interface extends IteratorAggregate. The calls are checked too, as listed under Prophecy calls.

Lifecycle methods

PHPUnit runs setUp(), assertPreConditions() and the #[Before] and #[PreCondition] methods before each test. Mago treats them like a constructor, so a property set there counts as initialized:

private Mailer $mailer;

#[Before]
protected function createMailer(): void
{
    $this->mailer = new Mailer();
}

Without the extension, $mailer is reported as uninitialized-property, and a readonly property set in setUp() is reported as possibly-invalid-property-write.

On PHPUnit 10 and 11, the methods with a @before, @after, @preCondition or @postCondition tag are run the same way. Mago can't register a method by its tag, so the extension marks the tagged methods as used instead, along with the private methods and properties used only by them. It also removes Mago's uninitialized-property report for a property set by a @before or @preCondition method, or by a method of the class called by it, and possibly-invalid-property-write for a readonly property set by the tagged method itself.

The #[Before], #[After], #[PreCondition] and #[PostCondition] methods are called by PHPUnit, so a private or protected one in a final class isn't reported as unused-method.

These hook methods are reported as phpunit/invalid-hook-method. A hook method that PHPUnit skips without a warning is reported as a warning, and one that makes PHPUnit report an error as an error:

  • A #[BeforeClass] or #[AfterClass] method that isn't static, as a warning. PHPUnit skips it.
  • A private hook method of an abstract class, as a warning. It isn't run for the tests of the subclasses.
  • Another static #[BeforeClass] or #[AfterClass] method that isn't public, as an error. PHPUnit reports an error for the class instead of running it.
  • A private #[Before], #[After], #[PreCondition] or #[PostCondition] method of a class that isn't abstract, on PHPUnit 13.1 and earlier, as an error. Each test fails with an error there. PHPUnit 13.2 and later run it for the tests of that class.

The matching tags of PHPUnit 10 and 11, such as @beforeClass, are checked the same way. A hook method declared in a trait isn't checked. A private hook method of a test class that isn't abstract isn't run for the tests of its subclasses either, and that isn't reported.

Exceptions

With Mago's check-throws setting on (under [analyzer]), the exceptions thrown by PHPUnit, sebastian/* and Prophecy aren't reported as unhandled. A failed assertion throws on purpose, and PHPUnit catches it.

Checks

Each check reports an analyzer issue with a phpunit/ code.

Code Level Reports
phpunit/redundant-assertion warning an assertion that always passes for the value's type, such as assertNotNull() on a Foo
phpunit/mock-undefined-method error method('name') for a method that can't be replaced in the double: missing, final, private, protected, a constructor or a destructor. Also a method given to createPartialMock(), or to onlyMethods() in a chain that starts at getMockBuilder() with a literal class name, that isn't found by reflection. On PHPUnit 11.5 and later, a $property::get or $property::set hook name for a property that's missing, static, final, private or protected, or for a hook that's final, or that's missing and isn't generated by PHPUnit
phpunit/mock-added-method-exists error a method given to addMethods() (PHPUnit 10 and 11) that already exists in the mocked class
phpunit/mock-call-on-plain-type error expects() or method() on a value typed without MockObject, when its class doesn't declare that method. expects('name') is the form used by Mockery, and it's left to Mago
phpunit/mock-call-order error with(), withAnyParameters(), willReturn() or willReturnMap() called on expects() before method(), and the withParameterSets*() methods and willReturnStrictMap() on PHPUnit 13. PHPUnit fails the test on every version, since these calls need the method name
phpunit/mock-invalid-return error, or warning for a value that fits in part a value returned by a double, set up with will() or a willReturn*() method, that doesn't fit the method's return type, listed under Mock return values
phpunit/prophecy-union warning a type documented as Foo|ObjectProphecy, which should be ObjectProphecy<Foo>
phpunit/prophecy-invalid-return error, or warning in some cases a value that the prophesized method can't return, listed under Prophecy calls
phpunit/prophecy-invalid-double error a class that Prophecy can't double, such as a final class
phpunit/prophecy-invalid-setup error a method that Prophecy can't set up, such as a final or a private method
phpunit/prophecy-unknown-type error a name given to Argument::type() that isn't a type or a class
phpunit/prophecy-token-mismatch error, or warning for a docblock type an argument token that can never match its parameter, listed under Prophecy calls
phpunit/prophecy-named-argument error a named argument that isn't at the position of its parameter, so it's matched by Prophecy against another parameter
phpunit/assert-same-count warning assertSame($n, count($value)) or assertSame($n, $value->count()), where assertCount() is clearer
phpunit/invalid-data-provider error, or warning in two cases a data provider or data set that PHPUnit can't use, listed under Data providers
phpunit/data-provider-mismatch error, or warning in two cases a data set that doesn't fit the test's parameters, listed under Data providers
phpunit/dependency-mismatch error, or warning for a docblock type a result of a test's dependency that doesn't fit its parameter, listed under Data providers
phpunit/invalid-dependency error, or warning for a dependency that PHPUnit always skips a dependency on a test that PHPUnit can't find, listed under Data providers
phpunit/non-public-test error a method with #[Test] or a @test tag that isn't public, which isn't run by PHPUnit. A non-public testFoo() is also reported when it has a PHPUnit attribute or a @dataProvider, @depends or @testWith tag, since PHPUnit only reads these on a test. Without them, such a method is often a helper. A hook method isn't reported
phpunit/invalid-hook-method error, or warning for a hook method that PHPUnit skips a hook method that isn't run or can't be called by PHPUnit, listed under Lifecycle methods
phpunit/missing-parent-call warning a setUp() or tearDown() that doesn't call the parent's, when a parent class other than TestCase has one
phpunit/invalid-covers warning a covers or uses tag or attribute that can't be used by PHPUnit, listed under Coverage
phpunit/redundant-covers warning a method's @covers Foo when the class already has @covers Foo
phpunit/requires-without-operator warning a version such as #[RequiresPhp('8.2')] without an operator like >=
phpunit/incomplete-requires-version warning a compared version without all of a major, minor and patch level, such as '>= 8.1' or '8.1dev', on PHPUnit 12.5 and later, where PHPUnit warns about it
phpunit/annotation-without-value warning an annotation without a space and a value, such as @covers\Foo (ignored by PHPUnit) or a bare @dataProvider (PHPUnit fails the test, also reported as invalid-data-provider)

To turn a check off, list its code under Mago's [analyzer] ignore, for the whole project or for some paths:

[analyzer]
ignore = [
    "phpunit/assert-same-count",
    { code = "phpunit/missing-parent-call", in = "tests/Legacy/" },
]

To silence one report, put a comment with the code on the line above it. analysis: is the category of analyzer issues:

// @mago-ignore analysis:phpunit/mock-undefined-method
$mailer->method('sendLater');

By default, mago analyze only fails on errors. Pass --minimum-fail-level=warning to fail on the warnings too.

Mock return values

interface Clock
{
    public function now(): int;
}

$clock = $this->createStub(Clock::class);
$clock->method('now')->willReturn('12:00');
// phpunit/mock-invalid-return: `willReturn()` gets `'12:00'`, but `App\Clock::now()` returns `int`, so PHPUnit throws.

PHPUnit compares each value given to willReturn() with the native return type of the method, and throws an IncompatibleReturnValueException for a value that doesn't fit. Scalars aren't converted, so 1 doesn't fit a float and '5' doesn't fit an int. A void or never method doesn't take any value, not even null. The extension reports the same values as errors. A value that fits only in part, such as an ?int for an int, is a warning.

The builder can be kept in a variable first, as in $send = $mailer->expects($this->once()); $send->method('send')->willReturn(true);. The method name is checked there too.

A method without a native return type takes any value, and so does a mixed or callable one. An object is reported when its class can't be the returned one. That's the case for a double of another class created right there, such as $this->createStub(Foo::class), or held in a local variable as described below, for an enum case, and for an instance of an unrelated class when both are classes. An object typed as an interface isn't reported, since its class may implement the returned one too. A double typed MockObject alone, or with a wider type than its class, isn't reported either.

PHPUnit doesn't check the values given to willReturnOnConsecutiveCalls(), or the values returned by a stub. It also takes any willReturn() value for a union of two or more types and null, such as int|string|null, for an intersection with null, and for null alone, and any stub for a method that returns any object. The double returns these values, and its code uses strict types, so PHP throws a TypeError for a value that doesn't fit. These values are reported with the same rules, except that an int fits a float and a value for an intersection type isn't checked.

A stub is called first. It's given to will() or willReturnOnConsecutiveCalls(), or created by willReturnCallback(), willReturnArgument(), willReturnSelf(), willReturnMap() or a TestCase method such as returnValue():

  • ReturnStub returns its value, and ReturnSelf returns the double.
  • ReturnArgument returns the argument, of the parameter's native type. A variadic parameter takes the index and those after it. A parameter type that fits only in part isn't reported, since the test may only pass values that fit. An index past the parameters gets null, unless the call passes more arguments, so it's a warning.
  • ReturnCallback returns the callback's result. A first-class callable, a function name such as 'strlen', an array such as [$this, 'helper'] and an invokable object have the native return type of the function or method. A closure has the type inferred by Mago from its body, which is only reported when no part of it fits. A closure that takes a variable by reference, and a callable typed by a docblock, aren't checked.
  • ReturnValueMap returns the last value of a matching row. A row matches a call when it has one value for each parameter plus the returned value, and willReturnMap() fills in a short row with the parameters' defaults. A map without such a row returns null, unless it's strict, as created by willReturnStrictMap() or with the $strict argument of PHPUnit 13.
  • ConsecutiveCalls returns its values in turn, and calls the stubs among them.

The results of other stubs aren't checked. A void method drops the values. A never method is reported when the double gets a value, other than from a stub that throws, since the double then ends without throwing.

An unpacked array that's written out, such as ...[1, 2], gives its values. A stub, an array, a closure or a double held in a local variable is followed to the expression assigned to it, such as $stub = new ReturnStub(5) or $owner = $this->createStub(Owner::class). That's done when the variable is assigned once, by a statement that's a plain = assignment, in the method's body before the call, and no other use can change it. A variable isn't followed when it's:

  • assigned again, in a branch or a loop, by a compound assignment such as .=, or through an element such as $list[] = ...;
  • taken by & reference, incremented or decremented, or used in foreach, global, static, unset, a catch clause or a closure's use clause;
  • passed to a function, method or constructor whose parameter isn't known to take it by value, such as array_shift($list);
  • in a body with eval, include, require, goto, extract(), parse_str() or variable variables.

A ConsecutiveCalls stub reached through a variable that's read more than once, also through a list that holds it, isn't followed, since its values are used up by every method it's given to.

Prophecy calls

interface Mailer
{
    public function send(string $address): bool;
}

$mailer->send(['a@example.com']);                   // invalid-argument
$mailer->send();                                    // too-few-arguments
$mailer->send('a@example.com')->willReturn('sent');
// phpunit/prophecy-invalid-return: `willReturn()` gets `string`, but `App\Mailer::send()` returns `bool`.

The arguments are checked by Mago against the prophesized method's parameters. A parameter also takes an argument token (any arguments after Argument::cetera()), a prophecy of an object it takes, also inside an array, and the values that Prophecy compares loosely with it: a number or a Stringable object for a string, a numeric string or the other kind of number for a number, a string or a number for an object with __toString(), and any DateTimeInterface for a date. A setup with a required argument missing is reported, also with shouldNotBeCalled(). It never matches a call, so such a test only passes through Prophecy's error for an unexpected call. An extra argument is reported too, though the setup still matches a call that passes it. Argument::cetera() matches any arguments.

Prophecy matches the arguments by their position and drops the names. A named argument that isn't at the position of its parameter, such as send(subject: 'Hi', to: 'a@example.com') or one after a skipped optional parameter, is matched against another parameter. The setup then usually doesn't match the calls it's written for. It's reported as prophecy-named-argument. A name collected by a variadic parameter is matched by position too, so it's reported the same way when it comes before the variadic parameter.

An argument token that can never match its parameter is reported as prophecy-token-mismatch, such as Argument::type('int') or Argument::containingString('x') for an int parameter, Argument::is(5) for a string one, or Argument::that(fn(Foo $foo) => ...) when the parameter takes a Bar. A callback is called without strict types, so a scalar parameter type that PHP converts to, such as string for an int argument, is fine. A token that matches only some of the arguments, such as a callback for a subclass, isn't reported. The issue is an error when the native parameter type rules the token out, and a warning when only the docblock type does.

willReturn() values are checked against the return type, also after calls such as ->shouldBeCalled() and when the MethodProphecy is kept in a local variable first, as in $send = $mailer->send('a@example.com'); $send->willReturn(true);. The value is an error when it fails with a TypeError. It's a warning when it only breaks a docblock type, when PHP converts it to the native type (the double isn't in strict mode, so '5' becomes an int), when it fits in part, such as ?int for an int, or when it's wider, such as object for a Foo. The parameter returned by willReturnArgument() is checked the same way, as a warning when the parameter type only overlaps the return type, and an index past the method's parameters returns null unless the call passes an extra argument. A negative index is an error, since Prophecy throws for it. The items given to willYield() are checked against the type the method yields, as a warning. Prophecy doesn't reveal the prophecies in them. These are errors too, since Prophecy throws for them or the double fails:

  • willReturn(), willReturnArgument() or willYield() on a void or never method.
  • prophesize() of a final class, an enum or a trait, willExtend() of an interface and willImplement() of a class. A class marked @final only in its docblock isn't reported.
  • Setting up a final method, or a method with an intersection in its native return type.
  • Setting up a method that isn't replaced in the double, so the setup never takes effect: a private method, a protected one that isn't abstract, or a class's method whose name starts with _. Prophecy does replace __construct(), __destruct(), __sleep(), __wakeup(), __toString(), __call() and __invoke(), the methods of an interface given to prophesize() or willImplement(), and the methods of @method tags.
  • Setting up a static method. The double's static method fails when it's called.
  • willYield() on a method whose native return type can't hold a Generator.
  • Argument::type() with a name that isn't a class or a type with an is_*() function.

A parameter or return type whose docblock type depends on a template, a type alias, a class constant, a conditional type or a type such as key-of<> is checked against its native type.

A prophecy documented without its class, such as @var ObjectProphecy, isn't checked.

Data providers

public static function cases(): iterable
{
    yield [1, 'one'];
    yield ['two', 'two'];
    // phpunit/data-provider-mismatch: Data set #1 has a value of type string for $number, but
    // the parameter takes int.
}

#[DataProvider('cases')]
public function testCase(int $number, string $name): void {}

A test's providers are found through its DataProvider and DataProviderExternal attributes, its @dataProvider tags, and the closures of its DataProviderClosure attributes on PHPUnit 13.1 and later. An invalid-data-provider error is reported for:

  • a provider that is missing, isn't public or static, or takes arguments;
  • a closure provider with a required parameter, or with a return type that isn't iterable, and a first-class callable such as self::cases(...) or Rows::cases(...) of a method that isn't static;
  • a bare @dataProvider tag;
  • a data set that isn't an array, or that has the key '' on PHPUnit 13;
  • the same string key in the data sets of two of a test's providers;
  • a provider without any data sets, when all of the test's providers are in its file and all are empty. A provider attribute with skipWhenEmpty set to true skips the test on PHPUnit 13, so it isn't reported then;
  • two #[TestWith] data sets with the same key, on PHPUnit 11 and later;
  • a #[TestWithJson] or @testWith data set that isn't a valid JSON array, and a @testWith tag without rows.

PHPUnit only warns about two cases, so they're invalid-data-provider warnings:

  • a data provider that is also a test method;
  • a #[TestWith*] attribute next to a #[DataProvider*] attribute, on PHPUnit 12.5 and later. PHPUnit ignores the #[TestWith*] data sets then.

The data sets of a provider are also matched against the test's parameters, following PHPUnit's rules for passing them. The provider can be a closure, or a method of the test class, a parent class, a trait, or the class named by #[DataProviderExternal], by a @dataProvider Class::name tag, or by a first-class callable in #[DataProviderClosure], such as Rows::cases(...) or parent::cases(...). A data set that doesn't fit is a data-provider-mismatch error:

  • The values go to the parameters in order. From PHPUnit 11 on, a value under a string key goes to the parameter of that name.
  • A test that depends on other tests, through an attribute or a @depends tag, gets their results after the data. From PHPUnit 11 on, a test that returns void or never doesn't pass a result. PHPUnit 11 stops at the first such dependency, and PHPUnit 12 skips each one.
  • A data set built at runtime, such as one yielded in a loop, is checked as far as its type is known. A value that may fit, such as a mixed one, isn't reported.
  • A value that only breaks the parameter's docblock type, such as 26 for @param string $min, is a warning. PHP doesn't check docblock types, so the test still runs. As in Mago, null fits a docblock type when the native type is nullable or the default is null. A docblock type isn't checked when it depends on a template, a type alias, a class constant, a conditional type or a type such as key-of<>. A value that holds a generic object, such as new Box(1), or a closure isn't checked against a docblock type with a generic object or a callable signature either.
  • On PHPUnit 12.5 and later, a data set with more values than the test has parameters is a warning, unless the provider is declared with #[DataProvider('cases', false)]. PHPUnit doesn't allow named arguments for its attributes.

PHPUnit looks up a provider named by #[DataProvider] in the class the test is run in. So a test inherited from a parent class or a trait is also matched against a provider of that name declared in the class, such as a providerSource() method declared in each subclass of an abstract test class. Such a provider is reported when it takes arguments, and when it's empty in a class that isn't abstract and declares all of the test's providers. A provider of an abstract class isn't checked when each subclass overrides the test or the provider, since PHPUnit doesn't call it then.

For a provider declared in another file than the test, the issue is reported on the data set in the provider's file, once all files are analyzed. A problem of such a data set is reported once, even when the provider is used by several tests, also when a test in the provider's own file has the same problem. The warning about extra values names the test, so it's reported for each test. The tests of a trait are checked the same way, for each class that uses the trait.

The data sets of #[TestWith] and #[TestWithJson] attributes, and of @testWith tags on PHPUnit 10 and 11, are matched the same way. PHPUnit ignores them on a test with a data provider, and so does the extension. For a #[TestWithJson] attribute with JSON that isn't valid, PHPUnit stops the whole run, even on a test with a data provider.

The result of each dependency is matched with the parameter it fills, and a result that doesn't fit is a dependency-mismatch error. PHPUnit runs the test with strict types, so the test fails with a TypeError. The result has the native return type of the test it comes from. A test that calls expectException() may pass null instead, so null is taken into account for it. A result that only breaks the parameter's docblock type is a warning, with the same rules as for a data set. A test without a native return type isn't checked. On PHPUnit 10, a test that returns void passes null. The results fill the first parameters of a test without data. In a test with data, they come after the values of each data set. The results after a data set with string keys aren't checked on PHPUnit 11 and later, since PHPUnit fails the test. A dependency named by a constant, such as #[Depends(self::TARGET)], is resolved. The results of a dependency that can't be used by PHPUnit aren't matched, and neither are the results passed to the tests of a class with its own invokeTestMethod() or to a test declared in a trait without a data provider attribute.

A dependency on a test that PHPUnit can't find is an invalid-dependency error: a class that doesn't exist or isn't a test case, a method that doesn't exist, isn't public or isn't a test, or a name that isn't a method name, such as @depends testFoo(). PHPUnit fails the test. A dependency on a test that exists but never runs under the name as written is an invalid-dependency warning, since PHPUnit always skips the test: a class or method name in another case, a class with a leading backslash, or a test of an abstract class. A class named by #[DependsOnClass] or @depends Foo::class is an error when it doesn't exist or is an interface or a trait, and a warning when it's abstract, isn't a test case, is the test's own class, or is written in another case or with a leading backslash. A dependency of a test in an abstract class on a test the class doesn't have is looked up in each concrete subclass, since PHPUnit runs the test there. On PHPUnit 10 and 11, a @depends tag is read like PHPUnit reads it: one clone, !clone, shallowClone or !shallowClone word is dropped, and the rest of the line is the name. The dependencies of a test declared in a trait aren't checked.

Coverage

An invalid-covers warning is reported for:

  • a covers tag or attribute for code that doesn't exist, or for a class, method or function built into PHP;
  • a covers tag for an interface, and #[CoversClass] for an interface on PHPUnit 11 and later;
  • on PHPUnit 12 and later, #[CoversClass] for a trait, #[CoversMethod] for an interface method, #[CoversMethod(Foo::class, 'bar')] when bar() isn't declared in Foo itself, and a target written with a leading backslash;
  • on PHPUnit 12 and later, #[CoversNamespace] for a namespace without any class, trait or function, #[CoversClassesThatExtendClass] for anything but a class that's extended by another class, and #[CoversClassesThatImplementInterface] for anything but an interface in the implements list of a class. A namespace with only interfaces or only code built into PHP isn't reported, although PHPUnit doesn't accept it;
  • a @coversDefaultClass tag on a method, given twice, or naming something that isn't a class;
  • @covers ::name for a name that isn't a function, when the class doesn't have @coversDefaultClass.

PHPUnit warns about each of these and leaves the test out of the coverage report. PHPUnit 12 and later only accept code in the <source> of phpunit.xml, which isn't known to the extension. So a target in vendor, or one that's only extended or implemented by code outside the source, isn't reported.

The #[UsesClass], #[UsesTrait], #[UsesMethod], #[UsesFunction], #[UsesNamespace], #[UsesClassesThatExtendClass] and #[UsesClassesThatImplementInterface] attributes, and the @uses and @usesDefaultClass tags on PHPUnit 10 and 11, are checked with the same rules. PHPUnit checks their targets like covers targets. A @uses tag for an interface is accepted. Method tags are only read on test methods, and a class or test with CoversNothing isn't checked, since PHPUnit doesn't collect coverage for it. On PHPUnit 10 and 11, a test's own @covers tag overrides the class's CoversNothing.

Annotations

PHPUnit 12 and later ignore annotations such as @covers and @dataProvider, and PHPUnit 10 and 11 ignore them on a class or method that also has a PHPUnit attribute. The checks follow the same rules, so annotations are only checked on PHPUnit 10 and 11, where PHPUnit would use them.

Linter rules

The extension also has the PHPUnit rules of Mago's linter, so they don't depend on Mago's built-in phpunit integration. They run with mago lint. The rules check traits and classes with an extends clause, since a test case needs a parent class. Interfaces, enums, anonymous classes and classes without a parent aren't checked. The assertion rules check test methods. A test method is named test followed by _ or an uppercase letter, or has the #[Test] attribute or a @test tag. phpunit/no-assert-empty and phpunit/data-provider-name-case match opt-in checks of phpstan-phpunit, and aren't limited to test methods.

Code Default Reports
phpunit/strict-assertions on assertEquals() or assertNotEquals() with an int, float or string literal, instead of assertSame() or assertNotSame()
phpunit/use-specific-assertions on assertSame(null, $x), assertEquals(true, $x) and similar, instead of assertNull(), assertTrue() or assertFalse()
phpunit/assertion-style-static on an assertion called with $this-> or self:: instead of static::
phpunit/assertion-style-self off an assertion called with $this-> or static:: instead of self::
phpunit/assertion-style-this off an assertion called with static:: or self:: instead of $this->
phpunit/prefer-test-attribute off a public method named testFoo() instead of foo() with #[Test]
phpunit/no-assert-empty off assertEmpty() or assertNotEmpty(), with which 0, '0', '', [], null and false all count as empty
phpunit/data-provider-name-case off a #[DataProvider] attribute or @dataProvider tag naming a method of the same class in another case

The style rules also cover helpers called the same way, such as markTestSkipped() and constraint factories like isTrue(). A parent:: call isn't reported. phpunit/assertion-style-this doesn't check static test methods, since they don't have $this.

phpunit/prefer-test-attribute doesn't report a method when the new name is taken by another method of the class or trait, when two methods would get the same new name, or when the new name isn't a valid method name or starts with __. The fix renames the method in its class or trait only.

All of them are warnings. Each comes with a fix, except no-assert-empty, since the stricter assertion depends on the value. Most fixes can change what a test does, so mago fix only applies them with --potentially-unsafe. Two fixes are applied by default. The first replaces assertSame() against null, true or false, and assertNotSame() against null. The second fixes the case of a data provider name. A use-specific-assertions report on a call with named or unpacked arguments doesn't have a fix, since the dedicated assertion doesn't have the same parameter names.

An extension's lint rule can be turned on or off and given a level, but doesn't take other options. So each assertion style is its own rule. To use $this->, switch the rules:

[linter.rules]
"phpunit/assertion-style-static" = { enabled = false }
"phpunit/assertion-style-this" = { enabled = true }

Any rule can be turned off with enabled = false in the same way. To silence one report, put a comment with the code on the line above it. lint: is the category of linter issues:

// @mago-ignore lint:phpunit/strict-assertions
$this->assertEquals(1, $count);

If you're switching from Mago's built-in rules, add the phpunit/ prefix to the codes in @mago-expect and @mago-ignore comments: lint:strict-assertions becomes lint:phpunit/strict-assertions. The built-in assertion-style rule with style = "this" becomes phpunit/assertion-style-this. The built-in assertion rules only check methods named test.... The built-in rules also check interfaces, enums, anonymous classes and classes without a parent class.

Compared with phpstan-phpunit

Everything in phpstan-phpunit's default configuration (extension.neon and rules.neon) has a counterpart, and so do its bleeding-edge rules for empty data providers and incomplete versions. assertSame(true, ...), assertSame(null, ...) and assertEquals() are reported by the linter rules. The differences:

  • A Foo|MockObject docblock stays a union in Mago. See mocks typed as a union.
  • #[TestWith], #[TestWithJson] and @testWith data sets are matched against the test's parameters, and their JSON is checked. phpstan-phpunit doesn't check them.
  • assertContainsEquals() and assertNotContainsEquals() don't narrow, since they compare loosely and '1' equals 1.
  • #[RequiresPhp] versions that can never match the PHP version aren't reported, since Mago analyzes for one PHP version and a test suite usually runs on several.
  • The opt-in checks for assertEmpty() (with phpstan-strict-rules) and for a @dataProvider name in the wrong case (checkFunctionNameCase) are the linter rules phpunit/no-assert-empty and phpunit/data-provider-name-case, off by default.
  • getMockForTrait() and getObjectForTrait() objects are typed with the trait. phpstan-phpunit doesn't type them.

Known limitations

These cases aren't checked, are checked only in part, or are reported though the code works.

PHPUnit:

  • On PHPUnit 10, an object checked by assertEmpty() is narrowed to never. Mago applies the @psalm-assert empty tag of PHPUnit 10, and an extension can't replace it.

Mocks:

  • A builder returned by a helper, passed as a parameter, or read from a property in another method or after a call to another method of the test doesn't have the double in its type. Its method name and return values aren't checked.
  • The call order is only checked in a chain that starts at expects() on the double. The method name of a builder kept in a variable can be set through another reference, such as a helper.
  • For a legacy Foo|MockObject double, the method name isn't checked after expects(), and the return values aren't checked when its builder is kept in a variable.
  • A protected method isn't reported by mock-undefined-method in a file that calls getMockBuilder(), createPartialMock(), onlyMethods(), getMockForTrait() or getMockForAbstractClass(), since the double may be built with one of them in a helper. On PHPUnit 10 and 11, a missing method isn't reported in a file that calls addMethods(), getMockForTrait() or getMockForAbstractClass(), for the same reason.
  • On PHPUnit 13.3 and later, a hook name for a property without hooks isn't reported in a file that calls doubleProperties(), for the same reason.
  • Before PHPUnit 13.3, a hook name can get a wrong result for a property that's read by its hook only inside a closure, or through $this->{'name'}.

Prophecy:

  • A MethodProphecy read from a property or an array element, returned by a helper, passed as a parameter or typed by a @var tag isn't checked. Neither is a variable set to the MethodProphecy of one method or another, as in $flag ? $mailer->send('a') : $mailer->find(1).
  • An Argument::cetera() token kept in a variable isn't recognized, so the call gets Mago's too-few-arguments or too-many-arguments. Mago doesn't give an extension the argument types at that point.
  • The calls on ObjectProphecy<A>|ObjectProphecy<B> aren't checked when the two classes have different methods of that name.
  • A docblock type that depends on a template, a type alias, a class constant or a conditional type is only checked against its native type. The extension doesn't get the template arguments of a class, such as User in @extends Repository<User>.

Lifecycle methods:

  • A property set by a lifecycle method from a trait is reported as uninitialized-property, or as missing-constructor when the trait declares it, unless the trait is declared above the class in the same file. So is a property set by a parent's lifecycle method through a method of the test class.
  • A readonly property written by two lifecycle methods, such as setUp() and a #[Before] method, isn't reported, though the second write throws. The order of the lifecycle methods isn't checked either.
  • On PHPUnit 10 and 11, a property assigned on only one branch of a @before or @preCondition method counts as set, while Mago reports the same code in setUp(). A readonly property set by such a method through another method is reported as possibly-invalid-property-write.

Data providers:

  • A provider in a file that isn't analyzed, such as one under includes, isn't checked.
  • An array of data sets appended in a loop without a key, as in $rows[] = [$value];, isn't checked. One keyed by a value of the loop, such as an enum case's name, is.
  • An array that spreads the result of a call, as in [...parent::rows(), ['x']], isn't checked, its own rows included. In an abstract class, the rows of the base provider used that way by every subclass aren't checked either.
  • On PHPUnit 10 and 11, a provider in another file named only by a @dataProvider tag isn't checked, unless the class also has a test with a provider attribute for another file. A test from a trait with only a tag isn't checked.
  • A test from a trait run under another name with as isn't checked under that name, unless the class declares the provider.
  • An inherited test is matched against a subclass's provider only when that provider is a public static method declared in the class body, named by a #[DataProvider] attribute, and its name isn't used elsewhere in the class. The test's dependency results aren't checked then.
  • For a test from another file with a #[Depends] attribute or a similar one, missing values and data sets with string keys aren't reported.

Linter rules:

  • The phpunit/prefer-test-attribute fix doesn't rename a declaration of the method in an interface or a parent class. It doesn't check the names of methods from a parent class, a trait or TestCase either. testCount() becomes count(), which overrides the final TestCase::count(), and PHP stops with a fatal error.
  • The phpunit/prefer-test-attribute fix doesn't change a reference to the old name, such as #[Depends('testCreate')], a @depends tag or a call like $this->testCreate().
  • A class that isn't a test but has a parent class is checked. A linter rule only gets the file's syntax, so the parent class isn't known.

For extension authors

Another extension, such as a framework's, can run these checks in its own worker by registering PHPUnitExtension::plugin() next to its own analyzer plugins, and PHPUnitExtension::linterRules() next to its own linter rules:

use MagoPHPUnit\PHPUnitExtension;

$plugins = [new MyPlugin(), PHPUnitExtension::plugin()];
$rules = [new MyRule(), ...PHPUnitExtension::linterRules()];

Projects that use that extension then leave out the phpunit host from the setup above.

Development

just check-ci runs every check. See CONTRIBUTING.md for the test corpus and the other checks.

License

MIT. See LICENSE.