Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletestlib/mock_objects.php
Go to the documentation of this file.
00001 <?php
00012 require_once(dirname(__FILE__) . '/expectation.php');
00013 require_once(dirname(__FILE__) . '/simpletest.php');
00014 require_once(dirname(__FILE__) . '/dumper.php');
00015 if (version_compare(phpversion(), '5') >= 0) {
00016     require_once(dirname(__FILE__) . '/reflection_php5.php');
00017 } else {
00018     require_once(dirname(__FILE__) . '/reflection_php4.php');
00019 }
00025 if (! defined('MOCK_ANYTHING')) {
00026     define('MOCK_ANYTHING', '*');
00027 }
00028 
00034 class ParametersExpectation extends SimpleExpectation {
00035     var $_expected;
00036 
00046     function ParametersExpectation($expected = false, $message = '%s') {
00047         $this->SimpleExpectation($message);
00048         $this->_expected = $expected;
00049     }
00050 
00057     function test($parameters) {
00058         if (! is_array($this->_expected)) {
00059             return true;
00060         }
00061         if (count($this->_expected) != count($parameters)) {
00062             return false;
00063         }
00064         for ($i = 0; $i < count($this->_expected); $i++) {
00065             if (! $this->_testParameter($parameters[$i], $this->_expected[$i])) {
00066                 return false;
00067             }
00068         }
00069         return true;
00070     }
00071 
00080     function _testParameter($parameter, $expected) {
00081         $comparison = $this->_coerceToExpectation($expected);
00082         return $comparison->test($parameter);
00083     }
00084 
00092     function testMessage($parameters) {
00093         if ($this->test($parameters)) {
00094             return "Expectation of " . count($this->_expected) .
00095                     " arguments of [" . $this->_renderArguments($this->_expected) .
00096                     "] is correct";
00097         } else {
00098             return $this->_describeDifference($this->_expected, $parameters);
00099         }
00100     }
00101 
00110     function _describeDifference($expected, $parameters) {
00111         if (count($expected) != count($parameters)) {
00112             return "Expected " . count($expected) .
00113                     " arguments of [" . $this->_renderArguments($expected) .
00114                     "] but got " . count($parameters) .
00115                     " arguments of [" . $this->_renderArguments($parameters) . "]";
00116         }
00117         $messages = array();
00118         for ($i = 0; $i < count($expected); $i++) {
00119             $comparison = $this->_coerceToExpectation($expected[$i]);
00120             if (! $comparison->test($parameters[$i])) {
00121                 $messages[] = "parameter " . ($i + 1) . " with [" .
00122                         $comparison->overlayMessage($parameters[$i], $this->_getDumper()) . "]";
00123             }
00124         }
00125         return "Parameter expectation differs at " . implode(" and ", $messages);
00126     }
00127 
00136     function _coerceToExpectation($expected) {
00137         if (SimpleExpectation::isExpectation($expected)) {
00138             return $expected;
00139         }
00140         return new IdenticalExpectation($expected);
00141     }
00142 
00150     function _renderArguments($args) {
00151         $descriptions = array();
00152         if (is_array($args)) {
00153             foreach ($args as $arg) {
00154                 $dumper = new SimpleDumper();
00155                 $descriptions[] = $dumper->describeValue($arg);
00156             }
00157         }
00158         return implode(', ', $descriptions);
00159     }
00160 }
00161 
00167 class CallCountExpectation extends SimpleExpectation {
00168     var $_method;
00169     var $_count;
00170 
00178     function CallCountExpectation($method, $count, $message = '%s') {
00179         $this->_method = $method;
00180         $this->_count = $count;
00181         $this->SimpleExpectation($message);
00182     }
00183 
00190     function test($compare) {
00191         return ($this->_count == $compare);
00192     }
00193 
00200     function testMessage($compare) {
00201         return 'Expected call count for [' . $this->_method .
00202                 '] was [' . $this->_count .
00203                 '] got [' . $compare . ']';
00204     }
00205 }
00206 
00212 class MinimumCallCountExpectation extends SimpleExpectation {
00213     var $_method;
00214     var $_count;
00215 
00223     function MinimumCallCountExpectation($method, $count, $message = '%s') {
00224         $this->_method = $method;
00225         $this->_count = $count;
00226         $this->SimpleExpectation($message);
00227     }
00228 
00235     function test($compare) {
00236         return ($this->_count <= $compare);
00237     }
00238 
00245     function testMessage($compare) {
00246         return 'Minimum call count for [' . $this->_method .
00247                 '] was [' . $this->_count .
00248                 '] got [' . $compare . ']';
00249     }
00250 }
00251 
00257 class MaximumCallCountExpectation extends SimpleExpectation {
00258     var $_method;
00259     var $_count;
00260 
00268     function MaximumCallCountExpectation($method, $count, $message = '%s') {
00269         $this->_method = $method;
00270         $this->_count = $count;
00271         $this->SimpleExpectation($message);
00272     }
00273 
00280     function test($compare) {
00281         return ($this->_count >= $compare);
00282     }
00283 
00290     function testMessage($compare) {
00291         return 'Maximum call count for [' . $this->_method .
00292                 '] was [' . $this->_count .
00293                 '] got [' . $compare . ']';
00294     }
00295 }
00296 
00303 class SimpleSignatureMap {
00304     var $_map;
00305 
00310     function SimpleSignatureMap() {
00311         $this->_map = array();
00312     }
00313 
00320     function add($parameters, &$action) {
00321         $place = count($this->_map);
00322         $this->_map[$place] = array();
00323         $this->_map[$place]['params'] = new ParametersExpectation($parameters);
00324         $this->_map[$place]['content'] = &$action;
00325     }
00326 
00336     function &findFirstAction($parameters) {
00337         $slot = $this->_findFirstSlot($parameters);
00338         if (isset($slot) && isset($slot['content'])) {
00339             return $slot['content'];
00340         }
00341         $null = null;
00342         return $null;
00343     }
00344 
00353     function isMatch($parameters) {
00354         return ($this->_findFirstSlot($parameters) != null);
00355     }
00356     
00366     function test(&$test, $parameters, $message) {
00367     }
00368 
00376     function &_findFirstSlot($parameters) {
00377         $count = count($this->_map);
00378         for ($i = 0; $i < $count; $i++) {
00379             if ($this->_map[$i]["params"]->test($parameters)) {
00380                 return $this->_map[$i];
00381             }
00382         }
00383         $null = null;
00384         return $null;
00385     }
00386 }
00387 
00396 class SimpleCallSchedule {
00397     var $_wildcard = MOCK_ANYTHING;
00398     var $_always;
00399     var $_at;
00400     
00405     function SimpleCallSchedule() {
00406         $this->_always = array();
00407         $this->_at = array();
00408     }
00409     
00419     function register($method, $args, &$action) {
00420         $args = $this->_replaceWildcards($args);
00421         $method = strtolower($method);
00422         if (! isset($this->_always[$method])) {
00423             $this->_always[$method] = new SimpleSignatureMap();
00424         }
00425         $this->_always[$method]->add($args, $action);
00426     }
00427     
00438     function registerAt($step, $method, $args, &$action) {
00439         $args = $this->_replaceWildcards($args);
00440         $method = strtolower($method);
00441         if (! isset($this->_at[$method])) {
00442             $this->_at[$method] = array();
00443         }
00444         if (! isset($this->_at[$method][$step])) {
00445             $this->_at[$method][$step] = new SimpleSignatureMap();
00446         }
00447         $this->_at[$method][$step]->add($args, $action);
00448     }
00449     
00450     function expectArguments($method, $args, $message) {
00451         $args = $this->_replaceWildcards($args);
00452         $message .= Mock::getExpectationLine();
00453         $this->_expected_args[strtolower($method)] =
00454                 new ParametersExpectation($args, $message);
00455 
00456     }
00457     
00468     function &respond($step, $method, $args) {
00469         $method = strtolower($method);
00470         if (isset($this->_at[$method][$step])) {
00471             if ($this->_at[$method][$step]->isMatch($args)) {
00472                 $action = &$this->_at[$method][$step]->findFirstAction($args);
00473                 if (isset($action)) {
00474                     return $action->act();
00475                 }
00476             }
00477         }
00478         if (isset($this->_always[$method])) {
00479             $action = &$this->_always[$method]->findFirstAction($args);
00480             if (isset($action)) {
00481                 return $action->act();
00482             }
00483         }
00484         $null = null;
00485         return $null;
00486     }
00487     
00496     function _replaceWildcards($args) {
00497         if ($args === false) {
00498             return false;
00499         }
00500         for ($i = 0; $i < count($args); $i++) {
00501             if ($args[$i] === $this->_wildcard) {
00502                 $args[$i] = new AnythingExpectation();
00503             }
00504         }
00505         return $args;
00506     }
00507 }
00508 
00515 class SimpleByReference {
00516     var $_reference;
00517     
00523     function SimpleByReference(&$reference) {
00524         $this->_reference = &$reference;
00525     }
00526     
00532     function &act() {
00533         return $this->_reference;
00534     }
00535 }
00536 
00543 class SimpleByValue {
00544     var $_value;
00545     
00553     function SimpleByValue($value) {
00554         $this->_value = $value;
00555     }
00556     
00562     function &act() {
00563         $dummy = $this->_value;
00564         return $dummy;
00565     }
00566 }
00567 
00574 class SimpleThrower {
00575     var $_exception;
00576     
00582     function SimpleThrower($exception) {
00583         $this->_exception = $exception;
00584     }
00585     
00590     function act() {
00591         eval('throw $this->_exception;');
00592     }
00593 }
00594 
00601 class SimpleErrorThrower {
00602     var $_error;
00603     var $_severity;
00604     
00611     function SimpleErrorThrower($error, $severity) {
00612         $this->_error = $error;
00613         $this->_severity = $severity;
00614     }
00615     
00621     function &act() {
00622         trigger_error($this->_error, $this->_severity);
00623         $null = null;
00624         return $null;
00625     }
00626 }
00627 
00639 class SimpleMock {
00640     var $_actions;
00641     var $_wildcard = MOCK_ANYTHING;
00642     var $_is_strict = true;
00643     var $_call_counts;
00644     var $_expected_counts;
00645     var $_max_counts;
00646     var $_expected_args;
00647     var $_expected_args_at;
00648 
00654     function SimpleMock() {
00655         $this->_actions = new SimpleCallSchedule();
00656         $this->_expectations = new SimpleCallSchedule();
00657         $this->_call_counts = array();
00658         $this->_expected_counts = array();
00659         $this->_max_counts = array();
00660         $this->_expected_args = array();
00661         $this->_expected_args_at = array();
00662         $test = &$this->_getCurrentTestCase();
00663         $test->tell($this);
00664     }
00665     
00671     function disableExpectationNameChecks() {
00672         $this->_is_strict = false;
00673     }
00674 
00680     function &_getCurrentTestCase() {
00681         $context = &SimpleTest::getContext();
00682         return $context->getTest();
00683     }
00684 
00692     function _checkArgumentsIsArray($args, $task) {
00693         if (! is_array($args)) {
00694             trigger_error(
00695                 "Cannot $task as \$args parameter is not an array",
00696                 E_USER_ERROR);
00697         }
00698     }
00699 
00707     function _dieOnNoMethod($method, $task) {
00708         if ($this->_is_strict && ! method_exists($this, $method)) {
00709             trigger_error(
00710                     "Cannot $task as no ${method}() in class " . get_class($this),
00711                     E_USER_ERROR);
00712         }
00713     }
00714 
00723     function _replaceWildcards($args) {
00724         if ($args === false) {
00725             return false;
00726         }
00727         for ($i = 0; $i < count($args); $i++) {
00728             if ($args[$i] === $this->_wildcard) {
00729                 $args[$i] = new AnythingExpectation();
00730             }
00731         }
00732         return $args;
00733     }
00734 
00741     function _addCall($method, $args) {
00742         if (! isset($this->_call_counts[$method])) {
00743             $this->_call_counts[$method] = 0;
00744         }
00745         $this->_call_counts[$method]++;
00746     }
00747 
00754     function getCallCount($method) {
00755         $this->_dieOnNoMethod($method, "get call count");
00756         $method = strtolower($method);
00757         if (! isset($this->_call_counts[$method])) {
00758             return 0;
00759         }
00760         return $this->_call_counts[$method];
00761     }
00762 
00772     function setReturnValue($method, $value, $args = false) {
00773         $this->_dieOnNoMethod($method, "set return value");
00774         $this->_actions->register($method, $args, new SimpleByValue($value));
00775     }
00776 
00791     function setReturnValueAt($timing, $method, $value, $args = false) {
00792         $this->_dieOnNoMethod($method, "set return value sequence");
00793         $this->_actions->registerAt($timing, $method, $args, new SimpleByValue($value));
00794     }
00795 
00805     function setReturnReference($method, &$reference, $args = false) {
00806         $this->_dieOnNoMethod($method, "set return reference");
00807         $this->_actions->register($method, $args, new SimpleByReference($reference));
00808     }
00809 
00824     function setReturnReferenceAt($timing, $method, &$reference, $args = false) {
00825         $this->_dieOnNoMethod($method, "set return reference sequence");
00826         $this->_actions->registerAt($timing, $method, $args, new SimpleByReference($reference));
00827     }
00828 
00840     function expect($method, $args, $message = '%s') {
00841         $this->_dieOnNoMethod($method, 'set expected arguments');
00842         $this->_checkArgumentsIsArray($args, 'set expected arguments');
00843         $this->_expectations->expectArguments($method, $args, $message);
00844         $args = $this->_replaceWildcards($args);
00845         $message .= Mock::getExpectationLine();
00846         $this->_expected_args[strtolower($method)] =
00847                 new ParametersExpectation($args, $message);
00848     }
00849 
00853     function expectArguments($method, $args, $message = '%s') {
00854         return $this->expect($method, $args, $message);
00855     }
00856 
00870     function expectAt($timing, $method, $args, $message = '%s') {
00871         $this->_dieOnNoMethod($method, 'set expected arguments at time');
00872         $this->_checkArgumentsIsArray($args, 'set expected arguments at time');
00873         $args = $this->_replaceWildcards($args);
00874         if (! isset($this->_expected_args_at[$timing])) {
00875             $this->_expected_args_at[$timing] = array();
00876         }
00877         $method = strtolower($method);
00878         $message .= Mock::getExpectationLine();
00879         $this->_expected_args_at[$timing][$method] =
00880                 new ParametersExpectation($args, $message);
00881     }
00882 
00886     function expectArgumentsAt($timing, $method, $args, $message = '%s') {
00887         return $this->expectAt($timing, $method, $args, $message);
00888     }
00889 
00900     function expectCallCount($method, $count, $message = '%s') {
00901         $this->_dieOnNoMethod($method, 'set expected call count');
00902         $message .= Mock::getExpectationLine();
00903         $this->_expected_counts[strtolower($method)] =
00904                 new CallCountExpectation($method, $count, $message);
00905     }
00906 
00916     function expectMaximumCallCount($method, $count, $message = '%s') {
00917         $this->_dieOnNoMethod($method, 'set maximum call count');
00918         $message .= Mock::getExpectationLine();
00919         $this->_max_counts[strtolower($method)] =
00920                 new MaximumCallCountExpectation($method, $count, $message);
00921     }
00922 
00932     function expectMinimumCallCount($method, $count, $message = '%s') {
00933         $this->_dieOnNoMethod($method, 'set minimum call count');
00934         $message .= Mock::getExpectationLine();
00935         $this->_expected_counts[strtolower($method)] =
00936                 new MinimumCallCountExpectation($method, $count, $message);
00937     }
00938 
00946     function expectNever($method, $message = '%s') {
00947         $this->expectMaximumCallCount($method, 0, $message);
00948     }
00949 
00959     function expectOnce($method, $args = false, $message = '%s') {
00960         $this->expectCallCount($method, 1, $message);
00961         if ($args !== false) {
00962             $this->expect($method, $args, $message);
00963         }
00964     }
00965 
00975     function expectAtLeastOnce($method, $args = false, $message = '%s') {
00976         $this->expectMinimumCallCount($method, 1, $message);
00977         if ($args !== false) {
00978             $this->expect($method, $args, $message);
00979         }
00980     }
00981     
00987     function throwOn($method, $exception = false, $args = false) {
00988         $this->_dieOnNoMethod($method, "throw on");
00989         $this->_actions->register($method, $args,
00990                 new SimpleThrower($exception ? $exception : new Exception()));
00991     }
00992     
00997     function throwAt($timing, $method, $exception = false, $args = false) {
00998         $this->_dieOnNoMethod($method, "throw at");
00999         $this->_actions->registerAt($timing, $method, $args,
01000                 new SimpleThrower($exception ? $exception : new Exception()));
01001     }
01002     
01007     function errorOn($method, $error = 'A mock error', $args = false, $severity = E_USER_ERROR) {
01008         $this->_dieOnNoMethod($method, "error on");
01009         $this->_actions->register($method, $args, new SimpleErrorThrower($error, $severity));
01010     }
01011     
01016     function errorAt($timing, $method, $error = 'A mock error', $args = false, $severity = E_USER_ERROR) {
01017         $this->_dieOnNoMethod($method, "error at");
01018         $this->_actions->registerAt($timing, $method, $args, new SimpleErrorThrower($error, $severity));
01019     }
01020 
01024     function tally() {
01025     }
01026 
01036     function atTestEnd($test_method, &$test) {
01037         foreach ($this->_expected_counts as $method => $expectation) {
01038             $test->assert($expectation, $this->getCallCount($method));
01039         }
01040         foreach ($this->_max_counts as $method => $expectation) {
01041             if ($expectation->test($this->getCallCount($method))) {
01042                 $test->assert($expectation, $this->getCallCount($method));
01043             }
01044         }
01045     }
01046 
01057     function &_invoke($method, $args) {
01058         $method = strtolower($method);
01059         $step = $this->getCallCount($method);
01060         $this->_addCall($method, $args);
01061         $this->_checkExpectations($method, $args, $step);
01062         $result = &$this->_emulateCall($method, $args, $step);
01063         return $result;
01064     }
01065     
01077     function &_emulateCall($method, $args, $step) {
01078         return $this->_actions->respond($step, $method, $args);
01079     }
01080 
01089     function _checkExpectations($method, $args, $timing) {
01090         $test = &$this->_getCurrentTestCase();
01091         if (isset($this->_max_counts[$method])) {
01092             if (! $this->_max_counts[$method]->test($timing + 1)) {
01093                 $test->assert($this->_max_counts[$method], $timing + 1);
01094             }
01095         }
01096         if (isset($this->_expected_args_at[$timing][$method])) {
01097             $test->assert(
01098                     $this->_expected_args_at[$timing][$method],
01099                     $args,
01100                     "Mock method [$method] at [$timing] -> %s");
01101         } elseif (isset($this->_expected_args[$method])) {
01102             $test->assert(
01103                     $this->_expected_args[$method],
01104                     $args,
01105                     "Mock method [$method] -> %s");
01106         }
01107     }
01108 }
01109 
01116 class Mock {
01117 
01122     function Mock() {
01123         trigger_error('Mock factory methods are static.');
01124     }
01125 
01141     function generate($class, $mock_class = false, $methods = false) {
01142         $generator = new MockGenerator($class, $mock_class);
01143         return $generator->generateSubclass($methods);
01144     }
01145 
01158     function generatePartial($class, $mock_class, $methods) {
01159         $generator = new MockGenerator($class, $mock_class);
01160         return $generator->generatePartial($methods);
01161     }
01162 
01168     function getExpectationLine() {
01169         $trace = new SimpleStackTrace(array('expect'));
01170         return $trace->traceMethod();
01171     }
01172 }
01173 
01179 class Stub extends Mock {
01180 }
01181 
01187 class MockGenerator {
01188     var $_class;
01189     var $_mock_class;
01190     var $_mock_base;
01191     var $_reflection;
01192 
01199     function MockGenerator($class, $mock_class) {
01200         $this->_class = $class;
01201         $this->_mock_class = $mock_class;
01202         if (! $this->_mock_class) {
01203             $this->_mock_class = 'Mock' . $this->_class;
01204         }
01205         $this->_mock_base = SimpleTest::getMockBaseClass();
01206         $this->_reflection = new SimpleReflection($this->_class);
01207     }
01208 
01219     function generate($methods) {
01220         if (! $this->_reflection->classOrInterfaceExists()) {
01221             return false;
01222         }
01223         $mock_reflection = new SimpleReflection($this->_mock_class);
01224         if ($mock_reflection->classExistsSansAutoload()) {
01225             return false;
01226         }
01227         $code = $this->_createClassCode($methods ? $methods : array());
01228         return eval("$code return \$code;");
01229     }
01230     
01242     function generateSubclass($methods) {
01243         if (! $this->_reflection->classOrInterfaceExists()) {
01244             return false;
01245         }
01246         $mock_reflection = new SimpleReflection($this->_mock_class);
01247         if ($mock_reflection->classExistsSansAutoload()) {
01248             return false;
01249         }
01250         if ($this->_reflection->isInterface() || $this->_reflection->hasFinal()) {
01251             $code = $this->_createClassCode($methods ? $methods : array());
01252             return eval("$code return \$code;");
01253         } else {
01254             $code = $this->_createSubclassCode($methods ? $methods : array());
01255             return eval("$code return \$code;");
01256         }
01257     }
01258 
01268     function generatePartial($methods) {
01269         if (! $this->_reflection->classExists($this->_class)) {
01270             return false;
01271         }
01272         $mock_reflection = new SimpleReflection($this->_mock_class);
01273         if ($mock_reflection->classExistsSansAutoload()) {
01274             trigger_error('Partial mock class [' . $this->_mock_class . '] already exists');
01275             return false;
01276         }
01277         $code = $this->_extendClassCode($methods);
01278         return eval("$code return \$code;");
01279     }
01280 
01287     function _createClassCode($methods) {
01288         $implements = '';
01289         $interfaces = $this->_reflection->getInterfaces();
01290         if (function_exists('spl_classes')) {
01291             $interfaces = array_diff($interfaces, array('Traversable'));
01292         }
01293         if (count($interfaces) > 0) {
01294             $implements = 'implements ' . implode(', ', $interfaces);
01295         }
01296         $code = "class " . $this->_mock_class . " extends " . $this->_mock_base . " $implements {\n";
01297         $code .= "    function " . $this->_mock_class . "() {\n";
01298         $code .= "        \$this->" . $this->_mock_base . "();\n";
01299         $code .= "    }\n";
01300         if (in_array('__construct', $this->_reflection->getMethods())) {
01301             $code .= "    " . $this->_reflection->getSignature('__construct') . " {\n";
01302             $code .= "        \$this->" . $this->_mock_base . "();\n";
01303             $code .= "    }\n";
01304         }
01305         $code .= $this->_createHandlerCode($methods);
01306         $code .= "}\n";
01307         return $code;
01308     }
01309 
01317     function _createSubclassCode($methods) {
01318         $code  = "class " . $this->_mock_class . " extends " . $this->_class . " {\n";
01319         $code .= "    var \$_mock;\n";
01320         $code .= $this->_addMethodList(array_merge($methods, $this->_reflection->getMethods()));
01321         $code .= "\n";
01322         $code .= "    function " . $this->_mock_class . "() {\n";
01323         $code .= "        \$this->_mock = new " . $this->_mock_base . "();\n";
01324         $code .= "        \$this->_mock->disableExpectationNameChecks();\n";
01325         $code .= "    }\n";
01326         $code .= $this->_chainMockReturns();
01327         $code .= $this->_chainMockExpectations();
01328         $code .= $this->_chainThrowMethods();
01329         $code .= $this->_overrideMethods($this->_reflection->getMethods());
01330         $code .= $this->_createNewMethodCode($methods);
01331         $code .= "}\n";
01332         return $code;
01333     }
01334 
01343     function _extendClassCode($methods) {
01344         $code  = "class " . $this->_mock_class . " extends " . $this->_class . " {\n";
01345         $code .= "    var \$_mock;\n";
01346         $code .= $this->_addMethodList($methods);
01347         $code .= "\n";
01348         $code .= "    function " . $this->_mock_class . "() {\n";
01349         $code .= "        \$this->_mock = new " . $this->_mock_base . "();\n";
01350         $code .= "        \$this->_mock->disableExpectationNameChecks();\n";
01351         $code .= "    }\n";
01352         $code .= $this->_chainMockReturns();
01353         $code .= $this->_chainMockExpectations();
01354         $code .= $this->_chainThrowMethods();
01355         $code .= $this->_overrideMethods($methods);
01356         $code .= "}\n";
01357         return $code;
01358     }
01359 
01368     function _createHandlerCode($methods) {
01369         $code = '';
01370         $methods = array_merge($methods, $this->_reflection->getMethods());
01371         foreach ($methods as $method) {
01372             if ($this->_isConstructor($method)) {
01373                 continue;
01374             }
01375             $mock_reflection = new SimpleReflection($this->_mock_base);
01376             if (in_array($method, $mock_reflection->getMethods())) {
01377                 continue;
01378             }
01379             $code .= "    " . $this->_reflection->getSignature($method) . " {\n";
01380             $code .= "        \$args = func_get_args();\n";
01381             $code .= "        \$result = &\$this->_invoke(\"$method\", \$args);\n";
01382             $code .= "        return \$result;\n";
01383             $code .= "    }\n";
01384         }
01385         return $code;
01386     }
01387 
01396     function _createNewMethodCode($methods) {
01397         $code = '';
01398         foreach ($methods as $method) {
01399             if ($this->_isConstructor($method)) {
01400                 continue;
01401             }
01402             $mock_reflection = new SimpleReflection($this->_mock_base);
01403             if (in_array($method, $mock_reflection->getMethods())) {
01404                 continue;
01405             }
01406             $code .= "    " . $this->_reflection->getSignature($method) . " {\n";
01407             $code .= "        \$args = func_get_args();\n";
01408             $code .= "        \$result = &\$this->_mock->_invoke(\"$method\", \$args);\n";
01409             $code .= "        return \$result;\n";
01410             $code .= "    }\n";
01411         }
01412         return $code;
01413     }
01414 
01422     function _isConstructor($method) {
01423         return in_array(
01424                 strtolower($method),
01425                 array('__construct', '__destruct'));
01426     }
01427 
01434     function _addMethodList($methods) {
01435         return "    var \$_mocked_methods = array('" .
01436                 implode("', '", array_map('strtolower', $methods)) .
01437                 "');\n";
01438     }
01439 
01446     function _bailOutIfNotMocked($alias) {
01447         $code  = "        if (! in_array(strtolower($alias), \$this->_mocked_methods)) {\n";
01448         $code .= "            trigger_error(\"Method [$alias] is not mocked\");\n";
01449         $code .= "            \$null = null;\n";
01450         $code .= "            return \$null;\n";
01451         $code .= "        }\n";
01452         return $code;
01453     }
01454 
01461     function _chainMockReturns() {
01462         $code  = "    function setReturnValue(\$method, \$value, \$args = false) {\n";
01463         $code .= $this->_bailOutIfNotMocked("\$method");
01464         $code .= "        \$this->_mock->setReturnValue(\$method, \$value, \$args);\n";
01465         $code .= "    }\n";
01466         $code .= "    function setReturnValueAt(\$timing, \$method, \$value, \$args = false) {\n";
01467         $code .= $this->_bailOutIfNotMocked("\$method");
01468         $code .= "        \$this->_mock->setReturnValueAt(\$timing, \$method, \$value, \$args);\n";
01469         $code .= "    }\n";
01470         $code .= "    function setReturnReference(\$method, &\$ref, \$args = false) {\n";
01471         $code .= $this->_bailOutIfNotMocked("\$method");
01472         $code .= "        \$this->_mock->setReturnReference(\$method, \$ref, \$args);\n";
01473         $code .= "    }\n";
01474         $code .= "    function setReturnReferenceAt(\$timing, \$method, &\$ref, \$args = false) {\n";
01475         $code .= $this->_bailOutIfNotMocked("\$method");
01476         $code .= "        \$this->_mock->setReturnReferenceAt(\$timing, \$method, \$ref, \$args);\n";
01477         $code .= "    }\n";
01478         return $code;
01479     }
01480 
01487     function _chainMockExpectations() {
01488         $code  = "    function expect(\$method, \$args = false, \$msg = '%s') {\n";
01489         $code .= $this->_bailOutIfNotMocked("\$method");
01490         $code .= "        \$this->_mock->expect(\$method, \$args, \$msg);\n";
01491         $code .= "    }\n";
01492         $code .= "    function expectArguments(\$method, \$args = false, \$msg = '%s') {\n";
01493         $code .= $this->_bailOutIfNotMocked("\$method");
01494         $code .= "        \$this->_mock->expectArguments(\$method, \$args, \$msg);\n";
01495         $code .= "    }\n";
01496         $code .= "    function expectAt(\$timing, \$method, \$args = false, \$msg = '%s') {\n";
01497         $code .= $this->_bailOutIfNotMocked("\$method");
01498         $code .= "        \$this->_mock->expectArgumentsAt(\$timing, \$method, \$args, \$msg);\n";
01499         $code .= "    }\n";
01500         $code .= "    function expectArgumentsAt(\$timing, \$method, \$args = false, \$msg = '%s') {\n";
01501         $code .= $this->_bailOutIfNotMocked("\$method");
01502         $code .= "        \$this->_mock->expectArgumentsAt(\$timing, \$method, \$args, \$msg);\n";
01503         $code .= "    }\n";
01504         $code .= "    function expectCallCount(\$method, \$count) {\n";
01505         $code .= $this->_bailOutIfNotMocked("\$method");
01506         $code .= "        \$this->_mock->expectCallCount(\$method, \$count, \$msg = '%s');\n";
01507         $code .= "    }\n";
01508         $code .= "    function expectMaximumCallCount(\$method, \$count, \$msg = '%s') {\n";
01509         $code .= $this->_bailOutIfNotMocked("\$method");
01510         $code .= "        \$this->_mock->expectMaximumCallCount(\$method, \$count, \$msg = '%s');\n";
01511         $code .= "    }\n";
01512         $code .= "    function expectMinimumCallCount(\$method, \$count, \$msg = '%s') {\n";
01513         $code .= $this->_bailOutIfNotMocked("\$method");
01514         $code .= "        \$this->_mock->expectMinimumCallCount(\$method, \$count, \$msg = '%s');\n";
01515         $code .= "    }\n";
01516         $code .= "    function expectNever(\$method) {\n";
01517         $code .= $this->_bailOutIfNotMocked("\$method");
01518         $code .= "        \$this->_mock->expectNever(\$method);\n";
01519         $code .= "    }\n";
01520         $code .= "    function expectOnce(\$method, \$args = false, \$msg = '%s') {\n";
01521         $code .= $this->_bailOutIfNotMocked("\$method");
01522         $code .= "        \$this->_mock->expectOnce(\$method, \$args, \$msg);\n";
01523         $code .= "    }\n";
01524         $code .= "    function expectAtLeastOnce(\$method, \$args = false, \$msg = '%s') {\n";
01525         $code .= $this->_bailOutIfNotMocked("\$method");
01526         $code .= "        \$this->_mock->expectAtLeastOnce(\$method, \$args, \$msg);\n";
01527         $code .= "    }\n";
01528         $code .= "    function tally() {\n";
01529         $code .= "    }\n";
01530         return $code;
01531     }
01532     
01538     function _chainThrowMethods() {
01539         $code  = "    function throwOn(\$method, \$exception = false, \$args = false) {\n";
01540         $code .= $this->_bailOutIfNotMocked("\$method");
01541         $code .= "        \$this->_mock->throwOn(\$method, \$exception, \$args);\n";
01542         $code .= "    }\n";
01543         $code .= "    function throwAt(\$timing, \$method, \$exception = false, \$args = false) {\n";
01544         $code .= $this->_bailOutIfNotMocked("\$method");
01545         $code .= "        \$this->_mock->throwAt(\$timing, \$method, \$exception, \$args);\n";
01546         $code .= "    }\n";
01547         $code .= "    function errorOn(\$method, \$error = 'A mock error', \$args = false, \$severity = E_USER_ERROR) {\n";
01548         $code .= $this->_bailOutIfNotMocked("\$method");
01549         $code .= "        \$this->_mock->errorOn(\$method, \$error, \$args, \$severity);\n";
01550         $code .= "    }\n";
01551         $code .= "    function errorAt(\$timing, \$method, \$error = 'A mock error', \$args = false, \$severity = E_USER_ERROR) {\n";
01552         $code .= $this->_bailOutIfNotMocked("\$method");
01553         $code .= "        \$this->_mock->errorAt(\$timing, \$method, \$error, \$args, \$severity);\n";
01554         $code .= "    }\n";
01555         return $code;
01556     }
01557 
01566     function _overrideMethods($methods) {
01567         $code = "";
01568         foreach ($methods as $method) {
01569             if ($this->_isConstructor($method)) {
01570                 continue;
01571             }
01572             $code .= "    " . $this->_reflection->getSignature($method) . " {\n";
01573             $code .= "        \$args = func_get_args();\n";
01574             $code .= "        \$result = &\$this->_mock->_invoke(\"$method\", \$args);\n";
01575             $code .= "        return \$result;\n";
01576             $code .= "    }\n";
01577         }
01578         return $code;
01579     }
01580 }
01581 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations