Completed
Branch master (c3d959)
by Alexandre
09:54
created

TestCase::generateSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 13
rs 9.4285
c 2
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Heri\Bundle\JobQueu...AppKernel('test', true) of type object<Heri\Bundle\JobQu...Bundle\Tests\AppKernel> is incompatible with the declared type object<Heri\Bundle\JobQu...t\HttpKernel\AppKernel> of property $kernel.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->kernel->getContainer() can also be of type object<Symfony\Component...ion\ContainerInterface>. However, the property $container is declared as type object<Heri\Bundle\JobQu...ncyInjection\Container>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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();
0 ignored issues
show
Unused Code introduced by
$fs is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
109
        //$fs->remove($dir);
110
    }
111
}
112