feat: add workdays endpoint

Added support for our new workdays endpoint that allows you to get the
number of working days for a country between two dates.
This commit is contained in:
Josh Sherman 2021-06-10 23:11:35 -05:00
parent c12f512f8a
commit 92d164d22c
No known key found for this signature in database
GPG key ID: 55B058A80530EF22
5 changed files with 172 additions and 15 deletions

View file

@ -23,7 +23,7 @@ jobs:
- name: Run Tests
run: vendor/bin/phpunit --coverage-clover ./coverage.xml
- name: Upload Coverage
if: ${{ matrix.php-version == '7.4' }}
if: ${{ matrix.php-version == '8.0' }}
uses: codecov/codecov-action@v1
with:
file: ./coverage.xml

View file

@ -8,19 +8,6 @@
Official PHP library for [Holiday API](https://holidayapi.com) providing quick
and easy access to holiday information from applications written in PHP.
## Migrating from 1.x
Please note, version 2.x of this library is a full rewrite of the 1.x series.
The interfacing to the library has been simplified and existing applications
upgrading to 2.x will need to be updated.
| Version 1.x Syntax (Old) | Version 2.x Syntax (New) |
|--------------------------------------------|-----------------------------------------------------------|
| `$holiday_api = new \HolidayAPI\v1($key);` | `$holiday_api = new \HolidayAPI\Client(['key' => $key]);` |
Version 1.x of the library can still be found
[here](https://github.com/joshtronic/php-holidayapi).
## Documentation
Full documentation of the Holiday API endpoints is available
@ -265,3 +252,16 @@ $holiday_api->workday([
'days' => 7,
]);
```
### Workdays
#### Fetch number of workdays between two dates
```php
<?php
$holiday_api->workdays([
'country' => 'US',
'start' => '2019-07-01',
'end' => '2019-07-10',
]);
```

View file

@ -1,7 +1,7 @@
{
"name": "holidayapi/holidayapi-php",
"description": "Official PHP library for Holiday API",
"version": "2.3.0",
"version": "2.4.0",
"type": "library",
"keywords": [
"calendar",

View file

@ -97,5 +97,18 @@ class Client
return $this->request('workday', $request);
}
public function workdays($request)
{
if (!isset($request['country'])) {
throw new \Exception('Missing country');
} elseif (!isset($request['start'])) {
throw new \Exception('Missing start date');
} elseif (!isset($request['end'])) {
throw new \Exception('Missing end date');
}
return $this->request('workdays', $request);
}
}

View file

@ -790,5 +790,149 @@ class ClientTest extends TestCase
$this->assertSame('Internal server error', $e->getMessage());
}
}
public function testReturnWorkdays()
{
$url = self::BASE_URL . 'workdays?key=' . self::KEY . '&country=US&start=2019-07-01&end=2019-07-10';
$request = new Request(array(
'execute' => array(
$url => function ()
{
return json_encode(array(
'status' => 200,
'workdays' => 7,
));
},
),
));
$client = new Client(array('key' => self::KEY, 'handler' => $request));
$this->assertEquals(array(
'status' => 200,
'workdays' => 7,
), $client->workdays(array(
'country' => 'US',
'start' => '2019-07-01',
'end' => '2019-07-10',
)));
}
public function testWorkdaysCountryMissing()
{
if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
$assertRegExp = 'assertMatchesRegularExpression';
} else {
$assertRegExp = 'assertRegExp';
}
$client = new Client(array('key' => self::KEY));
try {
$client->workdays(array());
} catch (\Exception $e) {
$this->$assertRegExp('/missing country/i', $e->getMessage());
}
}
public function testWorkdaysStartMissing()
{
if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
$assertRegExp = 'assertMatchesRegularExpression';
} else {
$assertRegExp = 'assertRegExp';
}
$client = new Client(array('key' => self::KEY));
try {
$client->workdays(array('country' => 'US'));
} catch (\Exception $e) {
$this->$assertRegExp('/missing start date/i', $e->getMessage());
}
}
public function testWorkdaysEndMissing()
{
if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
$assertRegExp = 'assertMatchesRegularExpression';
} else {
$assertRegExp = 'assertRegExp';
}
$client = new Client(array('key' => self::KEY));
try {
$client->workdays(array(
'country' => 'US',
'start' => '2019-07-01',
));
} catch (\Exception $e) {
$this->$assertRegExp('/missing end date/i', $e->getMessage());
}
}
public function testWorkdaysRaise4xxErrors()
{
$url = self::BASE_URL . 'workdays?key=' . self::KEY . '&country=US&start=2019-07-01&end=2019-07-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->workdays(array(
'country' => 'US',
'start' => '2019-07-01',
'end' => '2019-07-10',
));
} catch (\Exception $e) {
$this->assertSame(429, $e->getCode());
$this->assertSame('Rate limit exceeded', $e->getMessage());
}
}
public function testWorkdaysRaise5xxErrors()
{
$url = self::BASE_URL . 'workdays?key=' . self::KEY . '&country=US&start=2019-07-01&end=2019-07-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->workdays(array(
'country' => 'US',
'start' => '2019-07-01',
'end' => '2019-07-10',
));
} catch (\Exception $e) {
$this->assertSame('Internal server error', $e->getMessage());
}
}
}