No longer loading global functions by default?
Closed this issue · 4 comments
After upgrading to v2.0.0, many of my tests began showing errors like this:
Error: Call to undefined function not()
After digging around, I found this commit that removes Hamcrest.php
from being loaded by Composer by default: a4f1ba4
There doesn't appear to be any documentation about the proper way to load the global functions to ensure tests begin passing. Is the new proper way to register them from my tests' bootstrap?
\Hamcrest\Util::registerGlobalFunctions();
I'm just trying to make sure I'm not missing something obvious.
Thanks!
The global functions aren't loaded by default, because they're conflicting with global functions with same names that PHPUnit had.
I still strongly advise against using global functions and recommend using namespaced static methods instead like so:
use Hamcrest\MatchersAssert as h;
use Hamcrest\Matchers as m;
h::assertThat(array(), m::anArray());
If you absolutely must use global functions, then calling \Hamcrest\Util::registerGlobalFunctions();
in your test suite bootstrap is correct way.
@ramsey yes, I use
Hamcrest\Util::registerGlobalFunctions();
@aik099 I understand that using namespaced static methods is best, but I have over 5,000 unit tests, many of which are using Hamcrest functions, and I can't go through and update them all just so I can upgrade to 2.0.0. The reason I need to upgrade to 2.0.0 is because Mockery 1.0.0-alpha1 requires Hamcrest ~2.0
, and I wanted to upgrade Mockery to take advantage of some features it has that aren't in the 0.x series.
So, registering the global functions from our testing bootstrap is the best option for us. All tests run fine after doing that. I've not seen any conflicts with PHPUnit 5.7 or 6.0.
Good.