Skip to content

Instantly share code, notes, and snippets.

@liuggio
Created February 10, 2012 14:26
Show Gist options
  • Save liuggio/1789902 to your computer and use it in GitHub Desktop.
Save liuggio/1789902 to your computer and use it in GitHub Desktop.
phpunit-closure best practice
/**
* insert a new row into the Entity trackVisit
*
* @param int $affiliateCampainId
* @param string $ipAddress
*/
public function trackVisit($affiliateCampainId, $ipAddress)
{
$visit = new trackVisit();
$visit->setIdentifier($affiliateCampainId);
$visit->setIpAddress($ipAddress);
$this->getObjectManager()->persist($visit);
$this->getObjectManager()->flush();
}
/**
* @cover ............/PartnerManager::trackVisit
*/
public function testTrackVisit()
{
$ipaddressToAssert = '127.0.0.1';
$phpUnitThis = $this;
$this->em = $this->getMock('EntityManager', array('persist', 'flush'));
$this->em
->expects($this->any())
->method('persist')
->will($this->returnCallback(function($entity) use ($phpUnitThis, $ipaddressToAssert) {
$phpUnitThis->assertEquals($entity->getIpAddress(),$ipaddressToAssert);
return $entity; }));
$this->em
->expects($this->any())
->method('flush')
->will($this->returnValue(true));
$partnerManager = $this->getMockBuilder('Liuggio\..\Entity\PartnerManager')
->disableOriginalConstructor()
->setMethods(array('getObjectManager'))
->getMock();
$partnerManager->expects($this->any())
->method('getObjectManager')
->will($this->returnValue($this->em));
$partnerManager->trackVisit(123, $ipaddressToAssert);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment