HasEntityManager::teardownEntityManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Zenstruck\Porpaginas\Tests\Doctrine;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\Tools\SchemaTool;
7
use Doctrine\ORM\Tools\Setup;
8
use Zenstruck\Porpaginas\Tests\Doctrine\Fixtures\ORMEntity;
9
10
/**
11
 * @author Kevin Bond <[email protected]>
12
 */
13
trait HasEntityManager
14
{
15
    protected ?EntityManager $em = null;
16
17
    /**
18
     * @before
19
     */
20
    protected function setupEntityManager(): void
21
    {
22
        $paths = [];
23
        $isDevMode = false;
24
25
        // the connection configuration
26
        $dbParams = [
27
            'driver' => 'pdo_sqlite',
28
            'memory' => true,
29
        ];
30
31
        $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
32
        $this->em = EntityManager::create($dbParams, $config);
33
34
        $schemaTool = new SchemaTool($this->em);
35
        $schemaTool->createSchema([
36
            $this->em->getClassMetadata(ORMEntity::class),
37
        ]);
38
    }
39
40
    /**
41
     * @after
42
     */
43
    protected function teardownEntityManager(): void
44
    {
45
        $this->em = null;
46
    }
47
48
    protected function persistEntities(int $count): void
49
    {
50
        for ($i = 0; $i < $count; ++$i) {
51
            $this->em->persist(new ORMEntity('value '.($i + 1)));
0 ignored issues
show
Bug introduced by
The method persist() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
            $this->em->/** @scrutinizer ignore-call */ 
52
                       persist(new ORMEntity('value '.($i + 1)));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
        }
53
54
        $this->em->flush();
55
        $this->em->clear();
56
    }
57
}
58