I have this method:
public function getLocale()
{
$languageId = $this->_user->language->id;
$page = Wire::getModule('myModule')->getPage($languageId);
$locale = $page->locale;
if (!!$locale) {
return $locale;
}
// fallback to browser's locale
$browserLocale = new SBD_Language();
$locale = $browserLocale->getLanguageLocale('_');
return $locale;
}
Now I want to write a test for it, but I get this error:
Trying to get property of non-object
which is caused by Wire::getModule('myModule')
.
So I'd like to override the Wire::getModule
response with phpunit. I just don't know how to do that.
So far I have created a mock over the class in which the method getLocale
is placed and that is all working fine but how would I tell that mock class that it should actually call a mock of the Wire
class?
You can kind of mock static methods by proxying the call to the static method, as such
class StaticClass
{
static function myStaticFunction($param)
{
// do something with $param...
}
}
class ProxyClass
{
static function myStaticFunction($param)
{
StaticClass::myStaticFunction($param);
}
}
class Caller
{
// StaticClass::myStaticFunction($param);
(new ProxyClass())->myStaticFunction($param);
// the above would need injecting to mock correctly
}
class Test
{
$this->mockStaticClass = \Phake::mock(ProxyClass::class);
\Phake::verify($this->mockStaticClass)->myStaticMethod($param);
}
That example is with Phake, but it should work with PHPUnit the same way.