Completed
Push — master ( 482985...f93238 )
by
unknown
01:16
created

BaseTestCase::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Managlea\Tests;
4
5
6
use Doctrine\ORM\Tools\SchemaTool;
7
use Doctrine\ORM\Tools\Setup;
8
use Managlea\Component\EntityManager\DoctrineEntityManager;
9
use Managlea\Tests\Models\Product;
10
use Symfony\Component\Yaml\Yaml;
11
12
class BaseTestCase extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @var DoctrineEntityManager
16
     */
17
    protected $entityManager;
18
19
    /**
20
     * @var SchemaTool
21
     */
22
    protected $schemaTool;
23
24
    const SCHEMA_PRODUCT = 'Managlea\Tests\Models\Product';
25
26
    public function setUp()
27
    {
28
        $isDevMode = true;
29
        $paths = array(__DIR__ . "/Models");
30
31
        $connectionConfig = $this->getConfig();
32
33
        $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
34
        $this->entityManager = DoctrineEntityManager::initialize($connectionConfig['parameters'], $config);
35
36
        $this->schemaTool = new SchemaTool($this->entityManager);
37
38
        $this->schemaTool->dropSchema($this->getSchemaClasses());
39
        $this->schemaTool->createSchema($this->getSchemaClasses());
40
    }
41
42
    public function tearDown()
43
    {
44
        $this->schemaTool->dropSchema($this->getSchemaClasses());
45
    }
46
47
    public function getConfig()
48
    {
49
        return Yaml::parse(file_get_contents(__DIR__ . '/../config/database.yml'));
50
    }
51
52
    private function getSchemaClasses()
53
    {
54
        $classes = array(
55
            $this->entityManager->getClassMetadata(self::SCHEMA_PRODUCT)
56
        );
57
58
        return $classes;
59
    }
60
61
    /**
62
     * @return Product
63
     */
64
    protected function createProduct()
65
    {
66
        $product = new Product();
67
        $product->setName(rand(1, 9) . uniqid());
68
        $product->setDateOfBirth('1970-01-01');
69
70
        $this->entityManager->persist($product);
71
        $this->entityManager->flush();
72
73
        return $product;
74
    }
75
76
    protected function createProductsCollection($min = 25, $max = 35)
77
    {
78
        $noOfProducts = rand($min, $max);
79
80
        // Generate random objects
81
        $products = array();
82
        for ($i = 1; $i <= $noOfProducts; $i++) {
83
            $products[] = $this->createProduct();
84
        }
85
86
        return $products;
87
    }
88
}