Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper PHP support #74

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*.lock

coverage
.phpunit.cache

composer.phar

Expand Down
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,13 @@
"issues": "https://github.com/annexare/Countries/issues"
},
"require": {},
"homepage": "http://annexare.github.io/Countries/"
"autoload": {
"files": [
"dist/index.php"
]
},
"homepage": "http://annexare.github.io/Countries/",
"require-dev": {
"phpunit/phpunit": "^9.5"
}
}
82 changes: 82 additions & 0 deletions dist/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace Annexare\Countries;

use JsonException;

use function file_get_contents;
use function json_decode;

use const JSON_THROW_ON_ERROR;

/**
* Loads a JSON file relative to the current directory.
*
* @param string $path
*
* @return array
* @throws JsonException
* @internal
*/
function load(string $path): array
{
static $cache = [];

if (isset($cache[$path])) {
return $cache[$path];
}

return $cache[$path] = json_decode(
file_get_contents(__DIR__ . '/' . $path),
true,
512,
JSON_THROW_ON_ERROR
);
}

/**
* Continents, key-value object (key is alpha-2 code).
*
* @return array
* @throws JsonException
*/
function continents(): array
{
return load('continents.min.json');
}

/**
* Countries, key-value object (key is alpha-2 code, uppercase).
*
* @return array
* @throws JsonException
*/
function countries(): array
{
return load('countries.min.json');
}

/**
* Languages in use only, key-value object (key is alpha-2 code).
*
* @return array
* @throws JsonException
*/
function languages(): array
{
return load('languages.min.json');
}

/**
* Languages, key-value object (key is alpha-2 code).
* A complete list including not used by Countries list.
*
* @return array
* @throws JsonException
*/
function languagesAll(): array
{
return load('languages.all.min.json');
}
25 changes: 25 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">dist</directory>
</include>
</coverage>
</phpunit>
76 changes: 76 additions & 0 deletions tests/CountriesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;

use function Annexare\Countries\continents;
use function Annexare\Countries\countries;
use function Annexare\Countries\languages;
use function Annexare\Countries\languagesAll;

class CountriesTest extends TestCase
{
/**
* @throws JsonException
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testCountries(): void
{
$countries = countries();

self::assertIsArray($countries);
self::assertJsonStringEqualsJsonFile(
__DIR__ . '/../dist/countries.min.json',
json_encode($countries, JSON_THROW_ON_ERROR)
);
}

/**
* @throws JsonException
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testContinents(): void
{
$continents = continents();

self::assertIsArray($continents);
self::assertJsonStringEqualsJsonFile(
__DIR__ . '/../dist/continents.min.json',
json_encode($continents, JSON_THROW_ON_ERROR)
);
}

/**
* @throws JsonException
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testLanguages(): void
{
$languages = languages();

self::assertIsArray($languages);
self::assertJsonStringEqualsJsonFile(
__DIR__ . '/../dist/languages.min.json',
json_encode($languages, JSON_THROW_ON_ERROR)
);
}

/**
* @throws JsonException
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testLanguagesAll(): void
{
$languagesAll = languagesAll();

self::assertIsArray($languagesAll);
self::assertJsonStringEqualsJsonFile(
__DIR__ . '/../dist/languages.all.min.json',
json_encode($languagesAll, JSON_THROW_ON_ERROR)
);
}
}