PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/mockery/mockery/library/Mockery/Mock.php

https://gitlab.com/ealexis.t/trends
PHP | 758 lines | 382 code | 80 blank | 296 comment | 46 complexity | 8f7200d51dac3a7c5a3d56df2938a560 MD5 | raw file
  1. <?php
  2. /**
  3. * Mockery
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://github.com/padraic/mockery/blob/master/LICENSE
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to padraic@php.net so we can send you a copy immediately.
  14. *
  15. * @category Mockery
  16. * @package Mockery
  17. * @copyright Copyright (c) 2010-2014 Pádraic Brady (http://blog.astrumfutura.com)
  18. * @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
  19. */
  20. namespace Mockery;
  21. use Mockery\MockInterface;
  22. class Mock implements MockInterface
  23. {
  24. /**
  25. * Stores an array of all expectation directors for this mock
  26. *
  27. * @var array
  28. */
  29. protected $_mockery_expectations = array();
  30. /**
  31. * Flag to indicate whether we can ignore method calls missing from our
  32. * expectations
  33. *
  34. * @var bool
  35. */
  36. protected $_mockery_ignoreMissing = false;
  37. /**
  38. * Flag to indicate whether we can defer method calls missing from our
  39. * expectations
  40. *
  41. * @var bool
  42. */
  43. protected $_mockery_deferMissing = false;
  44. /**
  45. * Flag to indicate whether this mock was verified
  46. *
  47. * @var bool
  48. */
  49. protected $_mockery_verified = false;
  50. /**
  51. * Given name of the mock
  52. *
  53. * @var string
  54. */
  55. protected $_mockery_name = null;
  56. /**
  57. * Order number of allocation
  58. *
  59. * @var int
  60. */
  61. protected $_mockery_allocatedOrder = 0;
  62. /**
  63. * Current ordered number
  64. *
  65. * @var int
  66. */
  67. protected $_mockery_currentOrder = 0;
  68. /**
  69. * Ordered groups
  70. *
  71. * @var array
  72. */
  73. protected $_mockery_groups = array();
  74. /**
  75. * Mock container containing this mock object
  76. *
  77. * @var \Mockery\Container
  78. */
  79. protected $_mockery_container = null;
  80. /**
  81. * Instance of a core object on which methods are called in the event
  82. * it has been set, and an expectation for one of the object's methods
  83. * does not exist. This implements a simple partial mock proxy system.
  84. *
  85. * @var object
  86. */
  87. protected $_mockery_partial = null;
  88. /**
  89. * Flag to indicate we should ignore all expectations temporarily. Used
  90. * mainly to prevent expectation matching when in the middle of a mock
  91. * object recording session.
  92. *
  93. * @var bool
  94. */
  95. protected $_mockery_disableExpectationMatching = false;
  96. /**
  97. * Stores all stubbed public methods separate from any on-object public
  98. * properties that may exist.
  99. *
  100. * @var array
  101. */
  102. protected $_mockery_mockableProperties = array();
  103. /**
  104. * @var array
  105. */
  106. protected $_mockery_mockableMethods = array();
  107. /**
  108. * Just a local cache for this mock's target's methods
  109. *
  110. * @var ReflectionMethod[]
  111. */
  112. protected static $_mockery_methods;
  113. protected $_mockery_allowMockingProtectedMethods = false;
  114. protected $_mockery_receivedMethodCalls;
  115. /**
  116. * If shouldIgnoreMissing is called, this value will be returned on all calls to missing methods
  117. * @var mixed
  118. */
  119. protected $_mockery_defaultReturnValue = null;
  120. /**
  121. * We want to avoid constructors since class is copied to Generator.php
  122. * for inclusion on extending class definitions.
  123. *
  124. * @param \Mockery\Container $container
  125. * @param object $partialObject
  126. * @return void
  127. */
  128. public function mockery_init(\Mockery\Container $container = null, $partialObject = null)
  129. {
  130. if (is_null($container)) {
  131. $container = new \Mockery\Container;
  132. }
  133. $this->_mockery_container = $container;
  134. if (!is_null($partialObject)) {
  135. $this->_mockery_partial = $partialObject;
  136. }
  137. if (!\Mockery::getConfiguration()->mockingNonExistentMethodsAllowed()) {
  138. foreach ($this->mockery_getMethods() as $method) {
  139. if ($method->isPublic() && !$method->isStatic()) {
  140. $this->_mockery_mockableMethods[] = $method->getName();
  141. }
  142. }
  143. }
  144. }
  145. /**
  146. * Set expected method calls
  147. *
  148. * @param mixed
  149. * @return \Mockery\Expectation
  150. */
  151. public function shouldReceive()
  152. {
  153. /** @var array $nonPublicMethods */
  154. $nonPublicMethods = $this->getNonPublicMethods();
  155. $self = $this;
  156. $allowMockingProtectedMethods = $this->_mockery_allowMockingProtectedMethods;
  157. $lastExpectation = \Mockery::parseShouldReturnArgs(
  158. $this, func_get_args(), function ($method) use ($self, $nonPublicMethods, $allowMockingProtectedMethods) {
  159. $rm = $self->mockery_getMethod($method);
  160. if ($rm) {
  161. if ($rm->isPrivate()) {
  162. throw new \InvalidArgumentException("$method() cannot be mocked as it is a private method");
  163. }
  164. if (!$allowMockingProtectedMethods && $rm->isProtected()) {
  165. throw new \InvalidArgumentException("$method() cannot be mocked as it a protected method and mocking protected methods is not allowed for this mock");
  166. }
  167. }
  168. $director = $self->mockery_getExpectationsFor($method);
  169. if (!$director) {
  170. $director = new \Mockery\ExpectationDirector($method, $self);
  171. $self->mockery_setExpectationsFor($method, $director);
  172. }
  173. $expectation = new \Mockery\Expectation($self, $method);
  174. $director->addExpectation($expectation);
  175. return $expectation;
  176. }
  177. );
  178. return $lastExpectation;
  179. }
  180. /**
  181. * Shortcut method for setting an expectation that a method should not be called.
  182. *
  183. * @param mixed
  184. * @return \Mockery\Expectation
  185. */
  186. public function shouldNotReceive()
  187. {
  188. $expectation = call_user_func_array(array($this, 'shouldReceive'), func_get_args());
  189. $expectation->never();
  190. return $expectation;
  191. }
  192. /**
  193. * Allows additional methods to be mocked that do not explicitly exist on mocked class
  194. * @param String $method name of the method to be mocked
  195. * @return Mock
  196. */
  197. public function shouldAllowMockingMethod($method)
  198. {
  199. $this->_mockery_mockableMethods[] = $method;
  200. return $this;
  201. }
  202. /**
  203. * Set mock to ignore unexpected methods and return Undefined class
  204. * @param mixed $returnValue the default return value for calls to missing functions on this mock
  205. * @return Mock
  206. */
  207. public function shouldIgnoreMissing($returnValue = null)
  208. {
  209. $this->_mockery_ignoreMissing = true;
  210. $this->_mockery_defaultReturnValue = $returnValue;
  211. return $this;
  212. }
  213. public function asUndefined()
  214. {
  215. $this->_mockery_ignoreMissing = true;
  216. $this->_mockery_defaultReturnValue = new \Mockery\Undefined;
  217. return $this;
  218. }
  219. /**
  220. * @return Mock
  221. */
  222. public function shouldAllowMockingProtectedMethods()
  223. {
  224. $this->_mockery_allowMockingProtectedMethods = true;
  225. return $this;
  226. }
  227. /**
  228. * Set mock to defer unexpected methods to it's parent
  229. *
  230. * This is particularly useless for this class, as it doesn't have a parent,
  231. * but included for completeness
  232. *
  233. * @return Mock
  234. */
  235. public function shouldDeferMissing()
  236. {
  237. $this->_mockery_deferMissing = true;
  238. return $this;
  239. }
  240. /**
  241. * Create an obviously worded alias to shouldDeferMissing()
  242. *
  243. * @return Mock
  244. */
  245. public function makePartial()
  246. {
  247. return $this->shouldDeferMissing();
  248. }
  249. /**
  250. * Accepts a closure which is executed with an object recorder which proxies
  251. * to the partial source object. The intent being to record the
  252. * interactions of a concrete object as a set of expectations on the
  253. * current mock object. The partial may then be passed to a second process
  254. * to see if it fulfils the same (or exact same) contract as the original.
  255. *
  256. * @param Closure $closure
  257. */
  258. public function shouldExpect(\Closure $closure)
  259. {
  260. $recorder = new \Mockery\Recorder($this, $this->_mockery_partial);
  261. $this->_mockery_disableExpectationMatching = true;
  262. $closure($recorder);
  263. $this->_mockery_disableExpectationMatching = false;
  264. return $this;
  265. }
  266. /**
  267. * In the event shouldReceive() accepting one or more methods/returns,
  268. * this method will switch them from normal expectations to default
  269. * expectations
  270. *
  271. * @return self
  272. */
  273. public function byDefault()
  274. {
  275. foreach ($this->_mockery_expectations as $director) {
  276. $exps = $director->getExpectations();
  277. foreach ($exps as $exp) {
  278. $exp->byDefault();
  279. }
  280. }
  281. return $this;
  282. }
  283. /**
  284. * Capture calls to this mock
  285. */
  286. public function __call($method, array $args)
  287. {
  288. return $this->_mockery_handleMethodCall($method, $args);
  289. }
  290. public static function __callStatic($method, array $args)
  291. {
  292. return self::_mockery_handleStaticMethodCall($method, $args);
  293. }
  294. /**
  295. * Forward calls to this magic method to the __call method
  296. */
  297. public function __toString()
  298. {
  299. return $this->__call('__toString', array());
  300. }
  301. /**public function __set($name, $value)
  302. {
  303. $this->_mockery_mockableProperties[$name] = $value;
  304. return $this;
  305. }
  306. public function __get($name)
  307. {
  308. if (isset($this->_mockery_mockableProperties[$name])) {
  309. return $this->_mockery_mockableProperties[$name];
  310. } elseif(isset($this->{$name})) {
  311. return $this->{$name};
  312. }
  313. throw new \InvalidArgumentException (
  314. 'Property ' . __CLASS__ . '::' . $name . ' does not exist on this mock object'
  315. );
  316. }**/
  317. /**
  318. * Iterate across all expectation directors and validate each
  319. *
  320. * @throws \Mockery\CountValidator\Exception
  321. * @return void
  322. */
  323. public function mockery_verify()
  324. {
  325. if ($this->_mockery_verified) {
  326. return true;
  327. }
  328. if (isset($this->_mockery_ignoreVerification)
  329. && $this->_mockery_ignoreVerification == true) {
  330. return true;
  331. }
  332. $this->_mockery_verified = true;
  333. foreach ($this->_mockery_expectations as $director) {
  334. $director->verify();
  335. }
  336. }
  337. /**
  338. * Tear down tasks for this mock
  339. *
  340. * @return void
  341. */
  342. public function mockery_teardown()
  343. {
  344. }
  345. /**
  346. * Fetch the next available allocation order number
  347. *
  348. * @return int
  349. */
  350. public function mockery_allocateOrder()
  351. {
  352. $this->_mockery_allocatedOrder += 1;
  353. return $this->_mockery_allocatedOrder;
  354. }
  355. /**
  356. * Set ordering for a group
  357. *
  358. * @param mixed $group
  359. * @param int $order
  360. */
  361. public function mockery_setGroup($group, $order)
  362. {
  363. $this->_mockery_groups[$group] = $order;
  364. }
  365. /**
  366. * Fetch array of ordered groups
  367. *
  368. * @return array
  369. */
  370. public function mockery_getGroups()
  371. {
  372. return $this->_mockery_groups;
  373. }
  374. /**
  375. * Set current ordered number
  376. *
  377. * @param int $order
  378. */
  379. public function mockery_setCurrentOrder($order)
  380. {
  381. $this->_mockery_currentOrder = $order;
  382. return $this->_mockery_currentOrder;
  383. }
  384. /**
  385. * Get current ordered number
  386. *
  387. * @return int
  388. */
  389. public function mockery_getCurrentOrder()
  390. {
  391. return $this->_mockery_currentOrder;
  392. }
  393. /**
  394. * Validate the current mock's ordering
  395. *
  396. * @param string $method
  397. * @param int $order
  398. * @throws \Mockery\Exception
  399. * @return void
  400. */
  401. public function mockery_validateOrder($method, $order)
  402. {
  403. if ($order < $this->_mockery_currentOrder) {
  404. $exception = new \Mockery\Exception\InvalidOrderException(
  405. 'Method ' . __CLASS__ . '::' . $method . '()'
  406. . ' called out of order: expected order '
  407. . $order . ', was ' . $this->_mockery_currentOrder
  408. );
  409. $exception->setMock($this)
  410. ->setMethodName($method)
  411. ->setExpectedOrder($order)
  412. ->setActualOrder($this->_mockery_currentOrder);
  413. throw $exception;
  414. }
  415. $this->mockery_setCurrentOrder($order);
  416. }
  417. /**
  418. * Gets the count of expectations for this mock
  419. *
  420. * @return int
  421. */
  422. public function mockery_getExpectationCount()
  423. {
  424. $count = 0;
  425. foreach ($this->_mockery_expectations as $director) {
  426. $count += $director->getExpectationCount();
  427. }
  428. return $count;
  429. }
  430. /**
  431. * Return the expectations director for the given method
  432. *
  433. * @var string $method
  434. * @return \Mockery\ExpectationDirector|null
  435. */
  436. public function mockery_setExpectationsFor($method, \Mockery\ExpectationDirector $director)
  437. {
  438. $this->_mockery_expectations[$method] = $director;
  439. }
  440. /**
  441. * Return the expectations director for the given method
  442. *
  443. * @var string $method
  444. * @return \Mockery\ExpectationDirector|null
  445. */
  446. public function mockery_getExpectationsFor($method)
  447. {
  448. if (isset($this->_mockery_expectations[$method])) {
  449. return $this->_mockery_expectations[$method];
  450. }
  451. }
  452. /**
  453. * Find an expectation matching the given method and arguments
  454. *
  455. * @var string $method
  456. * @var array $args
  457. * @return \Mockery\Expectation|null
  458. */
  459. public function mockery_findExpectation($method, array $args)
  460. {
  461. if (!isset($this->_mockery_expectations[$method])) {
  462. return null;
  463. }
  464. $director = $this->_mockery_expectations[$method];
  465. return $director->findExpectation($args);
  466. }
  467. /**
  468. * Return the container for this mock
  469. *
  470. * @return \Mockery\Container
  471. */
  472. public function mockery_getContainer()
  473. {
  474. return $this->_mockery_container;
  475. }
  476. /**
  477. * Return the name for this mock
  478. *
  479. * @return string
  480. */
  481. public function mockery_getName()
  482. {
  483. return __CLASS__;
  484. }
  485. /**
  486. * @return array
  487. */
  488. public function mockery_getMockableProperties()
  489. {
  490. return $this->_mockery_mockableProperties;
  491. }
  492. public function __isset($name)
  493. {
  494. if (false === stripos($name, '_mockery_') && method_exists(get_parent_class($this), '__isset')) {
  495. return parent::__isset($name);
  496. }
  497. }
  498. public function mockery_getExpectations()
  499. {
  500. return $this->_mockery_expectations;
  501. }
  502. /**
  503. * Calls a parent class method and returns the result. Used in a passthru
  504. * expectation where a real return value is required while still taking
  505. * advantage of expectation matching and call count verification.
  506. *
  507. * @param string $name
  508. * @param array $args
  509. * @return mixed
  510. */
  511. public function mockery_callSubjectMethod($name, array $args)
  512. {
  513. return call_user_func_array('parent::' . $name, $args);
  514. }
  515. /**
  516. * @return string[]
  517. */
  518. public function mockery_getMockableMethods()
  519. {
  520. return $this->_mockery_mockableMethods;
  521. }
  522. /**
  523. * @return bool
  524. */
  525. public function mockery_isAnonymous()
  526. {
  527. $rfc = new \ReflectionClass($this);
  528. return false === $rfc->getParentClass();
  529. }
  530. public function __wakeup()
  531. {
  532. /**
  533. * This does not add __wakeup method support. It's a blind method and any
  534. * expected __wakeup work will NOT be performed. It merely cuts off
  535. * annoying errors where a __wakeup exists but is not essential when
  536. * mocking
  537. */
  538. }
  539. public function mockery_getMethod($name)
  540. {
  541. foreach ($this->mockery_getMethods() as $method) {
  542. if ($method->getName() == $name) {
  543. return $method;
  544. }
  545. }
  546. return null;
  547. }
  548. public function shouldHaveReceived($method, $args = null)
  549. {
  550. $expectation = new \Mockery\VerificationExpectation($this, $method);
  551. if (null !== $args) {
  552. $expectation->withArgs($args);
  553. }
  554. $expectation->atLeast()->once();
  555. $director = new \Mockery\VerificationDirector($this->_mockery_getReceivedMethodCalls(), $expectation);
  556. $director->verify();
  557. return $director;
  558. }
  559. public function shouldNotHaveReceived($method, $args = null)
  560. {
  561. $expectation = new \Mockery\VerificationExpectation($this, $method);
  562. if (null !== $args) {
  563. $expectation->withArgs($args);
  564. }
  565. $expectation->never();
  566. $director = new \Mockery\VerificationDirector($this->_mockery_getReceivedMethodCalls(), $expectation);
  567. $director->verify();
  568. return null;
  569. }
  570. protected static function _mockery_handleStaticMethodCall($method, array $args)
  571. {
  572. try {
  573. $associatedRealObject = \Mockery::fetchMock(__CLASS__);
  574. return $associatedRealObject->__call($method, $args);
  575. } catch (\BadMethodCallException $e) {
  576. throw new \BadMethodCallException(
  577. 'Static method ' . $associatedRealObject->mockery_getName() . '::' . $method
  578. . '() does not exist on this mock object'
  579. );
  580. }
  581. }
  582. protected function _mockery_getReceivedMethodCalls()
  583. {
  584. return $this->_mockery_receivedMethodCalls ?: $this->_mockery_receivedMethodCalls = new \Mockery\ReceivedMethodCalls();
  585. }
  586. protected function _mockery_handleMethodCall($method, array $args)
  587. {
  588. $this->_mockery_getReceivedMethodCalls()->push(new \Mockery\MethodCall($method, $args));
  589. $rm = $this->mockery_getMethod($method);
  590. if ($rm && $rm->isProtected() && !$this->_mockery_allowMockingProtectedMethods) {
  591. if ($rm->isAbstract()) {
  592. return;
  593. }
  594. try {
  595. $prototype = $rm->getPrototype();
  596. if ($prototype->isAbstract()) {
  597. return;
  598. }
  599. } catch (\ReflectionException $re) {
  600. // noop - there is no hasPrototype method
  601. }
  602. return call_user_func_array("parent::$method", $args);
  603. }
  604. if (isset($this->_mockery_expectations[$method])
  605. && !$this->_mockery_disableExpectationMatching) {
  606. $handler = $this->_mockery_expectations[$method];
  607. try {
  608. return $handler->call($args);
  609. } catch (\Mockery\Exception\NoMatchingExpectationException $e) {
  610. if (!$this->_mockery_ignoreMissing && !$this->_mockery_deferMissing) {
  611. throw $e;
  612. }
  613. }
  614. }
  615. if (!is_null($this->_mockery_partial) && method_exists($this->_mockery_partial, $method)) {
  616. return call_user_func_array(array($this->_mockery_partial, $method), $args);
  617. } elseif ($this->_mockery_deferMissing && is_callable("parent::$method")) {
  618. return call_user_func_array("parent::$method", $args);
  619. } elseif ($method == '__toString') {
  620. // __toString is special because we force its addition to the class API regardless of the
  621. // original implementation. Thus, we should always return a string rather than honor
  622. // _mockery_ignoreMissing and break the API with an error.
  623. return sprintf("%s#%s", __CLASS__, spl_object_hash($this));
  624. } elseif ($this->_mockery_ignoreMissing) {
  625. if (\Mockery::getConfiguration()->mockingNonExistentMethodsAllowed() || (method_exists($this->_mockery_partial, $method) || is_callable("parent::$method"))) {
  626. if ($this->_mockery_defaultReturnValue instanceof \Mockery\Undefined) {
  627. return call_user_func_array(array($this->_mockery_defaultReturnValue, $method), $args);
  628. } else {
  629. return $this->_mockery_defaultReturnValue;
  630. }
  631. }
  632. }
  633. throw new \BadMethodCallException(
  634. 'Method ' . __CLASS__ . '::' . $method . '() does not exist on this mock object'
  635. );
  636. }
  637. protected function mockery_getMethods()
  638. {
  639. if (static::$_mockery_methods) {
  640. return static::$_mockery_methods;
  641. }
  642. $methods = array();
  643. if (isset($this->_mockery_partial)) {
  644. $reflected = new \ReflectionObject($this->_mockery_partial);
  645. $methods = $reflected->getMethods();
  646. } else {
  647. $reflected = new \ReflectionClass($this);
  648. foreach ($reflected->getMethods() as $method) {
  649. try {
  650. $methods[] = $method->getPrototype();
  651. } catch (\ReflectionException $re) {
  652. /**
  653. * For some reason, private methods don't have a prototype
  654. */
  655. if ($method->isPrivate()) {
  656. $methods[] = $method;
  657. }
  658. }
  659. }
  660. }
  661. return static::$_mockery_methods = $methods;
  662. }
  663. /**
  664. * @return array
  665. */
  666. private function getNonPublicMethods()
  667. {
  668. return array_map(
  669. function ($method) {
  670. return $method->getName();
  671. },
  672. array_filter($this->mockery_getMethods(), function ($method) {
  673. return !$method->isPublic();
  674. })
  675. );
  676. }
  677. }