1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Tests; |
4
|
|
|
|
5
|
|
|
use Bankiru\Api\ClientRegistry; |
6
|
|
|
use Bankiru\Api\Doctrine\ApiEntityManager; |
7
|
|
|
use Bankiru\Api\Doctrine\Configuration; |
8
|
|
|
use Bankiru\Api\Doctrine\EntityManager; |
9
|
|
|
use Bankiru\Api\Doctrine\EntityMetadataFactory; |
10
|
|
|
use Bankiru\Api\Doctrine\Mapping\Driver\YmlMetadataDriver; |
11
|
|
|
use Bankiru\Api\Doctrine\Proxy\ProxyFactory; |
12
|
|
|
use Bankiru\Api\Doctrine\Type\BaseTypeRegistry; |
13
|
|
|
use Bankiru\Api\Doctrine\Type\TypeRegistry; |
14
|
|
|
use Bankiru\Api\Test\TestClient; |
15
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain; |
16
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator; |
17
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
18
|
|
|
use GuzzleHttp\Handler\MockHandler; |
19
|
|
|
use ScayTrase\Api\IdGenerator\IdGeneratorInterface; |
20
|
|
|
|
21
|
|
|
abstract class AbstractEntityManagerTest extends \PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
const DEFAULT_CLIENT = 'test-client'; |
24
|
|
|
private $registry; |
25
|
|
|
|
26
|
|
|
/** @var ObjectManager */ |
27
|
|
|
private $manager; |
28
|
|
|
/** @var MockHandler[] */ |
29
|
|
|
private $mocks = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
|
|
public function getRegistry() |
35
|
|
|
{ |
36
|
|
|
return $this->registry; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return ApiEntityManager |
41
|
|
|
*/ |
42
|
14 |
|
protected function getManager() |
43
|
|
|
{ |
44
|
14 |
|
return $this->manager; |
45
|
|
|
} |
46
|
|
|
|
47
|
14 |
|
protected function setUp() |
48
|
|
|
{ |
49
|
14 |
|
$this->createEntityManager($this->getClientNames()); |
50
|
14 |
|
parent::setUp(); |
51
|
14 |
|
} |
52
|
|
|
|
53
|
14 |
|
protected function createEntityManager($clients = [self::DEFAULT_CLIENT]) |
54
|
|
|
{ |
55
|
|
|
/** @var IdGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject $idGenerator */ |
56
|
14 |
|
$idGenerator = $this->getMock(IdGeneratorInterface::class); |
57
|
14 |
|
$idGenerator->method('getRequestIdentifier')->willReturn('test'); |
58
|
|
|
|
59
|
14 |
|
$this->registry = new ClientRegistry(); |
60
|
14 |
|
foreach ($clients as $name) { |
61
|
14 |
|
$this->registry->add($name, new TestClient($this->getResponseMock($name), $idGenerator)); |
62
|
14 |
|
} |
63
|
|
|
|
64
|
14 |
|
$configuration = $this->createConfiguration(); |
65
|
|
|
|
66
|
14 |
|
$this->manager = new EntityManager($configuration); |
67
|
14 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $name |
71
|
|
|
* |
72
|
|
|
* @return MockHandler |
73
|
|
|
*/ |
74
|
14 |
|
protected function getResponseMock($name = self::DEFAULT_CLIENT) |
75
|
|
|
{ |
76
|
14 |
|
if (!array_key_exists($name, $this->mocks)) { |
77
|
14 |
|
$this->mocks[$name] = new MockHandler(); |
78
|
14 |
|
} |
79
|
|
|
|
80
|
14 |
|
return $this->mocks[$name]; |
81
|
|
|
} |
82
|
|
|
|
83
|
10 |
|
protected function getClientNames() |
84
|
|
|
{ |
85
|
10 |
|
return [self::DEFAULT_CLIENT]; |
86
|
|
|
} |
87
|
|
|
|
88
|
14 |
|
protected function tearDown() |
89
|
|
|
{ |
90
|
14 |
|
foreach ($this->mocks as $mock) { |
91
|
14 |
|
self::assertCount(0, $mock); |
92
|
14 |
|
} |
93
|
|
|
|
94
|
14 |
|
$this->manager = null; |
95
|
14 |
|
$this->mocks = []; |
96
|
14 |
|
parent::tearDown(); // TODO: Change the autogenerated stub |
97
|
14 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return Configuration |
101
|
|
|
*/ |
102
|
14 |
|
protected function createConfiguration() |
103
|
|
|
{ |
104
|
14 |
|
$configuration = new Configuration(); |
105
|
14 |
|
$configuration->setMetadataFactory(new EntityMetadataFactory()); |
|
|
|
|
106
|
14 |
|
$configuration->setRegistry($this->registry); |
107
|
14 |
|
$configuration->setTypeRegistry(new BaseTypeRegistry(new TypeRegistry())); |
108
|
14 |
|
$configuration->setProxyDir(CACHE_DIR . '/doctrine/proxy/'); |
109
|
14 |
|
$configuration->setProxyNamespace('Bankiru\Api\Test\Proxy'); |
110
|
14 |
|
$driver = new MappingDriverChain(); |
111
|
14 |
|
$driver->addDriver( |
112
|
14 |
|
new YmlMetadataDriver( |
113
|
14 |
|
new SymfonyFileLocator( |
114
|
|
|
[ |
115
|
14 |
|
__DIR__ . '/../Test/Resources/config/api/' => 'Bankiru\Api\Test\Entity', |
116
|
14 |
|
], |
117
|
14 |
|
'.api.yml', |
118
|
14 |
|
DIRECTORY_SEPARATOR) |
119
|
14 |
|
), |
120
|
|
|
'Bankiru\Api\Test\Entity' |
121
|
14 |
|
); |
122
|
14 |
|
$configuration->setDriver($driver); |
123
|
|
|
|
124
|
14 |
|
return $configuration; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: