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:
21: namespace Mockery;
22:
23: class Recorder
24: {
25:
26: /**
27: * Mock object on which all recorded interactions will be set as
28: * expectations
29: *
30: * @var object
31: */
32: protected $_mock = null;
33:
34: /**
35: * The subject object whose interactions are being recorded
36: *
37: * @var object
38: */
39: protected $_subject = null;
40:
41: /**
42: * Flag indicating whether the recording should maintain a strict adherence
43: * to the recorded interactions, i.e. the usual Mockery flexibility is
44: * suspended, ordering is enforced, and arguments received are set as
45: * exact requirements.
46: *
47: * @var bool
48: */
49: protected $_strict = false;
50:
51: /**
52: * Construct accepting the mock object on which expectations are to be
53: * recorded. The second parameter is the subject object, passed into
54: * a \Mockery::mock() call in the same way as a partial mock requires.
55: *
56: * @param \Mockery\MockInterface $mock
57: * @param object $subject
58: * @return void
59: */
60: public function __construct(\Mockery\MockInterface $mock, $subject)
61: {
62: $this->_mock = $mock;
63: $this->_subject = $subject;
64: }
65:
66: /**
67: * Sets the recorded into strict mode where method calls are more strictly
68: * matched against the argument and call count and ordering is also
69: * set as enforceable.
70: *
71: * @return void
72: */
73: public function shouldBeStrict()
74: {
75: $this->_strict = true;
76: }
77:
78: /**
79: * Intercept all calls on the subject, and use the call details to create
80: * a new expectation. The actual return value is then returned after being
81: * recorded.
82: *
83: * @param string $method
84: * @param array $args
85: * @return mixed
86: */
87: public function __call($method, array $args)
88: {
89: $return = call_user_func_array(array($this->_subject, $method), $args);
90: $expectation = $this->_mock->shouldReceive($method)->andReturn($return);
91: if ($this->_strict) {
92: $exactArgs = array();
93: foreach ($args as $arg) {
94: $exactArgs[] = \Mockery::mustBe($arg);
95: }
96: $expectation->once()->ordered();
97: call_user_func_array(array($expectation, 'with'), $exactArgs);
98: } else {
99: call_user_func_array(array($expectation, 'with'), $args);
100: }
101: return $return;
102: }
103: }
104: