Mockery.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /*
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. //require 'Yay/MockGenerator.php';
  15. //require 'Yay/SimpleInvocation.php';
  16. //require 'Yay/SimpleDescription.php';
  17. //require 'Yay/InvocationHandler.php';
  18. //require 'Yay/MockObject.php';
  19. //require 'Yay/ExpectationProvider.php';
  20. //require 'Yay/NotSatisfiedException.php';
  21. //require 'Yay/StateMachine.php';
  22. //require 'Yay/SimpleSequence.php';
  23. /**
  24. * The main Yay context.
  25. * Handles the generation of MockObjects and the Invocation of methods.
  26. * @author Chris Corbyn <chris@w3style.co.uk>
  27. * @package Yay
  28. */
  29. class Yay_Mockery implements Yay_InvocationHandler
  30. {
  31. /**
  32. * The Expectation stack which is being checked.
  33. * @var array
  34. * @access private
  35. */
  36. private $_expectations = array();
  37. /**
  38. * Invocations which are not expected by any Expectations get caught here.
  39. * @var array
  40. * @access private
  41. */
  42. private $_unexpectedInvocations = array();
  43. /**
  44. * A mock class generator.
  45. * @var Yay_MockGenerator
  46. * @access private
  47. */
  48. private $_generator;
  49. /**
  50. * Create a new Mockery.
  51. */
  52. public function __construct()
  53. {
  54. $this->_generator = Yay_MockGenerator::getInstance();
  55. }
  56. /**
  57. * Create a MockObject matching $typeHint.
  58. * If the $typeHint is an interface the Mock will implement the interface
  59. * and maintain the method signatures from that interface.
  60. * If the $typeHint is a class name the Mock will extend the class overriding
  61. * all public methods (HOWEVER, if the class contains final methods it is not
  62. * possible to override all methods and hence, the mock will have no specific
  63. * type.
  64. * @param string $typeHint
  65. * @return Yay_MockObject
  66. */
  67. public function mock($typeHint)
  68. {
  69. $className = $this->_generator->generateMock($typeHint);
  70. $reflector = new ReflectionClass($className);
  71. return $reflector->newInstance($this);
  72. }
  73. /**
  74. * Specify an Expectation (or Expectations) to check.
  75. * @param Yay_ExpectationProvider $provider
  76. */
  77. public function checking(Yay_ExpectationProvider $provider)
  78. {
  79. foreach ($provider->getExpectations() as $expectation)
  80. {
  81. $this->_expectations[] = $expectation;
  82. }
  83. }
  84. /**
  85. * Get a state machine named $name.
  86. * @param string $name
  87. * @return Yay_States
  88. */
  89. public function states($name)
  90. {
  91. return new Yay_StateMachine($name);
  92. }
  93. /**
  94. * Create a new Sequence named $name.
  95. * @param string $name
  96. * @return Yay_Sequence
  97. */
  98. public function sequence($name)
  99. {
  100. return new Yay_SimpleSequence($name);
  101. }
  102. /**
  103. * Used by YayMock internally (ignore this method!).
  104. */
  105. public function &handleInvocation(Yay_Invocation $invocation)
  106. {
  107. $ret = null;
  108. $expected = false;
  109. foreach ($this->_expectations as $expectation)
  110. {
  111. if ($expectation->isExpected($invocation))
  112. {
  113. $expected = true;
  114. if ($action = $expectation->getAction($invocation))
  115. {
  116. $ret =& $action->invoke($invocation);
  117. }
  118. break;
  119. }
  120. }
  121. if (!$expected)
  122. {
  123. $this->_unexpectedInvocations[] = $invocation;
  124. }
  125. return $ret;
  126. }
  127. /**
  128. * Assert that all Expectations are satisfied.
  129. * Throws an Exception of type Yay_NotSatisfiedException if any Expecations
  130. * are not satisfied.
  131. * @throws Yay_NotSatisfiedException
  132. */
  133. public function assertIsSatisfied()
  134. {
  135. $description = new Yay_SimpleDescription();
  136. $satisfied = true;
  137. foreach ($this->_unexpectedInvocations as $invocation)
  138. {
  139. $description->appendText('Unexpected invocation');
  140. $invocation->describeTo($description);
  141. $description->appendText(PHP_EOL);
  142. $satisfied = false;
  143. }
  144. if (!$satisfied)
  145. {
  146. $description->appendText(PHP_EOL);
  147. }
  148. foreach ($this->_expectations as $expectation)
  149. {
  150. if (!$expectation->isSatisfied())
  151. {
  152. $description->appendText('* ');
  153. $satisfied = false;
  154. }
  155. $expectation->describeTo($description);
  156. $description->appendText(PHP_EOL);
  157. }
  158. if (!$satisfied)
  159. {
  160. throw new Yay_NotSatisfiedException(
  161. 'Not all expectations were satisfied or a method was invoked unexpectedly.' .
  162. PHP_EOL . PHP_EOL . $description->toString() . PHP_EOL
  163. );
  164. }
  165. }
  166. }