BookingControllerTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 13
dl 0
loc 165
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 26 2
A mockAuth() 0 23 1
A configureMockBookingService() 0 8 1
A configureMockTimeCalculatorService() 0 8 1
A getBookingService() 0 10 1
A getTimeCalculatorService() 0 10 1
A getForm() 0 4 1
A getMockBooking() 0 9 1
B testGetListCanBeAccessed() 0 41 1
1
<?php
2
3
namespace JhFlexiTimeTest\Controller;
4
5
use JhFlexiTime\Controller\BookingController;
6
7
use JhFlexiTime\DateTime\DateTime;
8
use JhFlexiTime\Entity\UserSettings;
9
use JhFlexiTime\Repository\UserSettingsRepositoryInterface;
10
use Zend\Http\Request;
11
use Zend\Http\Response;
12
use Zend\Mvc\MvcEvent;
13
use Zend\Mvc\Router\RouteMatch;
14
use Zend\Mvc\Router\Http\TreeRouteStack as HttpRouter;
15
use JhFlexiTimeTest\Util\ServiceManagerFactory;
16
use JhFlexiTime\Entity\Booking;
17
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
18
19
/**
20
 * Class BookingControllerTest
21
 * @package JhFlexiTimeTest\Controller
22
 * @author Aydin Hassan <[email protected]>
23
 */
24
class BookingControllerTest extends AbstractHttpControllerTestCase
25
{
26
27
    protected $controller;
28
    protected $routeMatch;
29
    protected $event;
30
    protected $request;
31
    protected $response;
32
    protected $user;
33
    protected $bookingService;
34
    protected $timeCalculatorService;
35
36
    /**
37
     * @var UserSettingsRepositoryInterface
38
     */
39
    protected $userSettingsRepository;
40
41
    public function setUp()
42
    {
43
        $this->userSettingsRepository
44
            = $this->getMock('JhFlexiTime\Repository\UserSettingsRepositoryInterface');
45
46
        $this->controller = new BookingController(
47
            $this->getBookingService(),
48
            $this->getTimeCalculatorService(),
49
            $this->getForm(),
50
            $this->userSettingsRepository
51
        );
52
53
        $this->request      = new Request();
54
        $this->routeMatch   = new RouteMatch([]);
55
        $this->event        = new MvcEvent();
56
57
        $serviceManager     = ServiceManagerFactory::getServiceManager();
58
        $config             = $serviceManager->get('Config');
59
        $routerConfig       = isset($config['router']) ? $config['router'] : [];
60
        $router             = HttpRouter::factory($routerConfig);
61
        $this->event->setRouter($router);
62
        $this->event->setRouteMatch($this->routeMatch);
63
        $this->controller->setEvent($this->event);
64
65
        $this->mockAuth();
66
    }
67
68
    public function mockAuth()
69
    {
70
        $ZfcUserMock = $this->getMock('ZfcUser\Entity\UserInterface');
71
72
        $ZfcUserMock->expects($this->any())
73
            ->method('getId')
74
            ->will($this->returnValue('1'));
75
76
        $authMock = $this->getMock('ZfcUser\Controller\Plugin\ZfcUserAuthentication');
77
78
        $authMock->expects($this->any())
79
            ->method('hasIdentity')
80
            -> will($this->returnValue(true));
81
82
        $authMock->expects($this->any())
83
            ->method('getIdentity')
84
            ->will($this->returnValue($ZfcUserMock));
85
86
        $this->controller->getPluginManager()
87
            ->setService('zfcUserAuthentication', $authMock);
88
89
        $this->user = $ZfcUserMock;
90
    }
91
92
    public function configureMockBookingService($method, array $params, $return)
93
    {
94
        $expects = $this->bookingService->expects($this->once())
95
            ->method($method)
96
            ->will($this->returnValue($return));
97
98
        call_user_func_array([$expects, "with"], $params);
99
    }
100
101
    public function configureMockTimeCalculatorService($method, array $params, $return)
102
    {
103
        $expects = $this->timeCalculatorService->expects($this->once())
104
            ->method($method)
105
            ->will($this->returnValue($return));
106
107
        call_user_func_array([$expects, "with"], $params);
108
    }
109
110
    public function getBookingService()
111
    {
112
        $mock = $this->getMockBuilder('JhFlexiTime\Service\BookingService')
113
            ->disableOriginalConstructor()
114
            ->getMock();
115
116
        $this->bookingService = $mock;
117
118
        return $mock;
119
    }
120
121
    public function getTimeCalculatorService()
122
    {
123
        $mock = $this->getMockBuilder('JhFlexiTime\Service\TimeCalculatorService')
124
            ->disableOriginalConstructor()
125
            ->getMock();
126
127
        $this->timeCalculatorService = $mock;
128
129
        return $mock;
130
    }
131
132
    public function getForm()
133
    {
134
        return $this->getMock('Zend\Form\FormInterface');
135
    }
136
137
    public function getMockBooking()
138
    {
139
        $booking = new Booking();
140
141
        return $booking
142
            ->setDate(new DateTime("25 March 2014"))
143
            ->setUser($this->user)
144
            ->setNotes("ALL THE TIME");
145
    }
146
147
    public function testGetListCanBeAccessed()
148
    {
149
        $booking    = $this->getMockBooking();
150
        $date       = new DateTime("25 March 2014");
151
        $startDate  = new DateTime("21 April 2014");
152
153
154
        $this->controller->setDate($date);
155
        $this->configureMockBookingService('getUserBookingsForMonth', [$this->user, $date], [$booking]);
156
        $this->configureMockBookingService('getPagination', [$date], []);
157
        $this->configureMockTimeCalculatorService('getTotals', [$this->user, $startDate, $date], ['some-total', 20]);
158
159
        $this->routeMatch->setParam('action', 'list');
160
161
        $userSettings = new UserSettings;
162
        $userSettings->setFlexStartDate($startDate);
163
164
        $this->userSettingsRepository
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JhFlexiTime\Repos...ngsRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
165
            ->expects($this->once())
166
            ->method('findOneByUser')
167
            ->with($this->user)
168
            ->will($this->returnValue($userSettings));
169
170
        $result   = $this->controller->dispatch($this->request);
171
        $response = $this->controller->getResponse();
172
173
        $this->assertEquals(200, $response->getStatusCode());
174
        $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
175
        $this->assertTrue(isset($result->bookings));
176
        $this->assertTrue(isset($result->pagination));
177
        $this->assertTrue(isset($result->date));
178
179
        $expectedTime = [
180
            'records' => [$booking],
181
            'totals'  => ['some-total', 20],
182
            'user'    => $this->user,
183
        ];
184
        $this->assertEquals($expectedTime, $result->getVariable('bookings'));
185
        $this->assertEquals([], $result->getVariable('pagination'));
186
        $this->assertSame($date, $result->getVariable('date'));
187
    }
188
}
189