I’m working on simple benchmark “framework” (reason: boredom and practice).
Now I’m trying to get my head around something.
First things first:
class DataManager {
private $persistanceStrategy;
public function __construct(IPersistence $persistenceStrategy) {
$this->persistanceStrategy = $persistenceStrategy;
}
public function saveData() {
$this->persistanceStrategy->saveData($params);
}
public function getData() {
$this->persistanceStrategy->getData($params);
}
}
interface IPersistence {
public function saveData(array $params);
public function getData(array $params);
}
class XMLPersistence implements IPersistence {
// Params would contain something like path to the
// xml file, and unique name of some tag
public function saveData(array $params) {
// write something to xml file
}
public function getData(array $params) {
// get something from xml file
}
}
class DBPersistence implements IPersistence {
// $params would contain unique name of data that is needed
// and data for db connection
...
}
class SessionPersistence implements IPersistence {
....
}
Questions:
-
Is there a better way of designing this part of code.
-
How would someone unit test DataManager class, and “strategy” classes?
Using the stragety pattern here makes sense to me, so I don’t have any improvement suggestions.
Here’s a blog with one way to test your DataManager class. Basically you give it a mock strategy class and make sure that the proper strategy methods are indeed called.
Testing Your Mocks
I think testing the persistence classes would straightforward, make sure the XMLPersistence makes expected xml data, DB puts data in expected database, and maybe Session is asserted against an expected serialization.