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

EntityManagerMock::addEntity()   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 1
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
        parent::__construct($options);
19
        $this->resultRepository = new ResultRepository($this);
20
    }
21
22
    /**
23
     * Add an entity to be fetched by primary key
24
     *
25
     * The entity needs to have a primary key if not it will be filled with random values between RANDOM_KEY_MIN and
26
     * RANDOM_KEY_MAX (at the time writing this it is 1000000000 and 1000999999).
27
     *
28
     * You can pass mocks from Entity too but we need to call `Entity::getPrimaryKey()`.
29
     *
30
     * @param Entity $entity
31
     * @codeCoverageIgnore proxy method
32
     */
33
    public function addEntity(Entity $entity)
34
    {
35
        $this->resultRepository->addEntity($entity);
36
    }
37
38
    /**
39
     * Retrieve an entity by $primaryKey
40
     *
41
     * @param string $class
42
     * @param array $primaryKey
43
     * @return Entity|null
44
     * @codeCoverageIgnore proxy method
45
     */
46
    public function retrieve($class, array $primaryKey)
47
    {
48
        return $this->resultRepository->retrieve($class, $primaryKey);
49
    }
50
51
    /**
52
     * Create and add a EntityFetcherMock\Result for $class
53
     *
54
     * As the results are mocked to come from the database they will also get a primary key if they don't have already.
55
     *
56
     * @param $class
57
     * @param Entity ...$entities
58
     * @return Result
59
     * @codeCoverageIgnore proxy method
60
     */
61
    public function addResult($class, Entity ...$entities)
62
    {
63
        return $this->resultRepository->addResult($class, ...$entities);
64
    }
65
66
    /**
67
     * Get the results for $class and $query
68
     *
69
     * The EntityFetcherMock\Result gets a quality for matching this query. Only the highest quality will be used.
70
     *
71
     * @param string $class
72
     * @param EntityFetcher $fetcher
73
     * @return array
74
     * @codeCoverageIgnore proxy method
75
     */
76
    public function getResults($class, EntityFetcher $fetcher)
77
    {
78
        return $this->resultRepository->getResults($class, $fetcher);
79
    }
80
81
    public function fetch($class, $primaryKey = null)
82
    {
83
        $reflection = new \ReflectionClass($class);
84
        if (!$reflection->isSubclassOf(Entity::class)) {
85
            throw new NoEntity($class . ' is not a subclass of Entity');
86
        }
87
        /** @var string|Entity $class */
88
89
        if ($primaryKey === null) {
90
            return new EntityFetcherMock($this, $class);
91
        }
92
93
        $primaryKey = $this->buildPrimaryKey($class, (array)$primaryKey);
94
        $checksum = $this->buildChecksum($primaryKey);
95
96
        if (isset($this->map[$class][$checksum])) {
97
            return $this->map[$class][$checksum];
98
        }
99
100
        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

100
        return $this->retrieve(/** @scrutinizer ignore-type */ $class, $primaryKey);
Loading history...
101
    }
102
}
103