5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
39 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @link http://www.phpunit.de/
44 * @since File available since Release 3.0.0
47 require_once 'PHPUnit/Framework/TestCase.php';
49 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'AnInterface.php';
50 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'FunctionCallback.php';
51 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'MethodCallback.php';
52 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'PartialMockTestClass.php';
59 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
60 * @author Patrick Mueller <elias0@gmx.net>
61 * @author Frank Kleine <mikey@stubbles.net>
62 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
63 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
64 * @version Release: 3.3.17
65 * @link http://www.phpunit.de/
66 * @since Class available since Release 3.0.0
68 class Framework_MockObjectTest extends PHPUnit_Framework_TestCase
70 public function testMockedMethodIsNeverCalled()
72 $mock = $this->getMock('AnInterface');
73 $mock->expects($this->never())
74 ->method('doSomething');
77 public function testMockedMethodIsCalledAtLeastOnce()
79 $mock = $this->getMock('AnInterface');
80 $mock->expects($this->atLeastOnce())
81 ->method('doSomething');
86 public function testMockedMethodIsCalledAtLeastOnce2()
88 $mock = $this->getMock('AnInterface');
89 $mock->expects($this->atLeastOnce())
90 ->method('doSomething');
96 public function testMockedMethodIsCalledOnce()
98 $mock = $this->getMock('AnInterface');
99 $mock->expects($this->once())
100 ->method('doSomething');
102 $mock->doSomething();
105 public function testMockedMethodIsCalledOnceWithParameter()
107 $mock = $this->getMock('AnInterface');
108 $mock->expects($this->once())
109 ->method('doSomething')
110 ->with($this->equalTo('something'));
112 $mock->doSomething('something');
115 public function testMockedMethodIsCalledExactly()
117 $mock = $this->getMock('AnInterface');
118 $mock->expects($this->exactly(2))
119 ->method('doSomething');
121 $mock->doSomething();
122 $mock->doSomething();
125 public function testStubbedException()
127 $mock = $this->getMock('AnInterface');
128 $mock->expects($this->any())
129 ->method('doSomething')
130 ->will($this->throwException(new Exception));
133 $mock->doSomething();
136 catch (Exception $e) {
143 public function testStubbedReturnValue()
145 $mock = $this->getMock('AnInterface');
146 $mock->expects($this->any())
147 ->method('doSomething')
148 ->will($this->returnValue('something'));
150 $this->assertEquals('something', $mock->doSomething());
153 public function testFunctionCallback()
155 $mock = $this->getMock('StdClass', array('callback'), array(), '', FALSE);
156 $mock->expects($this->once())
158 ->will($this->returnCallback('functionCallback'));
160 $this->assertEquals('pass', $mock->callback('foo', 'bar'));
163 public function testStaticMethodCallback()
165 $mock = $this->getMock('StdClass', array('callback'), array(), '', FALSE);
166 $mock->expects($this->once())
168 ->will($this->returnCallback(array('MethodCallback', 'staticCallback')));
170 $this->assertEquals('pass', $mock->callback('foo', 'bar'));
173 public function testPublicMethodCallback()
175 $mock = $this->getMock('StdClass', array('callback'), array(), '', FALSE);
176 $mock->expects($this->once())
178 ->will($this->returnCallback(array(new MethodCallback, 'nonStaticCallback')));
180 $this->assertEquals('pass', $mock->callback('foo', 'bar'));
183 public function testMockClassOnlyGeneratedOnce()
185 $mock1 = $this->getMock('AnInterface');
186 $mock2 = $this->getMock('AnInterface');
188 $this->assertEquals(get_class($mock1), get_class($mock2));
191 public function testMockClassDifferentForPartialMocks()
193 $mock1 = $this->getMock('PartialMockTestClass');
194 $mock2 = $this->getMock('PartialMockTestClass', array('doSomething'));
195 $mock3 = $this->getMock('PartialMockTestClass', array('doSomething'));
196 $mock4 = $this->getMock('PartialMockTestClass', array('doAnotherThing'));
197 $mock5 = $this->getMock('PartialMockTestClass', array('doAnotherThing'));
199 $this->assertNotEquals(get_class($mock1), get_class($mock2));
200 $this->assertNotEquals(get_class($mock1), get_class($mock3));
201 $this->assertNotEquals(get_class($mock1), get_class($mock4));
202 $this->assertNotEquals(get_class($mock1), get_class($mock5));
203 $this->assertEquals(get_class($mock2), get_class($mock3));
204 $this->assertNotEquals(get_class($mock2), get_class($mock4));
205 $this->assertNotEquals(get_class($mock2), get_class($mock5));
206 $this->assertEquals(get_class($mock4), get_class($mock5));
209 public function testMockClassStoreOverrulable()
211 $mock1 = $this->getMock('PartialMockTestClass');
212 $mock2 = $this->getMock('PartialMockTestClass', array(), array(), 'MyMockClassNameForPartialMockTestClass1');
213 $mock3 = $this->getMock('PartialMockTestClass');
214 $mock4 = $this->getMock('PartialMockTestClass', array('doSomething'), array(), 'AnotherMockClassNameForPartialMockTestClass');
215 $mock5 = $this->getMock('PartialMockTestClass', array(), array(), 'MyMockClassNameForPartialMockTestClass2');
217 $this->assertNotEquals(get_class($mock1), get_class($mock2));
218 $this->assertEquals(get_class($mock1), get_class($mock3));
219 $this->assertNotEquals(get_class($mock1), get_class($mock4));
220 $this->assertNotEquals(get_class($mock2), get_class($mock3));
221 $this->assertNotEquals(get_class($mock2), get_class($mock4));
222 $this->assertNotEquals(get_class($mock2), get_class($mock5));
223 $this->assertNotEquals(get_class($mock3), get_class($mock4));
224 $this->assertNotEquals(get_class($mock3), get_class($mock5));
225 $this->assertNotEquals(get_class($mock4), get_class($mock5));
228 public function testMockClassStoreOverruleSameClassNameThrowsException()
230 $mock1 = $this->getMock('PartialMockTestClass', array(), array(), __FUNCTION__);
231 $this->setExpectedException('RuntimeException');
232 $mock2 = $this->getMock('PartialMockTestClass', array(), array(), __FUNCTION__);
235 public function testOriginalConstructorSettingConsidered()
237 $mock1 = $this->getMock('PartialMockTestClass');
238 $mock2 = $this->getMock('PartialMockTestClass', array(), array(), '', FALSE);
240 $this->assertNotEquals(get_class($mock1), get_class($mock2));
243 public function testOriginalCloneSettingConsidered()
245 $mock1 = $this->getMock('PartialMockTestClass');
246 $mock2 = $this->getMock('PartialMockTestClass', array(), array(), '', TRUE, FALSE);
248 $this->assertNotEquals(get_class($mock1), get_class($mock2));