Skip to content

Commit 6da61c7

Browse files
committed
tests
1 parent d5e888f commit 6da61c7

File tree

14 files changed

+989
-0
lines changed

14 files changed

+989
-0
lines changed

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.* export-ignore
2+
/tests export-ignore
3+
/build export-ignore

.github/workflows/release.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Release
2+
3+
on:
4+
schedule:
5+
- cron: '0 4 2 * *'
6+
7+
workflow_dispatch:
8+
9+
jobs:
10+
release:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
with:
15+
ref: "4.0"
16+
17+
- name: Prepare environment
18+
run: |
19+
sudo apt install gettext
20+
sudo locale-gen uk_UA.UTF-8
21+
22+
- name: Start release
23+
run: ./bin/release.sh
24+
25+
- name: Create pull request
26+
uses: peter-evans/create-pull-request@v3
27+
with:
28+
committer: Dmytro Sokil <[email protected]>
29+
author: Dmytro Sokil <[email protected]>
30+
commit-message: Database update
31+
title: Database update
32+
base: "4.0"
33+
branch: "github-actions-release"

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build
2+
vendor
3+
.phpunit.result.cache
4+
.idea
5+
composer.lock

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Database for PHP-ISOCODES library
2+
3+
:1234: Database version: v4.8.0-29-ge291e84 from 2021-12-04 17:23
4+
5+
Database updated at 2-nd day of every month.
6+
7+
## PHP-ISOCODES library
8+
9+
Details about library you may find in [sokil/php-isocodes](https://github.com/sokil/php-isocodes) repository.
10+
11+
## Installation
12+
13+
To install "sokil/php-isocodes" library with full database and i18n files:
14+
15+
```
16+
composer require sokil/php-isocodes-db-i18n
17+
```
18+
19+
You may also install "sokil/php-isocodes" with only database (no i18n will be available):
20+
21+
```
22+
composer require sokil/php-isocodes-db-only
23+
```
24+
25+
Database and internationalisation files updated at 2-nd day of every month. If you prefer to update more often, you may
26+
use just "sokil/php-isocodes" library and tune update of database and i18n by yourself:
27+
28+
```
29+
composer require sokil/php-isocodes
30+
```
31+
32+
To setup manual update please read [instruction](https://github.com/sokil/php-isocodes#manual-database-update).
33+

bin/release.sh

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env bash
2+
3+
#####################################################################
4+
# #
5+
# Update internal database for #
6+
# publishing to GitHub. #
7+
# #
8+
# Used only by library maintainers. #
9+
# Usage: ./bin/release.sh [{build_dir}] #
10+
# #
11+
# build_dir: optional, dir where database and i18n messages #
12+
# prepared, by default is "/tmp/iso-codes-build" #
13+
# #
14+
#####################################################################
15+
16+
CURRENT_DIR=$(dirname $(readlink -f $0))
17+
PROJECT_DIR=$(dirname $CURRENT_DIR)
18+
19+
TMP_BUILD_DIR=${1:-/tmp/iso-codes-build}
20+
21+
# Prepare build dir
22+
if [[ ! -d $TMP_BUILD_DIR ]]; then
23+
# if not exists, create
24+
mkdir -p $TMP_BUILD_DIR
25+
fi
26+
27+
if [[ ! -w $TMP_BUILD_DIR ]]; then
28+
echo -e "[Release] Passed base directory \033[0;31m${TMP_BUILD_DIR}\033[0m is not writable"
29+
exit 1
30+
fi
31+
32+
echo -e "[Release] Build directory is \033[0;31m${TMP_BUILD_DIR}\033[0m"
33+
echo -e "[Release] Project directory is \033[0;31m${PROJECT_DIR}\033[0m"
34+
35+
# locate composer
36+
cd ${PROJECT_DIR}
37+
COMPOSER_PATH=`which composer`
38+
if [[ $? -eq 1 ]]; then
39+
COMPOSER_PATH=`which composer.phar`
40+
if [[ $? -eq 1 ]]; then
41+
echo -e "[Release] \033[0;31m\n\nCan not locate composer for testing build.\033[0m\n\n"
42+
exit 1
43+
fi
44+
fi
45+
46+
# if required, install composer dependencies
47+
if [[ ! -d ${PROJECT_DIR}/vendor ]]; then
48+
$COMPOSER_PATH install
49+
fi
50+
51+
# update database
52+
$PROJECT_DIR/vendor/sokil/php-isocodes/bin/update_iso_codes_db.sh db_only $PROJECT_DIR $TMP_BUILD_DIR
53+
if [[ $? -ne 0 ]]; then
54+
echo "[Release] Error when updating database"
55+
exit 1
56+
fi
57+
58+
# test updated database and message files
59+
echo -e "[Release] \033[0;32mTest updated database\033[0m"
60+
$COMPOSER_PATH test
61+
62+
# message on success test
63+
if [[ $? -ne 0 ]]; then
64+
echo "[Release] Unit tests failed. Release process stopped. Please, check test errors"
65+
exit 1
66+
fi
67+
68+
# update version in README.md
69+
echo -e "Logging of new database version to readme"
70+
71+
cd $TMP_BUILD_DIR
72+
VERSION=`git describe --tags`
73+
if [[ $? != 0 ]]; then
74+
echo -e "[Release] Can not detect database version"
75+
exit 1
76+
fi
77+
78+
BUILD_DATE=`date "+%Y-%m-%d %H:%M"`
79+
cd $PROJECT_DIR
80+
81+
sed -i -E "s/Database version: .*/Database version: ${VERSION} from ${BUILD_DATE}/" README.md
82+
83+
# success message
84+
echo -e "\n\n"
85+
echo -e "[Release] \033[0;32mDatabase successfully updated. Now you can verify difference, commit and add new release.\033[0m"
86+
echo -e "[Release] \033[0;32mDatabase version: ${VERSION} from ${BUILD_DATE}.\033[0m\n\n"
87+

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "sokil/php-isocodes-db-only",
3+
"license": "MIT",
4+
"description": "Database for ISO country, subdivision, language, currency and script definitions and their translations. Based on pythons pycountry and Debian's iso-codes.",
5+
"authors": [
6+
{
7+
"name": "Dmytro Sokil",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"require": {
12+
"php": ">=7.1",
13+
"ext-json": "*"
14+
},
15+
"require-dev": {
16+
"sokil/php-isocodes": " 4.0.x-dev",
17+
"phpunit/phpunit": "^9.5"
18+
},
19+
"scripts": {
20+
"test": "./vendor/bin/phpunit"
21+
}
22+
}

phpunit.xml.dist

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
5+
colors="true"
6+
bootstrap="tests/bootstrap.php"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
>
11+
<testsuites>
12+
<testsuite name="AllTests">
13+
<directory>tests/</directory>
14+
</testsuite>
15+
</testsuites>
16+
</phpunit>

tests/Database/CountriesTest.php

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sokil\IsoCodes\Database;
6+
7+
use Sokil\IsoCodes\Database\Countries\Country;
8+
use Sokil\IsoCodes\IsoCodesFactory;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class CountriesTest extends TestCase
12+
{
13+
public function testIterator(): void
14+
{
15+
$isoCodes = new IsoCodesFactory(BASE_DIRECTORY);
16+
$countries = $isoCodes->getCountries();
17+
foreach ($countries as $country) {
18+
$this->assertInstanceOf(
19+
Country::class,
20+
$country
21+
);
22+
}
23+
24+
$this->assertIsArray($countries->toArray());
25+
$this->assertGreaterThan(0, count($countries));
26+
}
27+
28+
public function testGetByAlpha2(): void
29+
{
30+
$isoCodes = new IsoCodesFactory(BASE_DIRECTORY);
31+
32+
$countries = $isoCodes->getCountries();
33+
$country = $countries->getByAlpha2('UA');
34+
35+
$this->assertInstanceOf(
36+
Country::class,
37+
$country
38+
);
39+
40+
$this->assertEquals(
41+
'Ukraine',
42+
$country->getName()
43+
);
44+
45+
$this->assertEquals(
46+
'Ukraine',
47+
$country->getLocalName()
48+
);
49+
50+
$this->assertEquals(
51+
'UA',
52+
$country->getAlpha2()
53+
);
54+
55+
$this->assertEquals(
56+
'UKR',
57+
$country->getAlpha3()
58+
);
59+
60+
$this->assertSame(
61+
'804',
62+
$country->getNumericCode()
63+
);
64+
65+
$this->assertEquals(
66+
null,
67+
$country->getOfficialName()
68+
);
69+
}
70+
71+
public function testGetByAlpha3(): void
72+
{
73+
$isoCodes = new IsoCodesFactory(BASE_DIRECTORY);
74+
75+
$countries = $isoCodes->getCountries();
76+
$country = $countries->getByAlpha3('UKR');
77+
78+
$this->assertInstanceOf(
79+
Country::class,
80+
$country
81+
);
82+
83+
$this->assertEquals(
84+
'Ukraine',
85+
$country->getName()
86+
);
87+
88+
$this->assertEquals(
89+
'Ukraine',
90+
$country->getLocalName()
91+
);
92+
}
93+
94+
public function getByNumericCodeDataProvider()
95+
{
96+
return [
97+
[
98+
804
99+
],
100+
[
101+
'804'
102+
],
103+
];
104+
}
105+
106+
/**
107+
* @dataProvider getByNumericCodeDataProvider
108+
*/
109+
public function testGetByNumericCode($code): void
110+
{
111+
$isoCodes = new IsoCodesFactory(BASE_DIRECTORY);
112+
113+
$countries = $isoCodes->getCountries();
114+
$country = $countries->getByNumericCode($code);
115+
116+
$this->assertInstanceOf(
117+
Country::class,
118+
$country
119+
);
120+
121+
$this->assertEquals(
122+
'Ukraine',
123+
$country->getName()
124+
);
125+
126+
$this->assertEquals(
127+
'Ukraine',
128+
$country->getLocalName()
129+
);
130+
}
131+
132+
public function testGetByNumericCodeNegative(): void
133+
{
134+
$this->expectException(\TypeError::class);
135+
136+
$isoCodes = new IsoCodesFactory(BASE_DIRECTORY);
137+
138+
$countries = $isoCodes->getCountries();
139+
$country = $countries->getByNumericCode('kek');
140+
}
141+
142+
public function testGetByNumericCodeLeadingZeroes(): void
143+
{
144+
$isoCodes = new IsoCodesFactory(BASE_DIRECTORY);
145+
146+
$countries = $isoCodes->getCountries();
147+
$country = $countries->getByNumericCode('036');
148+
149+
$this->assertInstanceOf(
150+
Country::class,
151+
$country
152+
);
153+
154+
$this->assertEquals(
155+
'Australia',
156+
$country->getName()
157+
);
158+
159+
$this->assertSame(
160+
'036',
161+
$country->getNumericCode()
162+
);
163+
}
164+
}

0 commit comments

Comments
 (0)