ModuleTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 8
dl 0
loc 166
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetConfig() 0 7 1
A testGetAutoloaderConfig() 0 5 1
A testListenersAreRegistered() 0 49 1
B getEvent() 0 27 2
A testConsoleUsage() 0 19 1
A testGetInstallService() 0 5 1
B testUserIsInstalledOnRegister() 0 29 1
1
<?php
2
3
namespace JhFlexiTimeTest;
4
5
use JhFlexiTime\Module;
6
use JhHubBase\Notification\NotificationService;
7
use JhUser\Entity\User;
8
use Zend\ServiceManager\ServiceManager;
9
10
/**
11
 * Class ModuleTest
12
 * @package JhFlexiTimeTest
13
 * @author Aydin Hassan <[email protected]>
14
 */
15
class ModuleTest extends \PHPUnit_Framework_TestCase
16
{
17
18
    /**
19
     * @var \Zend\ServiceManager\ServiceManager
20
     */
21
    protected $serviceLocator;
22
    protected $sharedEvm;
23
24
    /**
25
     * @var \Zend\EventManager\EventManagerInterface|\PHPUnit_Framework_MockObject_MockObject
26
     */
27
    protected $eventManager;
28
29
    public function testGetConfig()
30
    {
31
        $module = new Module();
32
33
        $this->assertInternalType('array', $module->getConfig());
34
        $this->assertSame($module->getConfig(), unserialize(serialize($module->getConfig())), 'Config is serializable');
35
    }
36
37
    public function testGetAutoloaderConfig()
38
    {
39
        $module = new Module;
40
        $this->assertInternalType('array', $module->getAutoloaderConfig());
41
    }
42
43
    public function testListenersAreRegistered()
44
    {
45
        $event = $this->getEvent(true);
46
        $module = new Module();
47
48
        $bookingSaveListener = $this->getMockBuilder('JhFlexiTime\Listener\BookingSaveListener')
49
            ->disableOriginalConstructor()
50
            ->getMock();
51
52
        $this->serviceLocator->setService('JhFlexiTime\Listener\BookingSaveListener', $bookingSaveListener);
53
        $this->eventManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Zend\EventManager\EventManagerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
54
            ->expects($this->at(0))
55
            ->method('attach')
56
            ->with($bookingSaveListener);
57
58
        $cappedCreditListener = $this->getMockBuilder('JhFlexiTime\Listener\CappedCreditListener')
59
            ->disableOriginalConstructor()
60
            ->getMock();
61
62
        $this->serviceLocator->setService('JhFlexiTime\Listener\CappedCreditListener', $cappedCreditListener);
63
        $this->eventManager
64
            ->expects($this->at(1))
65
            ->method('attach')
66
            ->with($cappedCreditListener);
67
68
        $this->sharedEvm
69
            ->expects($this->at(0))
70
            ->method('attach')
71
            ->with(
72
                'ScnSocialAuth\Authentication\Adapter\HybridAuth',
73
                'registerViaProvider.post',
74
                [$module, 'onRegister']
75
            );
76
77
        $this->sharedEvm
78
            ->expects($this->at(1))
79
            ->method('attach')
80
            ->with('ZfcUser\Service\User', 'register.post', [$module, 'onRegister']);
81
82
83
        $notificationService = new NotificationService;
84
        $this->serviceLocator->setService('JhHubBase\Notification\NotificationService', $notificationService);
85
        $this->serviceLocator->setService(
86
            'JhFlexiTime\NotificationHandler\MissedBookingEmailNotificationHandler',
87
            $this->getMock('JhHubBase\Notification\NotificationHandlerInterface')
88
        );
89
90
        $module->onBootstrap($event);
91
    }
92
93
    /**
94
     * @param bool $addEventManager
95
     * @return \PHPUnit_Framework_MockObject_MockObject
96
     */
97
    private function getEvent($addEventManager = false)
98
    {
99
        $this->serviceLocator = new ServiceManager();
100
        $this->eventManager = $this->getMock('Zend\EventManager\EventManagerInterface');
101
        $application = $this->getMock('Zend\Mvc\Application', [], [], '', false);
102
103
        $application->expects($this->any())
104
            ->method('getServiceManager')
105
            ->will($this->returnValue($this->serviceLocator));
106
107
        if ($addEventManager) {
108
            $application->expects($this->any())
109
                ->method('getEventManager')
110
                ->will($this->returnValue($this->eventManager));
111
112
            $this->sharedEvm = $this->getMock('Zend\EventManager\SharedEventManagerInterface');
113
            $this->eventManager
114
                ->expects($this->once())
115
                ->method('getSharedManager')
116
                ->will($this->returnValue($this->sharedEvm));
117
        }
118
119
        $event = $this->getMock('Zend\EventManager\EventInterface');
120
        $event->expects($this->any())->method('getTarget')->will($this->returnValue($application));
121
122
        return $event;
123
    }
124
125
    public function testConsoleUsage()
126
    {
127
        $mockConsole = $this->getMock('Zend\Console\Adapter\AdapterInterface');
128
        $module = new Module();
129
130
        $expected = [
131
            're-calc-balance-user <userEmail>' =>
132
                "Recalculate a User's running balance",
133
            're-calc-balance-all ' =>
134
                "Recalculate all User's running balance",
135
            'calc-prev-month-balance' =>
136
                "Calculate the previous month balance for all users and add it on to their running balance",
137
            'set user init-balance <userEmail> <balance>' =>
138
                "Set a user's starting balance",
139
            'notify-missing-bookings' =>
140
                'Send reminder e-mail to any user who has missed bookings for the period specified in the config'
141
        ];
142
        $this->assertSame($expected, $module->getConsoleUsage($mockConsole));
143
    }
144
145
    public function testUserIsInstalledOnRegister()
146
    {
147
        $event      = $this->getEvent();
148
        $user = new User();
149
        $event
150
            ->expects($this->once())
151
            ->method('getParam')
152
            ->with('user')
153
            ->will($this->returnValue($user));
154
155
        $installer  = $this->getMockBuilder('JhFlexiTime\Install\Installer')
156
            ->disableOriginalConstructor()
157
            ->getMock();
158
159
        $installer
160
            ->expects($this->once())
161
            ->method('createSettingsRow')
162
            ->with($user);
163
164
        $installer
165
            ->expects($this->once())
166
            ->method('createRunningBalanceRow')
167
            ->with($user);
168
169
        $this->serviceLocator->setService('JhFlexiTime\Install\Installer', $installer);
170
171
        $module = new Module();
172
        $module->onRegister($event);
173
    }
174
175
    public function testGetInstallService()
176
    {
177
        $module = new Module();
178
        $this->assertSame('JhFlexiTime\Install\Installer', $module->getInstallService());
179
    }
180
}
181