Home » Testing » PHPUnit2 » Bug #5053
PHPUnit2 problems with global variables
Details
| Submitted | 2005-08-10 08:19 UTC |
|---|---|
| From | paul at paulbutcher dot com |
| Assigned | sebastian |
| Status | Closed |
| Package | PHPUnit2 |
| PHP Version | 5.0.3 |
| OS | Windows |
| Roadmaps | (Not assigned) |
Comments
[2005-08-10 08:19 UTC] paul at paulbutcher dot com
Description:
------------
PHPUnit2 tests don't seem to be able to access global variables if they are run using the "phpunit.bat" script. They work as expected if called from a script via PHPUnit2_TextUI_TestRunner::run, however.
Test script:
---------------
<?php
$testvariable = "the quick brown fox jumped over the lazy dog";
class FooBar extends PHPUnit2_Framework_TestCase
{
public function testOne()
{
global $testvariable;
self::assertEquals($testvariable,
"the quick brown fox jumped over the lazy dog");
}
}
?>
Expected result:
----------------
I would expect that this test would pass. It does if it's called from the following script:
<?php
define('PHPUnit2_MAIN_METHOD', 'false');
require_once 'PHPUnit2/TextUI/TestRunner.php';
require_once 'foobar.php';
PHPUnit2_TextUI_TestRunner::run(new ReflectionClass('FooBar'));
?>
C:\phptest>php foobar_runner.php
PHPUnit 2.2.1 by Sebastian Bergmann.
.
Time: 0.004537
OK (1 test)
Actual result:
--------------
C:\phptest>phpunit foobar
PHPUnit 2.2.1 by Sebastian Bergmann.
F
Time: 0.007778
There was 1 failure:
1) testOne(FooBar)
expected same: <> was not: <the quick brown fox jumped over the lazy dog>
FAILURES!!!
Tests run: 1, Failures: 1, Errors: 0, Incomplete Tests: 0.
[2005-09-30 10:30 UTC] sebastian at php dot net
This is not a problem with PHPUnit2 itself, but with PHP: http://bugs.php.net/bug.php?id=34689.
[2005-09-30 10:46 UTC] sebastian at php dot net
Okay, this is not a PHP bug after all but expected behaviour.
The sourcefile that contains the test case class (and the declaration of the global variable) is loaded using include_once inside a method, thus any global variables in the included sourcefile become local ones to the PHPUnit method.
If you need to work with global variables declared in your test case class' sourcefile, use $GLOBAL['foo'] instead of $foo.
Hope this helps,
Sebastian
[2005-09-30 14:20 UTC] paul at paulbutcher dot com
OK - I understand the problem. I still believe that it's a bug in PHPUnit though. Having to refer to global variables in test code in the manner that you suggest is (just) acceptable. But what if the code that's under test refers to global variables? It's unreasonable to expect that the code under test will have to change, surely?
Here's an example - say that I want to write a test to verify that the following code works as expected:
Foo.php -------------------------------
<?php
$interesting_string = "the quick brown fox jumped over the lazy dog";
class Foo
{
public function getInterestingString()
{
global $interesting_string;
return $interesting_string;
}
}
?>
---------------------------------------
Here's a PHPUnit2 test class for the above:
FooTest -------------------------------
<?php
require_once("Foo.php");
class FooTest extends PHPUnit2_Framework_TestCase
{
public function testOne()
{
$foo = new Foo();
self::assertEquals($foo->getInterestingString(),
"the quick brown fox jumped over the lazy dog");
}
}
?>
---------------------------------------
This fails with the following:
---------------------------------------
>phpunit footest
PHPUnit 2.2.1 by Sebastian Bergmann.
F
Time: 0.006042
There was 1 failure:
1) testOne(FooTest)
expected same: <> was not: <the quick brown fox jumped over the lazy dog>
FAILURES!!!
Tests run: 1, Failures: 1, Errors: 0, Incomplete Tests: 0.
---------------------------------------
Again, if I run it from a script instead of from the phpunit shell script, it works fine:
---------------------------------------
>php AllTests.php
PHPUnit 2.2.1 by Sebastian Bergmann.
.
Time: 0.007130
OK (1 test)
---------------------------------------
[2005-10-07 08:20 UTC] sebastian at php dot net
Will fix as asoon as I have the means to do so, see http://bugs.xdebug.org/bug_view_page.php?bug_id=0000153 for details.
[2005-12-30 16:08 UTC] sebastian at php dot net
This bug has been fixed in CVS.
If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET).
If this was a problem with the pear.php.net website, the change should be live shortly.
Otherwise, the fix will appear in the package's next release.
Thank you for the report and for helping us make PEAR better.
Thanks to Derick Rethans, who implemented xdebug_get_declared_vars() today, I could finally fix this issue.
[2006-04-06 21:36 UTC] snoyes at gmail dot com
Error persists in 2.3.5 - tried with both $GLOBALS and the global keyword.
BugReport.php
-------------
<?php
$globalVar = "globalValue";
function myFunction() {
return $GLOBALS['globalVar'];
}
?>
BugReportTest.php
-----------------
<?php
require_once('PHPUnit2/Framework/TestCase.php');
require("BugReport.php");
class BugReportTest extends PHPUnit2_Framework_TestCase {
public function testGlobalVariable() {
$this->assertEquals("globalValue", myFunction());
}
}
?>
Output
-------
There was 1 failure:
1) testGlobalVariable(BugReportTest)
expected same: <globalValue> was not: <>
/home/snoyes/UnitTests/BugReportTest.php:10
[2006-04-07 04:25 UTC] sebastian at php dot net
The bug has been fixed for PHPUnit 3.0.