Include Hamcrest without clashing with PHPUnit globals

PHPUnit had a Functions.php with global methods like any() for a while (3.7 at least),
which clashes with similar globals from Hamcrest (used in Phockito).

Both PHPUnit and Phockito use 'classmap' composer autoloading,
but that's not directly requiring/evaluating the files.
The problem is caused by SilverStripe's ClassLoader
which does require ALL subclasses of SapphireTest.
This in turn causes PHP outside of the class context to execute,
which includes Hamcrest.

Changing the include_hamcrest() is not strictly necessary here,
but makes the code a bit more resilient against any preceding
test including Functions.php from PHPUnit.

See https://github.com/hafriedlander/phockito/issues/32 for context.
This commit is contained in:
Ingo Schommer 2015-04-29 14:28:08 +12:00
parent a35241cd10
commit 38659bac98
1 changed files with 9 additions and 3 deletions

View File

@ -1,7 +1,5 @@
<?php
if (class_exists('Phockito')) Phockito::include_hamcrest();
class ExternalLinksTest extends SapphireTest {
protected static $fixture_file = 'ExternalLinksTest.yml';
@ -10,6 +8,14 @@ class ExternalLinksTest extends SapphireTest {
'ExternalLinksTest_Page'
);
public function setUpOnce() {
if (class_exists('Phockito')) {
Phockito::include_hamcrest(false);
}
parent::setUpOnce();
}
public function setUp() {
parent::setUp();
@ -60,7 +66,7 @@ class ExternalLinksTest extends SapphireTest {
->return(null);
Phockito::when($checker)
->checkLink(anything()) // anything else is 404
->checkLink(Hamcrest_Matchers::anything()) // anything else is 404
->return(404);
Injector::inst()->registerService($checker, 'LinkChecker');