Completed
Push — master ( 37e07b...b44b47 )
by Tomasz
03:05
created

DependencyInjectionTest::testInvalidSendDriverInvalidInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1.0002

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 15
cts 16
cp 0.9375
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
crap 1.0002
1
<?php
2
3
/**
4
 * All rights reserved
5
 */
6
7
namespace Gendoria\CommandQueueBundle\Tests;
8
9
use Gendoria\CommandQueue\QueueManager\MultipleQueueManager;
10
use Gendoria\CommandQueue\SendDriver\SendDriverInterface;
11
use Gendoria\CommandQueueBundle\DependencyInjection\GendoriaCommandQueueExtension;
12
use Gendoria\CommandQueueBundle\DependencyInjection\Pass\PoolsPass;
13
use InvalidArgumentException;
14
use PHPUnit_Framework_TestCase;
15
use ReflectionObject;
16
use stdClass;
17
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Definition;
20
21
/**
22
 * Description of ManagerTest
23
 *
24
 * @author Tomasz Struczyński <[email protected]>
25
 * @group CommandQueue
26
 * @group legacy
27
 */
28
class DependencyInjectionTest extends PHPUnit_Framework_TestCase
29
{
30
    public function testHasDefaultPool()
31
    {
32
        $container = new ContainerBuilder();
33
        $extension = new GendoriaCommandQueueExtension();
34
        $sendDriverMock = $this->getMockBuilder(SendDriverInterface::class)->getMock();
35
        $config = array(
36
            'pools' => array(
37
                'default' => array(
38
                    'send_driver' => '@dummy',
39
                ),
40
            ),
41
        );
42
        $container->addDefinitions(array(
43
            'dummy' => new Definition(get_class($sendDriverMock)),
44
        ));
45
        $extension->load(array($config), $container);
46
        $container->addCompilerPass(new PoolsPass());
47
        $container->compile();
48
49
        /* @var $manager MultipleQueueManager */
50
        $manager = $container->get('gendoria_command_queue.manager');
51
        $this->assertInstanceOf(MultipleQueueManager::class, $manager);
52
        $reflectionClass = new ReflectionObject($manager);
53
        $defaultPoolProp = $reflectionClass->getProperty('defaultPool');
54
        $defaultPoolProp->setAccessible(true);
55
        $this->assertNotEmpty($defaultPoolProp->getValue($manager));
56
        $sendDriversProp = $reflectionClass->getProperty('sendDrivers');
57
        $sendDriversProp->setAccessible(true);
58
        $this->assertEquals($sendDriversProp->getValue($manager), array('default' => $sendDriverMock));
59
    }
60
    
61
    public function testNoPools()
62
    {
63
        $this->setExpectedException(InvalidConfigurationException::class, 'The child node "pools" at path "gendoria_command_queue" must be configured.');
64
        $container = new ContainerBuilder();
65
        $extension = new GendoriaCommandQueueExtension();
66
        $config = array(
67
        );
68
        $extension->load(array($config), $container);
69
    }
70
    
71
    public function testInvalidSendDriverNoService()
72
    {
73
        $this->setExpectedException(InvalidArgumentException::class, 'Non existing send driver service provided: @dummy.');
74
        $container = new ContainerBuilder();
75
        $extension = new GendoriaCommandQueueExtension();
76
        $config = array(
77
            'pools' => array(
78
                'default' => array(
79
                    'send_driver' => '@dummy',
80
                ),
81
            ),
82
        );
83
        $extension->load(array($config), $container);
84
        $container->addCompilerPass(new PoolsPass());
85
        $container->compile();
86
    }
87
    
88
    public function testInvalidSendDriverInvalidInterface()
89
    {
90
        $this->setExpectedException(InvalidArgumentException::class, 'Service "dummy" does not implement interface "'.SendDriverInterface::class.'".');
91
        $container = new ContainerBuilder();
92
        $sendDriverMock = $this->getMockBuilder(stdClass::class)->getMock();
93
        $extension = new GendoriaCommandQueueExtension();
94
        $config = array(
95
            'pools' => array(
96
                'default' => array(
97
                    'send_driver' => '@dummy',
98
                ),
99
            ),
100
        );
101
        $container->addDefinitions(array(
102
            'dummy' => new Definition(get_class($sendDriverMock)),
103
        ));
104
        $extension->load(array($config), $container);
105
        $container->addCompilerPass(new PoolsPass());
106
        $container->compile();
107
    }    
108
}
109