Last active
August 29, 2015 14:03
-
-
Save zyphlar/c959b2d66bce1c10c637 to your computer and use it in GitHub Desktop.
Wrapper for cleaning up repetitious assertions in PHPunit / Symfony tests
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
<?php | |
namespace Acme\YourCompanyBundle\Tests\Controller; | |
use Liip\FunctionalTestBundle\Test\WebTestCase; | |
use Doctrine\ORM\Tools\SchemaTool; | |
// Note: be sure to replace any instance of "Your"/"your" with the appropriate values. | |
class YourControllerTest extends WebTestCase | |
{ | |
private function validateRecord($entityName, $foreignKey, $validators){ | |
$nulls = $validators['null']; | |
$equals = $validators['equals']; | |
$record = $this->em->getRepository('YourCompanyBundle:'.$entityName)->findOneBy( array('your_foreign_key' => $foreignKey ) ); | |
foreach( $nulls as $nullKey ){ | |
$compareValue = call_user_func( array( $record, 'get'.ucfirst($nullKey) ) ); | |
$this->assertNull( | |
$compareValue, | |
"Failed asserting null for ".$nullKey | |
); | |
} | |
foreach( $equals as $equalKey => $equalValue ){ | |
$compareValue = call_user_func( array( $record, 'get'.ucfirst($equalKey) ) ); | |
$this->assertEquals( | |
$equalValue, | |
$compareValue, | |
"Failed asserting equality for ".$equalKey | |
); | |
} | |
} | |
} |
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
<?php | |
// instead of a bunch of... | |
$this->assertEquals(true, $myObject->getSomeName()); | |
$this->assertEquals(true, $myObject->getOtherName()); | |
$this->assertEquals(false, $myObject->getFinalName()); | |
$this->assertNull($myObject->getOneName()); | |
$this->assertNull($myObject->getTwoName()); | |
$this->assertNull($myObject->getThreeName()); | |
// you can just do this... | |
$this->validateRecord('YourEntityClassName', $myObject, array( | |
'null' => array( | |
'oneName', | |
'twoName', | |
'threeName', | |
), | |
'equals' => array( | |
'someName' => true, | |
'otherName' => true, | |
'finalName' => false, | |
) | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment