annotate vendor/phpunit/phpunit-mock-objects/src/Framework/MockObject/InvocationMocker.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /*
Chris@0 3 * This file is part of the PHPUnit_MockObject package.
Chris@0 4 *
Chris@0 5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
Chris@0 6 *
Chris@0 7 * For the full copyright and license information, please view the LICENSE
Chris@0 8 * file that was distributed with this source code.
Chris@0 9 */
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Mocker for invocations which are sent from
Chris@0 13 * PHPUnit_Framework_MockObject_MockObject objects.
Chris@0 14 *
Chris@0 15 * Keeps track of all expectations and stubs as well as registering
Chris@0 16 * identifications for builders.
Chris@0 17 *
Chris@0 18 * @since Class available since Release 1.0.0
Chris@0 19 */
Chris@0 20 class PHPUnit_Framework_MockObject_InvocationMocker implements PHPUnit_Framework_MockObject_Stub_MatcherCollection, PHPUnit_Framework_MockObject_Invokable, PHPUnit_Framework_MockObject_Builder_Namespace
Chris@0 21 {
Chris@0 22 /**
Chris@0 23 * @var PHPUnit_Framework_MockObject_Matcher_Invocation[]
Chris@0 24 */
Chris@0 25 protected $matchers = array();
Chris@0 26
Chris@0 27 /**
Chris@0 28 * @var PHPUnit_Framework_MockObject_Builder_Match[]
Chris@0 29 */
Chris@0 30 protected $builderMap = array();
Chris@0 31
Chris@0 32 /**
Chris@0 33 * @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
Chris@0 34 */
Chris@0 35 public function addMatcher(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
Chris@0 36 {
Chris@0 37 $this->matchers[] = $matcher;
Chris@0 38 }
Chris@0 39
Chris@0 40 /**
Chris@0 41 * @since Method available since Release 1.1.0
Chris@0 42 */
Chris@0 43 public function hasMatchers()
Chris@0 44 {
Chris@0 45 foreach ($this->matchers as $matcher) {
Chris@0 46 if ($matcher->hasMatchers()) {
Chris@0 47 return true;
Chris@0 48 }
Chris@0 49 }
Chris@0 50
Chris@0 51 return false;
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * @param mixed $id
Chris@0 56 * @return bool|null
Chris@0 57 */
Chris@0 58 public function lookupId($id)
Chris@0 59 {
Chris@0 60 if (isset($this->builderMap[$id])) {
Chris@0 61 return $this->builderMap[$id];
Chris@0 62 }
Chris@0 63
Chris@0 64 return;
Chris@0 65 }
Chris@0 66
Chris@0 67 /**
Chris@0 68 * @param mixed $id
Chris@0 69 * @param PHPUnit_Framework_MockObject_Builder_Match $builder
Chris@0 70 * @throws PHPUnit_Framework_Exception
Chris@0 71 */
Chris@0 72 public function registerId($id, PHPUnit_Framework_MockObject_Builder_Match $builder)
Chris@0 73 {
Chris@0 74 if (isset($this->builderMap[$id])) {
Chris@0 75 throw new PHPUnit_Framework_Exception(
Chris@0 76 'Match builder with id <' . $id . '> is already registered.'
Chris@0 77 );
Chris@0 78 }
Chris@0 79
Chris@0 80 $this->builderMap[$id] = $builder;
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
Chris@0 85 * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
Chris@0 86 */
Chris@0 87 public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
Chris@0 88 {
Chris@0 89 return new PHPUnit_Framework_MockObject_Builder_InvocationMocker(
Chris@0 90 $this,
Chris@0 91 $matcher
Chris@0 92 );
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * @param PHPUnit_Framework_MockObject_Invocation $invocation
Chris@0 97 * @return mixed
Chris@0 98 */
Chris@0 99 public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
Chris@0 100 {
Chris@0 101 $exception = null;
Chris@0 102 $hasReturnValue = false;
Chris@0 103
Chris@0 104 if (strtolower($invocation->methodName) == '__tostring') {
Chris@0 105 $returnValue = '';
Chris@0 106 } else {
Chris@0 107 $returnValue = null;
Chris@0 108 }
Chris@0 109
Chris@0 110 foreach ($this->matchers as $match) {
Chris@0 111 try {
Chris@0 112 if ($match->matches($invocation)) {
Chris@0 113 $value = $match->invoked($invocation);
Chris@0 114
Chris@0 115 if (!$hasReturnValue) {
Chris@0 116 $returnValue = $value;
Chris@0 117 $hasReturnValue = true;
Chris@0 118 }
Chris@0 119 }
Chris@0 120 } catch (Exception $e) {
Chris@0 121 $exception = $e;
Chris@0 122 }
Chris@0 123 }
Chris@0 124
Chris@0 125 if ($exception !== null) {
Chris@0 126 throw $exception;
Chris@0 127 }
Chris@0 128
Chris@0 129 return $returnValue;
Chris@0 130 }
Chris@0 131
Chris@0 132 /**
Chris@0 133 * @param PHPUnit_Framework_MockObject_Invocation $invocation
Chris@0 134 * @return bool
Chris@0 135 */
Chris@0 136 public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
Chris@0 137 {
Chris@0 138 foreach ($this->matchers as $matcher) {
Chris@0 139 if (!$matcher->matches($invocation)) {
Chris@0 140 return false;
Chris@0 141 }
Chris@0 142 }
Chris@0 143
Chris@0 144 return true;
Chris@0 145 }
Chris@0 146
Chris@0 147 /**
Chris@0 148 * @return bool
Chris@0 149 */
Chris@0 150 public function verify()
Chris@0 151 {
Chris@0 152 foreach ($this->matchers as $matcher) {
Chris@0 153 $matcher->verify();
Chris@0 154 }
Chris@0 155 }
Chris@0 156 }