]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Tests/Framework/MockObjectTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Tests / Framework / MockObjectTest.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, 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  * @category   Testing
38  * @package    PHPUnit
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
42
43  * @link       http://www.phpunit.de/
44  * @since      File available since Release 3.0.0
45  */
46
47 require_once 'PHPUnit/Framework/TestCase.php';
48
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';
53
54 /**
55  *
56  *
57  * @category   Testing
58  * @package    PHPUnit
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
67  */
68 class Framework_MockObjectTest extends PHPUnit_Framework_TestCase
69 {
70     public function testMockedMethodIsNeverCalled()
71     {
72         $mock = $this->getMock('AnInterface');
73         $mock->expects($this->never())
74              ->method('doSomething');
75     }
76
77     public function testMockedMethodIsCalledAtLeastOnce()
78     {
79         $mock = $this->getMock('AnInterface');
80         $mock->expects($this->atLeastOnce())
81              ->method('doSomething');
82
83         $mock->doSomething();
84     }
85
86     public function testMockedMethodIsCalledAtLeastOnce2()
87     {
88         $mock = $this->getMock('AnInterface');
89         $mock->expects($this->atLeastOnce())
90              ->method('doSomething');
91
92         $mock->doSomething();
93         $mock->doSomething();
94     }
95
96     public function testMockedMethodIsCalledOnce()
97     {
98         $mock = $this->getMock('AnInterface');
99         $mock->expects($this->once())
100              ->method('doSomething');
101
102         $mock->doSomething();
103     }
104
105     public function testMockedMethodIsCalledOnceWithParameter()
106     {
107         $mock = $this->getMock('AnInterface');
108         $mock->expects($this->once())
109              ->method('doSomething')
110              ->with($this->equalTo('something'));
111
112         $mock->doSomething('something');
113     }
114
115     public function testMockedMethodIsCalledExactly()
116     {
117         $mock = $this->getMock('AnInterface');
118         $mock->expects($this->exactly(2))
119              ->method('doSomething');
120
121         $mock->doSomething();
122         $mock->doSomething();
123     }
124
125     public function testStubbedException()
126     {
127         $mock = $this->getMock('AnInterface');
128         $mock->expects($this->any())
129              ->method('doSomething')
130              ->will($this->throwException(new Exception));
131
132         try {
133             $mock->doSomething();
134         }
135
136         catch (Exception $e) {
137             return;
138         }
139
140         $this->fail();
141     }
142
143     public function testStubbedReturnValue()
144     {
145         $mock = $this->getMock('AnInterface');
146         $mock->expects($this->any())
147              ->method('doSomething')
148              ->will($this->returnValue('something'));
149
150         $this->assertEquals('something', $mock->doSomething());
151     }
152
153     public function testFunctionCallback()
154     {
155         $mock = $this->getMock('StdClass', array('callback'), array(), '', FALSE);
156         $mock->expects($this->once())
157              ->method('callback')
158              ->will($this->returnCallback('functionCallback'));
159
160         $this->assertEquals('pass', $mock->callback('foo', 'bar'));
161     }
162
163     public function testStaticMethodCallback()
164     {
165         $mock = $this->getMock('StdClass', array('callback'), array(), '', FALSE);
166         $mock->expects($this->once())
167              ->method('callback')
168              ->will($this->returnCallback(array('MethodCallback', 'staticCallback')));
169
170         $this->assertEquals('pass', $mock->callback('foo', 'bar'));
171     }
172
173     public function testPublicMethodCallback()
174     {
175         $mock = $this->getMock('StdClass', array('callback'), array(), '', FALSE);
176         $mock->expects($this->once())
177              ->method('callback')
178              ->will($this->returnCallback(array(new MethodCallback, 'nonStaticCallback')));
179
180         $this->assertEquals('pass', $mock->callback('foo', 'bar'));
181     }
182
183     public function testMockClassOnlyGeneratedOnce()
184     {
185         $mock1 = $this->getMock('AnInterface');
186         $mock2 = $this->getMock('AnInterface');
187
188         $this->assertEquals(get_class($mock1), get_class($mock2));
189     }
190
191     public function testMockClassDifferentForPartialMocks()
192     {
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'));
198
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));
207     }
208
209     public function testMockClassStoreOverrulable()
210     {
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');
216
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));
226     }
227
228     public function testMockClassStoreOverruleSameClassNameThrowsException()
229     {
230         $mock1 = $this->getMock('PartialMockTestClass', array(), array(), __FUNCTION__);
231         $this->setExpectedException('RuntimeException');
232         $mock2 = $this->getMock('PartialMockTestClass', array(), array(), __FUNCTION__);
233     }
234
235     public function testOriginalConstructorSettingConsidered()
236     {
237         $mock1 = $this->getMock('PartialMockTestClass');
238         $mock2 = $this->getMock('PartialMockTestClass', array(), array(), '', FALSE);
239
240         $this->assertNotEquals(get_class($mock1), get_class($mock2));
241     }
242
243     public function testOriginalCloneSettingConsidered()
244     {
245         $mock1 = $this->getMock('PartialMockTestClass');
246         $mock2 = $this->getMock('PartialMockTestClass', array(), array(), '', TRUE, FALSE);
247
248         $this->assertNotEquals(get_class($mock1), get_class($mock2));
249     }
250 }
251 ?>