|
Apache/2.4.41 (Ubuntu) Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) server ip : 62.171.164.128 | your ip : 127.0.0.1 safemode OFF > / home / a / home / dev2.destoffenstraat.com / vendor / codeception / stub / |
Filename | /home/a/home/dev2.destoffenstraat.com/vendor/codeception/stub/Readme.md |
Size | 3.06 kb |
Permission | rw-r--r-- |
Owner | root : root |
Create time | 21-Aug-2025 12:26 |
Last modified | 10-Aug-2019 18:20 |
Last accessed | 23-Aug-2025 14:18 |
Actions | edit | rename | delete | download (gzip) |
View | text | code | image |
# Codeception\Stub
[](https://travis-ci.org/Codeception/Stub)
[](https://packagist.org/packages/codeception/stub)
[](https://packagist.org/packages/codeception/stub)
[](https://packagist.org/packages/codeception/stub)
Library on top of PHPUnit's mock builder providing a highly simplified syntax:
## Reference
* [Stub](https://github.com/Codeception/Stub/blob/master/docs/Stub.md) - creating stub classes using static methods
* [Stub Trait](https://github.com/Codeception/Stub/blob/master/docs/StubTrait.md) - creating stubs and mocks using trait
* [Expected](https://github.com/Codeception/Stub/blob/master/docs/Expected.md) - defining expectations for mocks
## Install
Enabled by default in Codeception.
For PHPUnit install this package:
```
composer require codeception/stub --dev
```
## Stubs
Stubs can be constructed with `Codeception\Stub` static calls:
```php
<?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
```
[See complete reference](https://github.com/Codeception/Stub/blob/master/docs/Stub.md)
Alternatively, stubs can be created by using [`Codeception\Test\Feature\Stub` trait](https://github.com/Codeception/Stub/blob/master/docs/StubTrait.md):
```php
<?php
$this->make(UserRepositry::class);
$this->makeEmpty(UserRepositry::class);
$this->construct(UserRepositry::class);
$this->constructEmpty(UserRepositry::class);
// ...
```
## Mocks
Mocks should be created by including [`Codeception\Test\Feature\Stub` trait](https://github.com/Codeception/Stub/blob/master/docs/StubTrait.md) into a test case.
Execution expectation are set with [`Codeception\Stub\Expected`](https://github.com/Codeception/Stub/blob/master/docs/Expected.md):
```php
<?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)
]);
```
## License
MIT
[](https://travis-ci.org/Codeception/Stub)
[](https://packagist.org/packages/codeception/stub)
[](https://packagist.org/packages/codeception/stub)
[](https://packagist.org/packages/codeception/stub)
Library on top of PHPUnit's mock builder providing a highly simplified syntax:
## Reference
* [Stub](https://github.com/Codeception/Stub/blob/master/docs/Stub.md) - creating stub classes using static methods
* [Stub Trait](https://github.com/Codeception/Stub/blob/master/docs/StubTrait.md) - creating stubs and mocks using trait
* [Expected](https://github.com/Codeception/Stub/blob/master/docs/Expected.md) - defining expectations for mocks
## Install
Enabled by default in Codeception.
For PHPUnit install this package:
```
composer require codeception/stub --dev
```
## Stubs
Stubs can be constructed with `Codeception\Stub` static calls:
```php
<?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
```
[See complete reference](https://github.com/Codeception/Stub/blob/master/docs/Stub.md)
Alternatively, stubs can be created by using [`Codeception\Test\Feature\Stub` trait](https://github.com/Codeception/Stub/blob/master/docs/StubTrait.md):
```php
<?php
$this->make(UserRepositry::class);
$this->makeEmpty(UserRepositry::class);
$this->construct(UserRepositry::class);
$this->constructEmpty(UserRepositry::class);
// ...
```
## Mocks
Mocks should be created by including [`Codeception\Test\Feature\Stub` trait](https://github.com/Codeception/Stub/blob/master/docs/StubTrait.md) into a test case.
Execution expectation are set with [`Codeception\Stub\Expected`](https://github.com/Codeception/Stub/blob/master/docs/Expected.md):
```php
<?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)
]);
```
## License
MIT