Created
July 22, 2012 14:23
-
-
Save tPl0ch/3159848 to your computer and use it in GitHub Desktop.
Symfony2 ContainerAware TestCase Base class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Symfony2 ContainerAware TestCase Base class example implementation | |
* | |
* @author Thomas Ploch | |
* @copyright 2012 REIZWERK GmbH | |
* @license The MIT License (MIT) | |
*/ | |
namespace Reizwerk\ContainerTestBundle\Tests; | |
use Reizwerk\ContainerTestBundle\Tests\Testsuite as Testsuite; | |
/** | |
* Diese Klasse erbt von unserer neuen Basis Klasse. | |
* | |
* @author Thomas Ploch | |
*/ | |
class MyServiceTest extends Testsuite\PHPUnit_Sf2Service_TestCase | |
{ | |
/** | |
* Hier wird der Kernel geladen. | |
* | |
* Die Methode wird von PHPUnit für jede Test Klasse nur einmal | |
* aufgerufen. | |
* | |
* @static | |
* @return void | |
*/ | |
public static function setUpBeforeClass() | |
{ | |
// Kernel in der Test-Umgebung mit Debug laden | |
static::loadKernel( | |
__DIR__ . '/../../../../../app/AppKernel.php', | |
'test', | |
true | |
); | |
} | |
/** | |
* Diese Methode wird von PHPUnit vor jedem Test aufgerufen. | |
* | |
* @return void | |
*/ | |
public function setUp() | |
{ | |
$this->container = self::$kernel->getContainer('my_container'); | |
$this->service = $this->container->get('my_service_id'); | |
} | |
/** | |
* Testet eine Service Methode | |
* | |
* @return void | |
*/ | |
public function testServiceMethod() | |
{ | |
$this->assertTrue($this->service->doSomething()); | |
} | |
/** | |
* Falls Ihr in euren Tests die statische Methode `tearDownAfterClass` | |
* überladet, könnt ihr seit PHP 5.3 und Late Static Binding die | |
* Methode der Eltern-Klasse aufrufen. | |
* | |
* Nutzt dazu das `static` keyword. | |
* | |
* @return void | |
*/ | |
public static function tearDownAfterClass() | |
{ | |
// Gibt den Kernel frei | |
static::tearDownAfterClass(); | |
// Hier mehr Zeugs SNIP... | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Symfony2 ContainerAware TestCase Base class | |
* | |
* @author Thomas Ploch | |
* @copyright 2012 REIZWERK GmbH | |
* @license The MIT License (MIT) | |
*/ | |
namespace Reizwerk\ContainerTestBundle\Tests\Testsuite; | |
/** | |
* Sf2 Service Container Test Klasse | |
* | |
* @author Thomas Ploch | |
*/ | |
class PHPUnit_Sf2Service_TestCase extends \PHPUnit_Framework_TestCase | |
{ | |
/** | |
* Hier wird die AppKernel Instanz gespeichert. | |
* | |
* @var \AppKernel | |
*/ | |
public static $kernel; | |
/** | |
* Nach den Tests den Kernel freigeben. | |
* | |
* @static | |
* @return void | |
*/ | |
public static function tearDownAfterClass() | |
{ | |
self::$kernel = null; | |
} | |
/** | |
* Den Kernel laden. | |
* | |
* Dazu wird der absolute Pfad zur AppKernel.php Datei in eurem 'app' | |
* Verzeichnis als erstes Argument benötigt. | |
* | |
* Ihr könnt auch noch verschiedene Umgebungen ('prod', 'dev', etc.) | |
* angeben, der default ist 'test'. Der dritte Parameter gibt an, ob | |
* der Kernel mit debug = true geladen werden soll. | |
* | |
* @static | |
* | |
* @param string $path Der Pfad zur 'AppKernel.php' Datei | |
* @param string $env [optional] Die Umgebung, default ist 'test' | |
* @param bool $debug [optional] Debug Einstellung, default ist TRUE | |
* | |
* @return void | |
*/ | |
public static function loadKernel($path, $env = 'test', $debug = true) | |
{ | |
require_once $path; | |
self::$kernel = new \AppKernel($env, $debug); | |
self::$kernel->boot(); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment