Skip to content

Instantly share code, notes, and snippets.

@fdubost
Last active June 13, 2018 14:53
Show Gist options
  • Save fdubost/6761662 to your computer and use it in GitHub Desktop.
Save fdubost/6761662 to your computer and use it in GitHub Desktop.
SQLite for Atoum tests in Symfony2
<?php
namespace M6\Helpers\Bundle\ORMBundle\Test\Controller;
use atoum\AtoumBundle\Test\Controller\ControllerTest as BaseControllerTest;
use atoum\AtoumBundle\Test\Asserters;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Output\NullOutput;
/**
* Test controller
*
* @author Morgan Brunot <[email protected]>
*/
abstract class ControllerTest extends BaseControllerTest
{
protected $application;
/**
* Get current entity manager name.
*
* @return string
*/
abstract protected function getEntityManangerName();
/**
* Execute tests.
*
* @return void
*/
abstract protected function runTests();
/**
* Get console application.
*
* @return Symfony\Bundle\FrameworkBundle\Console\Application
*/
protected function getApplication()
{
if (null === $this->application) {
$client = $this->createClient();
$this->application = new Application($client->getKernel());
$this->application->setAutoExit(false);
}
return $this->application;
}
/**
* Run command.
*
* @param array $arguments
*
* @return void
*/
protected function runCommand($arguments = array())
{
$input = new ArrayInput($arguments);
$this->getApplication()->run($input, new NullOutput());
}
/**
* Create database.
*
* @return void
*/
protected function createDatabase()
{
$this->runCommand(array(
'command' => 'doctrine:schema:create',
'--env' => 'test',
'--em' => $this->getEntityManangerName(),
));
}
/**
* Drop database.
*
* @return void
*/
protected function deleteDatabase()
{
$this->runCommand(array(
'command' => 'doctrine:schema:drop',
'--env' => 'test',
'--em' => $this->getEntityManangerName(),
'--force' => true,
));
}
/**
* Method call by atoum for run tests.
*
* @return void
*/
public function testController()
{
$this->deleteDatabase();
$this->createDatabase();
$this->runTests();
}
}
<?php
namespace M6\Contents\Bundle\SurveyBundle\Tests\Controller;
use M6\Helpers\Bundle\ORMBundle\Test\Controller\ControllerTest;
/**
* Class SurveyController
*
* @author Jérémy Jourdin <[email protected]>
*/
class SurveyController extends ControllerTest
{
public function getEntityManagerName()
{
return "survey";
}
public function checkBadParameters()
{
//--
return $this;
}
public function checkPostAndGet()
{
//--
return $this;
}
public function runTests()
{
$this->checkBadParameters()
->checkPostAndGet();
}
}
@mickaelandrieu
Copy link

Thanks for share. A little typo in https://gist.github.com/fdubost/6761662#file-gistfile1-php-L26 I guess Manager/Mananger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment