123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class Yay_Mockery implements Yay_InvocationHandler
- {
-
-
-
- private $_expectations = array();
-
-
-
- private $_unexpectedInvocations = array();
-
-
-
- private $_generator;
-
-
-
- public function __construct()
- {
- $this->_generator = Yay_MockGenerator::getInstance();
- }
-
-
-
- public function mock($typeHint)
- {
- $className = $this->_generator->generateMock($typeHint);
- $reflector = new ReflectionClass($className);
- return $reflector->newInstance($this);
- }
-
-
-
- public function checking(Yay_ExpectationProvider $provider)
- {
- foreach ($provider->getExpectations() as $expectation)
- {
- $this->_expectations[] = $expectation;
- }
- }
-
-
-
- public function states($name)
- {
- return new Yay_StateMachine($name);
- }
-
-
-
- public function sequence($name)
- {
- return new Yay_SimpleSequence($name);
- }
-
-
-
- public function &handleInvocation(Yay_Invocation $invocation)
- {
- $ret = null;
- $expected = false;
- foreach ($this->_expectations as $expectation)
- {
- if ($expectation->isExpected($invocation))
- {
- $expected = true;
- if ($action = $expectation->getAction($invocation))
- {
- $ret =& $action->invoke($invocation);
- }
- break;
- }
- }
- if (!$expected)
- {
- $this->_unexpectedInvocations[] = $invocation;
- }
- return $ret;
- }
-
-
-
- public function assertIsSatisfied()
- {
- $description = new Yay_SimpleDescription();
- $satisfied = true;
- foreach ($this->_unexpectedInvocations as $invocation)
- {
- $description->appendText('Unexpected invocation');
- $invocation->describeTo($description);
- $description->appendText(PHP_EOL);
- $satisfied = false;
- }
- if (!$satisfied)
- {
- $description->appendText(PHP_EOL);
- }
- foreach ($this->_expectations as $expectation)
- {
- if (!$expectation->isSatisfied())
- {
- $description->appendText('* ');
- $satisfied = false;
- }
- $expectation->describeTo($description);
- $description->appendText(PHP_EOL);
- }
- if (!$satisfied)
- {
- throw new Yay_NotSatisfiedException(
- 'Not all expectations were satisfied or a method was invoked unexpectedly.' .
- PHP_EOL . PHP_EOL . $description->toString() . PHP_EOL
- );
- }
- }
-
- }
|