Thanks

I still can't make sense of what you're trying to demonstrate but I may have been misleading with my question. Effectively I have a situation like this:
Code: Select all
class Message {
public function setHeaders($headers) { //array of type Header[]
}
public function registerFieldChangeObserver(FieldChangeObserver $observer) {
}
}
What I wanted was to be able to put logic in setHeaders() which checks if each instance in the array implements FieldChangeObserver too, and if so, register it internally.
Code: Select all
public function setHeaders($headers) { //array of type Header[]
foreach ($headers as $header) {
if ($header instanceof FieldChangeObserver) {
$this->registerFieldChangeObserver($header);
}
}
$this->_headers = $headers;
}
However, I can't physically mock that because if I mock the Header interface, although I can set the headers, it won't register it as an observer. And the same for vice-versa.
I ended up just mocking both interfaces as two objects and passing one to setHeaders() and the other to registerFieldChangeObserver() but the result isn't quite the same... mostly down to implementation details though.
Looking at it, it does feel a bit wrong to be automating this observer registration and perhaps it should be done with a factory.
Trying to write a system 100% focused on DI is a fun experience
