From 45ca80d8462b95855bca45bbb1dfe89d596a5409 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Fri, 19 Jun 2020 18:39:13 -0500 Subject: [PATCH] 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. --- README.md | 13 ++++ composer.json | 2 +- src/Client.php | 15 +++++ tests/ClientTest.php | 157 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 179 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f587bf7..9d33b02 100644 --- a/README.md +++ b/README.md @@ -252,3 +252,16 @@ $holiday_api->holidays([ 'year' => 2019, ]); ``` + +### Workday + +#### Fetch workday 7 business days after a date + +```php +workday([ + 'country' => 'US', + 'start' => '2019-07-01', + 'days' => 7, +]); +``` diff --git a/composer.json b/composer.json index c1c0886..b5e0b93 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Client.php b/src/Client.php index c482865..f855d06 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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); + } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 30a96b3..08d07da 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -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()); + } + } }