1<?php
2/**
3 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
4 * all the essential functionalities required for any enterprise.
5 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
6 *
7 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
8 * the GNU General Public License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along with this program;
16 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA  02110-1301, USA
18 */
19
20use Orangehrm\Rest\Api\Exception\BadRequestException;
21use Orangehrm\Rest\Api\Exception\InvalidParamException;
22use Orangehrm\Rest\Http\Request;
23use Orangehrm\Rest\Http\Response;
24
25/**
26 * @group API
27 */
28class ApiLeaveAPITest extends PHPUnit\Framework\TestCase
29{
30    /**
31     * @var Request
32     */
33    private $request = null;
34
35    protected function setUp()
36    {
37        $sfEvent = new sfEventDispatcher();
38        $sfRequest = new sfWebRequest($sfEvent);
39        $this->request = new Request($sfRequest);
40        TestDataService::truncateSpecificTables(array('AttendanceRecord','Employee','LeaveType','Leave'));
41        TestDataService::populate(sfConfig::get('sf_plugins_dir') . '/orangehrmLeavePlugin/test/fixtures/AttendanceLeaveDao.yml');
42    }
43
44    public function testGetLeaveRecords()
45    {
46        $leaveRecord = TestDataService::fetchObject('Leave', 10);
47        $leaveRequestService = $this->getMockBuilder(
48            'LeaveRequestService'
49        )
50            ->setMethods(
51                [
52                    'getLeaveRecordsBetweenTwoDays',
53                ]
54            )
55            ->getMock();
56        $leaveRequestService->expects($this->once())
57            ->method('getLeaveRecordsBetweenTwoDays')
58            ->will($this->returnValue(array($leaveRecord)));
59        $params = [
60            'fromDate' => "2020-12-23",
61            'toDate' => "2020-12-29",
62            'empNumber' => 1,
63            'pendingApproval' => 'true',
64            'scheduled' => 'true',
65            'taken' => 'true'
66        ];
67        $leaveAPI = $this->getMockBuilder('Orangehrm\Rest\Api\User\Leave\LeaveAPI')
68            ->setMethods(
69                [
70                    'getParameters',
71                    'getLoggedInEmployeeNumber',
72                    'getEmployeeDetails',
73                    'getWorkHours',
74                    'getAccessibleEmpNumbers',
75                ]
76            )
77            ->setConstructorArgs([$this->request])
78            ->getMock();
79        $leaveAPI->setLeaveRequestService($leaveRequestService);
80        $leaveAPI->expects($this->once())
81            ->method('getParameters')
82            ->will($this->returnValue($params));
83        $leaveAPI->expects($this->once())
84            ->method('getLoggedInEmployeeNumber')
85            ->will($this->returnValue(1));
86        $leaveAPI
87            ->method('getWorkHours')
88            ->will($this->returnValue([$leaveRecord->toArray()]));
89        $leaveAPI
90            ->method('getAccessibleEmpNumbers')
91            ->will($this->returnValue([1]));
92        $actual = $leaveAPI->getLeaveRecords();
93        $expected = new Response(array(
94            array(
95                'id' => "10",
96                'date' => "2010-09-21",
97                'lengthHours' => "8.00",
98                'lengthDays' => "1.0000",
99                'leaveType' => array("id" => "2", "type" => "Medical"),
100                'startTime' => null,
101                'endTime' => null,
102                'status' => "TAKEN"
103            ))
104        );
105        $this->assertEquals($expected, $actual);
106    }
107
108    public function testGetLeaveRecordsForNotValidEmployee()
109    {
110        $leaveRecord = TestDataService::fetchObject('Leave', 10);
111        $leaveRequestService = $this->getMockBuilder(
112            'LeaveRequestService'
113        )
114            ->setMethods(
115                [
116                    'getLeaveRecordsBetweenTwoDays',
117                ]
118            )
119            ->getMock();
120        $leaveRequestService
121            ->method('getLeaveRecordsBetweenTwoDays')
122            ->will($this->returnValue(array($leaveRecord)));
123        $params = [
124            'fromDate' => "2020-12-29",
125            'toDate' => "2020-12-29",
126            'empNumber' => 10000,
127            'pendingApproval' => 'true',
128            'scheduled' => 'true',
129            'taken' => 'true'
130        ];
131        $leaveAPI = $this->getMockBuilder('Orangehrm\Rest\Api\User\Leave\LeaveAPI')
132            ->setMethods(
133                [
134                    'getParameters',
135                    'getLoggedInEmployeeNumber',
136                    'getEmployeeDetails',
137                    'getWorkHours',
138                    'getAccessibleEmpNumbers',
139                ]
140            )
141            ->setConstructorArgs([$this->request])
142            ->getMock();
143        $leaveAPI->setLeaveRequestService($leaveRequestService);
144        $leaveAPI->expects($this->once())
145            ->method('getParameters')
146            ->will($this->returnValue($params));
147        $leaveAPI->expects($this->once())
148            ->method('getLoggedInEmployeeNumber')
149            ->will($this->returnValue(1));
150        $leaveAPI
151            ->method('getWorkHours')
152            ->will($this->returnValue([$leaveRecord->toArray()]));
153        $leaveAPI
154            ->method('getAccessibleEmpNumbers')
155            ->will($this->returnValue([]));
156        $this->expectException(BadRequestException::class);
157        $leaveAPI->getLeaveRecords();
158    }
159    public function testGetLeaveRecordsForNotValidDatePeriod()
160    {
161        $leaveRecord = TestDataService::fetchObject('Leave', 10);
162        $leaveRequestService = $this->getMockBuilder(
163            'LeaveRequestService'
164        )
165            ->setMethods(
166                [
167                    'getLeaveRecordsBetweenTwoDays',
168                ]
169            )
170            ->getMock();
171        $leaveRequestService
172            ->method('getLeaveRecordsBetweenTwoDays')
173            ->will($this->returnValue(array($leaveRecord)));
174        $params = [
175            'fromDate' => "2020-12-31",
176            'toDate' => "2020-12-29",
177            'empNumber' => 10000,
178            'pendingApproval' => 'true',
179            'scheduled' => 'true',
180            'taken' => 'true'
181        ];
182        $leaveAPI = $this->getMockBuilder('Orangehrm\Rest\Api\User\Leave\LeaveAPI')
183            ->setMethods(
184                [
185                    'getParameters',
186                    'getLoggedInEmployeeNumber',
187                    'getEmployeeDetails',
188                    'getWorkHours',
189                    'getAccessibleEmpNumbers',
190                ]
191            )
192            ->setConstructorArgs([$this->request])
193            ->getMock();
194        $leaveAPI->setLeaveRequestService($leaveRequestService);
195        $leaveAPI->expects($this->once())
196            ->method('getParameters')
197            ->will($this->returnValue($params));
198        $leaveAPI->expects($this->once())
199            ->method('getLoggedInEmployeeNumber')
200            ->will($this->returnValue(1));
201        $leaveAPI
202            ->method('getWorkHours')
203            ->will($this->returnValue([$leaveRecord->toArray()]));
204        $leaveAPI
205            ->method('getAccessibleEmpNumbers')
206            ->will($this->returnValue([]));
207        $this->expectException(InvalidParamException::class);
208        $leaveAPI->getLeaveRecords();
209    }
210}
211