Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 14:03
What would you like to do?
Wrapper for cleaning up repetitious assertions in PHPunit / Symfony tests
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) ) );
"Failed asserting null for ".$nullKey
foreach( $equals as $equalKey => $equalValue ){
$compareValue = call_user_func( array( $record, 'get'.ucfirst($equalKey) ) );
"Failed asserting equality for ".$equalKey
// instead of a bunch of...
$this->assertEquals(true, $myObject->getSomeName());
$this->assertEquals(true, $myObject->getOtherName());
$this->assertEquals(false, $myObject->getFinalName());
// you can just do this...
$this->validateRecord('YourEntityClassName', $myObject, array(
'null' => array(
'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