Giter Site home page Giter Site logo

stub's Introduction

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:

<?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

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

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)
]);

License

MIT

stub's People

Contributors

davertmik avatar naktibalda avatar dmalykh avatar slamdunk avatar zebraf1 avatar krazzer avatar svycka avatar w0rma avatar tiger-seo avatar xdimedrolx avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.