PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/public_html/vendor/phpunit/phpunit-mock-objects/PHPUnit/Framework/MockObject/Matcher.php

https://bitbucket.org/rybadour/todo_list_site
PHP | 308 lines | 166 code | 47 blank | 95 comment | 28 complexity | 7a543f9b5ade8eb8ecea59c5b98c29ca MD5 | raw file
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2010-2012, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package PHPUnit_MockObject
  38. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  39. * @copyright 2010-2012 Sebastian Bergmann <sb@sebastian-bergmann.de>
  40. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  41. * @link http://github.com/sebastianbergmann/phpunit-mock-objects
  42. * @since File available since Release 1.0.0
  43. */
  44. /**
  45. * Main matcher which defines a full expectation using method, parameter and
  46. * invocation matchers.
  47. * This matcher encapsulates all the other matchers and allows the builder to
  48. * set the specific matchers when the appropriate methods are called (once(),
  49. * where() etc.).
  50. *
  51. * All properties are public so that they can easily be accessed by the builder.
  52. *
  53. * @package PHPUnit_MockObject
  54. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  55. * @copyright 2010-2012 Sebastian Bergmann <sb@sebastian-bergmann.de>
  56. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  57. * @version Release: @package_version@
  58. * @link http://github.com/sebastianbergmann/phpunit-mock-objects
  59. * @since Class available since Release 1.0.0
  60. */
  61. class PHPUnit_Framework_MockObject_Matcher implements PHPUnit_Framework_MockObject_Matcher_Invocation
  62. {
  63. /**
  64. * @var PHPUnit_Framework_MockObject_Matcher_Invocation
  65. */
  66. public $invocationMatcher;
  67. /**
  68. * @var mixed
  69. */
  70. public $afterMatchBuilderId = NULL;
  71. /**
  72. * @var boolean
  73. */
  74. public $afterMatchBuilderIsInvoked = FALSE;
  75. /**
  76. * @var PHPUnit_Framework_MockObject_Matcher_MethodName
  77. */
  78. public $methodNameMatcher = NULL;
  79. /**
  80. * @var PHPUnit_Framework_MockObject_Matcher_Parameters
  81. */
  82. public $parametersMatcher = NULL;
  83. /**
  84. * @var PHPUnit_Framework_MockObject_Stub
  85. */
  86. public $stub = NULL;
  87. /**
  88. * @param PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher
  89. */
  90. public function __construct(PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher)
  91. {
  92. $this->invocationMatcher = $invocationMatcher;
  93. }
  94. /**
  95. * @return string
  96. */
  97. public function toString()
  98. {
  99. $list = array();
  100. if ($this->invocationMatcher !== NULL) {
  101. $list[] = $this->invocationMatcher->toString();
  102. }
  103. if ($this->methodNameMatcher !== NULL) {
  104. $list[] = 'where ' . $this->methodNameMatcher->toString();
  105. }
  106. if ($this->parametersMatcher !== NULL) {
  107. $list[] = 'and ' . $this->parametersMatcher->toString();
  108. }
  109. if ($this->afterMatchBuilderId !== NULL) {
  110. $list[] = 'after ' . $this->afterMatchBuilderId;
  111. }
  112. if ($this->stub !== NULL) {
  113. $list[] = 'will ' . $this->stub->toString();
  114. }
  115. return join(' ', $list);
  116. }
  117. /**
  118. * @param PHPUnit_Framework_MockObject_Invocation $invocation
  119. * @return mixed
  120. */
  121. public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
  122. {
  123. if ($this->invocationMatcher === NULL) {
  124. throw new PHPUnit_Framework_Exception(
  125. 'No invocation matcher is set'
  126. );
  127. }
  128. if ($this->methodNameMatcher === NULL) {
  129. throw new PHPUnit_Framework_Exception('No method matcher is set');
  130. }
  131. if ($this->afterMatchBuilderId !== NULL) {
  132. $builder = $invocation->object
  133. ->__phpunit_getInvocationMocker()
  134. ->lookupId($this->afterMatchBuilderId);
  135. if (!$builder) {
  136. throw new PHPUnit_Framework_Exception(
  137. sprintf(
  138. 'No builder found for match builder identification <%s>',
  139. $this->afterMatchBuilderId
  140. )
  141. );
  142. }
  143. $matcher = $builder->getMatcher();
  144. if ($matcher && $matcher->invocationMatcher->hasBeenInvoked()) {
  145. $this->afterMatchBuilderIsInvoked = TRUE;
  146. }
  147. }
  148. $this->invocationMatcher->invoked($invocation);
  149. try {
  150. if ( $this->parametersMatcher !== NULL &&
  151. !$this->parametersMatcher->matches($invocation)) {
  152. $this->parametersMatcher->verify();
  153. }
  154. }
  155. catch (PHPUnit_Framework_ExpectationFailedException $e) {
  156. throw new PHPUnit_Framework_ExpectationFailedException(
  157. sprintf(
  158. "Expectation failed for %s when %s\n%s",
  159. $this->methodNameMatcher->toString(),
  160. $this->invocationMatcher->toString(),
  161. $e->getMessage()
  162. ),
  163. $e->getComparisonFailure()
  164. );
  165. }
  166. if ($this->stub) {
  167. return $this->stub->invoke($invocation);
  168. }
  169. return NULL;
  170. }
  171. /**
  172. * @param PHPUnit_Framework_MockObject_Invocation $invocation
  173. * @return boolean
  174. */
  175. public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
  176. {
  177. if ($this->afterMatchBuilderId !== NULL) {
  178. $builder = $invocation->object
  179. ->__phpunit_getInvocationMocker()
  180. ->lookupId($this->afterMatchBuilderId);
  181. if (!$builder) {
  182. throw new PHPUnit_Framework_Exception(
  183. sprintf(
  184. 'No builder found for match builder identification <%s>',
  185. $this->afterMatchBuilderId
  186. )
  187. );
  188. }
  189. $matcher = $builder->getMatcher();
  190. if (!$matcher) {
  191. return FALSE;
  192. }
  193. if (!$matcher->invocationMatcher->hasBeenInvoked()) {
  194. return FALSE;
  195. }
  196. }
  197. if ($this->invocationMatcher === NULL) {
  198. throw new PHPUnit_Framework_Exception(
  199. 'No invocation matcher is set'
  200. );
  201. }
  202. if ($this->methodNameMatcher === NULL) {
  203. throw new PHPUnit_Framework_Exception('No method matcher is set');
  204. }
  205. if (!$this->invocationMatcher->matches($invocation)) {
  206. return FALSE;
  207. }
  208. try {
  209. if (!$this->methodNameMatcher->matches($invocation)) {
  210. return FALSE;
  211. }
  212. }
  213. catch (PHPUnit_Framework_ExpectationFailedException $e) {
  214. throw new PHPUnit_Framework_ExpectationFailedException(
  215. sprintf(
  216. "Expectation failed for %s when %s\n%s",
  217. $this->methodNameMatcher->toString(),
  218. $this->invocationMatcher->toString(),
  219. $e->getMessage()
  220. ),
  221. $e->getComparisonFailure()
  222. );
  223. }
  224. return TRUE;
  225. }
  226. /**
  227. * @throws PHPUnit_Framework_Exception
  228. * @throws PHPUnit_Framework_ExpectationFailedException
  229. */
  230. public function verify()
  231. {
  232. if ($this->invocationMatcher === NULL) {
  233. throw new PHPUnit_Framework_Exception(
  234. 'No invocation matcher is set'
  235. );
  236. }
  237. if ($this->methodNameMatcher === NULL) {
  238. throw new PHPUnit_Framework_Exception('No method matcher is set');
  239. }
  240. try {
  241. $this->invocationMatcher->verify();
  242. if ($this->parametersMatcher === NULL) {
  243. $this->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_AnyParameters;
  244. }
  245. $invocationIsAny = get_class($this->invocationMatcher) === 'PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount';
  246. if (!$invocationIsAny) {
  247. $this->parametersMatcher->verify();
  248. }
  249. }
  250. catch (PHPUnit_Framework_ExpectationFailedException $e) {
  251. throw new PHPUnit_Framework_ExpectationFailedException(
  252. sprintf(
  253. "Expectation failed for %s when %s.\n%s",
  254. $this->methodNameMatcher->toString(),
  255. $this->invocationMatcher->toString(),
  256. $e->getMessage()
  257. )
  258. );
  259. }
  260. }
  261. }