/
SymfonyDoctrineContext.php
92 lines (81 loc) · 2.19 KB
/
SymfonyDoctrineContext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace Behat\CommonContexts;
use Behat\Behat\Context\BehatContext;
use Behat\Behat\Event\ScenarioEvent;
use Behat\Symfony2Extension\Context\KernelAwareInterface;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* Provides hooks for building and cleaning up a database schema with Doctrine.
*
* While building the schema it takes all the entity metadata known to Doctrine.
*
* @author Jakub Zalas <jakub@zalas.pl>
*/
class SymfonyDoctrineContext extends BehatContext implements KernelAwareInterface
{
/**
* @var \Symfony\Component\HttpKernel\KernelInterface $kernel
*/
private $kernel = null;
/**
* @param \Behat\Behat\Event\ScenarioEvent|\Behat\Behat\Event\OutlineExampleEvent $event
*
* @BeforeScenario
*
* @return null
*/
public function buildSchema($event)
{
$metadata = $this->getMetadata();
if (!empty($metadata)) {
$tool = new SchemaTool($this->getEntityManager());
$tool->dropSchema($metadata);
$tool->createSchema($metadata);
}
}
/**
* @param \Behat\Behat\Event\ScenarioEvent|\Behat\Behat\Event\OutlineExampleEvent $event
*
* @AfterScenario
*
* @return null
*/
public function closeDBALConnections($event)
{
$this->getEntityManager()->clear();
foreach ($this->getConnections() as $connection) {
$connection->close();
}
}
/**
* @param \Symfony\Component\HttpKernel\KernelInterface $kernel
*
* @return null
*/
public function setKernel(KernelInterface $kernel)
{
$this->kernel = $kernel;
}
/**
* @return array
*/
protected function getMetadata()
{
return $this->getEntityManager()->getMetadataFactory()->getAllMetadata();
}
/**
* @return \Doctrine\ORM\EntityManager
*/
protected function getEntityManager()
{
return $this->kernel->getContainer()->get('doctrine.orm.entity_manager');
}
/**
* @return array
*/
protected function getConnections()
{
return $this->kernel->getContainer()->get('doctrine')->getConnections();
}
}