|
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); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|