Completed
Push — master ( 086a41...409693 )
by Thomas
25s queued 11s
created

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

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