Created
February 10, 2012 14:26
-
-
Save liuggio/1789902 to your computer and use it in GitHub Desktop.
phpunit-closure best practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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