php-extended / placeholder-phpunit
A library that shows only the interfaces of phpunit, without requiring all the dependancies
Package info
gitlab.com/php-extended/placeholder-phpunit
pkg:composer/php-extended/placeholder-phpunit
Requires
- php: >=7.4
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
Replaces
None
README
A library that shows only the interfaces of phpunit, without requiring all the dependancies, for IDE autocompletion purposes. Tests still requires phpunit libraries or phars to work !
Installation
The installation of this library is made via composer and the autoloading of all classes of this library is made through their autoloader.
- Download
composer.pharfrom their website. - Then run the following command to install this library as dependency :
php composer.phar install php-extended/placeholder-phpunit ^9
This library ^X mocks phpunit X.
What is mirrored, and what is deliberately different
The goal is that static analysers (phpstan, psalm) see the same types as with the real phpunit, so that they can analyse test code without it.
Mirrored from phpunit 10 :
MockObject\StubandMockObject\MockObjectare interfaces, as in phpunit. This is what makescreateMock(SomeConcreteClass::class)resolve toMockObject&SomeConcreteClass: with classes, the intersection of two unrelated classes isnever.method()is available on both (returning anInvocationStubberon a stub, anInvocationMockeron a mock) and onInvocationMockeritself, so thatexpects(...)->method('x')->willReturn(...)resolves.- The
PHPUnit\Framework\Attributesclasses (DataProvider,CoversClass,Group, ...) and the constraint factories ofAssert(anything(),equalTo(),isInstanceOf(),stringContains(), ...) that return aConstraint. fail(),markTestIncomplete()andmarkTestSkipped()returnnever.
Deliberately different :
- No
@throwsis declared on the assertions (assert*()), the mock factories (createMock(),createStub(), ...) and the invocation builders (with(),willReturn(), ...). In phpunit they throwExpectationFailedExceptionorMockObject\Exception, which end the test and are never handled by the test code. Declaring them makes an analyser that checks exceptions demand a@throwstag on every single test method, for no information. Only the exceptions thrown by the code under test have to be declared. PlaceholderMockObjectis a class that does not exist in phpunit (there, the doubles are generated at runtime) : the factory methods need to return an object that implementsMockObject.Constraintis a single class here, where phpunit has one class per constraint.
License
MIT (See license file).