Compare commits

..

14 commits

Author SHA1 Message Date
f8e945bacd chore: drop EOL PHP versions
Seemed overdue to finally drop support for PHP 5.x, as well as 7.x as
the 7 series has also reached end of life.

Expanded testing to include both the soon to be released 8.3 and the
next development version 8.4.
2023-11-06 12:43:17 -06:00
15052790c5 feat: get previous workday
Added the ability to get the previous workday by passing in a negative
number of days along with the date to start from.

This update also includes some overhauling to allow for the library to
be more resilient to changes in the upstream API.
2022-04-05 18:02:30 -05:00
95bba75265 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.
2021-06-14 18:01:47 -05:00
peter279k
c12f512f8a Add assertRegExp assertion checking 2021-06-10 23:01:28 -05:00
peter279k
b398ba5013 Add PHP8.0 and JSON & cURL extension checking 2020-12-29 16:26:42 -06:00
peter279k
b15f862193 Using PHPUnit ^4.8.36 and some PHPUnit stuffs 2020-12-22 16:17:12 -06:00
49d20e06a8 docs: fix build and coverage badges
Had the wrong URL in there, whoops.
2020-06-19 19:12:30 -05:00
45ca80d846 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.
2020-06-19 18:56:47 -05:00
b6fad34f19 build: swap travis for github actions 2020-06-13 10:19:15 -05:00
b9ceb5947c docs: improve syntax highlighting of code snippets
* Seems PHP snippets require the delimiter to function properly.
* Updated a bunch of dependencies.
2020-06-13 10:19:15 -05:00
12f1ea69f8 feat: filter out countries without public holidays
Fairly minor update, no code change, just added a new test to cover the
flag that was recently added to the API and expanded the documentation
to also include it.
2020-03-03 22:08:57 -06:00
054eab9c02 feat: pull country or language by code
* feat: pull country or language by code.

This differs from the existing `search` functionality in that it allows
you to explicitly pull back a single country or language instead of
anything that matches (which could result in more items returned than
expected).

* docs(readme): update to include examples for new functionality.
* test: expand tests to include new parameters.
* fix(client): dropped some commented out code that wasn't in use.
* docs(license): bump the year of the license.
2020-02-11 18:14:18 -06:00
0f45ca781a chore(ci): include 7.4 and nightly
* chore(ci): Added 7.4 (just released) and nightly (as an allowed failure).
* chore(composer): Swap out abandoned coveralls package for maintained version.
* chore(composer): Added PHPUnit as a dev dependency.
* chore(ci): Dropped a ton of the juggling in favor of version juggling in Composer.
* fix(readme): Corrected the badge for the license.
* chore(composer): Bumped the package's version number.
2019-12-05 19:32:12 -06:00
e97edee508 feat: include all endpoints
* Reworked interfacing to drop the versioning in the class name.
* Refactored / cleaned up existing code.
* Stuck with support for PHP 5.3+ since PHP 5.x still seems to hold majority.
* Abstracted request logic to another class for mocking.
* Added test suite and hit full code coverage.
* Updated readme, included migration guide and the full gamut of examples.
* Wired up travis-ci and coveralls for testing and coverage.
2019-10-10 00:12:13 -05:00
12 changed files with 1108 additions and 93 deletions

29
.github/workflows/test.yml vendored Normal file
View file

@ -0,0 +1,29 @@
name: Test
on: [push, pull_request]
jobs:
test:
name: Test PHP ${{ matrix.php-version }}
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['8.0', '8.1', '8.2', '8.3', '8.4']
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
- name: PHP Version
run: php --version
- name: Composer Version
run: composer --version
- name: Install Dependencies
run: COMPOSER_MEMORY_LIMIT=-1 composer install
- name: Run Tests
run: vendor/bin/phpunit --coverage-clover ./coverage.xml
- name: Upload Coverage
if: ${{ matrix.php-version == '8.2' }}
uses: codecov/codecov-action@v1
with:
file: ./coverage.xml

6
.gitignore vendored
View file

@ -1,6 +1,4 @@
composer.lock
composer.phar
.phpunit.result.cache
/vendor/
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock

View file

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2016 Josh Sherman
Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Gravity Boulevard, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

270
README.md
View file

