1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
abstract class MockBuilderBaseTest extends TuleapTestCase { |
4
|
|
|
|
5
|
|
|
public function setUp() { |
6
|
|
|
parent::setUp(); |
7
|
|
|
Mock::generate('Toto'); |
8
|
|
|
$this->mockToto = new MockToto(); |
9
|
|
|
} |
10
|
|
|
public function itWorksWithoutArguments() { |
11
|
|
|
$this->mockWithoutArguments(); |
12
|
|
|
|
13
|
|
|
$this->assertEqual($this->mockToto->greet(), "Hello"); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function itIsPossibleToSpecifyAnArgument() { |
17
|
|
|
$this->mockWithOneArgument(); |
18
|
|
|
|
19
|
|
|
$this->assertEqual($this->mockToto->greet('Rasmus Lerdorf'), "Hello, Rasmus Lerdorf"); |
20
|
|
|
$this->assertNotEqual($this->mockToto->greet('Linus Thorvalds'), "Hello, Rasmus Lerdorf"); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function itIsPossibleToSpecifySeveralArguments() { |
24
|
|
|
$this->mockWith2Arguments(); |
25
|
|
|
|
26
|
|
|
$this->assertEqual($this->mockToto->greet('Rasmus', 'Lerdorf'), "Hello, Rasmus Lerdorf"); |
27
|
|
|
$this->assertNotEqual($this->mockToto->greet('Linus', 'Lerdorf'), "Hello, Rasmus Lerdorf"); |
28
|
|
|
$this->assertNotEqual($this->mockToto->greet('Rasmus', 'Torvalds'), "Hello, Rasmus Lerdorf"); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
public abstract function mockWithoutArguments(); |
33
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
class MockBuilderIntelligentsTest extends MockBuilderBaseTest { |
37
|
|
|
public function mockWithoutArguments() { |
38
|
|
|
stub($this->mockToto) |
39
|
|
|
->greet() |
40
|
|
|
->returns("Hello"); |
41
|
|
|
} |
42
|
|
|
public function mockWithOneArgument() { |
43
|
|
|
stub($this->mockToto) |
44
|
|
|
->greet('Rasmus Lerdorf') |
45
|
|
|
->returns("Hello, Rasmus Lerdorf"); |
46
|
|
|
} |
47
|
|
|
public function mockWith2Arguments() { |
48
|
|
|
stub($this->mockToto) |
49
|
|
|
->greet('Rasmus', 'Lerdorf') |
50
|
|
|
->returns("Hello, Rasmus Lerdorf"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function itCanAlsoBuildTheMock() { |
54
|
|
|
$mockOfSomeClass = stub('SomeClass') |
55
|
|
|
->someMethod() |
56
|
|
|
->returns("a precise result"); |
57
|
|
|
$this->assertEqual("a precise result", $mockOfSomeClass->someMethod()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function itCanStubMoreThanOneMethod() { |
61
|
|
|
$mock = stub('Toto') |
62
|
|
|
->greet('John Doe') |
63
|
|
|
->returns('Hello, John Doe'); |
64
|
|
|
|
65
|
|
|
stub($mock) |
66
|
|
|
->greet('Rasmus', 'Lerdorf') |
67
|
|
|
->returns('Hello, Rasmus Lerdorf'); |
68
|
|
|
|
69
|
|
|
$this->assertEqual($mock->greet('John Doe'), 'Hello, John Doe'); |
70
|
|
|
$this->assertEqual($mock->greet('Rasmus', 'Lerdorf'), 'Hello, Rasmus Lerdorf'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function itEnsuresThatMethodIsCalledOnceWithoutArguments() { |
74
|
|
|
$mock = mock('Toto'); |
75
|
|
|
stub($mock)->greet()->once(); |
76
|
|
|
$mock->greet(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function itEnsuresThatMethodIsCalledOnceWithArguments() { |
80
|
|
|
$mock = mock('Toto'); |
81
|
|
|
stub($mock)->greet('Rasmus', 'Lerdorf')->once(); |
82
|
|
|
$mock->greet('Rasmus', 'Lerdorf'); |
83
|
|
|
//$mock->greet('Rasmus'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function itEnsuresThatMethodIsNeverCalled() { |
87
|
|
|
$mock = mock('Toto'); |
88
|
|
|
stub($mock)->greet()->never(); |
89
|
|
|
//$mock->greet(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function itEnsuresThatMethodIsCalledAtWithArguments() { |
93
|
|
|
$mock = mock('Toto'); |
94
|
|
|
stub($mock)->greet('Lerdorf')->at(0); |
95
|
|
|
stub($mock)->greet()->at(2); |
96
|
|
|
stub($mock)->greet('Rasmus', 'Lerdorf')->at(1); |
97
|
|
|
$mock->greet('Lerdorf'); |
98
|
|
|
//$mock->greet('Tutu'); |
99
|
|
|
$mock->greet('Rasmus', 'Lerdorf'); |
100
|
|
|
$mock->greet(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function itEnsuresThatMethodIsCalledNTimes() { |
104
|
|
|
$mock = mock('Toto'); |
105
|
|
|
stub($mock)->greet()->count(3); |
106
|
|
|
$mock->greet('Lerdorf'); |
107
|
|
|
//$mock->greet('Tutu'); |
108
|
|
|
$mock->greet('Rasmus', 'Lerdorf'); |
109
|
|
|
$mock->greet(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function itCanThrowExceptions() { |
113
|
|
|
$mock = stub('Toto')->greet()->throws(new SomeException()); |
114
|
|
|
$this->expectException('SomeException'); |
115
|
|
|
$mock->greet(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function itCanThrowExceptionAtASpecificTime() { |
119
|
|
|
$mock = stub('Toto')->greet()->throwsAt(2, new SomeException()); |
120
|
|
|
$mock = stub('Toto')->sayGoodBye()->throwsAt(2, new AnotherException()); |
121
|
|
|
$this->expectException('AnotherException'); |
122
|
|
|
$mock->greet(); |
123
|
|
|
$mock->greet(); |
124
|
|
|
$mock->sayGoodBye(); |
125
|
|
|
$mock->sayGoodBye(); |
126
|
|
|
$mock->sayGoodBye(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function itCanThrowExceptionsDependingOnArguments() { |
130
|
|
|
$mock = stub('Toto')->greet('john')->throws(new SomeException()); |
131
|
|
|
$mock->greet('dave'); |
132
|
|
|
$this->expectException('SomeException'); |
133
|
|
|
$mock->greet('john'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function itDoesNotStoreArgumentsBetweenThrowConfigurations() { |
137
|
|
|
$mock = stub('Toto')->greet('john')->throws(new SomeException()); |
138
|
|
|
stub($mock)->sayGoodbye()->throws(new SomeException()); |
139
|
|
|
$this->expectException('SomeException'); |
140
|
|
|
$mock->sayGoodbye('dave'); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
class Toto { |
145
|
|
|
|
146
|
|
|
function greet() { |
147
|
|
|
// this function is mocked out in the tests |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
function sayGoodBye() { |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
class SomeClass { |
156
|
|
|
|
157
|
|
|
function someMethod() { |
158
|
|
|
// mocked |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
class SomeException extends Exception { |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
class AnotherException extends Exception { |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
require_once 'SomeClassWithNamespace.php'; |
171
|
|
|
class MockBuilder_WithNamespacesTest extends TuleapTestCase { |
172
|
|
|
|
173
|
|
|
public function itCanBuildTheMockIfThereIsANamespace() { |
174
|
|
|
$classname_to_mock = 'Tuleap\Test\SomeClassWithNamespace'; |
175
|
|
|
|
176
|
|
|
$mock = mock($classname_to_mock); |
177
|
|
|
stub($mock)->someMethod()->returns("a precise result"); |
178
|
|
|
|
179
|
|
|
$this->assertEqual(get_class($mock), 'MockTuleap_Test_SomeClassWithNamespace'); |
180
|
|
|
$this->assertIsA($mock, $classname_to_mock); |
181
|
|
|
$this->assertEqual("a precise result", $mock->someMethod()); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.