ClientFactoryTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 7
dl 0
loc 82
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testCreateService() 0 68 1
1
<?php
2
3
namespace PamiModuleTest\Service;
4
5
use PamiModule\Service\ClientFactory;
6
use Zend\Stdlib\ArrayUtils;
7
use Zend\Test\Util\ModuleLoader;
8
9
class ClientFactoryTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * @var ModuleLoader
13
     */
14
    protected $moduleLoader;
15
16
    protected function setUp()
17
    {
18
        parent::setUp();
19
        $this->moduleLoader = new ModuleLoader(include __DIR__ . '/../../TestConfiguration.php');
20
    }
21
22
    public function testCreateService()
23
    {
24
        $configuration = [
25
            'pami_module' => [
26
                'connection' => [
27
                    'default' => [
28
                        'host' => 'local.host',
29
                        'scheme' => 'tcp://',
30
                        'port' => 123,
31
                        'username' => 'admin',
32
                        'secret' => 'foosecret',
33
                        'connect_timeout' => 123,
34
                        'read_timeout' => 123,
35
                    ],
36
                    'other' => [
37
                        'host' => 'local2.host',
38
                        'scheme' => 'tcp://',
39
                        'port' => 123,
40
                        'username' => 'admin',
41
                        'secret' => 'foosecret',
42
                        'connect_timeout' => 123,
43
                        'read_timeout' => 123,
44
                    ],
45
                ],
46
                'client' => [
47
                    'default' => [
48
                        'connection' => 'default',
49
                        'params' => [
50
                            'foo' => 'bar',
51
                        ],
52
                    ],
53
                ],
54
            ],
55
        ];
56
57
        $connectionMock = $this->getMockBuilder('PAMI\\Client\\Impl\\ClientImpl')
58
            ->disableOriginalConstructor()
59
            ->setMethods(['registerEventListener'])
60
            ->getMock();
61
62
        $connectionMock->expects(static::once())
63
            ->method('registerEventListener')
64
            ->with(static::isInstanceOf('PamiModule\\Event\\EventForwarder'));
65
66
        $serviceManager = $this->moduleLoader->getServiceManager();
67
        $serviceManager->setAllowOverride(true);
68
69
        $config = $serviceManager->get('config');
70
        $config = ArrayUtils::merge($config, $configuration);
71
        $serviceManager->setService('config', $config);
72
        $serviceManager->setService('pami.connection.default', $connectionMock);
73
74
        $factory = new ClientFactory('default');
75
        $service = $factory->createService($serviceManager);
76
77
        static::assertInstanceOf('PamiModule\\Service\\Client', $service);
78
        static::assertEquals(['foo' => 'bar'], $service->getParams());
79
        static::assertInstanceOf('Zend\\EventManager\\EventManager', $service->getEventManager());
80
81
        // Test with client not in in configuration
82
83
        $factory = new ClientFactory('other');
84
        $service = $factory->createService($serviceManager);
85
86
        static::assertInstanceOf('PamiModule\\Service\\Client', $service);
87
        static::assertEquals([], $service->getParams());
88
        static::assertInstanceOf('Zend\\EventManager\\EventManager', $service->getEventManager());
89
    }
90
}
91