Skip to content

Instantly share code, notes, and snippets.

@zyphlar
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zyphlar/c959b2d66bce1c10c637 to your computer and use it in GitHub Desktop.
Save zyphlar/c959b2d66bce1c10c637 to your computer and use it in GitHub Desktop.
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