EntityTestCase::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace WebCMS\Tests;
4
5
abstract class EntityTestCase extends BasicTestCase
6
{
7
    protected $exceptions = array(
8
        'BreadcrumbsItem.php',
9
    );
10
11
    protected $tool;
12
13
    public function __construct()
14
    {
15
        parent::__construct();
16
17
        $this->tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
18
    }
19
20
    public function setUp()
21
    {
22
        parent::setUp();
23
24
        $this->tool->createSchema($this->getClassesMetadata(__DIR__.'/../Entity', 'WebCMS\\Entity'));
25
    }
26
27
    public function tearDown()
28
    {
29
        parent::tearDown();
30
31
        $this->em->clear();
32
33
        $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
34
        $tool->dropDatabase();
35
    }
36
37
    /**
38
     * @param string $path
39
     * @param string $namespace
40
     */
41
    protected function getClassesMetadata($path, $namespace)
42
    {
43
        $metadata = array();
44
45
        if ($handle = opendir($path)) {
46
            while (false !== ($file = readdir($handle))) {
47
                if (strstr($file, '.php') && $this->isEntity($file)) {
48
                    list($class) = explode('.', $file);
49
                    $metadata[] = $this->em->getClassMetadata($namespace.'\\'.$class);
50
                }
51
            }
52
        }
53
54
        return $metadata;
55
    }
56
57
    /**
58
     * @param string $path
59
     */
60
    private function isEntity($path)
61
    {
62
        foreach ($this->exceptions as $exception) {
63
            if (strpos($exception, $path) !== false) {
64
                return false;
65
            }
66
        }
67
68
        return true;
69
    }
70
}
71