Commit 8069932716324ac7b7cde2f71c84e217751b1cb5
1 parent
2c2ce466
Exists in
master
Aplicado patch da feature "cleanMock" disponível em http://github.com/eriksencos…
…ta/phpunit/tree/cleanMock
Showing
3 changed files
with
320 additions
and
3 deletions
Show diff stats
... | ... | @@ -0,0 +1,33 @@ |
1 | +-- | |
2 | +-- @author Eriksen Costa Paixão <eriksen.paixao_bs@cobra.com.br> | |
3 | +-- @license http://creativecommons.org/licenses/GPL/2.0/legalcode.pt CC GNU GPL | |
4 | +-- @link http://phing.info/docs/guide/current/ Phing Documentation | |
5 | +-- | |
6 | + | |
7 | +Patch para o PHPUnit com a diretiva de configuração "cleanMock" para o arquivo | |
8 | +phpunit.xml | |
9 | + | |
10 | +Na release 3.4.7 do PHPUnit, foi corrigido o bug TRAC-986, reduzindo o consumo | |
11 | +de memória para testes que utilizam mock objects: | |
12 | + | |
13 | +http://github.com/sebastianbergmann/phpunit/commit/8b5044ff919f51e1da653e49bc7413f3e9f6dc29 | |
14 | + | |
15 | +O código simplesmente a desaloca o objeto na memória, disponibilizando-o ao GC | |
16 | +do PHP. | |
17 | + | |
18 | +No entanto, essa correção quebrou alguns testes do i-Educar que lidam com | |
19 | +classes legadas (namespace App). As instâncias dessas classes são mantidas no | |
20 | +registry da classe CoreExt_Entity para facilitar o setup dos testes. | |
21 | + | |
22 | +Para resolver esse problema, foi implementado uma nova opção de configuração no | |
23 | +PHPUnit chamada "cleanMock". Quando seu valor está "false", o PHPUnit não irá | |
24 | +desalocar o mock automaticamente. Caso "cleanMock" não seja configurado, o | |
25 | +comportamento default do PHPUnit é realizado. | |
26 | + | |
27 | +Todo o histórico de implementação dessa feature está disponível em: | |
28 | + | |
29 | +http://github.com/eriksencosta/phpunit/commit/ae26bcc3c3bc81321f741c6d99e8389b1b937fb4 | |
30 | + | |
31 | +Enquanto a feature não for incorporada por padrão no core do PHPUnit, esse | |
32 | +patch deverá ser mantido e a sua aplicação deverá ser estimulada através da | |
33 | +documentação oficial. | |
0 | 34 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,284 @@ |
1 | +diff --git a/PHPUnit/Framework/TestCase.php b/PHPUnit/Framework/TestCase.php | |
2 | +index 1c52ccb..dc0002f 100644 | |
3 | +--- PHPUnit/Framework/TestCase.php | |
4 | ++++ PHPUnit/Framework/TestCase.php | |
5 | +@@ -116,6 +116,14 @@ PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT'); | |
6 | + abstract class PHPUnit_Framework_TestCase extends PHPUnit_Framework_Assert implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing | |
7 | + { | |
8 | + /** | |
9 | ++ * Enable or disable the clean of mock objects to reduce the memory | |
10 | ++ * footprint. | |
11 | ++ * | |
12 | ++ * @var boolean | |
13 | ++ */ | |
14 | ++ protected $cleanMock = NULL; | |
15 | ++ | |
16 | ++ /** | |
17 | + * Enable or disable the backup and restoration of the $GLOBALS array. | |
18 | + * Overwrite this attribute in a child class of TestCase. | |
19 | + * Setting this attribute in setUp() has no effect! | |
20 | +@@ -726,7 +734,10 @@ abstract class PHPUnit_Framework_TestCase extends PHPUnit_Framework_Assert imple | |
21 | + foreach ($this->mockObjects as $mockObject) { | |
22 | + $this->numAssertions++; | |
23 | + $mockObject->__phpunit_verify(); | |
24 | +- $mockObject->__phpunit_cleanup(); | |
25 | ++ | |
26 | ++ if ($this->cleanMock === TRUE || $this->cleanMock === NULL) { | |
27 | ++ $mockObject->__phpunit_cleanup(); | |
28 | ++ } | |
29 | + } | |
30 | + | |
31 | + $this->status = PHPUnit_Runner_BaseTestRunner::STATUS_PASSED; | |
32 | +@@ -906,6 +917,17 @@ abstract class PHPUnit_Framework_TestCase extends PHPUnit_Framework_Assert imple | |
33 | + } | |
34 | + | |
35 | + /** | |
36 | ++ * @param boolean $cleanMock | |
37 | ++ * @since Method available since Release 3.4.X | |
38 | ++ */ | |
39 | ++ public function setCleanMock($cleanMock) | |
40 | ++ { | |
41 | ++ if (is_null($this->cleanMock) && is_bool($cleanMock)) { | |
42 | ++ $this->cleanMock = $cleanMock; | |
43 | ++ } | |
44 | ++ } | |
45 | ++ | |
46 | ++ /** | |
47 | + * Calling this method in setUp() has no effect! | |
48 | + * | |
49 | + * @param boolean $backupGlobals | |
50 | +diff --git a/PHPUnit/Framework/TestSuite.php b/PHPUnit/Framework/TestSuite.php | |
51 | +index aed6726..e365412 100644 | |
52 | +--- PHPUnit/Framework/TestSuite.php | |
53 | ++++ PHPUnit/Framework/TestSuite.php | |
54 | +@@ -94,6 +94,14 @@ PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT'); | |
55 | + class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing, IteratorAggregate | |
56 | + { | |
57 | + /** | |
58 | ++ * Enable or disable the clean of mock objects to reduce the memory | |
59 | ++ * footprint. | |
60 | ++ * | |
61 | ++ * @var boolean | |
62 | ++ */ | |
63 | ++ protected $cleanMock = NULL; | |
64 | ++ | |
65 | ++ /** | |
66 | + * Enable or disable the backup and restoration of the $GLOBALS array. | |
67 | + * | |
68 | + * @var boolean | |
69 | +@@ -679,6 +687,7 @@ class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Fra | |
70 | + } | |
71 | + | |
72 | + if ($test instanceof PHPUnit_Framework_TestSuite) { | |
73 | ++ $test->setCleanMock($this->cleanMock); | |
74 | + $test->setBackupGlobals($this->backupGlobals); | |
75 | + $test->setBackupStaticAttributes($this->backupStaticAttributes); | |
76 | + $test->setSharedFixture($this->sharedFixture); | |
77 | +@@ -718,6 +727,7 @@ class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Fra | |
78 | + | |
79 | + if ($runTest) { | |
80 | + if ($test instanceof PHPUnit_Framework_TestCase) { | |
81 | ++ $test->setCleanMock($this->cleanMock); | |
82 | + $test->setBackupGlobals($this->backupGlobals); | |
83 | + $test->setBackupStaticAttributes( | |
84 | + $this->backupStaticAttributes | |
85 | +@@ -878,6 +888,17 @@ class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Fra | |
86 | + } | |
87 | + | |
88 | + /** | |
89 | ++ * @param boolean $cleanMock | |
90 | ++ * @since Method available since Release 3.4.X | |
91 | ++ */ | |
92 | ++ public function setCleanMock($cleanMock) | |
93 | ++ { | |
94 | ++ if (is_null($this->cleanMock) && is_bool($cleanMock)) { | |
95 | ++ $this->cleanMock = $cleanMock; | |
96 | ++ } | |
97 | ++ } | |
98 | ++ | |
99 | ++ /** | |
100 | + * @param boolean $backupGlobals | |
101 | + * @since Method available since Release 3.3.0 | |
102 | + */ | |
103 | +diff --git a/PHPUnit/Tests/Framework/TestCaseTest.php b/PHPUnit/Tests/Framework/TestCaseTest.php | |
104 | +index e1bcf59..f8c7581 100644 | |
105 | +--- PHPUnit/Tests/Framework/TestCaseTest.php | |
106 | ++++ PHPUnit/Tests/Framework/TestCaseTest.php | |
107 | +@@ -52,6 +52,7 @@ require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIREC | |
108 | + require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ExceptionInTearDownTest.php'; | |
109 | + require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ExceptionInTest.php'; | |
110 | + require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Failure.php'; | |
111 | ++require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'MockCleaned.php'; | |
112 | + require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'NoArgTestCaseTest.php'; | |
113 | + require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Singleton.php'; | |
114 | + require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Success.php'; | |
115 | +@@ -322,5 +323,35 @@ class Framework_TestCaseTest extends PHPUnit_Framework_TestCase | |
116 | + | |
117 | + $this->assertNotSame($GLOBALS['singleton'], Singleton::getInstance()); | |
118 | + } | |
119 | ++ | |
120 | ++ public function testMockInvocationMockerCleaned() | |
121 | ++ { | |
122 | ++ $test = new MockCleaned(); | |
123 | ++ $mock = $test->getMock('StdClass'); | |
124 | ++ | |
125 | ++ $mock->expects($this->any()) | |
126 | ++ ->method('__toString') | |
127 | ++ ->will($this->returnValue('foo')); | |
128 | ++ | |
129 | ++ $test->run(); | |
130 | ++ | |
131 | ++ $this->assertNull($this->readAttribute($mock, 'invocationMocker')); | |
132 | ++ } | |
133 | ++ | |
134 | ++ public function testMockInvocationMockerNotCleaned() | |
135 | ++ { | |
136 | ++ $test = new MockCleaned(); | |
137 | ++ $test->setCleanMock(FALSE); | |
138 | ++ | |
139 | ++ $mock = $test->getMock('StdClass'); | |
140 | ++ | |
141 | ++ $mock->expects($this->any()) | |
142 | ++ ->method('__toString') | |
143 | ++ ->will($this->returnValue('foo')); | |
144 | ++ | |
145 | ++ $test->run(); | |
146 | ++ | |
147 | ++ $this->assertNotNull($this->readAttribute($mock, 'invocationMocker')); | |
148 | ++ } | |
149 | + } | |
150 | + ?> | |
151 | +diff --git a/PHPUnit/Tests/Util/ConfigurationTest.php b/PHPUnit/Tests/Util/ConfigurationTest.php | |
152 | +index e341cb5..ff6aaf2 100644 | |
153 | +--- PHPUnit/Tests/Util/ConfigurationTest.php | |
154 | ++++ PHPUnit/Tests/Util/ConfigurationTest.php | |
155 | +@@ -257,6 +257,7 @@ class Util_ConfigurationTest extends PHPUnit_Framework_TestCase | |
156 | + 'backupGlobals' => TRUE, | |
157 | + 'backupStaticAttributes' => FALSE, | |
158 | + 'bootstrap' => '/path/to/bootstrap.php', | |
159 | ++ 'cleanMock' => TRUE, | |
160 | + 'colors' => FALSE, | |
161 | + 'convertErrorsToExceptions' => TRUE, | |
162 | + 'convertNoticesToExceptions' => TRUE, | |
163 | +diff --git a/PHPUnit/Tests/_files/MockCleaned.php b/PHPUnit/Tests/_files/MockCleaned.php | |
164 | +new file mode 100644 | |
165 | +index 0000000..4540972 | |
166 | +--- /dev/null | |
167 | ++++ PHPUnit/Tests/_files/MockCleaned.php | |
168 | +@@ -0,0 +1,8 @@ | |
169 | ++<?php | |
170 | ++class MockCleaned extends PHPUnit_Framework_TestCase | |
171 | ++{ | |
172 | ++ public function runTest() | |
173 | ++ { | |
174 | ++ } | |
175 | ++} | |
176 | ++?> | |
177 | +diff --git a/PHPUnit/Tests/_files/configuration.xml b/PHPUnit/Tests/_files/configuration.xml | |
178 | +index fbca0c6..bf862db 100644 | |
179 | +--- PHPUnit/Tests/_files/configuration.xml | |
180 | ++++ PHPUnit/Tests/_files/configuration.xml | |
181 | +@@ -3,6 +3,7 @@ | |
182 | + <phpunit backupGlobals="true" | |
183 | + backupStaticAttributes="false" | |
184 | + bootstrap="/path/to/bootstrap.php" | |
185 | ++ cleanMock="true" | |
186 | + colors="false" | |
187 | + convertErrorsToExceptions="true" | |
188 | + convertNoticesToExceptions="true" | |
189 | +diff --git a/PHPUnit/TextUI/TestRunner.php b/PHPUnit/TextUI/TestRunner.php | |
190 | +index 570c17f..7a65dd6 100644 | |
191 | +--- PHPUnit/TextUI/TestRunner.php | |
192 | ++++ PHPUnit/TextUI/TestRunner.php | |
193 | +@@ -168,6 +168,10 @@ class PHPUnit_TextUI_TestRunner extends PHPUnit_Runner_BaseTestRunner | |
194 | + } | |
195 | + } | |
196 | + | |
197 | ++ if ($arguments['cleanMock'] === FALSE) { | |
198 | ++ $suite->setCleanMock(FALSE); | |
199 | ++ } | |
200 | ++ | |
201 | + if ($arguments['backupGlobals'] === FALSE) { | |
202 | + $suite->setBackupGlobals(FALSE); | |
203 | + } | |
204 | +@@ -660,6 +664,11 @@ class PHPUnit_TextUI_TestRunner extends PHPUnit_Runner_BaseTestRunner | |
205 | + $arguments['bootstrap'] = $phpunitConfiguration['bootstrap']; | |
206 | + } | |
207 | + | |
208 | ++ if (isset($phpunitConfiguration['cleanMock']) && | |
209 | ++ !isset($arguments['cleanMock'])) { | |
210 | ++ $arguments['cleanMock'] = $phpunitConfiguration['cleanMock']; | |
211 | ++ } | |
212 | ++ | |
213 | + if (isset($phpunitConfiguration['colors']) && | |
214 | + !isset($arguments['colors'])) { | |
215 | + $arguments['colors'] = $phpunitConfiguration['colors']; | |
216 | +@@ -852,6 +861,7 @@ class PHPUnit_TextUI_TestRunner extends PHPUnit_Runner_BaseTestRunner | |
217 | + $arguments['backupStaticAttributes'] = isset($arguments['backupStaticAttributes']) ? $arguments['backupStaticAttributes'] : NULL; | |
218 | + $arguments['cpdMinLines'] = isset($arguments['cpdMinLines']) ? $arguments['cpdMinLines'] : 5; | |
219 | + $arguments['cpdMinMatches'] = isset($arguments['cpdMinMatches']) ? $arguments['cpdMinMatches'] : 70; | |
220 | ++ $arguments['cleanMock'] = isset($arguments['cleanMock']) ? $arguments['cleanMock'] : TRUE; | |
221 | + $arguments['colors'] = isset($arguments['colors']) ? $arguments['colors'] : FALSE; | |
222 | + $arguments['convertErrorsToExceptions'] = isset($arguments['convertErrorsToExceptions']) ? $arguments['convertErrorsToExceptions'] : TRUE; | |
223 | + $arguments['convertNoticesToExceptions'] = isset($arguments['convertNoticesToExceptions']) ? $arguments['convertNoticesToExceptions'] : TRUE; | |
224 | +diff --git a/PHPUnit/Util/Configuration.php b/PHPUnit/Util/Configuration.php | |
225 | +index 39d749b..4c64aea 100644 | |
226 | +--- PHPUnit/Util/Configuration.php | |
227 | ++++ PHPUnit/Util/Configuration.php | |
228 | +@@ -59,6 +59,7 @@ PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT'); | |
229 | + * <phpunit backupGlobals="true" | |
230 | + * backupStaticAttributes="false" | |
231 | + * bootstrap="/path/to/bootstrap.php" | |
232 | ++ * cleanMock="true" | |
233 | + * colors="false" | |
234 | + * convertErrorsToExceptions="true" | |
235 | + * convertNoticesToExceptions="true" | |
236 | +@@ -526,20 +527,6 @@ class PHPUnit_Util_Configuration | |
237 | + { | |
238 | + $result = array(); | |
239 | + | |
240 | +- if ($this->document->documentElement->hasAttribute('colors')) { | |
241 | +- $result['colors'] = $this->getBoolean( | |
242 | +- (string)$this->document->documentElement->getAttribute('colors'), | |
243 | +- FALSE | |
244 | +- ); | |
245 | +- } | |
246 | +- | |
247 | +- else if ($this->document->documentElement->hasAttribute('ansi')) { | |
248 | +- $result['colors'] = $this->getBoolean( | |
249 | +- (string)$this->document->documentElement->getAttribute('ansi'), | |
250 | +- FALSE | |
251 | +- ); | |
252 | +- } | |
253 | +- | |
254 | + if ($this->document->documentElement->hasAttribute('backupGlobals')) { | |
255 | + $result['backupGlobals'] = $this->getBoolean( | |
256 | + (string)$this->document->documentElement->getAttribute('backupGlobals'), | |
257 | +@@ -558,6 +545,27 @@ class PHPUnit_Util_Configuration | |
258 | + $result['bootstrap'] = (string)$this->document->documentElement->getAttribute('bootstrap'); | |
259 | + } | |
260 | + | |
261 | ++ if ($this->document->documentElement->hasAttribute('cleanMock')) { | |
262 | ++ $result['cleanMock'] = $this->getBoolean( | |
263 | ++ (string)$this->document->documentElement->getAttribute('cleanMock'), | |
264 | ++ TRUE | |
265 | ++ ); | |
266 | ++ } | |
267 | ++ | |
268 | ++ if ($this->document->documentElement->hasAttribute('colors')) { | |
269 | ++ $result['colors'] = $this->getBoolean( | |
270 | ++ (string)$this->document->documentElement->getAttribute('colors'), | |
271 | ++ FALSE | |
272 | ++ ); | |
273 | ++ } | |
274 | ++ | |
275 | ++ else if ($this->document->documentElement->hasAttribute('ansi')) { | |
276 | ++ $result['colors'] = $this->getBoolean( | |
277 | ++ (string)$this->document->documentElement->getAttribute('ansi'), | |
278 | ++ FALSE | |
279 | ++ ); | |
280 | ++ } | |
281 | ++ | |
282 | + if ($this->document->documentElement->hasAttribute('convertErrorsToExceptions')) { | |
283 | + $result['convertErrorsToExceptions'] = $this->getBoolean( | |
284 | + (string)$this->document->documentElement->getAttribute('convertErrorsToExceptions'), | ... | ... |
ieducar/phpunit.xml
... | ... | @@ -4,10 +4,10 @@ |
4 | 4 | - Configurações do PHPUnit. |
5 | 5 | --> |
6 | 6 | |
7 | -<phpunit colors='true' bootstrap='tests/bootstrap.php'> | |
7 | +<phpunit cleanMock='false' colors='true' bootstrap='tests/bootstrap.php'> | |
8 | 8 | <filter> |
9 | 9 | <blacklist> |
10 | - <directory suffix=".php">../tests</directory> | |
10 | + <directory suffix='.php'>../tests</directory> | |
11 | 11 | </blacklist> |
12 | 12 | </filter> |
13 | -</phpunit> | |
14 | 13 | \ No newline at end of file |
14 | +</phpunit> | ... | ... |