Completed
Push — master ( 2c2a55...46eca6 )
by Gianluca
05:40
created

TestCase::getEntityManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineORMModuleTest\Framework;
21
22
use PHPUnit_Framework_TestCase;
23
use Doctrine\ORM\EntityManager;
24
use Doctrine\ORM\Tools\SchemaTool;
25
use DoctrineORMModuleTest\Util\ServiceManagerFactory;
26
27
/**
28
 * Base test case for tests using the entity manager
29
 */
30
class TestCase extends PHPUnit_Framework_TestCase
31
{
32
    /**
33
     * @var boolean
34
     */
35
    protected $hasDb = false;
36
37
    /**
38
     * @var EntityManager
39
     */
40
    private $entityManager;
41
42
    /**
43
     * Creates a database if not done already.
44
     */
45
    public function createDb()
46
    {
47
        if ($this->hasDb) {
48
            return;
49
        }
50
51
        $em   = $this->getEntityManager();
52
        $tool = new SchemaTool($em);
53
        $tool->updateSchema($em->getMetadataFactory()->getAllMetadata());
54
        $this->hasDb = true;
55
    }
56
57
    /**
58
     * Drops existing database
59
     */
60
    public function dropDb()
61
    {
62
        $em   = $this->getEntityManager();
63
        $tool = new SchemaTool($em);
64
        $tool->dropSchema($em->getMetadataFactory()->getAllMetadata());
65
        $em->clear();
66
67
        $this->hasDb = false;
68
    }
69
70
    /**
71
     * Get EntityManager.
72
     *
73
     * @return EntityManager
74
     */
75
    public function getEntityManager()
76
    {
77
        if ($this->entityManager) {
78
            return $this->entityManager;
79
        }
80
81
        $serviceManager = ServiceManagerFactory::getServiceManager();
82
        $serviceManager->get('doctrine.entity_resolver.orm_default');
83
        $this->entityManager = $serviceManager->get('doctrine.entitymanager.orm_default');
0 ignored issues
show
Documentation Bug introduced by
It seems like $serviceManager->get('do...tymanager.orm_default') can also be of type array. However, the property $entityManager is declared as type object<Doctrine\ORM\EntityManager>. 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...
84
85
        return $this->entityManager;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->entityManager; of type object|array adds the type array to the return on line 85 which is incompatible with the return type documented by DoctrineORMModuleTest\Fr...tCase::getEntityManager of type Doctrine\ORM\EntityManager.
Loading history...
86
    }
87
}
88