mirror of
https://github.com/holidayapi/holidayapi-php.git
synced 2025-06-21 04:16:31 +00:00
feat: add workday endpoint
Added support for our latest endpoint, which calculates the workday a given number of business days into the future from the given date for the given country.
This commit is contained in:
parent
b6fad34f19
commit
b248d566c6
4 changed files with 179 additions and 8 deletions
13
README.md
13
README.md
|
@ -252,3 +252,16 @@ $holiday_api->holidays([
|
|||
'year' => 2019,
|
||||
]);
|
||||
```
|
||||
|
||||
### Workday
|
||||
|
||||
#### Fetch workday 7 business days after a date
|
||||
|
||||
```php
|
||||
<?php
|
||||
$holiday_api->workday([
|
||||
'country' => 'US',
|
||||
'start' => '2019-07-01',
|
||||
'days' => 7,
|
||||
]);
|
||||
```
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "holidayapi/holidayapi-php",
|
||||
"description": "Official PHP library for Holiday API",
|
||||
"version": "2.2.2",
|
||||
"version": "2.3.0",
|
||||
"type": "library",
|
||||
"keywords": [
|
||||
"calendar",
|
||||
|
|
|
@ -82,5 +82,20 @@ class Client
|
|||
{
|
||||
return $this->request('languages', $request);
|
||||
}
|
||||
|
||||
public function workday($request)
|
||||
{
|
||||
if (!isset($request['country'])) {
|
||||
throw new \Exception('Missing country');
|
||||
} elseif (!isset($request['start'])) {
|
||||
throw new \Exception('Missing start date');
|
||||
} elseif (!isset($request['days'])) {
|
||||
throw new \Exception('Missing days');
|
||||
} elseif ($request['days'] < 1) {
|
||||
throw new \Exception('Days must be 1 or more');
|
||||
}
|
||||
|
||||
return $this->request('workday', $request);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -255,8 +255,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
public function testReturnHolidays()
|
||||
{
|
||||
$url = self::BASE_URL . 'holidays?key=' . self::KEY
|
||||
. '&country=US&year=2015&month=7&day=4';
|
||||
$url = self::BASE_URL . 'holidays?key=' . self::KEY . '&country=US&year=2015&month=7&day=4';
|
||||
|
||||
$request = new Request(array(
|
||||
'execute' => array(
|
||||
|
@ -299,8 +298,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
public function testSearchHolidays()
|
||||
{
|
||||
$url = self::BASE_URL . 'holidays?key=' . self::KEY
|
||||
. '&country=US&year=2015&search=Independence';
|
||||
$url = self::BASE_URL . 'holidays?key=' . self::KEY . '&country=US&year=2015&search=Independence';
|
||||
|
||||
$request = new Request(array(
|
||||
'execute' => array(
|
||||
|
@ -340,7 +338,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||
)));
|
||||
}
|
||||
|
||||
public function testCountryMissing()
|
||||
public function testHolidaysCountryMissing()
|
||||
{
|
||||
$client = new Client(array('key' => self::KEY));
|
||||
|
||||
|
@ -351,7 +349,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
}
|
||||
|
||||
public function testYearMissing()
|
||||
public function testHolidaysYearMissing()
|
||||
{
|
||||
$client = new Client(array('key' => self::KEY));
|
||||
|
||||
|
@ -362,7 +360,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
}
|
||||
|
||||
public function testBothPreviousAndUpcoming()
|
||||
public function testHolidaysBothPreviousAndUpcoming()
|
||||
{
|
||||
$client = new Client(array('key' => self::KEY));
|
||||
|
||||
|
@ -589,5 +587,150 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertSame('Internal server error', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testReturnWorkday()
|
||||
{
|
||||
$url = self::BASE_URL . 'workday?key=' . self::KEY . '&country=US&start=2019-07-01&days=10';
|
||||
|
||||
$request = new Request(array(
|
||||
'execute' => array(
|
||||
$url => function ()
|
||||
{
|
||||
return json_encode(array(
|
||||
'status' => 200,
|
||||
'workday' => array(
|
||||
'date' => '2019-07-16',
|
||||
),
|
||||
));
|
||||
},
|
||||
),
|
||||
));
|
||||
|
||||
$client = new Client(array('key' => self::KEY, 'handler' => $request));
|
||||
|
||||
$this->assertEquals(array(
|
||||
'status' => 200,
|
||||
'workday' => array(
|
||||
'date' => '2019-07-16',
|
||||
),
|
||||
), $client->workday(array(
|
||||
'country' => 'US',
|
||||
'start' => '2019-07-01',
|
||||
'days' => 10,
|
||||
)));
|
||||
}
|
||||
|
||||
public function testWorkdayCountryMissing()
|
||||
{
|
||||
$client = new Client(array('key' => self::KEY));
|
||||
|
||||
try {
|
||||
$client->workday(array());
|
||||
} catch (\Exception $e) {
|
||||
$this->assertRegExp('/missing country/i', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testWorkdayStartMissing()
|
||||
{
|
||||
$client = new Client(array('key' => self::KEY));
|
||||
|
||||
try {
|
||||
$client->workday(array('country' => 'US'));
|
||||
} catch (\Exception $e) {
|
||||
$this->assertRegExp('/missing start date/i', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testWorkdayDaysMissing()
|
||||
{
|
||||
$client = new Client(array('key' => self::KEY));
|
||||
|
||||
try {
|
||||
$client->workday(array(
|
||||
'country' => 'US',
|
||||
'start' => '2019-07-01',
|
||||
));
|
||||
} catch (\Exception $e) {
|
||||
$this->assertRegExp('/missing days/i', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testWorkdayDaysNegative()
|
||||
{
|
||||
$client = new Client(array('key' => self::KEY));
|
||||
|
||||
try {
|
||||
$client->workday(array(
|
||||
'country' => 'US',
|
||||
'start' => '2019-07-01',
|
||||
'days' => -10,
|
||||
));
|
||||
} catch (\Exception $e) {
|
||||
$this->assertRegExp('/days must be 1 or more/i', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testWorkdayRaise4xxErrors()
|
||||
{
|
||||
$url = self::BASE_URL . 'workday?key=' . self::KEY . '&country=US&start=2019-07-01&days=10';
|
||||
|
||||
$request = new Request(array(
|
||||
'execute' => array(
|
||||
$url => function ()
|
||||
{
|
||||
return json_encode(array(
|
||||
'status' => 429,
|
||||
'error' => 'Rate limit exceeded',
|
||||
));
|
||||
},
|
||||
),
|
||||
));
|
||||
|
||||
$client = new Client(array('key' => self::KEY, 'handler' => $request));
|
||||
|
||||
try {
|
||||
$client->workday(array(
|
||||
'country' => 'US',
|
||||
'start' => '2019-07-01',
|
||||
'days' => 10,
|
||||
));
|
||||
} catch (\Exception $e) {
|
||||
$this->assertSame(429, $e->getCode());
|
||||
$this->assertSame('Rate limit exceeded', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testWorkdayRaise5xxErrors()
|
||||
{
|
||||
$url = self::BASE_URL . 'workday?key=' . self::KEY . '&country=US&start=2019-07-01&days=10';
|
||||
|
||||
$request = new Request(array(
|
||||
'execute' => array(
|
||||
$url => function ()
|
||||
{
|
||||
return false;
|
||||
},
|
||||
),
|
||||
'error' => array(
|
||||
$url => function ()
|
||||
{
|
||||
return 'Internal server error';
|
||||
},
|
||||
),
|
||||
));
|
||||
|
||||
$client = new Client(array('key' => self::KEY, 'handler' => $request));
|
||||
|
||||
try {
|
||||
$client->workday(array(
|
||||
'country' => 'US',
|
||||
'start' => '2019-07-01',
|
||||
'days' => 10,
|
||||
));
|
||||
} catch (\Exception $e) {
|
||||
$this->assertSame('Internal server error', $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue