codeception/stub
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
phpunit/phpunit | 11.1.3 | - | BSD-3-Clause | prod dev |
Codeception\Stub is a PHP package designed to provide a simplified wrapper for PHPUnit's Mock Builder. This invaluable tool is a vital addition to the PHP developer's toolbox, designed to create stub classes using static methods, as well as mocks through a stub trait. Through the use of this package, developers can streamline and enhance their development and testing processes.
To utilize the benefits of Codeception\Stub, you need to install it first. If you are using PHPUnit, simply run the command composer require codeception/stub --dev
in your terminal. Once installed, the Stub package can be used to create Stubs and Mocks.
Here are a few examples of how to use the Codeception\Stub:
// Assume we have a UserRepository class and a User class
// Create a stub with 'find' method replaced
$userRepository = Stub::make(UserRepository::class, ['find' => new User]);
$userRepository->find(1); // => Returns User
// Create a dummy UserRepository
$userRepository = Stub::makeEmpty(UserRepository::class);
// Create a stub with all methods replaced except 'validate'
$user = Stub::makeEmptyExcept(User::class, 'validate');
$user->validate($data);
For more fine-grained control, you can utilize the Stub Trait methods to replace, mock and interact with classes:
// Create method expectation where 'find' method should be never called
$userRepository = $this->make(UserRepository::class, [
'find' => Codeception\Stub\Expected::never()
]);
The documentation for the codeception/stub
package can be found at the GitHub repository. For comprehensive insights into the workings of the stub, check the Stub.md file. For information on creating stubs and mocks using trait, consult the StubTrait.md file. To define expectations for mocks, the Expected.md file is your go-to guide.