| 1 | <?php |
||
| 10 | class Client extends BaseClient |
||
| 11 | { |
||
| 12 | protected static $connection; |
||
| 13 | |||
| 14 | protected $requested = true; |
||
| 15 | |||
| 16 | public static function initClient() |
||
| 17 | { |
||
| 18 | $kernel = new AppKernel('test', true); |
||
| 19 | $kernel->loadClassCache(); |
||
| 20 | $client = new self($kernel); |
||
| 21 | |||
| 22 | return $client; |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @see http://alexandre-salome.fr/blog/Symfony2-Isolation-Of-Tests |
||
| 27 | * |
||
| 28 | * @param Request $request A Request instance |
||
| 29 | * |
||
| 30 | * @return Response A Response instance |
||
| 31 | */ |
||
| 32 | public function doRequest($request) |
||
| 33 | { |
||
| 34 | if ($this->requested) { |
||
| 35 | $this->getKernel()->shutdown(); |
||
| 36 | $this->getKernel()->boot(); |
||
| 37 | } |
||
| 38 | if (null === self::$connection) { |
||
| 39 | self::$connection = $this->getContainer()->get('doctrine.dbal.default_connection'); |
||
| 40 | } else { |
||
| 41 | $this->getContainer()->set('doctrine.dbal.default_connection', self::$connection); |
||
| 42 | } |
||
| 43 | $this->requested = true; |
||
| 44 | self::$connection->beginTransaction(); |
||
| 45 | $response = $this->getKernel()->handle($request); |
||
| 46 | self::$connection->rollback(); |
||
| 47 | |||
| 48 | return $response; |
||
| 49 | } |
||
| 50 | } |
||
| 51 |