Skip to content

Commit

Permalink
More coverage (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlocati authored Jun 30, 2024
1 parent 307703f commit 8eada0d
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 25 deletions.
12 changes: 0 additions & 12 deletions src/Build/Province.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,6 @@ public function checkMunicipalityData(stdClass $data): void
}
}

public function checkFlatEntry(FlatMunicipality $flatMunicipality): void
{
if ($this->id !== $flatMunicipality->utsID
|| $this->oldID !== $flatMunicipality->utsOldID
|| $this->vehicleCode !== $flatMunicipality->utsVehicleCode
|| $this->type !== $flatMunicipality->utsType
|| $this->name !== $flatMunicipality->utsName
) {
throw new RuntimeException('Incompatible FlatMunicipality for ' . __CLASS__);
}
}

public function serialize(Collator $collator): array
{
$result = [
Expand Down
37 changes: 29 additions & 8 deletions src/Service/SorterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ trait SorterTrait
*/
protected function sortTerritoriesByName(array $territories): array
{
if (class_exists(Collator::class)) {
return $this->sortTerritoriesByNameWithCollator($territories);
}

return $this->sortTerritoriesByNameWithoutCollator($territories);
return class_exists(Collator::class) ? $this->sortTerritoriesByNameWithCollator($territories) : $this->sortTerritoriesByNameWithoutCollator($territories);
}

/**
Expand All @@ -34,7 +30,7 @@ protected function sortTerritoriesByNameWithCollator(array $territories): array
$collator->setStrength(Collator::SECONDARY);

usort($territories, static function (Territory $a, Territory $b) use ($collator): int {
return $collator->compare($a->getName(), $b->getName());
return $collator->compare((string) $a, (string) $b);
});

return $territories;
Expand All @@ -47,10 +43,35 @@ protected function sortTerritoriesByNameWithCollator(array $territories): array
*/
protected function sortTerritoriesByNameWithoutCollator(array $territories): array
{
usort($territories, static function (Territory $a, Territory $b): int {
return strcasecmp($a->getName(), $b->getName());
$map = static::getMultibyteToAsciiMap();
usort($territories, static function (Territory $a, Territory $b) use ($map): int {
return strcasecmp(
strtr((string) $a, $map),
strtr((string) $b, $map)
);
});

return $territories;
}

protected static function getMultibyteToAsciiMap(): array
{
return [
"\u{e0}" => 'a',
"\u{e2}" => 'a',
"\u{e7}" => 'c',
"\u{10d}" => 'c',
"\u{e8}" => 'e',
"\u{e9}" => 'e',
"\u{ea}" => 'e',
"\u{ec}" => 'i',
"\u{f2}" => 'o',
"\u{f6}" => 'o',
"\u{f4}" => 'o',
"\u{f9}" => 'u',
"\u{fc}" => 'u',
"\u{df}" => 'ss',
"\u{17e}" => 'z',
];
}
}
23 changes: 18 additions & 5 deletions test/tests/PropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,31 @@
use MLocati\ComuniItaliani\Region;
use MLocati\ComuniItaliani\Province;
use MLocati\ComuniItaliani\GeographicalSubdivision;
use MLocati\ComuniItaliani\Service\SorterTrait;

class PropertiesTest extends TerritoryTestCase
{
private const RX_VALID_NAMES = '{^\p{L}[\p{L} \-\/\'.]*\p{L}\'?$}u';
use SorterTrait;

private static string $rxValidNames;

public static function setUpBeforeClass(): void
{
$mbChars = '';
foreach (array_keys(self::getMultibyteToAsciiMap()) as $mbChar) {
$mbChars .= $mbChar;
}
$letter = "A-Za-z{$mbChars}";
self::$rxValidNames = "_^[{$letter}][{$letter} \-\/'.]*[$letter]'?\$_";
}

/**
* @dataProvider provideGeographicalSubdivisions
*/
public function testGeographicalSubdivisionProperties(GeographicalSubdivision $geographicalSubdivision): void
{
$this->assertGreaterThanOrEqual(1, $geographicalSubdivision->getID());
$this->assertMatchesRegularExpression(self::RX_VALID_NAMES, $geographicalSubdivision->getName());
$this->assertMatchesRegularExpression(self::$rxValidNames, $geographicalSubdivision->getName());
$this->assertSame($geographicalSubdivision->getName(), (string) $geographicalSubdivision);
$this->assertMatchesRegularExpression('/^IT[A-Z]$/', $geographicalSubdivision->getNuts1());
}
Expand All @@ -35,7 +48,7 @@ public function testRegionProperties(Region $region): void
{
$this->assertMatchesRegularExpression('/^[0-9]{2}$/', $region->getID());
$this->assertNotSame('00', $region->getID());
$this->assertMatchesRegularExpression(self::RX_VALID_NAMES, $region->getName());
$this->assertMatchesRegularExpression(self::$rxValidNames, $region->getName());
$this->assertSame($region->getName(), (string) $region);
$this->assertContains($region->getType(), [
Region\Type::ORDINARY_STATUS,
Expand All @@ -60,7 +73,7 @@ public function testProvinceProperties(Province $province): void
{
$this->assertMatchesRegularExpression('/^[0-9]{3}$/', $province->getID());
$this->assertNotSame('000', $province->getID());
$this->assertMatchesRegularExpression(self::RX_VALID_NAMES, $province->getName());
$this->assertMatchesRegularExpression(self::$rxValidNames, $province->getName());
$this->assertSame($province->getName(), (string) $province);
$this->assertMatchesRegularExpression('/^[0-9]{3}$/', $province->getOldID());
$this->assertNotSame('000', $province->getOldID());
Expand All @@ -85,7 +98,7 @@ public function testMunicipalityProperties(Municipality $municipality): void
$this->assertMatchesRegularExpression('/^[0-9]{6}$/', $municipality->getID());
$this->assertNotSame('000000', $municipality->getID());
$this->assertStringStartsWith($municipality->getParent()->getOldID(), $municipality->getID());
$this->assertMatchesRegularExpression(self::RX_VALID_NAMES, $municipality->getName());
$this->assertMatchesRegularExpression(self::$rxValidNames, $municipality->getName());
$this->assertSame("{$municipality->getName()} ({$municipality->getParent()->getVehicleCode()})", (string) $municipality);
$nameIT = $municipality->getNameIT();
if ($nameIT === '') {
Expand Down
49 changes: 49 additions & 0 deletions test/tests/SingletonsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use MLocati\ComuniItaliani\TerritoryWithChildren;
use MLocati\ComuniItaliani\Region;
use MLocati\ComuniItaliani\Province;
use MLocati\ComuniItaliani\GeographicalSubdivision;

class SingletonsTest extends TerritoryTestCase
{
Expand All @@ -32,4 +33,52 @@ public function testSingletons(bool $unminified): void
$geographicalSubdivisions = $factory->getGeographicalSubdivisions();
$this->assertSame($geographicalSubdivisions, $factory->getGeographicalSubdivisions());
}

/**
* @dataProvider provideGeographicalSubdivisions
*/
public function testGeographicalSubdivision(GeographicalSubdivision $geographicalSubdivision): void
{
$this->assertNull($geographicalSubdivision->getParent());
$children = $geographicalSubdivision->getChildren();
$this->assertSame($children, $geographicalSubdivision->getRegions());
foreach ($children as $child) {
$this->assertSame($geographicalSubdivision, $child->getParent());
}
}

/**
* @dataProvider provideRegions
*/
public function testRegion(Region $region): void
{
$this->assertInstanceOf(GeographicalSubdivision::class, $region->getParent());
$children = $region->getChildren();
$this->assertSame($children, $region->getProvinces());
foreach ($children as $child) {
$this->assertSame($region, $child->getParent());
}
}

/**
* @dataProvider provideProvinces
*/
public function testProvince(Province $province): void
{
$this->assertInstanceOf(Region::class, $province->getParent());
$children = $province->getChildren();
$this->assertSame($children, $province->getMunicipalities());
foreach ($children as $child) {
$this->assertSame($province, $child->getParent());
}
}

/**
* @dataProvider provideMunicipalities
*/
public function testMunicipality(Municipality $municipality): void
{
$this->assertInstanceOf(Province::class, $municipality->getParent());
$this->assertNotInstanceOf(TerritoryWithChildren::class, $municipality);
}
}
56 changes: 56 additions & 0 deletions test/tests/SortTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace MLocati\ComuniItaliani\Test;

use MLocati\ComuniItaliani\Test\Service\TerritoryTestCase;
use PHPUnit\Framework\TestCase;
use MLocati\ComuniItaliani\Factory;
use MLocati\ComuniItaliani\Municipality;
use MLocati\ComuniItaliani\TerritoryWithChildren;
use MLocati\ComuniItaliani\Region;
use MLocati\ComuniItaliani\Province;
use MLocati\ComuniItaliani\GeographicalSubdivision;
use MLocati\ComuniItaliani\Service\SorterTrait;

class SortTest extends TerritoryTestCase
{
use SorterTrait;

public function testRegions(): void
{
$factory = new Factory();
$this->testSort($factory->getRegions());
}

public function testProvinces(): void
{
$factory = new Factory();
$this->testSort($factory->getProvinces());
}

public function testMunicipalities(): void
{
$factory = new Factory();
$this->testSort($factory->getMunicipalities());
}

/**
* @param \MLocati\ComuniItaliani\Territory[]
*/
private function testSort(array $territories): void
{
$expectedNames = array_map('strval', $territories);
$reversed = array_reverse($territories, false);
$this->assertNotSame($territories, $reversed);
$actualNames = array_map('strval', array_reverse($reversed, false));
$this->assertSame($expectedNames, $actualNames);
$actualNames = array_map('strval', $this->sortTerritoriesByName($reversed));
$this->assertSame($expectedNames, $actualNames);
$actualNames = array_map('strval', $this->sortTerritoriesByNameWithCollator($reversed));
$this->assertSame($expectedNames, $actualNames);
$actualNames = array_map('strval', $this->sortTerritoriesByNameWithoutCollator($reversed));
$this->assertSame($expectedNames, $actualNames);
}
}

0 comments on commit 8eada0d

Please sign in to comment.