0
我創建的學說事件監聽器(堅持,更新&刪除),在遠程數據庫保存所有的動作(模式:誰做什麼,什麼時候,舊的價值,新價值)。Symfony2的 - 測試主義監聽
它的工作原理。
爲了持續集成,我在每次操作後都通過檢索這些日誌來編寫一些單元測試。
/**
* @param $uid user id
* @param $isValid is user need to be added
*/
public function testGetLogs($uid, $isValid) {
$um = static::$kernel->getContainer()->get('user_manager');
/** @var EntityManager $em */
$em = $this->doctrine->getManager();
$referentielUser = $um->getReferentielUser($uid);
// Lazy method to retrieve application's identifiers
$auditDummy = new AuditEntry();
$dir = $auditDummy->getAppDir();
$host = $auditDummy->getAppHost();
if (false !== $referentielUser) {
$user = $referentielUser->toUser();
// add the user
$valid = $um->addUser($user);
} else {
$valid = false;
}
$this->assertEquals($isValid, $valid);
if ($valid) {
// find latest logs for insertion
$logs = array_reverse($this->doctrineListener->getLogs(array('appdir'=>$dir, 'apphost'=>$host)));
$this->logTesting($logs[0], 'insert');
// edit the user
$user->setLocked(true);
$em->commit();
// find latest logs for edition
$logs = array_reverse($this->doctrineListener->getLogs(array('appdir'=>$dir, 'apphost'=>$host)));
$this->logTesting($logs[0], 'update');
// remove the user
$em->remove($user);
$em->commit();
// find latest logs for removal
$logs = array_reverse($this->doctrineListener->getLogs(array('appdir'=>$dir, 'apphost'=>$host)));
$this->logTesting($logs[0], 'delete');
}
}
private function logTesting($log, $type) {
$this->assertEquals(/* Folder */, $log['APPDIR']);
$this->assertEquals('cli', $log['IP']);
$this->assertEquals(/* Table */, $log['TARGETTABLE']);
$this->assertEquals(strtoupper($type), $log['ACTION']);
$dateLog = date_create_from_format('Y-m-d H:i:s', $log['DATETIME']);
$diff = $dateLog->diff(new \DateTime());
$this->assertLessThan(5, $diff->s);
}
用戶已正確添加,但未被編輯或刪除。 你能幫我嗎?
我可以連續使用兩次沖洗嗎? $ user-> setEnabled(false); $ em-> flush(); $ em-> remove($ user); $ em-> flush(); – Senorihl 2014-11-17 08:01:49
當然,您可以根據需要多次調用'flush'方法 - 它會寫入您目前所做的數據庫更改。 – kozlice 2014-11-17 10:34:53
我使用'flush'就像你說的,但它沒有通過'$ this-> logTesting()'方法,所以我不得不在'testGetLog'方法的主體中重寫它。 – Senorihl 2014-11-17 11:54:23