Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Wrapper for cleaning up repetitious assertions in PHPunit / Symfony tests
<?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
);
}
}
}
<?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