@ -1,30 +1,266 @@
# php-holidayapi
Official PHP library for [Holiday API](https://holidayapi.com)
# Holiday API PHP Library
[![License](https://img.shields.io/packagist/l/holidayapi/holidayapi-php?style=for-the-badge)](https://github.com/holidayapi/holidayapi-php/blob/master/LICENSE)
![PHP Version](https://img.shields.io/packagist/php-v/holidayapi/holidayapi-php?style=for-the-badge)
[![Code Coverage](https://img.shields.io/codecov/c/github/holidayapi/holidayapi-php?style=for-the-badge)](https://codecov.io/gh/holidayapi/holidayapi-php)
Official PHP library for [Holiday API](https://holidayapi.com) providing quick
and easy access to holiday information from applications written in PHP.
## Documentation
Full documentation of the Holiday API endpoints is available
[here](https://holidayapi.com/docs).
## Installation
```shell
composer require "joshtronic/php-holidayapi:dev-master"
composer require holidayapi/holidayapi-php
```
## Usage
```php
$hapi = new HolidayAPI\v1('_YOUR_API_KEY_');
<?php
$key = 'Insert your API key here';
$holiday_api = new \HolidayAPI\Client(['key' => $key]);
$parameters = array(
// Required
'country' => 'US',
'year' => 2016,
// Optional
// 'month' => 7,
// 'day' => 4,
// 'previous' => true,
// 'upcoming' => true,
// 'public' => true,
// 'pretty' => true,
);
try {
// Fetch supported countries and subdivisions
$countries = $holiday_api->countries();
$response = $hapi->holidays($parameters);
// Fetch supported languages
$languages = $holiday_api->languages();
// Fetch holidays with minimum parameters
$holidays = $holiday_api->holidays([
'country' => 'US',
'year' => 2019,
]);
var_dump($countries, $languages, $holidays);
} catch (Exception $e) {
var_dump($e);
}
```
## Examples
### Countries
#### Fetch all supported countries
```php
<?php
$holiday_api->countries();
```
#### Fetch only countries with public holidays
```php
<?php
$holiday_api->countries([
'public' => true,
]);
```
#### Fetch a supported country by code
```php
<?php
$holiday_api->countries([
'country' => 'NO',
]);
```
#### Search for countries by code or name
```php
<?php
$holiday_api->countries([
'search' => 'united',
]);
```
### Languages
#### Fetch all supported languages
```php
<?php
$holiday_api->languages();
```
#### Fetch a supported language by code
```php
<?php
$holiday_api->languages([
'language' => 'es',
]);
```
#### Search for languages by code or name
```php
<?php
$holiday_api->languages([
'search' => 'Chinese',
]);
```
### Holidays
#### Fetch holidays for a specific year
```php
<?php
$holiday_api->holidays([
'country' => 'US',
'year' => 2019,
]);
```
#### Fetch holidays for a specific month
```php
<?php
$holiday_api->holidays([
'country' => 'US',
'year' => 2019,
'month' => 7,
]);
```
#### Fetch holidays for a specific day
```php
<?php
$holiday_api->holidays([
'country' => 'US',
'year' => 2019,
'month' => 7,
'day' => 4,
]);
```
#### Fetch upcoming holidays based on a specific date
```php
<?php
$holiday_api->holidays([
'country' => 'US',
'year' => 2019,
'month' => 7,
'day' => 4,
'upcoming' => true,
]);
```
#### Fetch previous holidays based on a specific date
```php
<?php
$holiday_api->holidays([
'country' => 'US',
'year' => 2019,
'month' => 7,
'day' => 4,
'previous' => true,
]);
```
#### Fetch only public holidays
```php
<?php
$holiday_api->holidays([
'country' => 'US',
'year' => 2019,
'public' => true,
]);
```
#### Fetch holidays for a specific subdivision
```php
<?php
$holiday_api->holidays([
'country' => 'GB-ENG',
'year' => 2019,
]);
```
#### Include subdivision holidays with countrywide holidays
```php
<?php
$holiday_api->holidays([
'country' => 'US',
'year' => 2019,
'subdivisions' => true,
]);
```
#### Search for a holiday by name
```php
<?php
$holiday_api->holidays([
'country' => 'US',
'year' => 2019,
'search' => 'New Year',
]);
```
#### Translate holidays to another language
```php
<?php
$holiday_api->holidays([
'country' => 'US',
'year' => 2019,
'language' => 'zh', // Chinese (Simplified)
]);
```
#### Fetch holidays for multiple countries
```php
<?php
$holiday_api->holidays([
'country' => 'US,GB,NZ',
'year' => 2019,
]);
$holiday_api->holidays([
'country' => ['US', 'GB', 'NZ'],
'year' => 2019,
]);
```
### Workday
#### Fetch workday 7 business days after a date
```php
<?php
$holiday_api->workday([
'country' => 'US',
'start' => '2019-07-01',
'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',
]);
```

20
codecov.yml Normal file
View file

@ -0,0 +1,20 @@
codecov:
require_ci_to_pass: yes
coverage:
precision: 2
round: down
range: "70...100"
parsers:
gcov:
branch_detection:
conditional: yes
loop: yes
method: no
macro: no
comment:
layout: "reach,diff,flags,tree"
behavior: default
require_changes: no

View file

@ -1,23 +1,37 @@
{
"name": "joshtronic/php-holidayapi",
"description": "Official PHP library for Holiday API",
"version": "1.0.0",
"type": "library",
"keywords": [ "holiday", "holidays", "holidayapi" ],
"homepage": "https://github.com/joshtronic/php-holidayapi",
"license": "MIT",
"authors": [{
"name": "Josh Sherman",
"email": "hello@holidayapi.com",
"homepage": "https://holidayapi.com,"
}],
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"satooshi/php-coveralls": "~1.0"
},
"autoload": {
"psr-4": { "HolidayAPI\\": "src/" }
"name": "holidayapi/holidayapi-php",
"description": "Official PHP library for Holiday API",
"version": "4.0.0",
"type": "library",
"keywords": [
"calendar",
"holiday",
"holidays",
"holidayapi"
],
"homepage": "https://github.com/holidayapi/holidayapi-php",
"license": "MIT",
"authors": [{
"name": "Josh Sherman",
"email": "hello@holidayapi.com",
"homepage": "https://holidayapi.com"
}],
"require": {
"php": ">=8.0",
"ext-json": "*",
"ext-curl": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
},
"autoload": {
"psr-4": {
"HolidayAPI\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"HolidayAPI\\Tests\\": "tests/"
}
}
}

24
phpunit.xml Normal file
View file

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
bootstrap="./vendor/autoload.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
failOnRisky="true"
stopOnError="true"
stopOnFailure="true"
stopOnIncomplete="true"
verbose="true"
>
<testsuites>
<testsuite name="Holiday API">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>

85
src/Client.php Normal file
View file

@ -0,0 +1,85 @@
<?php
/**
* Copyright (c) Gravity Boulevard, LLC
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
namespace HolidayAPI;
use HolidayAPI;
class Client
{
private $handler;
public $baseUrl;
public $key;
public function __construct($options)
{
$getYours = 'get yours at HolidayAPI.com';
$uuidRegExp = '/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/';
if (!isset($options['key'])) {
throw new \Exception("Missing API key, {$getYours}");
}
if (!preg_match($uuidRegExp, $options['key'])) {
throw new \Exception("Invalid API key, {$getYours}");
}
$version = (isset($options['version']) ? $options['version'] : 1);
if ($version != 1) {
throw new \Exception('Invalid version number, expected "1"');
}
$this->baseUrl = "https://holidayapi.com/v{$version}/";
$this->key = $options['key'];
if (isset($options['handler'])) {
$this->handler = $options['handler'];
} else {
$this->handler = new Request();
}
}
private function createUrl($endpoint, $request = array())
{
$parameters = array_merge(array('key' => $this->key), $request);
$parameters = http_build_query($parameters);
return "{$this->baseUrl}{$endpoint}?{$parameters}";
}
private function request($endpoint, $request)
{
return $this->handler->get($this->createUrl($endpoint, $request));
}
public function countries($request = array())
{
return $this->request('countries', $request);
}
public function holidays($request)
{
return $this->request('holidays', $request);
}
public function languages($request = array())
{
return $this->request('languages', $request);
}
public function workday($request)
{
return $this->request('workday', $request);
}
public function workdays($request)
{
return $this->request('workdays', $request);
}
}

79
src/Request.php Normal file
View file

@ -0,0 +1,79 @@
<?php
/**
* Copyright (c) Gravity Boulevard, LLC
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
namespace HolidayAPI;
class Request
{
private $handlers = array();
public function __construct($handlers = array())
{
$this->handlers = $handlers;
}
public function execute($curl)
{
if (isset($this->handlers['execute'])) {
$info = curl_getinfo($curl);
$url = $info['url'];
if (isset($this->handlers['execute'][$url])) {
return $this->handlers['execute'][$url]($curl);
}
}
return curl_exec($curl);
}
public function error($curl)
{
if (isset($this->handlers['error'])) {
$info = curl_getinfo($curl);
$url = $info['url'];
if (isset($this->handlers['error'][$url])) {
return $this->handlers['error'][$url]($curl);
}
}
return curl_error($curl);
}
public function get($url)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_RETURNTRANSFER => true,
));
$response = $this->execute($curl);
if ($error = $this->error($curl)) {
throw new \Exception($error);
}
curl_close($curl);
$response = json_decode($response, true);
if (!$response) {
throw new \Exception('Empty response received');
}
if (isset($response['error'])) {
throw new \Exception($response['error'], $response['status']);
}
return $response;
}
}

View file

@ -1,51 +0,0 @@
<?php
namespace HolidayAPI;
class v1
{
private $parameters = array();
public function __set($variable, $value)
{
$this->parameters[$variable] = $value;
}
public function __construct($key = null)
{
if ($key) {
$this->key = $key;
}
}
public function holidays($parameters = array())
{
$parameters = array_merge($this->parameters, $parameters);
$parameters = http_build_query($parameters);
$url = 'https://holidayapi.com/v1/holidays?' . $parameters;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_RETURNTRANSFER => true,
));
$response = curl_exec($curl);
if ($error = curl_error($curl)) {
return false;
}
curl_close($curl);
$response = json_decode($response, true);
if (!$response) {
return false;
}
return $response;
}
}

539
tests/ClientTest.php Normal file
View file

@ -0,0 +1,539 @@
<?php
namespace HolidayAPI\Tests;
use HolidayAPI\Client;
use HolidayAPI\Request;
use PHPUnit\Framework\TestCase;
class ClientTest extends TestCase
{
const BASE_URL = 'https://holidayapi.com/v1/';
const KEY = '8e4de28c-4b18-49f0-9aba-0bd6b424fc38';
public function testMissingKey()
{
if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
$assertRegExp = 'assertMatchesRegularExpression';
} else {
$assertRegExp = 'assertRegExp';
}
try {
new Client(array());
} catch (\Exception $e) {
$this->$assertRegExp('/missing api key/i', $e->getMessage());
}
}
public function testInvalidKey()
{
if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
$assertRegExp = 'assertMatchesRegularExpression';
} else {
$assertRegExp = 'assertRegExp';
}
try {
new Client(array(
'key' => 'zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz',
));
} catch (\Exception $e) {
$this->$assertRegExp('/invalid api key/i', $e->getMessage());
}
}
public function testVersionTooLow()
{
if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
$assertRegExp = 'assertMatchesRegularExpression';
} else {
$assertRegExp = 'assertRegExp';
}
try {
new Client(array('key' => self::KEY, 'version' => 0));
} catch (\Exception $e) {
$this->$assertRegExp('/invalid version/i', $e->getMessage());
}
}
public function testVersionTooHigh()
{
if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
$assertRegExp = 'assertMatchesRegularExpression';
} else {
$assertRegExp = 'assertRegExp';
}
try {
new Client(array('key' => self::KEY, 'version' => 2));
} catch (\Exception $e) {
$this->$assertRegExp('/invalid version/i', $e->getMessage());
}
}
public function testAssignClassMembers()
{
$client = new Client(array('key' => self::KEY));
$this->assertSame(self::BASE_URL, $client->baseUrl);
$this->assertSame(self::KEY, $client->key);
}
public function testReturnCountries()
{
$url = self::BASE_URL . 'countries?key=' . self::KEY;
$request = new Request(array(
'execute' => array(
$url => function ()
{
return json_encode(array(
'status' => 200,
'countries' => array(
array(
'code' => 'ST',
'name' => 'Sao Tome and Principle',
),
),
));
},
),
));
$client = new Client(array('key' => self::KEY, 'handler' => $request));
$this->assertEquals(array(
'status' => 200,
'countries' => array(
array(
'code' => 'ST',
'name' => 'Sao Tome and Principle',
),
),
), $client->countries());
}
public function testCountriesRaise4xxErrors()
{
$url = self::BASE_URL . 'countries?key=' . self::KEY;
$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->countries();
} catch (\Exception $e) {
$this->assertSame(429, $e->getCode());
$this->assertSame('Rate limit exceeded', $e->getMessage());
}
}
public function testCountriesRaise5xxErrors()
{
$url = self::BASE_URL . 'countries?key=' . self::KEY;
$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->countries();
} catch (\Exception $e) {
$this->assertSame('Internal server error', $e->getMessage());
}
}
public function testReturnHolidays()
{
$url = self::BASE_URL . 'holidays?key=' . self::KEY . '&country=US&year=2015&month=7&day=4';
$request = new Request(array(
'execute' => array(
$url => function ()
{
return json_encode(array(
'status' => 200,
'holidays' => array(
array(
'name' => 'Independence Day',
'date' => '2015-07-04',
'observed' => '2015-07-03',
'public' => true,
),
),
));
},
),
));
$client = new Client(array('key' => self::KEY, 'handler' => $request));
$this->assertEquals(array(
'status' => 200,
'holidays' => array(
array(
'name' => 'Independence Day',
'date' => '2015-07-04',
'observed' => '2015-07-03',
'public' => true,
),
),
), $client->holidays(array(
'country' => 'US',
'year' => 2015,
'month' => 7,
'day' => 4,
)));
}
public function testHolidaysRaise4xxErrors()
{
$url = self::BASE_URL . 'holidays?key=' . self::KEY . '&country=US&year=2019';
$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->holidays(array('country' => 'US', 'year' => 2019));
} catch (\Exception $e) {
$this->assertSame(429, $e->getCode());
$this->assertSame('Rate limit exceeded', $e->getMessage());
}
}
public function testHolidaysRaise5xxErrors()
{
$url = self::BASE_URL . 'holidays?key=' . self::KEY . '&country=US&year=2019';
$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->holidays(array('country' => 'US', 'year' => 2019));
} catch (\Exception $e) {
$this->assertSame('Internal server error', $e->getMessage());
}
}
public function testReturnLanguages()
{
$url = self::BASE_URL . 'languages?key=' . self::KEY;
$request = new Request(array(
'execute' => array(
$url => function ()
{
return json_encode(array(
'status' => 200,
'languages' => array(
array(
'code' => 'en',
'name' => 'English',
),
),
));
},
),
));
$client = new Client(array('key' => self::KEY, 'handler' => $request));
$this->assertEquals(array(
'status' => 200,
'languages' => array(
array(
'code' => 'en',
'name' => 'English',
),
),
), $client->languages());
}
public function testLanguagesRaise4xxErrors()
{
$url = self::BASE_URL . 'languages?key=' . self::KEY;
$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->languages();
} catch (\Exception $e) {
$this->assertSame(429, $e->getCode());
$this->assertSame('Rate limit exceeded', $e->getMessage());
}
}
public function testLanguagesRaise5xxErrors()
{
$url = self::BASE_URL . 'languages?key=' . self::KEY;
$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->languages();
} catch (\Exception $e) {
$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 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());
}
}
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 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());
}
}
}

42
tests/RequestTest.php Normal file
View file

@ -0,0 +1,42 @@
<?php
namespace HolidayAPI\Tests;
use HolidayAPI\Request;
use PHPUnit\Framework\TestCase;
class RequestTest extends TestCase
{
public function testExecute()
{
$curl = curl_init();
$request = new Request();
$this->assertFalse($request->execute($curl));
}
public function testGet()
{
if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
$assertRegExp = 'assertMatchesRegularExpression';
} else {
$assertRegExp = 'assertRegExp';
}
$url = 'https://holidayapi.com';
$request = new Request(array(
'execute' => array(
$url => function ()
{
return '';
},
),
));
try {
$request->get($url);
} catch (\Exception $e) {
$this->$assertRegExp('/empty response/i', $e->getMessage());
}
}
}