This commit is contained in:
tmont 2009-06-23 07:00:48 +00:00
parent 97400f9e87
commit 586215680e
3 changed files with 309 additions and 12 deletions

View File

@ -1,50 +1,164 @@
<?php <?php
/**
* InvocationExpectation
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
/**
* Represents what is expected of a mock object's method
* invocation
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
class InvocationExpectation { class InvocationExpectation {
protected $className; /**
* The method of the mock invocation
*
* @var string
*/
protected $method; protected $method;
/**
* The number of times the method is expected to be invoked
*
* @var int
*/
protected $count; protected $count;
/**
* The arguments the method should be invoked with
*
* @var array
*/
protected $args; protected $args;
/**
* The value to return after invocation
*
* @var mixed
*/
protected $returnValue; protected $returnValue;
/**
* String to echo after invocation
*
* @var mixed
*/
protected $echoString; protected $echoString;
/**
* Whether the expectation has been verified
*
* @var bool
*/
protected $verified; protected $verified;
/**
* Constructor
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param sttring $methodName
*/
public function __construct($methodName) { public function __construct($methodName) {
$this->method = $methodName; $this->method = $methodName;
$this->count = 0; $this->count = 0;
$this->args = array(); $this->args = array();
$this->returnValue = null; $this->returnValue = null;
$this->echoString = null; $this->echoString = null;
$this->verified = false; $this->verified = false;
} }
/**
* Gets the method name
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
public final function getMethod() { public final function getMethod() {
return $this->method; return $this->method;
} }
/**
* Gets the count
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return int
*/
public final function getCount() { public final function getCount() {
return $this->count; return $this->count;
} }
/**
* Gets the expected arguments
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return array
*/
public final function getArgs() { public final function getArgs() {
return $this->args; return $this->args;
} }
/**
* The number of times you expect the method to be called
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param int $count
* @return InvocationExpectation
*/
public function toBeCalled($count) { public function toBeCalled($count) {
$this->count = $count; $this->count = $count;
return $this; return $this;
} }
public function toBeCallExactly($count) { /**
throw new BadMethodCallException('Not implemented yet'); * The arguments you expect the method to be called with
} *
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return InvocationExpectation
*/
public function withArguments() { public function withArguments() {
$this->args = func_get_args(); $this->args = func_get_args();
return $this; return $this;
} }
/**
* The string you want the method to echo
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $value Primitive or __toString()-able object
* @throws InvalidArgumentException
* @return InvocationExpectation
*/
public function toEcho($value) { public function toEcho($value) {
if (!is_scalar($value)) { if (!is_scalar($value)) {
if (is_object($value)) { if (is_object($value)) {
@ -63,18 +177,57 @@
return $this; return $this;
} }
/**
* The string you want the method to echo
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses toEcho()
*
* @param mixed $value
* @return InvocationExpectation
*/
public function andToEcho($value) { public function andToEcho($value) {
return $this->toEcho($value); return $this->toEcho($value);
} }
/**
* The value you want the method to return
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $value
*/
public function toReturn($value) { public function toReturn($value) {
$this->returnValue = $value; $this->returnValue = $value;
} }
/**
* The value you want the method to return
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses toReturn()
*
* @param mixed $value
*/
public function andToReturn($value) { public function andToReturn($value) {
$this->toReturn($value); $this->toReturn($value);
} }
/**
* Executes the invocation expectation
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return mixed The return value given by {@link toReturn()}
*/
public function execute() { public function execute() {
if (!empty($this->echoString)) { if (!empty($this->echoString)) {
echo $this->echoString; echo $this->echoString;
@ -83,10 +236,34 @@
return $this->returnValue; return $this->returnValue;
} }
/**
* Determines whether this expectation matches the given invocation
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses MockInvocation::getMethod()
* @uses MockInvocation::getCount()
* @uses MockInvocation::getArgs()
* @uses countIsAcceptable()
*
* @param MockInvocation $invocation
* @return bool
*/
public function matchesInvocation(MockInvocation $invocation) { public function matchesInvocation(MockInvocation $invocation) {
return $this->method === $invocation->getMethod() && $this->countIsAcceptable($invocation->getCount()) && $this->args == $invocation->getArgs(); return $this->method === $invocation->getMethod() && $this->countIsAcceptable($invocation->getCount()) && $this->args == $invocation->getArgs();
} }
/**
* Determines if the given count is acceptable to this expectation
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param int $count
* @return bool
*/
protected function countIsAcceptable($count) { protected function countIsAcceptable($count) {
switch ($this->count) { switch ($this->count) {
case TestCase::ANY: case TestCase::ANY:
@ -98,10 +275,28 @@
} }
} }
/**
* Gets whether this expectation has been verified
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public final function isVerified() { public final function isVerified() {
return $this->verified; return $this->verified;
} }
/**
* Sets whether this expectation has been verified
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param bool $verified
*/
public final function setVerified($verified) { public final function setVerified($verified) {
$this->verified = (bool)$verified; $this->verified = (bool)$verified;
} }

View File

@ -1,13 +1,54 @@
<?php <?php
/**
* InvocationTracker
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
/**
* Tracks mock invocations
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
class InvocationTracker { class InvocationTracker {
/**
* @var array
*/
protected $invocations; protected $invocations;
/**
* Constructor
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
public function __construct() { public function __construct() {
$this->invocations = array(); $this->invocations = array();
} }
/**
* Registers a invocation
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses MockRegistry::getExpectations()
* @uses MockInvocation::getClass()
* @uses InvocationExpectation::matchsInvocation()
* @uses InvocationExpectation::setVerified()
*
* @param MockInvocation $invocation
* @return InvocationExpectation|bool Returns the matching InvocationExpectation if there is one, or false if not
*/
public function registerInvocation(MockInvocation $invocation) { public function registerInvocation(MockInvocation $invocation) {
$this->invocations[] = $invocation; $this->invocations[] = $invocation;
@ -21,10 +62,30 @@
return false; return false;
} }
public function getInvocations() { /**
* Gets
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return mixed
*/
public final function getInvocations() {
return $this->invocations; return $this->invocations;
} }
/**
* Verifies all invocations
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses MockRegistry::getAllExpectations()
* @uses InvocationExpectation::isVerified()
*
* @return bool true if all invocation expectations have been verified, false if not
*/
public function verify() { public function verify() {
foreach (MockRegistry::getAllExpectations() as $expectation) { foreach (MockRegistry::getAllExpectations() as $expectation) {
if (!$expectation->isVerified()) { if (!$expectation->isVerified()) {

View File

@ -1,13 +1,54 @@
<?php <?php
/**
* MockHandler
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
/**
* Simple wrapper for mock objects so we don't pollute
* objects with member variables
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
final class MockHandler { final class MockHandler {
/**
* @var string
*/
private $className; private $className;
/**
* Constructor
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param object $mock The mock object
*/
public function __construct($mock) { public function __construct($mock) {
$this->className = get_class($mock); $this->className = get_class($mock);
} }
/**
* The method the mock object expects to call
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses MockRegistry::addExpectation()
*
* @param string $methodName The method that is expected to be invoked
* @return InvocationExpectation
*/
public function expectsMethod($methodName) { public function expectsMethod($methodName) {
$expectation = new InvocationExpectation($methodName); $expectation = new InvocationExpectation($methodName);
MockRegistry::addExpectation($this->className, $expectation); MockRegistry::addExpectation($this->className, $expectation);