Skip to content

Commit c89ff38

Browse files
committed
add LanguageAssertions
normalize CountryAssertions method naming
1 parent ee9a6c2 commit c89ff38

File tree

6 files changed

+87
-15
lines changed

6 files changed

+87
-15
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ This will prevent any method name conflicts with core, your custom or other trai
2727
`composer require --dev league/iso3166:^3.0`
2828

2929
```php
30-
\Astrotomic\PhpunitAssertions\CountryAssertions::assertCountryName('Germany');
31-
\Astrotomic\PhpunitAssertions\CountryAssertions::assertCountryAlpha2('DE');
32-
\Astrotomic\PhpunitAssertions\CountryAssertions::assertCountryAlpha3('DEU');
33-
\Astrotomic\PhpunitAssertions\CountryAssertions::assertCountryNumeric('276');
30+
\Astrotomic\PhpunitAssertions\CountryAssertions::assertName('Germany');
31+
\Astrotomic\PhpunitAssertions\CountryAssertions::assertAlpha2('DE');
32+
\Astrotomic\PhpunitAssertions\CountryAssertions::assertAlpha3('DEU');
33+
\Astrotomic\PhpunitAssertions\CountryAssertions::assertNumeric('276');
3434
```
3535

3636
### Email
@@ -64,6 +64,15 @@ This will prevent any method name conflicts with core, your custom or other trai
6464
\Astrotomic\PhpunitAssertions\HashidAssertions::assertHashId('yr8', 'this is my salt');
6565
```
6666

67+
### Language
68+
69+
`composer require --dev astrotomic/iso639:^1.0`
70+
71+
```php
72+
\Astrotomic\PhpunitAssertions\LanguageAssertions::assertName('German');
73+
\Astrotomic\PhpunitAssertions\LanguageAssertions::assertAlpha2('de');
74+
```
75+
6776
### Nullable Type
6877

6978
```php

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"phpunit/phpunit": "^9.1"
2121
},
2222
"require-dev": {
23+
"astrotomic/iso639": "^1.0",
2324
"egulias/email-validator": "^2.1 || ^3.0",
2425
"gajus/dindent": "^2.0",
2526
"giggsey/libphonenumber-for-php": "^8.12",
@@ -30,6 +31,7 @@
3031
"vinkla/hashids": "^9.0"
3132
},
3233
"suggest": {
34+
"astrotomic/iso639": "\\Astrotomic\\PhpunitAssertions\\LanguageAssertions (^1.0)",
3335
"egulias/email-validator": "\\Astrotomic\\PhpunitAssertions\\EmailAssertions (^2.1 || ^3.0)",
3436
"gajus/dindent": "\\Astrotomic\\PhpunitAssertions\\Laravel\\BladeAssertions (^2.0)",
3537
"giggsey/libphonenumber-for-php": "\\Astrotomic\\PhpunitAssertions\\PhoneNumberAssertions (^8.12)",

src/CountryAssertions.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,33 @@
77

88
trait CountryAssertions
99
{
10-
public static function assertCountryName($actual): void
10+
public static function assertName($actual): void
1111
{
1212
PHPUnit::assertIsString($actual);
1313
$country = (new ISO3166)->name($actual);
1414
PHPUnit::assertIsArray($country);
1515
}
1616

17-
public static function assertCountryAlpha2($actual): void
17+
public static function assertAlpha2($actual): void
1818
{
1919
PHPUnit::assertIsString($actual);
20-
PHPUnit::assertSame(2, strlen($actual));
20+
StringLengthAssertions::assertSame(2, $actual);
2121
$country = (new ISO3166)->alpha2($actual);
2222
PHPUnit::assertIsArray($country);
2323
}
2424

25-
public static function assertCountryAlpha3($actual): void
25+
public static function assertAlpha3($actual): void
2626
{
2727
PHPUnit::assertIsString($actual);
28-
PHPUnit::assertSame(3, strlen($actual));
28+
StringLengthAssertions::assertSame(3, $actual);
2929
$country = (new ISO3166)->alpha3($actual);
3030
PHPUnit::assertIsArray($country);
3131
}
3232

33-
public static function assertCountryNumeric($actual): void
33+
public static function assertNumeric($actual): void
3434
{
3535
PHPUnit::assertIsString($actual);
36-
PHPUnit::assertSame(3, strlen($actual));
36+
StringLengthAssertions::assertSame(3, $actual);
3737
$country = (new ISO3166)->numeric($actual);
3838
PHPUnit::assertIsArray($country);
3939
}

src/LanguageAssertions.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Astrotomic\PhpunitAssertions;
4+
5+
use Astrotomic\ISO639\ISO639;
6+
use PHPUnit\Framework\Assert as PHPUnit;
7+
8+
trait LanguageAssertions
9+
{
10+
public static function assertName($actual): void
11+
{
12+
PHPUnit::assertIsString($actual);
13+
$language = (new ISO639())->name($actual);
14+
PHPUnit::assertIsArray($language);
15+
}
16+
17+
public static function assertAlpha2($actual): void
18+
{
19+
PHPUnit::assertIsString($actual);
20+
StringLengthAssertions::assertSame(2, $actual);
21+
$language = (new ISO639)->alpha2($actual);
22+
PHPUnit::assertIsArray($language);
23+
}
24+
}

tests/CountryAssertionsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class CountryAssertionsTest extends TestCase
1313
*/
1414
public static function it_can_validate_country_by_name(string $actual): void
1515
{
16-
CountryAssertions::assertCountryName($actual);
16+
CountryAssertions::assertName($actual);
1717
}
1818

1919
/**
@@ -22,7 +22,7 @@ public static function it_can_validate_country_by_name(string $actual): void
2222
*/
2323
public static function it_can_validate_country_by_alpha2(string $actual): void
2424
{
25-
CountryAssertions::assertCountryAlpha2($actual);
25+
CountryAssertions::assertAlpha2($actual);
2626
}
2727

2828
/**
@@ -31,7 +31,7 @@ public static function it_can_validate_country_by_alpha2(string $actual): void
3131
*/
3232
public static function it_can_validate_country_by_alpha3(string $actual): void
3333
{
34-
CountryAssertions::assertCountryAlpha3($actual);
34+
CountryAssertions::assertAlpha3($actual);
3535
}
3636

3737
/**
@@ -40,7 +40,7 @@ public static function it_can_validate_country_by_alpha3(string $actual): void
4040
*/
4141
public static function it_can_validate_country_by_numeric(string $actual): void
4242
{
43-
CountryAssertions::assertCountryNumeric($actual);
43+
CountryAssertions::assertNumeric($actual);
4444
}
4545

4646
public function countryName(): iterable

tests/LanguageAssertionsTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Astrotomic\PhpunitAssertions\Tests;
4+
5+
use Astrotomic\ISO639\ISO639;
6+
use Astrotomic\PhpunitAssertions\LanguageAssertions;
7+
8+
final class LanguageAssertionsTest extends TestCase
9+
{
10+
/**
11+
* @test
12+
* @dataProvider languageName
13+
*/
14+
public static function it_can_validate_language_by_name(string $actual): void
15+
{
16+
LanguageAssertions::assertName($actual);
17+
}
18+
19+
/**
20+
* @test
21+
* @dataProvider languageAlpha2
22+
*/
23+
public static function it_can_validate_language_by_alpha2(string $actual): void
24+
{
25+
LanguageAssertions::assertAlpha2($actual);
26+
}
27+
28+
public function languageName(): iterable
29+
{
30+
return array_map(fn (array $country): array => [$country[ISO639::KEY_NAME]], (new ISO639())->all());
31+
}
32+
33+
public function languageAlpha2(): iterable
34+
{
35+
return array_map(fn (array $country): array => [$country[ISO639::KEY_639_1]], (new ISO639())->all());
36+
}
37+
}

0 commit comments

Comments
 (0)