1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Mapado\RestClientSdk\Tests\Units; |
6
|
|
|
|
7
|
|
|
use atoum; |
8
|
|
|
use Mapado\RestClientSdk\Exception\SdkClientNotFoundException; |
9
|
|
|
use Mapado\RestClientSdk\Mapping; |
|
|
|
|
10
|
|
|
use Mapado\RestClientSdk\SdkClient; |
|
|
|
|
11
|
|
|
|
12
|
|
|
class SdkClientRegistry extends atoum |
13
|
|
|
{ |
14
|
|
|
public function testGetSdkClientList() |
15
|
|
|
{ |
16
|
|
|
$this |
17
|
|
|
->given($sdkClientList = $this->createSdkClientList(['foo', 'bar'])) |
18
|
|
|
->and($this->newTestedInstance($sdkClientList)) |
19
|
|
|
->then |
20
|
|
|
->array($this->testedInstance->getSdkClientList()) |
21
|
|
|
->isIdenticalTo($sdkClientList) |
22
|
|
|
; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testGetSdkClient() |
26
|
|
|
{ |
27
|
|
|
$this |
28
|
|
|
->given($sdkClientList = $this->createSdkClientList(['foo', 'bar'])) |
29
|
|
|
->and($this->newTestedInstance($sdkClientList)) |
30
|
|
|
->then |
31
|
|
|
->object($this->testedInstance->getSdkClient('bar')) |
32
|
|
|
->isInstanceOf(SdkClient::class) |
33
|
|
|
->exception(function () { |
34
|
|
|
$this->testedInstance->getSdkClient('barrrrr'); |
35
|
|
|
}) |
36
|
|
|
->isInstanceOf(SdkClientNotFoundException::class) |
37
|
|
|
->hasMessage('Sdk client not found for name barrrrr') |
38
|
|
|
; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testGetSdkClientForClass() |
42
|
|
|
{ |
43
|
|
|
$sdkClientList = $this->createSdkClientList(['foo', 'bar']); |
44
|
|
|
$fooMapping = $this->newMockInstance(Mapping::class); |
45
|
|
|
$barMapping = $this->newMockInstance(Mapping::class); |
46
|
|
|
$fooMapping->getMockController()->hasClassMetadata = function ($name) { |
47
|
|
|
return 0 === mb_strpos($name, 'Foo'); |
48
|
|
|
}; |
49
|
|
|
$barMapping->getMockController()->hasClassMetadata = function ($name) { |
50
|
|
|
return 0 === mb_strpos($name, 'Bar'); |
51
|
|
|
}; |
52
|
|
|
$sdkClientList['foo']->getMockController()->getMapping = $fooMapping; |
53
|
|
|
$sdkClientList['bar']->getMockController()->getMapping = $barMapping; |
54
|
|
|
|
55
|
|
|
$this |
56
|
|
|
->given($this->newTestedInstance($sdkClientList)) |
57
|
|
|
->then |
58
|
|
|
->object($this->testedInstance->getSdkClientForClass('Foo\Entity\A')) |
59
|
|
|
->isInstanceOf(SdkClient::class) |
60
|
|
|
->isIdenticalTo($sdkClientList['foo']) |
61
|
|
|
|
62
|
|
|
->object($this->testedInstance->getSdkClientForClass('Bar\Entity\B')) |
63
|
|
|
->isInstanceOf(SdkClient::class) |
64
|
|
|
->isIdenticalTo($sdkClientList['bar']) |
65
|
|
|
|
66
|
|
|
->exception(function () { |
67
|
|
|
$this->testedInstance->getSdkClientForClass('NotMapped\Entity\C'); |
68
|
|
|
}) |
69
|
|
|
->isInstanceOf(SdkClientNotFoundException::class) |
70
|
|
|
->hasMessage('Sdk client not found for entity class NotMapped\Entity\C') |
71
|
|
|
; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array<string> $nameList |
76
|
|
|
*/ |
77
|
|
|
private function createSdkClientList(array $nameList) |
78
|
|
|
{ |
79
|
|
|
$sdkClientList = []; |
80
|
|
|
|
81
|
|
|
foreach ($nameList as $name) { |
82
|
|
|
$this->mockGenerator->orphanize('__construct'); |
83
|
|
|
$this->mockGenerator->shuntParentClassCalls(); |
84
|
|
|
$sdkClientList[$name] = $this->newMockInstance(SdkClient::class); |
85
|
|
|
$this->mockGenerator->unshuntParentClassCalls(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $sdkClientList; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: