testCreateService()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 25
nc 1
nop 0
1
<?php
2
3
namespace PamiModuleTest;
4
5
use PamiModule\Factory\AbstractPamiServiceFactory;
6
use Zend\Stdlib\ArrayUtils;
7
use Zend\Test\Util\ModuleLoader;
8
9
class AbstractPamiServiceFactoryTest 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 testCanCreateServiceWithName()
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
                ],
37
            ],
38
        ];
39
40
        $serviceManager = $this->moduleLoader->getServiceManager();
41
        $serviceManager->setAllowOverride(true);
42
43
        $config = $serviceManager->get('config');
44
        $config = ArrayUtils::merge($config, $configuration);
45
        $serviceManager->setService('config', $config);
46
47
        $serviceFactory = new AbstractPamiServiceFactory();
48
49
        // Test creating client
50
        static::assertFalse($serviceFactory->canCreateServiceWithName(
51
            $serviceManager,
52
            'pami.connection.foo',
53
            'pami.connection.foo'
54
        ));
55
        static::assertFalse($serviceFactory->canCreateServiceWithName(
56
            $serviceManager,
57
            'foo.client.default',
58
            'foo.client.default'
59
        ));
60
        static::assertTrue($serviceFactory->canCreateServiceWithName(
61
            $serviceManager,
62
            'pami.connection.default',
63
            'pami.connection.default'
64
        ));
65
    }
66
67
    public function testCreateService()
68
    {
69
        $configuration = [
70
            'pami_module' => [
71
                'connection' => [
72
                    'default' => [
73
                        'host' => 'local.host',
74
                        'scheme' => 'tcp://',
75
                        'port' => 123,
76
                        'username' => 'admin',
77
                        'secret' => 'foosecret',
78
                        'connect_timeout' => 123,
79
                        'read_timeout' => 123,
80
                    ],
81
                ],
82
            ],
83
        ];
84
85
        $serviceManager = $this->moduleLoader->getServiceManager();
86
        $serviceManager->setAllowOverride(true);
87
88
        $config = $serviceManager->get('config');
89
        $config = ArrayUtils::merge($config, $configuration);
90
        $serviceManager->setService('config', $config);
91
92
        $serviceFactory = new AbstractPamiServiceFactory();
93
94
        // Test creating connection
95
96
        $service = $serviceFactory->createServiceWithName(
97
            $serviceManager,
98
            'pami.connection.default',
99
            'pami.connection.default'
100
        );
101
        static::assertInstanceOf(
102
            'PAMI\\Client\\Impl\\ClientImpl',
103
            $service
104
        );
105
    }
106
107
    /**
108
     * @expectedException \Zend\ServiceManager\Exception\ServiceNotFoundException
109
     */
110
    public function testCreateServiceWithNameAndInvalidService()
111
    {
112
        $serviceManager = $this->moduleLoader->getServiceManager();
113
114
        $serviceFactory = new AbstractPamiServiceFactory();
115
116
        // Test creating client
117
118
        $serviceFactory->createServiceWithName(
119
            $serviceManager,
120
            'pami.foo.default',
121
            'pami.foo.default'
122
        );
123
    }
124
}
125