I have the following situation. Let's say I have two classes:
class Session {
public function start() {
return session_start();
}
// methods for all the other session functions of PHP
}
And a child class which extends Session
class TrustedSession extends Session {
public function start() {
if(parent::start() === false)
return false;
$requestRemoteAddress = isset($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:null;
$requestUserAgent = isset($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:null;
$trustedRemoteAddress = $this->get('TRUSTED_REMOTE_ADDR');
$prevUserAgent = $this->get('PREV_USERAGENT');
if($trustedRemoteAddress !== $requestRemoteAddress || $prevUserAgent !== $requestUserAgent)
$this->regenerateID(true);
return true;
}
}
So no I want to test my TrustedSession
class. But I have to use a mock of Session
because according to this question here I can't use the real session functions of PHP because they lead to a Output already started
error with PHPUnit.
And now the question: How to mock up the Session
class. I mean I can create a SessionInterface
which defines the methods I have to implement for Session
and the mockup of it. But how can I extend my TrustedSession
class with the mockup then?
My idea is to swap parent class at runtime but I think this is not possible (at least in PHP). It's a design question and I'm thinking around the interface I described above but this does not help me out of this situation. Is there a "clean" and nice solution?