Dmitriy Gnatenko 40085f0615 Codeception added | 3 years ago | |
---|---|---|
.. | ||
docs | 3 years ago | |
src | 3 years ago | |
tests | 3 years ago | |
.gitignore | 3 years ago | |
.travis.yml | 3 years ago | |
Readme.md | 3 years ago | |
RoboFile.php | 3 years ago | |
composer.json | 3 years ago |
Library on top of PHPUnit's mock builder providing a highly simplified syntax:
Enabled by default in Codeception. For PHPUnit install this package:
composer require codeception/stub --dev
Stubs can be constructed with Codeception\Stub
static calls:
<?php
// create a stub with find method replaced
$userRepository = Stub::make(UserRepository::class, ['find' => new User]);
$userRepository->find(1); // => User
// create a dummy
$userRepository = Stub::makeEmpty(UserRepository::class);
// create a stub with all methods replaced except one
$user = Stub::makeEmptyExcept(User::class, 'validate');
$user->validate($data);
// create a stub by calling constructor and replacing a method
$user = Stub::construct(User::class, ['name' => 'davert'], ['save' => false]);
// create a stub by calling constructor with empty methods
$user = Stub::constructEmpty(User::class, ['name' => 'davert']);
// create a stub by calling constructor with empty methods
$user = Stub::constructEmptyExcept(User::class, 'getName', ['name' => 'davert']);
$user->getName(); // => davert
$user->setName('jane'); // => this method is empty
$user->getName(); // => davert
Alternatively, stubs can be created by using Codeception\Test\Feature\Stub
trait:
<?php
$this->make(UserRepositry::class);
$this->makeEmpty(UserRepositry::class);
$this->construct(UserRepositry::class);
$this->constructEmpty(UserRepositry::class);
// ...
Mocks should be created by including Codeception\Test\Feature\Stub
trait into a test case.
Execution expectation are set with Codeception\Stub\Expected
:
<?php
// find should be never called
$userRepository = $this->make(UserRepository::class, [
'find' => Codeception\Stub\Expected::never()
]);
// find should be called once and return a new user
$userRepository = $this->make(UserRepository::class, [
'find' => Codeception\Stub\Expected::once(new User)
]);
MIT