testSetUserStartingBalance()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 32
rs 8.8571
cc 1
eloc 24
nc 1
nop 0
1
<?php
2
3
namespace JhUserTest\Controller;
4
5
use JhFlexiTime\Controller\RunningBalanceCliController;
6
use JhUser\Entity\User;
7
use JhUser\Entity\Role;
8
use Zend\Console\Request;
9
use Zend\Http\Request as HttpRequest;
10
use Zend\Test\PHPUnit\Controller\AbstractConsoleControllerTestCase;
11
use Zend\Console\ColorInterface;
12
use JhFlexiTime\DateTime\DateTime;
13
14
/**
15
 * Class RunningBalanceCliControllerControllerTest
16
 * @package JhFlexiTimeTest\Controller
17
 * @author Aydin Hassan <[email protected]>
18
 */
19
class RunningBalanceCliControllerControllerTest extends AbstractConsoleControllerTestCase
20
{
21
    protected $controller;
22
    protected $userRepository;
23
    protected $runningBalanceService;
24
    protected $consoleAdapter;
25
26
    public function setUp()
27
    {
28
        $this->setApplicationConfig(
29
            include __DIR__ . "/../../TestConfiguration.php.dist"
30
        );
31
        parent::setUp();
32
33
        $this->userRepository = $this->getMock('JhUser\Repository\UserRepositoryInterface');
34
        $this->runningBalanceService = $this->getMockBuilder('JhFlexiTime\Service\RunningBalanceService')
35
            ->disableOriginalConstructor()
36
            ->getMock();
37
38
        $this->consoleAdapter = $this->getMock('Zend\Console\Adapter\AdapterInterface');
39
40
41
        $serviceManager = $this->getApplicationServiceLocator();
42
        $serviceManager->setAllowOverride(true);
43
        $serviceManager->setService('JhUser\Repository\UserRepository', $this->userRepository);
44
        $serviceManager->setService('JhFlexiTime\Service\RunningBalanceService', $this->runningBalanceService);
45
        $serviceManager->setService('Console', $this->consoleAdapter);
46
        $serviceManager->setService('Console', $this->consoleAdapter);
47
    }
48
49
    public function testCalculatePreviousMonth()
50
    {
51
        $this->consoleAdapter
52
             ->expects($this->at(0))
53
             ->method('writeLine')
54
             ->with("Calculating Running Balance for all Users for previous month", ColorInterface::GREEN);
55
56
        $this->consoleAdapter
57
            ->expects($this->at(1))
58
            ->method('writeLine')
59
            ->with("Finished! ", ColorInterface::GREEN);
60
61
        $this->runningBalanceService
62
            ->expects($this->once())
63
            ->method('indexPreviousMonthBalance');
64
65
        $this->dispatch(new Request(['scriptname.php', "calc-prev-month-balance"]));
66
67
        $this->assertResponseStatusCode(0);
68
        $this->assertModuleName('jhflexitime');
69
        $this->assertControllerName('jhflexitime\controller\runningbalancecli');
70
        $this->assertControllerClass('runningbalanceclicontroller');
71
        $this->assertActionName('calc-prev-month-balance');
72
        $this->assertMatchedRouteName('calc-prev-month-balance');
73
    }
74
75
    public function testReCalculateBalanceProcessesIndividualUserIfEmailParamIsPresent()
76
    {
77
        $user   = new User;
78
        $email  = '[email protected]';
79
80
        $this->userRepository
81
             ->expects($this->once())
82
             ->method('findOneByEmail')
83
             ->with($email)
84
             ->will($this->returnValue($user));
85
86
        $this->consoleAdapter
87
            ->expects($this->at(0))
88
            ->method('writeLine')
89
            ->with("Recalculating Running Balance for $email", ColorInterface::GREEN);
90
91
        $this->consoleAdapter
92
            ->expects($this->at(1))
93
            ->method('writeLine')
94
            ->with("Finished! ", ColorInterface::GREEN);
95
96
        $this->runningBalanceService
97
             ->expects($this->once())
98
             ->method('reIndexIndividualUserRunningBalance')
99
             ->with($user);
100
101
        $this->runningBalanceService
102
            ->expects($this->never())
103
            ->method('reIndexAllUsersRunningBalance');
104
105
        $this->dispatch(new Request(['scriptname.php', "re-calc-balance-user $email"]));
106
107
        $this->assertResponseStatusCode(0);
108
        $this->assertModuleName('jhflexitime');
109
        $this->assertControllerName('jhflexitime\controller\runningbalancecli');
110
        $this->assertControllerClass('runningbalanceclicontroller');
111
        $this->assertActionName('re-calc-running-balance');
112
        $this->assertMatchedRouteName('re-calc-balance-user');
113
    }
114
115
    public function testReCalculateBalanceThrowsExceptionIfUserDoesNotExist()
116
    {
117
        $email  = '[email protected]';
118
119
        $this->userRepository
120
            ->expects($this->once())
121
            ->method('findOneByEmail')
122
            ->with($email)
123
            ->will($this->returnValue(null));
124
125
        $this->runningBalanceService
126
            ->expects($this->never())
127
            ->method('reIndexAllUsersRunningBalance');
128
129
        $this->runningBalanceService
130
            ->expects($this->never())
131
            ->method('reIndexIndividualUserRunningBalance');
132
133
        $this->dispatch(new Request(['scriptname.php', "re-calc-balance-user $email"]));
134
        $this->assertResponseStatusCode(1);
135
        $this->assertModuleName('jhflexitime');
136
        $this->assertControllerName('jhflexitime\controller\runningbalancecli');
137
        $this->assertControllerClass('runningbalanceclicontroller');
138
        $this->assertActionName('re-calc-running-balance');
139
        $this->assertMatchedRouteName('re-calc-balance-user');
140
    }
141
142
    public function testReCalculateBalanceProcessesAllUsersIfNoEmailIsPresent()
143
    {
144
        $this->consoleAdapter
145
            ->expects($this->at(0))
146
            ->method('writeLine')
147
            ->with("Recalculating Running Balance for all Users", ColorInterface::GREEN);
148
149
        $this->consoleAdapter
150
            ->expects($this->at(1))
151
            ->method('writeLine')
152
            ->with("Finished! ", ColorInterface::GREEN);
153
154
        $this->runningBalanceService
155
            ->expects($this->once())
156
            ->method('reIndexAllUsersRunningBalance');
157
158
        $this->dispatch(new Request(['scriptname.php', "re-calc-balance-all"]));
159
160
        $this->assertResponseStatusCode(0);
161
        $this->assertModuleName('jhflexitime');
162
        $this->assertControllerName('jhflexitime\controller\runningbalancecli');
163
        $this->assertControllerClass('runningbalanceclicontroller');
164
        $this->assertActionName('re-calc-running-balance');
165
        $this->assertMatchedRouteName('re-calc-balance-all');
166
    }
167
168
    public function testSetUserStartingBalance()
169
    {
170
        $balance = 10;
171
172
        $user   = new User;
173
        $email  = '[email protected]';
174
175
        $this->userRepository
176
            ->expects($this->once())
177
            ->method('findOneByEmail')
178
            ->with($email)
179
            ->will($this->returnValue($user));
180
181
        $this->runningBalanceService
182
            ->expects($this->once())
183
            ->method('setUserStartingBalance')
184
            ->with($user, $balance);
185
186
        $this->runningBalanceService
187
            ->expects($this->once())
188
            ->method('reIndexIndividualUserRunningBalance')
189
            ->with($user);
190
191
        $this->dispatch(new Request(['scriptname.php', "set user init-balance $email $balance"]));
192
193
        $this->assertResponseStatusCode(0);
194
        $this->assertModuleName('jhflexitime');
195
        $this->assertControllerName('jhflexitime\controller\runningbalancecli');
196
        $this->assertControllerClass('runningbalanceclicontroller');
197
        $this->assertActionName('set-user-starting-balance');
198
        $this->assertMatchedRouteName('set-user-starting-balance');
199
    }
200
201
    public function testSetUserStartingBalanceThrowsExceptionIfUserNotExist()
202
    {
203
        $balance    = 10;
204
        $email      = '[email protected]';
205
206
        $this->userRepository
207
            ->expects($this->once())
208
            ->method('findOneByEmail')
209
            ->with($email)
210
            ->will($this->returnValue(null));
211
212
        $this->runningBalanceService
213
            ->expects($this->never())
214
            ->method('setUserStartingBalance');
215
216
        $this->runningBalanceService
217
            ->expects($this->never())
218
            ->method('reIndexIndividualUserRunningBalance');
219
220
        $this->dispatch(new Request(['scriptname.php', "set user init-balance $email $balance"]));
221
222
        $this->assertResponseStatusCode(1);
223
        $this->assertModuleName('jhflexitime');
224
        $this->assertControllerName('jhflexitime\controller\runningbalancecli');
225
        $this->assertControllerClass('runningbalanceclicontroller');
226
        $this->assertActionName('set-user-starting-balance');
227
        $this->assertMatchedRouteName('set-user-starting-balance');
228
    }
229
}
230