Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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 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 ExpectationDirector
24
{
25
    /**
26
     * Method name the director is directing
27
     *
28
     * @var string
29
     */
30
    protected $_name = null;
31
 
32
    /**
33
     * Mock object the director is attached to
34
     *
35
     * @var \Mockery\MockInterface|\Mockery\LegacyMockInterface
36
     */
37
    protected $_mock = null;
38
 
39
    /**
40
     * Stores an array of all expectations for this mock
41
     *
42
     * @var array
43
     */
44
    protected $_expectations = array();
45
 
46
    /**
47
     * The expected order of next call
48
     *
49
     * @var int
50
     */
51
    protected $_expectedOrder = null;
52
 
53
    /**
54
     * Stores an array of all default expectations for this mock
55
     *
56
     * @var array
57
     */
58
    protected $_defaults = array();
59
 
60
    /**
61
     * Constructor
62
     *
63
     * @param string $name
64
     * @param \Mockery\LegacyMockInterface $mock
65
     */
66
    public function __construct($name, \Mockery\LegacyMockInterface $mock)
67
    {
68
        $this->_name = $name;
69
        $this->_mock = $mock;
70
    }
71
 
72
    /**
73
     * Add a new expectation to the director
74
     *
75
     * @param \Mockery\Expectation $expectation
76
     */
77
    public function addExpectation(\Mockery\Expectation $expectation)
78
    {
79
        $this->_expectations[] = $expectation;
80
    }
81
 
82
    /**
83
     * Handle a method call being directed by this instance
84
     *
85
     * @param array $args
86
     * @return mixed
87
     */
88
    public function call(array $args)
89
    {
90
        $expectation = $this->findExpectation($args);
91
        if (is_null($expectation)) {
92
            $exception = new \Mockery\Exception\NoMatchingExpectationException(
93
                'No matching handler found for '
94
                . $this->_mock->mockery_getName() . '::'
95
                . \Mockery::formatArgs($this->_name, $args)
96
                . '. Either the method was unexpected or its arguments matched'
97
                . ' no expected argument list for this method'
98
                . PHP_EOL . PHP_EOL
99
                . \Mockery::formatObjects($args)
100
            );
101
            $exception->setMock($this->_mock)
102
                ->setMethodName($this->_name)
103
                ->setActualArguments($args);
104
            throw $exception;
105
        }
106
        return $expectation->verifyCall($args);
107
    }
108
 
109
    /**
110
     * Verify all expectations of the director
111
     *
112
     * @throws \Mockery\CountValidator\Exception
113
     * @return void
114
     */
115
    public function verify()
116
    {
117
        if (!empty($this->_expectations)) {
118
            foreach ($this->_expectations as $exp) {
119
                $exp->verify();
120
            }
121
        } else {
122
            foreach ($this->_defaults as $exp) {
123
                $exp->verify();
124
            }
125
        }
126
    }
127
 
128
    /**
129
     * Attempt to locate an expectation matching the provided args
130
     *
131
     * @param array $args
132
     * @return mixed
133
     */
134
    public function findExpectation(array $args)
135
    {
136
        $expectation = null;
137
 
138
        if (!empty($this->_expectations)) {
139
            $expectation = $this->_findExpectationIn($this->_expectations, $args);
140
        }
141
 
142
        if ($expectation === null && !empty($this->_defaults)) {
143
            $expectation = $this->_findExpectationIn($this->_defaults, $args);
144
        }
145
 
146
        return $expectation;
147
    }
148
 
149
    /**
150
     * Make the given expectation a default for all others assuming it was
151
     * correctly created last
152
     *
153
     * @param \Mockery\Expectation $expectation
154
     */
155
    public function makeExpectationDefault(\Mockery\Expectation $expectation)
156
    {
157
        $last = end($this->_expectations);
158
        if ($last === $expectation) {
159
            array_pop($this->_expectations);
160
            array_unshift($this->_defaults, $expectation);
161
        } else {
162
            throw new \Mockery\Exception(
163
                'Cannot turn a previously defined expectation into a default'
164
            );
165
        }
166
    }
167
 
168
    /**
169
     * Search current array of expectations for a match
170
     *
171
     * @param array $expectations
172
     * @param array $args
173
     * @return mixed
174
     */
175
    protected function _findExpectationIn(array $expectations, array $args)
176
    {
177
        foreach ($expectations as $exp) {
178
            if ($exp->isEligible() && $exp->matchArgs($args)) {
179
                return $exp;
180
            }
181
        }
182
        foreach ($expectations as $exp) {
183
            if ($exp->matchArgs($args)) {
184
                return $exp;
185
            }
186
        }
187
    }
188
 
189
    /**
190
     * Return all expectations assigned to this director
191
     *
192
     * @return array
193
     */
194
    public function getExpectations()
195
    {
196
        return $this->_expectations;
197
    }
198
 
199
    /**
200
     * Return all expectations assigned to this director
201
     *
202
     * @return array
203
     */
204
    public function getDefaultExpectations()
205
    {
206
        return $this->_defaults;
207
    }
208
 
209
    /**
210
     * Return the number of expectations assigned to this director.
211
     *
212
     * @return int
213
     */
214
    public function getExpectationCount()
215
    {
216
        $count = 0;
217
        /** @var Expectation $expectations */
218
        $expectations = $this->getExpectations() ?: $this->getDefaultExpectations();
219
        foreach ($expectations as $expectation) {
220
            if ($expectation->isCallCountConstrained()) {
221
                $count++;
222
            }
223
        }
224
        return $count;
225
    }
226
}