Reputation: 1380
I am having trouble testing some simple services that I have in my application. This is my service method:
public function createNewCourse($details = array())
{
$course = new Course($details);
$this->persistenceManager->save($course);
}
Basically I am sending an array to this service , create the $course object which sets the properties of the object. After that I call the persistenceManager which basically has a save method which inserts the object in the database. Anyway any tips of how to test this method without actually testing the persistence, because that will be another test.
Upvotes: 0
Views: 111
Reputation: 181
Use a stub persistenceManager and provide it to the class that contains the createNewCourse method. The stub should look like this:
class StubPersistenceManager
{
private $course;
public function save($course)
{
$this->course = $course;
}
public function getCourse()
{
return $this->course;
}
}
Ensure you inject an instance of this stub as the $this->persistenceManager in your class that contains the createNewCourse method.
In the test you call the createNewCourse method and can subsequently fetch the course instance provided to the persistence manager by using the getCourse function of the stub.
Upvotes: 0
Reputation: 18440
If persistanceManager
is injected to your object as a dependancy then you can create a mock object to represent it in your tests.
If it is not, then you will have great difficulty unit testing and you should refactor your code to use dependancy injection.
Don't worry, dependancy injection is just a fancy phrase for a pretty simple concept.
You are going to have similar problems unit testing the Course object.
Upvotes: 3