I have a class similar to this (some logic removed for brevity):
class FooCollection {
protected $_foos;
public function __construct() {
$this->_foos = new SplObjectStorage();
}
public function addFoo(FooInterface $foo) {
$this->_foos->attach($foo);
}
public function removeFoo(FooInterface $foo) {
$this->_foos->detach($foo);
}
}
I'd like to test the addFoo()
and removeFoo()
methods using PHPUnit, and I was wondering what the best strategy would be to do this? As far as I can tell, I have only a few options:
- Add a method
hasFoo(FooInterface $foo)
and check this after the add. - Add a method
getFoos()
that directly returns theSplObjectStorage
instance and check if$foo
is in it after the add. - Try to
removeFoo($foo)
afteraddFoo($foo)
and check for an exception. - Make
$_foos
a public property and check it directly after the add (bad, bad, bad...).
Options #1 and #2 are changing the public interface solely for testing purposes, and I'm not sure how I feel about that. They seem like pretty generic, useful methods to have anyway on the surface, but in my particular case, I never have a need to check for the existance of a particular Foo
instance in the collection, nor retrieve all of the instances, so it really would just be bloat. Also, it seems like if I'm testing multiple parts of the interface in one test, I'm not really testing a "unit," but that's more or less just a philosophical hangup.
Option #3 seems awkward to me.
Option #4 is a really bad idea, and I shouldn't have even listed it, as I wouldn't do it even if it were suggested here.