Skip to content

Instantly share code, notes, and snippets.

@bamper bamper/ApiTestCase.php
Last active Feb 18, 2016

Embed
What would you like to do?
SQLite setup
<?php
namespace AppBundle\Test;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\CreateSchemaDoctrineCommand;
use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use GuzzleHttp\Client;
use GuzzleHttp\Subscriber\History;
use GuzzleHttp\Event\BeforeEvent;
use AppBundle\Entity\User;
use AppBundle\Entity\Programmer;
use Doctrine\ORM\EntityManager;
use Symfony\Component\PropertyAccess\PropertyAccess;
class ApiTestCase extends WebTestCase
{
/**
* @var EntityManager
*/
private $em;
private $application;
private static $staticClient;
/**
* @var Client
*/
protected $client;
/**
* @var History
*/
private static $history;
private $responseAsserter;
public static function setUpBeforeClass()
{
$baseUrl = getenv('TEST_BASE_URL');
self::$staticClient = new Client([
'base_url' => $baseUrl,
'defaults' => [
'exceptions' => false
]
]);
self::$history = new History();
self::$staticClient->getEmitter()->attach(self::$history);
self::$staticClient->getEmitter()->on('before', function(BeforeEvent $event) {
$path = $event->getRequest()->getPath();
if (strpos($path, '/api') === 0) {
$event->getRequest()->setPath('/app_test.php'.$path);
}
});
}
protected function setUp()
{
static::$kernel = static::createKernel();
static::$kernel->boot();
$this->application = new Application(static::$kernel);
// drop the database
$command = new DropDatabaseDoctrineCommand();
$this->application->add($command);
$input = new ArrayInput(array('command' => 'doctrine:database:drop','--force' => true));
$command->run($input, new NullOutput());
// close the connection after dropping the database so we don't get "No database selected" error
$connection = $this->getService('doctrine')->getConnection();
if ($connection->isConnected()) {
$connection->close();
}
// create the database
$command = new CreateDatabaseDoctrineCommand();
$this->application->add($command);
$input = new ArrayInput(array('command' => 'doctrine:database:create'));
$command->run($input, new NullOutput());
// create schema
$command = new CreateSchemaDoctrineCommand();
$this->application->add($command);
$input = new ArrayInput(array('command' => 'doctrine:schema:create'));
$command->run($input, new NullOutput());
// get the Entity Manager
$this->em = $this->getService('doctrine.orm.entity_manager');
// load fixtures
$clientFixture = static::createClient();
$loader = new ContainerAwareLoader($clientFixture->getContainer());
$loader->loadFromDirectory(static::$kernel->locateResource('@AppBundle/DataFixtures/ORM'));
$purger = new ORMPurger($this->em);
$executor = new ORMExecutor($this->em, $purger);
$executor->execute($loader->getFixtures());
$this->client = self::$staticClient;
}
protected function getService($id)
{
return self::$kernel->getContainer()->get($id);
}
protected function createUser($username, $plainPassword = 'foo')
{
$user = new User();
$user->setUsername($username);
$user->setEmail($username.'@foo.com');
$password = $this->getService('security.password_encoder')->encodePassword($user, $plainPassword);
$user->setPassword($password);
$em = $this->em;
$em->persist($user);
$em->flush();
return $user;
}
protected function createProgrammer(array $data)
{
$data = array_merge(array(
'powerLevel' => rand(0, 10),
'user' => $this->em->getRepository('AppBundle:User')->findAny()
), $data);
// Use PropertyAccess component instead of iterating and calling each setter.
// Call $accessor->setValue(), pass in $programmer, $key (property name) and $value.
$accessor = PropertyAccess::createPropertyAccessor();
$programmer = new Programmer();
foreach ($data as $key => $value) {
$accessor->setValue($programmer, $key, $value);
}
$this->em->persist($programmer);
$this->em->flush();
return $programmer;
}
/**
* @return ResponseAsserter
*/
protected function asserter()
{
if ($this->responseAsserter === null) {
$this->responseAsserter = new ResponseAsserter();
}
return $this->responseAsserter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.