Stub

by Codeception

Codeception / Stub

Flexible Stub wrapper for PHPUnit's Mock Builder

223 Stars 11 Forks Last release: 4 months ago (3.7.0) 53 Commits 14 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

Codeception\Stub

Build Status Latest Stable Version Total Downloads License

Library on top of PHPUnit's mock builder providing a highly simplified syntax:

Reference

  • Stub - creating stub classes using static methods
  • Stub Trait - creating stubs and mocks using trait
  • Expected - 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:
 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

Alternatively, stubs can be created by using

Codeception\Test\Feature\Stub
trait:

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 into a test case. Execution expectation are set with
Codeception\Stub\Expected
:

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

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.