Download the PHP package jasny/phpunit-extension without Composer
On this page you can find all versions of the php package jasny/phpunit-extension. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jasny/phpunit-extension
More information about jasny/phpunit-extension
Files in jasny/phpunit-extension
Package phpunit-extension
Short Description Additional functionality for PHPUnit (callback mock, expected warning, ...)
License MIT
Informations about the package phpunit-extension
Jasny PHPUnit extension
Additional functionality for PHPUnit.
- Callback mock - assert that callback is called with correct arguments.
- Expected warning - assert notice/warning is triggered and continue running.
- In context of - Access private/protected methods and properties.
- Consecutive calls - Replacement for removed
withConsecutive()
method.
Installation
composer require jasny/phpunit-extension
Usage
Callback mock
MockObject createCallbackMock(InvocationOrder $matcher, array|Closure $assert = null, $return = null)
The method takes either the expected arguments as array and the return value or a Closure
. If a Closure
is given,
it will be called with an InvocationMocker
, which can be used for more advanced matching.
Safe Mocks
The SafeMocksTrait
overwrites the createMock
method to disable auto-return value generation. This means that the
test will fail if any method is called that isn't configured.
In the example above, the method bye()
isn't configured for $foo
. Normally the test would succeed, but with
SafeMocksTrait
this test will result in the failure
Return value inference disabled and no expectation set up for Foo::bye()
Expected warning
ExpectedWarningTrait
overwrites the following methods;
expectNotice()
expectNoticeMessage(string $message)
expectNoticeMessageMatches(string $regexp)
expectWarning()
expectWarningMessage(string $message)
expectWarningMessageMatches(string $regexp)
expectDeprecation()
expectDeprecationMessage(string $message)
expectDeprecationMessageMatches(string $regexp)
Take the following example function;
PHPUnit converts notices and warning to exceptions. While you can catch these exceptions through methods like
expectDeprecation
, any code after the the notice/warning isn't run and therefor untestable.
The following test succeeds, while it should fail.
ExpectedWarningTrait
sets a custom error handler, that catches expected warnings and notices, without converting them
to exceptions. Code will continue to run.
After all other assertions succeed, the code will check if there are any expected warnings/notices that haven't been triggered.
In context of
mixed inContextOf(object $object, \Closure $function)
The function is called in context of the given object. This allows to call private and protected methods and get or set private and protected properties.
Beware: You should only test via public methods and properties. When you're required to access private methods or properties to perform tests, something is likely wrong in the architecture of your code.
Consecutive calls
ConsecutiveTrait
is a replacement for PHPUnit's withConsecutive
method which was removed in PHPUnit 10.