Passed
Pull Request — master (#55)
by Thomas
01:33
created

EntityManagerMock::retrieve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace ORM\Testing;
4
5
use ORM\Entity;
6
use ORM\EntityFetcher;
7
use ORM\EntityManager;
8
use ORM\Exception\NoEntity;
9
use ORM\Testing\EntityFetcherMock\Result;
10
use ORM\Testing\EntityFetcherMock\ResultRepository;
11
12
class EntityManagerMock extends EntityManager
13
{
14
    protected $resultRepository;
15
16
    public function __construct($options = [])
17
    {
18
        static::$emMapping = [
19
            'byClass'     => [],
20
            'byNameSpace' => [],
21
            'byParent'    => [],
22
            'last'        => null,
23
        ];
24
        parent::__construct($options);
25
        $this->resultRepository = new ResultRepository($this);
26
    }
27
28
    /**
29
     * Add an entity to be fetched by primary key
30
     *
31
     * The entity needs to have a primary key if not it will be filled with random values between RANDOM_KEY_MIN and
32
     * RANDOM_KEY_MAX (at the time writing this it is 1000000000 and 1000999999).
33
     *
34
     * You can pass mocks from Entity too but we need to call `Entity::getPrimaryKey()`.
35
     *
36
     * @param Entity $entity
37
     * @codeCoverageIgnore proxy method
38
     */
39
    public function addEntity(Entity $entity)
40
    {
41
        $this->resultRepository->addEntity($entity);
42
    }
43
44
    /**
45
     * Retrieve an entity by $primaryKey
46
     *
47
     * @param string $class
48
     * @param array $primaryKey
49
     * @return Entity|null
50
     * @codeCoverageIgnore proxy method
51
     */
52
    public function retrieve($class, array $primaryKey)
53
    {
54
        return $this->resultRepository->retrieve($class, $primaryKey);
55
    }
56
57
    /**
58
     * Create and add a EntityFetcherMock\Result for $class
59
     *
60
     * As the results are mocked to come from the database they will also get a primary key if they don't have already.
61
     *
62
     * @param $class
63
     * @param Entity ...$entities
64
     * @return Result
65
     * @codeCoverageIgnore proxy method
66
     */
67
    public function addResult($class, Entity ...$entities)
68
    {
69
        return $this->resultRepository->addResult($class, ...$entities);
70
    }
71
72
    /**
73
     * Get the results for $class and $query
74
     *
75
     * The EntityFetcherMock\Result gets a quality for matching this query. Only the highest quality will be used.
76
     *
77
     * @param string $class
78
     * @param EntityFetcher $fetcher
79
     * @return array
80
     * @codeCoverageIgnore proxy method
81
     */
82
    public function getResults($class, EntityFetcher $fetcher)
83
    {
84
        return $this->resultRepository->getResults($class, $fetcher);
85
    }
86
87
    public function fetch($class, $primaryKey = null)
88
    {
89
        $reflection = new \ReflectionClass($class);
90
        if (!$reflection->isSubclassOf(Entity::class)) {
91
            throw new NoEntity($class . ' is not a subclass of Entity');
92
        }
93
        /** @var string|Entity $class */
94
95
        if ($primaryKey === null) {
96
            return new EntityFetcherMock($this, $class);
97
        }
98
99
        $primaryKey = $this->buildPrimaryKey($class, (array)$primaryKey);
100
        $checksum = $this->buildChecksum($primaryKey);
101
102
        if (isset($this->map[$class][$checksum])) {
103
            return $this->map[$class][$checksum];
104
        }
105
106
        return $this->retrieve($class, $primaryKey);
0 ignored issues
show
Bug introduced by
It seems like $class can also be of type ORM\Entity; however, parameter $class of ORM\Testing\EntityManagerMock::retrieve() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

106
        return $this->retrieve(/** @scrutinizer ignore-type */ $class, $primaryKey);
Loading history...
107
    }
108
}
109