cleanMock.patch
12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
diff --git a/PHPUnit/Framework/TestCase.php b/PHPUnit/Framework/TestCase.php
index 1c52ccb..dc0002f 100644
--- PHPUnit/Framework/TestCase.php
+++ PHPUnit/Framework/TestCase.php
@@ -116,6 +116,14 @@ PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
abstract class PHPUnit_Framework_TestCase extends PHPUnit_Framework_Assert implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing
{
/**
+ * Enable or disable the clean of mock objects to reduce the memory
+ * footprint.
+ *
+ * @var boolean
+ */
+ protected $cleanMock = NULL;
+
+ /**
* Enable or disable the backup and restoration of the $GLOBALS array.
* Overwrite this attribute in a child class of TestCase.
* Setting this attribute in setUp() has no effect!
@@ -726,7 +734,10 @@ abstract class PHPUnit_Framework_TestCase extends PHPUnit_Framework_Assert imple
foreach ($this->mockObjects as $mockObject) {
$this->numAssertions++;
$mockObject->__phpunit_verify();
- $mockObject->__phpunit_cleanup();
+
+ if ($this->cleanMock === TRUE || $this->cleanMock === NULL) {
+ $mockObject->__phpunit_cleanup();
+ }
}
$this->status = PHPUnit_Runner_BaseTestRunner::STATUS_PASSED;
@@ -906,6 +917,17 @@ abstract class PHPUnit_Framework_TestCase extends PHPUnit_Framework_Assert imple
}
/**
+ * @param boolean $cleanMock
+ * @since Method available since Release 3.4.X
+ */
+ public function setCleanMock($cleanMock)
+ {
+ if (is_null($this->cleanMock) && is_bool($cleanMock)) {
+ $this->cleanMock = $cleanMock;
+ }
+ }
+
+ /**
* Calling this method in setUp() has no effect!
*
* @param boolean $backupGlobals
diff --git a/PHPUnit/Framework/TestSuite.php b/PHPUnit/Framework/TestSuite.php
index aed6726..e365412 100644
--- PHPUnit/Framework/TestSuite.php
+++ PHPUnit/Framework/TestSuite.php
@@ -94,6 +94,14 @@ PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing, IteratorAggregate
{
/**
+ * Enable or disable the clean of mock objects to reduce the memory
+ * footprint.
+ *
+ * @var boolean
+ */
+ protected $cleanMock = NULL;
+
+ /**
* Enable or disable the backup and restoration of the $GLOBALS array.
*
* @var boolean
@@ -679,6 +687,7 @@ class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Fra
}
if ($test instanceof PHPUnit_Framework_TestSuite) {
+ $test->setCleanMock($this->cleanMock);
$test->setBackupGlobals($this->backupGlobals);
$test->setBackupStaticAttributes($this->backupStaticAttributes);
$test->setSharedFixture($this->sharedFixture);
@@ -718,6 +727,7 @@ class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Fra
if ($runTest) {
if ($test instanceof PHPUnit_Framework_TestCase) {
+ $test->setCleanMock($this->cleanMock);
$test->setBackupGlobals($this->backupGlobals);
$test->setBackupStaticAttributes(
$this->backupStaticAttributes
@@ -878,6 +888,17 @@ class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Fra
}
/**
+ * @param boolean $cleanMock
+ * @since Method available since Release 3.4.X
+ */
+ public function setCleanMock($cleanMock)
+ {
+ if (is_null($this->cleanMock) && is_bool($cleanMock)) {
+ $this->cleanMock = $cleanMock;
+ }
+ }
+
+ /**
* @param boolean $backupGlobals
* @since Method available since Release 3.3.0
*/
diff --git a/PHPUnit/Tests/Framework/TestCaseTest.php b/PHPUnit/Tests/Framework/TestCaseTest.php
index e1bcf59..f8c7581 100644
--- PHPUnit/Tests/Framework/TestCaseTest.php
+++ PHPUnit/Tests/Framework/TestCaseTest.php
@@ -52,6 +52,7 @@ require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIREC
require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ExceptionInTearDownTest.php';
require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ExceptionInTest.php';
require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Failure.php';
+require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'MockCleaned.php';
require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'NoArgTestCaseTest.php';
require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Singleton.php';
require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Success.php';
@@ -322,5 +323,35 @@ class Framework_TestCaseTest extends PHPUnit_Framework_TestCase
$this->assertNotSame($GLOBALS['singleton'], Singleton::getInstance());
}
+
+ public function testMockInvocationMockerCleaned()
+ {
+ $test = new MockCleaned();
+ $mock = $test->getMock('StdClass');
+
+ $mock->expects($this->any())
+ ->method('__toString')
+ ->will($this->returnValue('foo'));
+
+ $test->run();
+
+ $this->assertNull($this->readAttribute($mock, 'invocationMocker'));
+ }
+
+ public function testMockInvocationMockerNotCleaned()
+ {
+ $test = new MockCleaned();
+ $test->setCleanMock(FALSE);
+
+ $mock = $test->getMock('StdClass');
+
+ $mock->expects($this->any())
+ ->method('__toString')
+ ->will($this->returnValue('foo'));
+
+ $test->run();
+
+ $this->assertNotNull($this->readAttribute($mock, 'invocationMocker'));
+ }
}
?>
diff --git a/PHPUnit/Tests/Util/ConfigurationTest.php b/PHPUnit/Tests/Util/ConfigurationTest.php
index e341cb5..ff6aaf2 100644
--- PHPUnit/Tests/Util/ConfigurationTest.php
+++ PHPUnit/Tests/Util/ConfigurationTest.php
@@ -257,6 +257,7 @@ class Util_ConfigurationTest extends PHPUnit_Framework_TestCase
'backupGlobals' => TRUE,
'backupStaticAttributes' => FALSE,
'bootstrap' => '/path/to/bootstrap.php',
+ 'cleanMock' => TRUE,
'colors' => FALSE,
'convertErrorsToExceptions' => TRUE,
'convertNoticesToExceptions' => TRUE,
diff --git a/PHPUnit/Tests/_files/MockCleaned.php b/PHPUnit/Tests/_files/MockCleaned.php
new file mode 100644
index 0000000..4540972
--- /dev/null
+++ PHPUnit/Tests/_files/MockCleaned.php
@@ -0,0 +1,8 @@
+<?php
+class MockCleaned extends PHPUnit_Framework_TestCase
+{
+ public function runTest()
+ {
+ }
+}
+?>
diff --git a/PHPUnit/Tests/_files/configuration.xml b/PHPUnit/Tests/_files/configuration.xml
index fbca0c6..bf862db 100644
--- PHPUnit/Tests/_files/configuration.xml
+++ PHPUnit/Tests/_files/configuration.xml
@@ -3,6 +3,7 @@
<phpunit backupGlobals="true"
backupStaticAttributes="false"
bootstrap="/path/to/bootstrap.php"
+ cleanMock="true"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
diff --git a/PHPUnit/TextUI/TestRunner.php b/PHPUnit/TextUI/TestRunner.php
index 570c17f..7a65dd6 100644
--- PHPUnit/TextUI/TestRunner.php
+++ PHPUnit/TextUI/TestRunner.php
@@ -168,6 +168,10 @@ class PHPUnit_TextUI_TestRunner extends PHPUnit_Runner_BaseTestRunner
}
}
+ if ($arguments['cleanMock'] === FALSE) {
+ $suite->setCleanMock(FALSE);
+ }
+
if ($arguments['backupGlobals'] === FALSE) {
$suite->setBackupGlobals(FALSE);
}
@@ -660,6 +664,11 @@ class PHPUnit_TextUI_TestRunner extends PHPUnit_Runner_BaseTestRunner
$arguments['bootstrap'] = $phpunitConfiguration['bootstrap'];
}
+ if (isset($phpunitConfiguration['cleanMock']) &&
+ !isset($arguments['cleanMock'])) {
+ $arguments['cleanMock'] = $phpunitConfiguration['cleanMock'];
+ }
+
if (isset($phpunitConfiguration['colors']) &&
!isset($arguments['colors'])) {
$arguments['colors'] = $phpunitConfiguration['colors'];
@@ -852,6 +861,7 @@ class PHPUnit_TextUI_TestRunner extends PHPUnit_Runner_BaseTestRunner
$arguments['backupStaticAttributes'] = isset($arguments['backupStaticAttributes']) ? $arguments['backupStaticAttributes'] : NULL;
$arguments['cpdMinLines'] = isset($arguments['cpdMinLines']) ? $arguments['cpdMinLines'] : 5;
$arguments['cpdMinMatches'] = isset($arguments['cpdMinMatches']) ? $arguments['cpdMinMatches'] : 70;
+ $arguments['cleanMock'] = isset($arguments['cleanMock']) ? $arguments['cleanMock'] : TRUE;
$arguments['colors'] = isset($arguments['colors']) ? $arguments['colors'] : FALSE;
$arguments['convertErrorsToExceptions'] = isset($arguments['convertErrorsToExceptions']) ? $arguments['convertErrorsToExceptions'] : TRUE;
$arguments['convertNoticesToExceptions'] = isset($arguments['convertNoticesToExceptions']) ? $arguments['convertNoticesToExceptions'] : TRUE;
diff --git a/PHPUnit/Util/Configuration.php b/PHPUnit/Util/Configuration.php
index 39d749b..4c64aea 100644
--- PHPUnit/Util/Configuration.php
+++ PHPUnit/Util/Configuration.php
@@ -59,6 +59,7 @@ PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
* <phpunit backupGlobals="true"
* backupStaticAttributes="false"
* bootstrap="/path/to/bootstrap.php"
+ * cleanMock="true"
* colors="false"
* convertErrorsToExceptions="true"
* convertNoticesToExceptions="true"
@@ -526,20 +527,6 @@ class PHPUnit_Util_Configuration
{
$result = array();
- if ($this->document->documentElement->hasAttribute('colors')) {
- $result['colors'] = $this->getBoolean(
- (string)$this->document->documentElement->getAttribute('colors'),
- FALSE
- );
- }
-
- else if ($this->document->documentElement->hasAttribute('ansi')) {
- $result['colors'] = $this->getBoolean(
- (string)$this->document->documentElement->getAttribute('ansi'),
- FALSE
- );
- }
-
if ($this->document->documentElement->hasAttribute('backupGlobals')) {
$result['backupGlobals'] = $this->getBoolean(
(string)$this->document->documentElement->getAttribute('backupGlobals'),
@@ -558,6 +545,27 @@ class PHPUnit_Util_Configuration
$result['bootstrap'] = (string)$this->document->documentElement->getAttribute('bootstrap');
}
+ if ($this->document->documentElement->hasAttribute('cleanMock')) {
+ $result['cleanMock'] = $this->getBoolean(
+ (string)$this->document->documentElement->getAttribute('cleanMock'),
+ TRUE
+ );
+ }
+
+ if ($this->document->documentElement->hasAttribute('colors')) {
+ $result['colors'] = $this->getBoolean(
+ (string)$this->document->documentElement->getAttribute('colors'),
+ FALSE
+ );
+ }
+
+ else if ($this->document->documentElement->hasAttribute('ansi')) {
+ $result['colors'] = $this->getBoolean(
+ (string)$this->document->documentElement->getAttribute('ansi'),
+ FALSE
+ );
+ }
+
if ($this->document->documentElement->hasAttribute('convertErrorsToExceptions')) {
$result['convertErrorsToExceptions'] = $this->getBoolean(
(string)$this->document->documentElement->getAttribute('convertErrorsToExceptions'),