Stub
extends Stub
in package
Table of Contents
Properties
- $magicMethods : mixed
Methods
- atLeastOnce() : mixed
- consecutive() : ConsecutiveMap
- Stubbing a method call to return a list of values in the specified order.
- construct() : MockObject|RealInstanceType
- Instantiates a class instance by running constructor.
- constructEmpty() : MockObject|RealInstanceType
- Instantiates a class instance by running constructor with all methods replaced with dummies.
- constructEmptyExcept() : MockObject|RealInstanceType
- Instantiates a class instance by running constructor with all methods replaced with dummies, except one.
- copy() : mixed
- Clones an object and redefines it's properties (even protected and private)
- exactly() : mixed
- factory() : array<string|int, mixed>
- Creates $num instances of class through `Stub::make`.
- make() : MockObject|RealInstanceType
- Instantiates a class without executing a constructor.
- makeEmpty() : MockObject|RealInstanceType
- Instantiates class having all methods replaced with dummies.
- makeEmptyExcept() : MockObject|RealInstanceType
- Instantiates class having all methods replaced with dummies except one.
- never() : mixed
- once() : mixed
- update() : mixed
- Replaces properties of current stub
- bindParameters() : mixed
- getClassname() : mixed
- getMethodsToReplace() : array<string|int, mixed>
- doGenerateMock() : mixed
- extractTestCaseFromArgs() : mixed
- generateMock() : mixed
- generateMockForAbstractClass() : object
- Returns a mock object for the specified abstract class with all abstract methods of the class mocked. Concrete methods to mock can be specified with the last parameter
- markAsMock() : object
- Set __mock flag, if at all possible
Properties
$magicMethods
public
static mixed
$magicMethods
= ['__isset', '__get', '__set']
Methods
atLeastOnce()
public
static atLeastOnce([mixed $params = null ]) : mixed
Parameters
- $params : mixed = null
consecutive()
Stubbing a method call to return a list of values in the specified order.
public
static consecutive() : ConsecutiveMap
<?php
$user = Stub::make('User', ['getName' => Stub::consecutive('david', 'emma', 'sam', 'amy')]);
$user->getName(); //david
$user->getName(); //emma
$user->getName(); //sam
$user->getName(); //amy
?>
Return values
ConsecutiveMapconstruct()
Instantiates a class instance by running constructor.
public
static construct(RealInstanceType>|RealInstanceType|callable(): RealInstanceType> $class[, array<string|int, mixed> $constructorParams = [] ][, array<string|int, mixed> $params = [] ][, bool|TestCase $testCase = false ]) : MockObject|RealInstanceType
Parameters for constructor passed as second argument Properties and methods can be set in third argument. Even protected and private properties can be set.
<?php
Stub::construct('User', ['autosave' => false]);
Stub::construct('User', ['autosave' => false], ['name' => 'davert']);
?>
Accepts either name of class or object of that class
<?php
Stub::construct(new User, ['autosave' => false], ['name' => 'davert']);
?>
To replace method provide it's name as a key in third parameter and it's return value or callback function as parameter
<?php
Stub::construct('User', [], ['save' => function () { return true; }]);
Stub::construct('User', [], ['save' => true]);
?>
To create a mock, pass current testcase name as last argument:
<?php
Stub::construct('User', [], [
'save' => \Codeception\Stub\Expected::once()
], $this);
Parameters
- $class : RealInstanceType>|RealInstanceType|callable(): RealInstanceType>
-
- A class to be mocked
- $constructorParams : array<string|int, mixed> = []
- $params : array<string|int, mixed> = []
- $testCase : bool|TestCase = false
Tags
Return values
MockObject|RealInstanceTypeconstructEmpty()
Instantiates a class instance by running constructor with all methods replaced with dummies.
public
static constructEmpty(RealInstanceType>|RealInstanceType|callable(): RealInstanceType> $class[, array<string|int, mixed> $constructorParams = [] ][, array<string|int, mixed> $params = [] ][, bool|TestCase $testCase = false ]) : MockObject|RealInstanceType
Parameters for constructor passed as second argument Properties and methods can be set in third argument. Even protected and private properties can be set.
<?php
Stub::constructEmpty('User', ['autosave' => false]);
Stub::constructEmpty('User', ['autosave' => false], ['name' => 'davert']);
Accepts either name of class or object of that class
<?php
Stub::constructEmpty(new User, ['autosave' => false], ['name' => 'davert']);
To replace method provide it's name as a key in third parameter and it's return value or callback function as parameter
<?php
Stub::constructEmpty('User', [], ['save' => function () { return true; }]);
Stub::constructEmpty('User', [], ['save' => true]);
To create a mock, pass current testcase name as last argument:
<?php
Stub::constructEmpty('User', [], [
'save' => \Codeception\Stub\Expected::once()
], $this);
Parameters
- $class : RealInstanceType>|RealInstanceType|callable(): RealInstanceType>
-
- A class to be mocked
- $constructorParams : array<string|int, mixed> = []
- $params : array<string|int, mixed> = []
- $testCase : bool|TestCase = false
Tags
Return values
MockObject|RealInstanceTypeconstructEmptyExcept()
Instantiates a class instance by running constructor with all methods replaced with dummies, except one.
public
static constructEmptyExcept(RealInstanceType>|RealInstanceType|callable(): RealInstanceType> $class, string $method[, array<string|int, mixed> $constructorParams = [] ][, array<string|int, mixed> $params = [] ][, bool|TestCase $testCase = false ]) : MockObject|RealInstanceType
Parameters for constructor passed as second argument Properties and methods can be set in third argument. Even protected and private properties can be set.
<?php
Stub::constructEmptyExcept('User', 'save');
Stub::constructEmptyExcept('User', 'save', ['autosave' => false], ['name' => 'davert']);
?>
Accepts either name of class or object of that class
<?php
Stub::constructEmptyExcept(new User, 'save', ['autosave' => false], ['name' => 'davert']);
?>
To replace method provide it's name as a key in third parameter and it's return value or callback function as parameter
<?php
Stub::constructEmptyExcept('User', 'save', [], ['save' => function () { return true; }]);
Stub::constructEmptyExcept('User', 'save', [], ['save' => true]);
?>
To create a mock, pass current testcase name as last argument:
<?php
Stub::constructEmptyExcept('User', 'save', [], [
'save' => \Codeception\Stub\Expected::once()
], $this);
Parameters
- $class : RealInstanceType>|RealInstanceType|callable(): RealInstanceType>
-
- A class to be mocked
- $method : string
- $constructorParams : array<string|int, mixed> = []
- $params : array<string|int, mixed> = []
- $testCase : bool|TestCase = false
Tags
Return values
MockObject|RealInstanceTypecopy()
Clones an object and redefines it's properties (even protected and private)
public
static copy(mixed $obj[, array<string|int, mixed> $params = [] ]) : mixed
Parameters
- $obj : mixed
- $params : array<string|int, mixed> = []
Tags
exactly()
public
static exactly(mixed $count[, mixed $params = null ]) : mixed
Parameters
- $count : mixed
- $params : mixed = null
factory()
Creates $num instances of class through `Stub::make`.
public
static factory(mixed $class[, int $num = 1 ][, array<string|int, mixed> $params = [] ]) : array<string|int, mixed>
Parameters
- $class : mixed
- $num : int = 1
- $params : array<string|int, mixed> = []
Tags
Return values
array<string|int, mixed>make()
Instantiates a class without executing a constructor.
public
static make(RealInstanceType>|RealInstanceType|callable(): RealInstanceType> $class[, array<string|int, mixed> $params = [] ][, bool|TestCase $testCase = false ]) : MockObject|RealInstanceType
Properties and methods can be set as a second parameter. Even protected and private properties can be set.
<?php
Stub::make('User');
Stub::make('User', ['name' => 'davert']);
?>
Accepts either name of class or object of that class
<?php
Stub::make(new User, ['name' => 'davert']);
?>
To replace method provide it's name as a key in second parameter and it's return value or callback function as parameter
<?php
Stub::make('User', ['save' => function () { return true; }]);
Stub::make('User', ['save' => true]);
?>
To create a mock, pass current testcase name as last argument:
<?php
Stub::make('User', [
'save' => \Codeception\Stub\Expected::once()
], $this);
Parameters
- $class : RealInstanceType>|RealInstanceType|callable(): RealInstanceType>
-
- A class to be mocked
- $params : array<string|int, mixed> = []
-
- properties and methods to set
- $testCase : bool|TestCase = false
Tags
Return values
MockObject|RealInstanceType —- mock
makeEmpty()
Instantiates class having all methods replaced with dummies.
public
static makeEmpty(RealInstanceType>|RealInstanceType|callable(): RealInstanceType> $class[, array<string|int, mixed> $params = [] ][, bool|TestCase $testCase = false ]) : MockObject|RealInstanceType
Constructor is not triggered. Properties and methods can be set as a second parameter. Even protected and private properties can be set.
<?php
Stub::makeEmpty('User');
Stub::makeEmpty('User', ['name' => 'davert']);
Accepts either name of class or object of that class
<?php
Stub::makeEmpty(new User, ['name' => 'davert']);
To replace method provide it's name as a key in second parameter and it's return value or callback function as parameter
<?php
Stub::makeEmpty('User', ['save' => function () { return true; }]);
Stub::makeEmpty('User', ['save' => true]);
To create a mock, pass current testcase name as last argument:
<?php
Stub::makeEmpty('User', [
'save' => \Codeception\Stub\Expected::once()
], $this);
Parameters
- $class : RealInstanceType>|RealInstanceType|callable(): RealInstanceType>
-
- A class to be mocked
- $params : array<string|int, mixed> = []
- $testCase : bool|TestCase = false
Tags
Return values
MockObject|RealInstanceTypemakeEmptyExcept()
Instantiates class having all methods replaced with dummies except one.
public
static makeEmptyExcept(RealInstanceType>|RealInstanceType|callable(): RealInstanceType> $class, string $method[, array<string|int, mixed> $params = [] ][, bool|TestCase $testCase = false ]) : MockObject|RealInstanceType
Constructor is not triggered. Properties and methods can be replaced. Even protected and private properties can be set.
<?php
Stub::makeEmptyExcept('User', 'save');
Stub::makeEmptyExcept('User', 'save', ['name' => 'davert']);
?>
Accepts either name of class or object of that class
<?php
* Stub::makeEmptyExcept(new User, 'save');
?>
To replace method provide it's name as a key in second parameter and it's return value or callback function as parameter
<?php
Stub::makeEmptyExcept('User', 'save', ['isValid' => function () { return true; }]);
Stub::makeEmptyExcept('User', 'save', ['isValid' => true]);
?>
To create a mock, pass current testcase name as last argument:
<?php
Stub::makeEmptyExcept('User', 'validate', [
'save' => \Codeception\Stub\Expected::once()
], $this);
Parameters
- $class : RealInstanceType>|RealInstanceType|callable(): RealInstanceType>
-
- A class to be mocked
- $method : string
- $params : array<string|int, mixed> = []
- $testCase : bool|TestCase = false
Tags
Return values
MockObject|RealInstanceTypenever()
public
static never([mixed $params = null ]) : mixed
Parameters
- $params : mixed = null
once()
public
static once([mixed $params = null ]) : mixed
Parameters
- $params : mixed = null
update()
Replaces properties of current stub
public
static update(MockObject $mock, array<string|int, mixed> $params) : mixed
Parameters
- $mock : MockObject
- $params : array<string|int, mixed>
Tags
bindParameters()
protected
static bindParameters(MockObject $mock, array<string|int, mixed> $params) : mixed
Parameters
- $mock : MockObject
- $params : array<string|int, mixed>
Tags
getClassname()
protected
static getClassname(mixed $object) : mixed
Parameters
- $object : mixed
Tags
getMethodsToReplace()
protected
static getMethodsToReplace(ReflectionClass $reflection, mixed $params) : array<string|int, mixed>
Parameters
- $reflection : ReflectionClass
- $params : mixed
Return values
array<string|int, mixed>doGenerateMock()
private
static doGenerateMock(mixed $args[, mixed $isAbstract = false ]) : mixed
Parameters
- $args : mixed
- $isAbstract : mixed = false
extractTestCaseFromArgs()
private
static extractTestCaseFromArgs(mixed &$args) : mixed
Parameters
- $args : mixed
generateMock()
private
static generateMock() : mixed
generateMockForAbstractClass()
Returns a mock object for the specified abstract class with all abstract methods of the class mocked. Concrete methods to mock can be specified with the last parameter
private
static generateMockForAbstractClass() : object
Tags
Return values
objectmarkAsMock()
Set __mock flag, if at all possible
private
static markAsMock(object $mock, ReflectionClass $reflection) : object
Parameters
- $mock : object
- $reflection : ReflectionClass