Re: Class Posing

From: Date: Thu, 04 Oct 2007 08:27:21 +0000
Subject: Re: Class Posing
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-32629@lists.php.net to get a copy of this message
Hi,
public function foo() {
      $bar    = new Bar;
[...]
} When I want to test the foo() method, I want to stub out the usage of the Bar class, for example, and have Bar::doSomething() return a pre-configured value instead of performing its normal operation.
This is where you'd start refactoring the above sourcecode. I don't like this idea at all, because it really only helps you with testing legacy source you might not be able to or do not want to change (want to say: rare use-case IMO). If you really need to intercept construction, you can still weave your interception functionality into sourcecode as you'd be doing in any AOP library. Simply write a stream wrapper for the file:// stream or add a filter to it to intercept all includes and / or requires your legacy code might contain and rewrite the sourcecode while loading, replacing "new Foo" by "newinstance('Foo')" - this might even be possible in a safe manner by using preg_replace. - Timm

« previous php.internals (#32629) next »