diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ddf8b8d..19263c5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/README.md b/README.md index e03b325..c74f9d1 100644 --- a/README.md +++ b/README.md @@ -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 +workdays([ + 'country' => 'US', + 'start' => '2019-07-01', + 'end' => '2019-07-10', +]); +``` diff --git a/composer.json b/composer.json index 4b004bc..0cd3dee 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Client.php b/src/Client.php index f855d06..b3e295e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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); + } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index fd368c6..e999192 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -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()); + } + } }