Skip to content

Commit

Permalink
Merge branch 'main' of github.com:stefanzweifel/php-swiss-cantons
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanzweifel committed Apr 16, 2023
2 parents b6281fd + 51d907b commit 0bda6ee
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 59 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/format_php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ jobs:
with:
ref: ${{ github.head_ref }}

- name: Install
run: composer install

- name: Run php-cs-fixer
uses: docker://oskarstark/php-cs-fixer-ga

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [7.4, 8.0, 8.1, 8.2]
php: [8.2]
stability: [prefer-lowest, prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Using Javascript? Check out [@stefanzweifel/js-swiss-cantons](https://github.com

## Installation

The easiest way to install the package is by using composer. The package requires PHP 7.4.
The easiest way to install the package is by using composer. The package requires PHP 8.2.

```shell
composer require "wnx/php-swiss-cantons"
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
}
],
"require": {
"php": ">=7.4.0 || ^8.0",
"php": "^8.2",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^9 | ^10",
"phpunit/phpunit": "^10.1",
"friendsofphp/php-cs-fixer": "^3",
"vimeo/psalm": "^5.0",
"league/csv": "^9.7",
"symfony/console": "^5.0 | ^6.0",
"symfony/console": "^6.0",
"rector/rector": "^0.15.24"
},
"autoload": {
Expand Down
6 changes: 4 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
Expand All @@ -17,6 +18,7 @@

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_74
]);
LevelSetList::UP_TO_PHP_82,
PHPUnitSetList::PHPUNIT_100,
]);
};
12 changes: 6 additions & 6 deletions src/Canton.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

class Canton
{
public const LANG_GERMAN = 'de';
final public const LANG_GERMAN = 'de';

public const LANG_FRENCH = 'fr';
final public const LANG_FRENCH = 'fr';

public const LANG_ITALIAN = 'it';
final public const LANG_ITALIAN = 'it';

public const LANG_ENGLISH = 'en';
final public const LANG_ENGLISH = 'en';

public const LANG_ROMANSH = 'rm';
final public const LANG_ROMANSH = 'rm';

public const AVAILABLE_LANGUAGES = [
final public const AVAILABLE_LANGUAGES = [
self::LANG_GERMAN,
self::LANG_FRENCH,
self::LANG_ITALIAN,
Expand Down
8 changes: 2 additions & 6 deletions src/CantonSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ public function __construct()

public function findByAbbreviation(string $abbreviation): ?Canton
{
$result = array_filter($this->dataSet, function (Canton $canton) use ($abbreviation) {
return $canton->getAbbreviation() === strtoupper($abbreviation);
});
$result = array_filter($this->dataSet, fn (Canton $canton) => $canton->getAbbreviation() === strtoupper($abbreviation));

if (count($result) === 0) {
return null;
Expand All @@ -26,9 +24,7 @@ public function findByAbbreviation(string $abbreviation): ?Canton

public function findByName(string $name): ?Canton
{
$result = array_filter($this->dataSet, function (Canton $canton) use ($name) {
return in_array($name, $canton->getNamesArray());
});
$result = array_filter($this->dataSet, fn (Canton $canton) => in_array($name, $canton->getNamesArray()));

if (count($result) === 0) {
return null;
Expand Down
6 changes: 2 additions & 4 deletions src/Cantons.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Cantons

public function __construct()
{
$this->cantons = json_decode(file_get_contents(__DIR__.'/data/cantons.json'), true);
$this->cantons = json_decode(file_get_contents(__DIR__.'/data/cantons.json'), true, 512, JSON_THROW_ON_ERROR);
}

/**
Expand All @@ -18,9 +18,7 @@ public function __construct()
*/
public function getAll(): array
{
return array_map(function ($canton) {
return new Canton($canton);
}, $this->cantons);
return array_map(fn ($canton) => new Canton($canton), $this->cantons);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Console/UpdateZipcodeDatasetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

class UpdateZipcodeDatasetCommand extends Command
{
public const PATH_TO_CSV = __DIR__ . '/../data/zipcodes.csv';
public const PATH_TO_JSON = __DIR__ . '/../data/zipcodes.json';
final public const PATH_TO_CSV = __DIR__ . '/../data/zipcodes.csv';
final public const PATH_TO_JSON = __DIR__ . '/../data/zipcodes.json';

protected function configure(): void
{
Expand Down
6 changes: 2 additions & 4 deletions src/ZipcodeSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ public function __construct()
*/
public function findbyZipcode(int $zipcode): ?array
{
$result = array_filter($this->dataSet, function (array $city) use ($zipcode) {
return $city['zipcode'] === intval($zipcode);
});
$result = array_filter($this->dataSet, fn (array $city) => $city['zipcode'] === intval($zipcode));

if (count($result) === 0) {
return null;
Expand All @@ -29,6 +27,6 @@ public function findbyZipcode(int $zipcode): ?array

public function getDataSet(): array
{
return json_decode(file_get_contents(__DIR__.'/data/zipcodes.json'), true);
return json_decode(file_get_contents(__DIR__.'/data/zipcodes.json'), true, 512, JSON_THROW_ON_ERROR);
}
}
17 changes: 9 additions & 8 deletions tests/CantonManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Wnx\SwissCantons\Tests;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Wnx\SwissCantons\CantonManager;
use Wnx\SwissCantons\Exceptions\CantonException;

class CantonManagerTest extends TestCase
{
/** @test */
#[Test]
public function it_returns_correct_canton_instance_for_abbreviation()
{
$canton = new CantonManager();
Expand All @@ -20,7 +21,7 @@ public function it_returns_correct_canton_instance_for_abbreviation()
);
}

/** @test */
#[Test]
public function it_returns_correct_canton_if_abbreviation_is_lowercase()
{
$cantonManager = new CantonManager();
Expand All @@ -44,7 +45,7 @@ public function it_returns_correct_canton_if_abbreviation_is_lowercase()
);
}

/** @test */
#[Test]
public function it_throws_exception_if_no_canton_for_abbreviation_is_found()
{
$this->expectException(CantonException::class);
Expand All @@ -53,7 +54,7 @@ public function it_throws_exception_if_no_canton_for_abbreviation_is_found()
$result = $canton->getByAbbreviation('FOO');
}

/** @test */
#[Test]
public function it_returns_correct_canton_instance_for_name()
{
$canton = new CantonManager();
Expand All @@ -62,7 +63,7 @@ public function it_returns_correct_canton_instance_for_name()
$this->assertEquals('ZH', $result->getAbbreviation());
}

/** @test */
#[Test]
public function it_throws_exception_if_not_canton_for_name_is_found()
{
$this->expectException(CantonException::class);
Expand All @@ -71,7 +72,7 @@ public function it_throws_exception_if_not_canton_for_name_is_found()
$result = $canton->getByName('FOO');
}

/** @test */
#[Test]
public function it_returns_canton_for_zipcode()
{
$canton = new CantonManager();
Expand All @@ -82,7 +83,7 @@ public function it_returns_canton_for_zipcode()
$this->assertEquals('Berne', $result->setLanguage('en')->getName());
}

/** @test */
#[Test]
public function it_throws_exception_if_no_canton_for_zipcode_could_be_found()
{
$this->expectException(CantonException::class);
Expand All @@ -91,7 +92,7 @@ public function it_throws_exception_if_no_canton_for_zipcode_could_be_found()
$result = $canton->getByZipcode(9999);
}

/** @test */
#[Test]
public function it_throws_exception_if_lichtenstein_zipcode_is_searched_for()
{
$this->expectException(CantonException::class);
Expand Down
9 changes: 5 additions & 4 deletions tests/CantonSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Wnx\SwissCantons\Tests;

use PHPUnit\Framework\Attributes\Test;
use Wnx\SwissCantons\Canton;
use PHPUnit\Framework\TestCase;
use Wnx\SwissCantons\CantonSearch;

class CantonSearchTest extends TestCase
{
/** @test */
#[Test]
public function it_finds_canton_by_abbreviation()
{
$cantonSearch = new CantonSearch();
Expand All @@ -18,7 +19,7 @@ public function it_finds_canton_by_abbreviation()
$this->assertEquals('SH', $canton->getAbbreviation());
}

/** @test */
#[Test]
public function it_returns_null_if_no_canton_for_abbreviation_was_found()
{
$cantonSearch = new CantonSearch();
Expand All @@ -27,7 +28,7 @@ public function it_returns_null_if_no_canton_for_abbreviation_was_found()
$this->assertNull($canton);
}

/** @test */
#[Test]
public function it_finds_canton_by_name()
{
$cantonSearch = new CantonSearch();
Expand All @@ -38,7 +39,7 @@ public function it_finds_canton_by_name()
$this->assertEquals('Zürich', $canton->getNamesArray()['de']);
}

/** @test */
#[Test]
public function it_returns_null_if_no_canton_for_name_was_found()
{
$cantonSearch = new CantonSearch();
Expand Down
13 changes: 7 additions & 6 deletions tests/CantonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wnx\SwissCantons\Tests;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Wnx\SwissCantons\Canton;
use Wnx\SwissCantons\Exceptions\InvalidLanguageException;
Expand All @@ -22,7 +23,7 @@ protected function getExampleCanton(): array
];
}

/** @test */
#[Test]
public function it_sets_language()
{
$canton = new Canton($this->getExampleCanton());
Expand All @@ -31,7 +32,7 @@ public function it_sets_language()
$this->assertEquals('fr', $canton->getLanguage());
}

/** @test */
#[Test]
public function it_transformers_uppercase_language_string_to_lowercase()
{
$canton = new Canton($this->getExampleCanton());
Expand All @@ -40,7 +41,7 @@ public function it_transformers_uppercase_language_string_to_lowercase()
$this->assertEquals('de', $canton->getLanguage());
}

/** @test */
#[Test]
public function it_only_allows_national_languages()
{
$this->expectException(InvalidLanguageException::class);
Expand All @@ -49,15 +50,15 @@ public function it_only_allows_national_languages()
$canton->setLanguage('es');
}

/** @test */
#[Test]
public function it_sets_and_returns_abbreviation()
{
$canton = new Canton($this->getExampleCanton());

$this->assertEquals('ZH', $canton->getAbbreviation());
}

/** @test */
#[Test]
public function it_sets_names_array()
{
$canton = new Canton($this->getExampleCanton());
Expand All @@ -68,7 +69,7 @@ public function it_sets_names_array()
);
}

/** @test */
#[Test]
public function it_returns_correct_name_for_given_language()
{
$canton = new Canton($this->getExampleCanton());
Expand Down
11 changes: 6 additions & 5 deletions tests/CantonsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
namespace Wnx\SwissCantons\Tests;

use Exception;
use PHPUnit\Framework\Attributes\Test;
use Wnx\SwissCantons\Cantons;
use PHPUnit\Framework\TestCase;

class CantonsTest extends TestCase
{
/** @test */
#[Test]
public function it_returns_json_source_as_array()
{
$cantons = new Cantons();

$this->assertTrue(is_array($cantons->getAll()));
}

/** @test */
#[Test]
public function it_contains_abbreviation_and_name()
{
$cantons = (new Cantons())->getAll();
Expand All @@ -29,7 +30,7 @@ public function it_contains_abbreviation_and_name()
$this->assertArrayHasKey('en', $cantons[0]->getNamesArray());
}

/** @test */
#[Test]
public function it_returns_an_array_with_abbreviation_as_key_and_name_as_value()
{
$cantons = (new Cantons())->getAllAsArray();
Expand All @@ -41,7 +42,7 @@ public function it_returns_an_array_with_abbreviation_as_key_and_name_as_value()
$this->assertEquals('Zurich', $cantons['ZH']);
}

/** @test */
#[Test]
public function it_returns_an_array_with_abbreviation_and_name_but_in_a_different_language()
{
$cantons = (new Cantons())->getAllAsArray('de');
Expand All @@ -53,7 +54,7 @@ public function it_returns_an_array_with_abbreviation_and_name_but_in_a_differen
$this->assertEquals('Zürich', $cantons['ZH']);
}

/** @test */
#[Test]
public function it_throws_an_exception_if_passed_langauge_is_not_available()
{
$this->expectException(Exception::class);
Expand Down
Loading

0 comments on commit 0bda6ee

Please sign in to comment.