Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 196 lines (180 sloc) 4.875 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace lithium\test;

/**
* Mocker chain is used to aid in assertion of method calls.
*
* Asserting if `method1` was not called
* {{{
* $mock = new \lithium\tests\mocks\test\mockStdClass\Mock();
* $this->assertFalse(Mocker::chain($mock)->called('method1')->success());
* }}}
*
* Asserting if `method1` was called 2 times
* {{{
* $mock = new \lithium\tests\mocks\test\mockStdClass\Mock();
* $this->assertTrue(Mocker::chain($mock)->called('method1')->eq(2)->success());
* }}}
*
* Asserting if `method2` was called after `method1`
* {{{
* $mock = new \lithium\tests\mocks\test\mockStdClass\Mock();
* $this->assertTrue(Mocker::chain($mock)->called('method1')->called('method2')->success());
* }}}
*
* Asserting if `method2` was called after `method1`, and `method2` had specific arguments.
* {{{
* $mock = new \lithium\tests\mocks\test\mockStdClass\Mock();
* $this->assertTrue(Mocker::chain($mock)
* ->called('method1')
* ->called('method2')->with('foo', 'bar')
* ->success());
* }}}
*/
class MockerChain extends \lithium\core\Object {

/**
* Data to be used in the class.
*
* `results` Cached mock results
* `method` Method we are asserting
* `args` Args we are asserting
* `success` Success flag
* `callTime` Last method call
*
* @var array
*/
protected $_data = array(
'results' => null,
'method' => false,
'args' => false,
'success' => true,
'callTime' => 0,
);

/**
* Saves the results from the mock.
*
* @param array $results Results from the mock
*/
public function __construct($results) {
$this->_data['results'] = $results;
}

/**
* Validates that a given methodis called a set number of times.
*
* @param string $comparison Comparison type 'gt', 'gte', 'lt', 'lte', or 'eq'.
* @param array $args The first argument is the expected result.
* @return object
*/
public function __call($comparison, $args) {
$methodExists = in_array($comparison, array('gt', 'gte', 'lt', 'lte', 'eq'), true);
if (!$this->_data['success'] || !$methodExists) {
return $this;
}
if (count($args) === 0 || !is_int($args[0])) {
$this->_data['success'] = false;
return $this;
}
$result = 0;
$expected = $args[0];
$method = $this->_data['method'];
$args = $this->_data['args'];
foreach ($this->_data['results'][$method] as $call) {
$correctTime = $this->_data['callTime'] <= $call['time'];
$correctArgs = !is_array($args) || $args === $call['args'];
if ($correctTime && $correctArgs) {
$this->_data['callTime'] = $call['time'];
$result++;
}
}
switch ($comparison) {
case 'gt':
$this->_data['success'] = $result > $expected;
break;
case 'gte':
$this->_data['success'] = $result >= $expected;
break;
case 'lt':
$this->_data['success'] = $result < $expected;
break;
case 'lte':
$this->_data['success'] = $result <= $expected;
break;
case 'eq':
$this->_data['success'] = $result === $expected;
break;
}
return $this;
}

/**
* Valides the method was called after the last call.
*
* @param string $method Method to assert
* @return object
*/
function called($method) {
if (!$this->_data['success']) {
return $this;
}

$this->_data['method'] = $method;
$this->_data['args'] = false;
if (!isset($this->_data['results'][$method])) {
$this->_data['success'] = false;
return $this;
}

foreach ($this->_data['results'][$method] as $call) {
if ($this->_data['callTime'] < $call['time']) {
$this->_data['callTime'] = $call['time'];
return $this;
}
}

$this->_data['success'] = false;
return $this;
}

/**
* Will further narrow down the original 'called' method.
*
* Valides the cached method name was called with these args
*
* @param mixed $arg,... Optional arguments to test against
* @return object
*/
public function with() {
if (!$this->_data['success']) {
return $this;
}

$method = $this->_data['method'];
$this->_data['args'] = $args = func_get_args();

foreach ($this->_data['results'][$method] as $call) {
$correctTime = $this->_data['callTime'] <= $call['time'];
$correctArgs = $args === $call['args'];
if ($correctTime && $correctArgs) {
$this->_data['callTime'] = $call['time'];
return $this;
}
}

$this->_data['success'] = false;
return $this;
}

/**
* Gives back the success flag
*
* @return bool
*/
public function success() {
$success = $this->_data['success'];
$this->_data = array(
'results' => $this->_data['results'],
'method' => false,
'args' => false,
'success' => true,
'callTime' => 0,
);
return $success;
}

}

?>
Something went wrong with that request. Please try again.