SdkClientRegistry::createSdkClientList()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Mapado\RestClientSdk\Tests\Units\Mapping. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use Mapado\RestClientSdk\SdkClient;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Mapado\RestClientSdk\Tests\Units\SdkClient. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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