Reputation: 1491
I'm creating a small bundle for Symfony2, which does nothing more than provide a service; it can be called to insert a line into the database, containing a few parameters:
class AuditLogService {
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function log($type, $channel, $message) {
$log = new AuditLog();
$log->setType($type);
$log->setChannel($channel);
$log->setMessage($message);
$this->em->persist($log);
$this->em->flush();
}
}
Now I need to write a test for it, so basically here are the steps I'm assuming I should take:
write a test that gets logger service, but uses a mock entityManager
instead?
call function on that logger which uses the mocked em
try to fetch it back from the mocked em, using the parameters I used in step 2
assert if I find a result or not
I'm pretty sure that in theory these are the correct steps, but can anyone point me into the right direction as to how to actually do it?
I have this:
public function testServiceLoaded()
{
$this->assertEquals(get_class($this->container->get('bbit_audit_log.service')), 'BBIT\AuditLogBundle\Services\AuditLogService');
}
This comes back with the following error:
"bbit_audit_log.service" has a dependency on a non-existent service "doctrine.orm.entity_manager"
It seems like my container does not contain the entitymanager
?
Upvotes: 0
Views: 67
Reputation: 335
In unit test skip 3 and 4. It's not responsibility of AuditLogService to save in the database (but to call em). 3 and 4 should be tested by functional test.
Upvotes: 1