1<?php
2/**
3 * PHPUnit
4 *
5 * Copyright (c) 2010-2013, 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     Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
39 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40 * @copyright  2010-2013 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
42 * @link       http://github.com/sebastianbergmann/phpunit-mock-objects
43 * @since      File available since Release 1.0.0
44 */
45
46/**
47 * Implementation of the Builder pattern for Mock objects.
48 *
49 * @package    PHPUnit_MockObject
50 * @author     Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
51 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
52 * @copyright  2010-2013 Sebastian Bergmann <sb@sebastian-bergmann.de>
53 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
54 * @link       http://github.com/sebastianbergmann/phpunit-mock-objects
55 * @since      File available since Release 1.0.0
56 */
57class PHPUnit_Framework_MockObject_MockBuilder
58{
59    /**
60     * @var PHPUnit_Framework_TestCase
61     */
62    protected $testCase;
63
64    /**
65     * @var string
66     */
67    protected $className;
68
69    /**
70     * @var array
71     */
72    protected $methods = array();
73
74    /**
75     * @var string
76     */
77    protected $mockClassName = '';
78
79    /**
80     * @var array
81     */
82    protected $constructorArgs = array();
83
84    /**
85     * @var boolean
86     */
87    protected $originalConstructor = TRUE;
88
89    /**
90     * @var boolean
91     */
92    protected $originalClone = TRUE;
93
94    /**
95     * @var boolean
96     */
97    protected $autoload = TRUE;
98
99    /**
100     * @var boolean
101     */
102    protected $cloneArguments = FALSE;
103
104    /**
105     * @param PHPUnit_Framework_TestCase
106     * @param string
107     */
108    public function __construct(PHPUnit_Framework_TestCase $testCase, $className)
109    {
110        $this->testCase  = $testCase;
111        $this->className = $className;
112    }
113
114    /**
115     * Creates a mock object using a fluent interface.
116     *
117     * @return PHPUnit_Framework_MockObject_MockObject
118     */
119    public function getMock()
120    {
121        return $this->testCase->getMock(
122          $this->className,
123          $this->methods,
124          $this->constructorArgs,
125          $this->mockClassName,
126          $this->originalConstructor,
127          $this->originalClone,
128          $this->autoload,
129          $this->cloneArguments
130        );
131    }
132
133    /**
134     * Creates a mock object for an abstract class using a fluent interface.
135     *
136     * @return PHPUnit_Framework_MockObject_MockObject
137     */
138    public function getMockForAbstractClass()
139    {
140        return $this->testCase->getMockForAbstractClass(
141          $this->className,
142          $this->constructorArgs,
143          $this->mockClassName,
144          $this->originalConstructor,
145          $this->originalClone,
146          $this->autoload,
147          $this->methods,
148          $this->cloneArguments
149        );
150    }
151
152    /**
153     * Specifies the subset of methods to mock. Default is to mock all of them.
154     *
155     * @param  array|null $methods
156     * @return PHPUnit_Framework_MockObject_MockBuilder
157     */
158    public function setMethods($methods)
159    {
160        $this->methods = $methods;
161
162        return $this;
163    }
164
165    /**
166     * Specifies the arguments for the constructor.
167     *
168     * @param  array $args
169     * @return PHPUnit_Framework_MockObject_MockBuilder
170     */
171    public function setConstructorArgs(array $args)
172    {
173        $this->constructorArgs = $args;
174
175        return $this;
176    }
177
178    /**
179     * Specifies the name for the mock class.
180     *
181     * @param string $name
182     * @return PHPUnit_Framework_MockObject_MockBuilder
183     */
184    public function setMockClassName($name)
185    {
186        $this->mockClassName = $name;
187
188        return $this;
189    }
190
191    /**
192     * Disables the invocation of the original constructor.
193     *
194     * @return PHPUnit_Framework_MockObject_MockBuilder
195     */
196    public function disableOriginalConstructor()
197    {
198        $this->originalConstructor = FALSE;
199
200        return $this;
201    }
202
203    /**
204     * Enables the invocation of the original constructor.
205     *
206     * @return PHPUnit_Framework_MockObject_MockBuilder
207     * @since  Method available since Release 1.2.0
208     */
209    public function enableOriginalConstructor()
210    {
211        $this->originalConstructor = TRUE;
212
213        return $this;
214    }
215
216    /**
217     * Disables the invocation of the original clone constructor.
218     *
219     * @return PHPUnit_Framework_MockObject_MockBuilder
220     */
221    public function disableOriginalClone()
222    {
223        $this->originalClone = FALSE;
224
225        return $this;
226    }
227
228    /**
229     * Enables the invocation of the original clone constructor.
230     *
231     * @return PHPUnit_Framework_MockObject_MockBuilder
232     * @since  Method available since Release 1.2.0
233     */
234    public function enableOriginalClone()
235    {
236        $this->originalClone = TRUE;
237
238        return $this;
239    }
240
241    /**
242     * Disables the use of class autoloading while creating the mock object.
243     *
244     * @return PHPUnit_Framework_MockObject_MockBuilder
245     */
246    public function disableAutoload()
247    {
248        $this->autoload = FALSE;
249
250        return $this;
251    }
252
253    /**
254     * Enables the use of class autoloading while creating the mock object.
255     *
256     * @return PHPUnit_Framework_MockObject_MockBuilder
257     * @since  Method available since Release 1.2.0
258     */
259    public function enableAutoload()
260    {
261        $this->autoload = TRUE;
262
263        return $this;
264    }
265
266    /**
267     * Disables the cloning of arguments passed to mocked methods.
268     *
269     * @return PHPUnit_Framework_MockObject_MockBuilder
270     * @since  Method available since Release 1.2.0
271     */
272    public function disableArgumentCloning()
273    {
274        $this->cloneArguments = FALSE;
275
276        return $this;
277    }
278
279    /**
280     * Enables the cloning of arguments passed to mocked methods.
281     *
282     * @return PHPUnit_Framework_MockObject_MockBuilder
283     * @since  Method available since Release 1.2.0
284     */
285    public function enableArgumentCloning()
286    {
287        $this->cloneArguments = TRUE;
288
289        return $this;
290    }
291}
292