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\Http\Request; 22use Orangehrm\Rest\Http\Response; 23 24/** 25 * @group API 26 */ 27class ApiApplyLeaveRequestAPITest extends PHPUnit\Framework\TestCase 28{ 29 /** 30 * @var Request 31 */ 32 private $request = null; 33 34 protected function setUp() 35 { 36 $sfEvent = new sfEventDispatcher(); 37 $sfRequest = new sfWebRequest($sfEvent); 38 $this->request = new Request($sfRequest); 39 } 40 41 public function testSaveLeaveRequest() 42 { 43 $applyLeaveRequestApi = $this->getMockBuilder('Orangehrm\Rest\Api\User\Leave\ApplyLeaveRequestAPI') 44 ->setMethods(['filterParameters', 'validateLeaveType', 'isValidToDate']) 45 ->setConstructorArgs([$this->request]) 46 ->getMock(); 47 $applyLeaveRequestApi->expects($this->once()) 48 ->method('filterParameters') 49 ->will($this->returnValue([])); 50 $applyLeaveRequestApi->expects($this->once()) 51 ->method('validateLeaveType') 52 ->will($this->returnValue(true)); 53 $applyLeaveRequestApi->expects($this->once()) 54 ->method('isValidToDate') 55 ->will($this->returnValue(true)); 56 57 $apiLeaveApplicationService = $this->getMockBuilder( 58 'Orangehrm\Rest\Api\User\Service\APILeaveApplicationService' 59 )->getMock(); 60 $apiLeaveApplicationService->expects($this->once()) 61 ->method('applyLeave') 62 ->will($this->returnValue(new LeaveRequest())); 63 64 $applyLeaveRequestApi->setApiLeaveApplicationService($apiLeaveApplicationService); 65 $responseSaveLeaveRequest = $applyLeaveRequestApi->saveLeaveRequest(); 66 $success = new Response(['success' => 'Successfully Saved']); 67 68 $this->assertEquals($success, $responseSaveLeaveRequest); 69 } 70 71 public function testSaveLeaveRequestSaveFailed() 72 { 73 $applyLeaveRequestApi = $this->getMockBuilder('Orangehrm\Rest\Api\User\Leave\ApplyLeaveRequestAPI') 74 ->setMethods(['filterParameters', 'validateLeaveType', 'isValidToDate']) 75 ->setConstructorArgs([$this->request]) 76 ->getMock(); 77 $applyLeaveRequestApi->expects($this->once()) 78 ->method('filterParameters') 79 ->will($this->returnValue([])); 80 $applyLeaveRequestApi->expects($this->once()) 81 ->method('validateLeaveType') 82 ->will($this->returnValue(false)); 83 $applyLeaveRequestApi->expects($this->once()) 84 ->method('isValidToDate') 85 ->will($this->returnValue(true)); 86 87 $this->expectException(BadRequestException::class); 88 $applyLeaveRequestApi->saveLeaveRequest(); 89 } 90 91 public function testSaveLeaveRequestAllocationException() 92 { 93 $this->expectException(BadRequestException::class); 94 $applyLeaveRequestApi = $this->getMockBuilder('Orangehrm\Rest\Api\User\Leave\ApplyLeaveRequestAPI') 95 ->setMethods(['filterParameters', 'validateLeaveType', 'isValidToDate']) 96 ->setConstructorArgs([$this->request]) 97 ->getMock(); 98 $applyLeaveRequestApi->expects($this->once()) 99 ->method('filterParameters') 100 ->will($this->returnValue([])); 101 $applyLeaveRequestApi->expects($this->once()) 102 ->method('validateLeaveType') 103 ->will($this->returnValue(true)); 104 $applyLeaveRequestApi->expects($this->once()) 105 ->method('isValidToDate') 106 ->will($this->returnValue(true)); 107 $apiLeaveApplicationService = $this->getMockBuilder( 108 'Orangehrm\Rest\Api\User\Service\APILeaveApplicationService' 109 )->getMock(); 110 $apiLeaveApplicationService->expects($this->once()) 111 ->method('applyLeave') 112 ->will( 113 $this->returnCallback( 114 function () { 115 throw new LeaveAllocationServiceException('Leave Balance Exceeded'); 116 } 117 ) 118 ); 119 120 $applyLeaveRequestApi->setApiLeaveApplicationService($apiLeaveApplicationService); 121 122 $this->expectException(BadRequestException::class); 123 $applyLeaveRequestApi->saveLeaveRequest(); 124 } 125} 126