Reputation: 190
I have a class that is a container for a bunch of module objects. Right now the container takes in an array of file paths, then includes the files and instantiates the modules
class module {
function execute();
}
class container {
public $Name;
public $Modules;
public __construct($Obj){
$this->Name = $Obj->Name;
foreach($this->Modules as $M){
include_once($M);
$className = $this->GetClassName($M);
$this->Modules[] = new $className;
}
}
public execute(){
foreach($this->Modules as $M){
$M->execute();
}
}
}
I'm not certain what the best way to refactor this so that it is easily testable is.
class containerFactory{
public function createContainer($Obj){
$C = new Container($Obj);
$C->InstiateModules();
return $C;
}
}
class container{
public function InstiateModules(){
$tmp = array();
foreach($this->Modules as $M){
include_once($M);
$className = $this->GetClassName($M);
`enter code here`$this->Modules[] = new $className;
}
$this->Modules = $tmp;
}
public function __construct($Obj){
$this->Name = $Obj->Name;
$this->Modules = $Obj->Modules;
}
}
Any suggestions as to what else I should do? Thanks
Upvotes: 1
Views: 272
Reputation: 17516
There are two things you could do:
Keep the code as it is, and test that object actually does what its supposed to do. You'd do that by passing a known array to the constructor, and then testing that those files were actually included and those modules actually imported. I'm not familiar with PHP, but I'm assuming there is a method to check if a file has been imported / module initialized.
If the above methods aren't available, then you can cut down your uncertainty footprint... you'd do this by having the import actions and initialization actions in a separate class, and testing that this class is correctly calling the importer and initializer for each of the files after parsing them. Mocking / stubbing the importer and initializer will be necessary.
Upvotes: 2