Completed
Pull Request — master (#14)
by Pavel
28:57
created

AbstractEntityManagerTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 104
c 1
b 0
f 0
wmc 11
lcom 1
cbo 12
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getRegistry() 0 4 1
A getManager() 0 4 1
A setUp() 0 5 1
A createEntityManager() 0 11 2
A getClient() 0 8 2
A getClientNames() 0 4 1
A tearDown() 0 10 2
B createConfiguration() 0 26 1
1
<?php
2
3
namespace Bankiru\Api\Doctrine\Tests;
4
5
use Bankiru\Api\Doctrine\ApiEntityManager;
6
use Bankiru\Api\Doctrine\ClientRegistry;
7
use Bankiru\Api\Doctrine\ClientRegistryInterface;
8
use Bankiru\Api\Doctrine\Configuration;
9
use Bankiru\Api\Doctrine\ConstructorFactoryResolver;
10
use Bankiru\Api\Doctrine\EntityManager;
11
use Bankiru\Api\Doctrine\EntityMetadataFactory;
12
use Bankiru\Api\Doctrine\Mapping\Driver\YmlMetadataDriver;
13
use Bankiru\Api\Doctrine\Type\BaseTypeRegistry;
14
use Bankiru\Api\Doctrine\Type\TypeRegistry;
15
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
16
use Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator;
17
use ScayTrase\Api\Rpc\Test\RpcMockClient;
18
use ScayTrase\Api\Rpc\Tests\AbstractRpcTest;
19
20
abstract class AbstractEntityManagerTest extends AbstractRpcTest
21
{
22
    const DEFAULT_CLIENT = 'test-client';
23
    /** @var  ClientRegistryInterface */
24
    private $registry;
25
    /** @var  ApiEntityManager */
26
    private $manager;
27
    /** @var  RpcMockClient[] */
28
    private $clients = [];
29
30
    /**
31
     * @return mixed
32
     */
33
    public function getRegistry()
34
    {
35
        return $this->registry;
36
    }
37
38
    /**
39
     * @return ApiEntityManager
40
     */
41
    protected function getManager()
42
    {
43
        return $this->manager;
44
    }
45
46
    protected function setUp()
47
    {
48
        $this->createEntityManager($this->getClientNames());
49
        parent::setUp();
50
    }
51
52
    protected function createEntityManager($clients = [self::DEFAULT_CLIENT])
53
    {
54
        $this->registry = new ClientRegistry();
55
        foreach ($clients as $name) {
56
            $this->registry->add($name, $this->getClient($name));
57
        }
58
59
        $configuration = $this->createConfiguration();
60
61
        $this->manager = new EntityManager($configuration);
62
    }
63
64
    /**
65
     * @param string $name
66
     *
67
     * @return RpcMockClient
68
     */
69
    protected function getClient($name = self::DEFAULT_CLIENT)
70
    {
71
        if (!array_key_exists($name, $this->clients)) {
72
            $this->clients[$name] = new RpcMockClient();
73
        }
74
75
        return $this->clients[$name];
76
    }
77
78
    protected function getClientNames()
79
    {
80
        return [self::DEFAULT_CLIENT];
81
    }
82
83
    protected function tearDown()
84
    {
85
        foreach ($this->clients as $name => $client) {
86
            self::assertCount(0, $client, sprintf('Response not used for "%s" client', $name));
87
        }
88
89
        $this->manager = null;
90
        $this->clients = [];
91
        parent::tearDown();
92
    }
93
94
    /**
95
     * @return Configuration
96
     */
97
    protected function createConfiguration()
98
    {
99
        $configuration = new Configuration();
100
        $configuration->setMetadataFactory(new EntityMetadataFactory());
0 ignored issues
show
Documentation introduced by
new \Bankiru\Api\Doctrine\EntityMetadataFactory() is of type object<Bankiru\Api\Doctr...\EntityMetadataFactory>, but the function expects a string.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
        $configuration->setRegistry($this->registry);
102
        $configuration->setTypeRegistry(new BaseTypeRegistry(new TypeRegistry()));
103
        $configuration->setResolver(new ConstructorFactoryResolver());
104
        $configuration->setProxyDir(CACHE_DIR.'/doctrine/proxy/');
105
        $configuration->setProxyNamespace('Bankiru\Api\Doctrine\Test\Proxy');
106
        $driver = new MappingDriverChain();
107
        $driver->addDriver(
108
            new YmlMetadataDriver(
109
                new SymfonyFileLocator(
110
                    [
111
                        __DIR__.'/../Test/Resources/config/api/' => 'Bankiru\Api\Doctrine\Test\Entity',
112
                    ],
113
                    '.api.yml',
114
                    DIRECTORY_SEPARATOR
115
                )
116
            ),
117
            'Bankiru\Api\Doctrine\Test\Entity'
118
        );
119
        $configuration->setDriver($driver);
120
121
        return $configuration;
122
    }
123
}
124