BaseTestCase   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 7
Bugs 0 Features 1
Metric Value
wmc 9
c 7
b 0
f 1
lcom 1
cbo 10
dl 0
loc 95
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 14 1
A tearDown() 0 4 1
A getConfig() 0 4 1
A getSchemaClasses() 0 8 1
A createProduct() 0 11 1
A createProductsCollection() 0 12 2
A getEntityManagerFactory() 0 5 1
A getContainer() 0 9 1
1
<?php
2
3
namespace Managlea\Tests;
4
5
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\ORM\Tools\SchemaTool;
8
use Doctrine\ORM\Tools\Setup;
9
use Managlea\Component\EntityManager\DoctrineEntityManager;
10
use Managlea\Component\EntityManagerFactory;
11
use Managlea\Tests\Models\Product;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\Yaml\Yaml;
14
15
class BaseTestCase extends \PHPUnit_Framework_TestCase
16
{
17
    /**
18
     * @var DoctrineEntityManager
19
     */
20
    protected $entityManager;
21
22
    /**
23
     * @var SchemaTool
24
     */
25
    protected $schemaTool;
26
27
    const SCHEMA_PRODUCT = 'Managlea\Tests\Models\Product';
28
29
    /**
30
     * @throws \Doctrine\ORM\Tools\ToolsException
31
     */
32
    public function setUp()
33
    {
34
        $paths = array(__DIR__ . "/Models");
35
        $config = Setup::createAnnotationMetadataConfiguration($paths, true);
36
37
        $connectionConfig = $this->getConfig();
38
        $entityManager = EntityManager::create($connectionConfig['parameters'], $config);
39
40
        $this->entityManager = new DoctrineEntityManager($entityManager);
41
        $this->schemaTool = new SchemaTool($this->entityManager);
42
43
        $this->schemaTool->dropSchema($this->getSchemaClasses());
44
        $this->schemaTool->createSchema($this->getSchemaClasses());
45
    }
46
47
    public function tearDown()
48
    {
49
        $this->schemaTool->dropSchema($this->getSchemaClasses());
50
    }
51
52
    public function getConfig()
53
    {
54
        return Yaml::parse(file_get_contents(__DIR__ . '/../config/database.yml'));
55
    }
56
57
    private function getSchemaClasses()
58
    {
59
        $classes = array(
60
            $this->entityManager->getClassMetadata(self::SCHEMA_PRODUCT)
61
        );
62
63
        return $classes;
64
    }
65
66
    /**
67
     * @return Product
68
     */
69
    protected function createProduct()
70
    {
71
        $product = new Product();
72
        $product->setName(rand(1, 9) . uniqid());
73
        $product->setDateOfBirth('1970-01-01');
74
75
        $this->entityManager->persist($product);
76
        $this->entityManager->flush();
77
78
        return $product;
79
    }
80
81
    protected function createProductsCollection($min = 25, $max = 35)
82
    {
83
        $noOfProducts = rand($min, $max);
84
85
        // Generate random objects
86
        $products = array();
87
        for ($i = 1; $i <= $noOfProducts; $i++) {
88
            $products[] = $this->createProduct();
89
        }
90
91
        return $products;
92
    }
93
94
    protected function getEntityManagerFactory()
95
    {
96
        $container = $this->getContainer();
97
        return new EntityManagerFactory($container);
98
    }
99
100
    private function getContainer()
101
    {
102
        $container = new ContainerBuilder();
103
        $container
104
            ->register('doctrine_entity_manager', 'Managlea\Component\EntityManager\DoctrineEntityManager')
105
            ->addArgument($this->entityManager);
106
107
        return $container;
108
    }
109
}