Completed
Push — master ( f4b85b...244823 )
by Alexandre
02:30
created

TestCase   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 103
rs 10
c 6
b 0
f 1
1
<?php
2
3
namespace Heri\Bundle\JobQueueBundle\Tests;
4
5
use Symfony\Component\HttpKernel\Kernel;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Doctrine\ORM\Tools\SchemaTool;
8
9
abstract class TestCase extends \PHPUnit_Extensions_Database_TestCase
10
{
11
    /**
12
     * @var Symfony\Component\HttpKernel\AppKernel
13
     */
14
    protected $kernel;
15
16
    /**
17
     * @var Doctrine\ORM\EntityManager
18
     */
19
    protected $em;
20
21
    /**
22
     * @var Symfony\Component\DependencyInjection\Container
23
     */
24
    protected $container;
25
26
    public function setUp()
27
    {
28
        require_once __DIR__.'/Fixtures/app/AppKernel.php';
29
30
        // boot the AppKernel in the test environment and with the debug.
31
        $this->kernel = new \Heri\Bundle\JobQueueBundle\Tests\AppKernel('test', true);
32
        $this->kernel->boot();
33
34
        $this->deleteTmpDir();
35
36
        // store the container and the entity manager in test case properties
37
        $this->container = $this->kernel->getContainer();
38
        $this->em = $this->container->get('doctrine')->getManager();
39
40
        parent::setUp();
41
    }
42
43
    public function getConnection()
44
    {
45
        // Retrieve PDO instance
46
        $pdo = $this->em->getConnection()->getWrappedConnection();
47
48
        // Clear Doctrine to be safe
49
        $this->em->clear();
50
51
        // Schema Tool to process our entities
52
        $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
53
        $classes = $this->em->getMetaDataFactory()->getAllMetaData();
54
55
        // Drop all classes and re-build them for each test case
56
        $tool->dropSchema($classes);
57
        $tool->createSchema($classes);
58
59
        // Pass to PHPUnit
60
        return $this->createDefaultDBConnection($pdo, 'db_name');
61
    }
62
63
    /**
64
     * @return PHPUnit_Extensions_Database_DataSet_IDataSet
65
     */
66
    public function getDataSet()
67
    {
68
        return $this->createFlatXMLDataSet(dirname(__FILE__).'/Fixtures/data.xml');
69
    }
70
71
    public function tearDown()
72
    {
73
        // shutdown the kernel.
74
        $this->kernel->shutdown();
75
76
        parent::tearDown();
77
    }
78
79
    protected function generateSchema()
80
    {
81
        // get the metadata of the application to create the schema.
82
        $metadata = $this->getMetadata();
83
84
        if (!empty($metadata)) {
85
            // create SchemaTool
86
            $tool = new SchemaTool($this->em);
87
            $tool->createSchema($metadata);
88
        } else {
89
            throw new Doctrine\DBAL\Schema\SchemaException('No Metadata classes to process.');
90
        }
91
    }
92
93
    /**
94
     * Overwrite this method to get specific metadata.
95
     *
96
     * @return Array
97
     */
98
    protected function getMetadata()
99
    {
100
        return $this->em->getMetadataFactory()->getAllMetadata();
101
    }
102
103
    protected function deleteTmpDir()
104
    {
105
        if (!file_exists($dir = sys_get_temp_dir().'/'.Kernel::VERSION)) {
106
            return;
107
        }
108
        $fs = new Filesystem();
109
        //$fs->remove($dir);
110
    }
111
}
112