Skip to content

Instantly share code, notes, and snippets.

@kix
Created June 4, 2013 12:55
Show Gist options
  • Save kix/5705677 to your computer and use it in GitHub Desktop.
Save kix/5705677 to your computer and use it in GitHub Desktop.
<?php
/**
* WebTestCase client-aware test case
*/
trait ClientAware
{
/**
* @var \Symfony\Bundle\FrameworkBundle\Client
*/
private $client;
/**
* Sets up client
*/
public function setUp()
{
$this->client = static::createClient();
}
/**
* @return \Symfony\Component\DependencyInjection\ContainerInterface
*/
public function getContainer()
{
return $this->client->getContainer();
}
}
<?php
use \Doctrine\Common\DataFixtures;
/**
* Purges DB, needs container
*/
trait PurgeDb
{
use ClientAware {
setUp as clientAwareSetUp;
}
/**
* Calls ClientAware trait method
*/
public function setUp()
{
$this->clientAwareSetUp();
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$purger = new DataFixtures\Purger\ORMPurger($em);
$purger->purge();
}
}
<?php
use \Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use PurgeDb;
class FakerTest extends WebTestCase
{
use PurgeDb;
public function testPurgeAndPopulateEntity()
{
/** @var $em \Doctrine\ORM\EntityManager */
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$this->assertEquals(
0,
count($em->getRepository('WtProMainBundle:User')->findAll())
);
$faker = new \My\Faker($this->getContainer());
$populator = $faker->getPopulator();
$populator->addEntity('Entity\User', 3);
$populator->execute(
$this->getContainer()->get('doctrine.orm.entity_manager')
);
$this->assertEquals(
3,
count($em->getRepository('WtProMainBundle:User')->findAll())
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment