|
1 |
| -# phpunit-assertions |
| 1 | +# PHPUnit Assertions |
| 2 | + |
| 3 | +This package provides a set of common [PHPUnit](https://phpunit.de/) custom assertions. |
| 4 | +Some require optional packages - check the `composer.json[suggest]` section for more details. |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +```bash |
| 9 | +composer require --dev astrotomic/phpunit-assertions |
| 10 | +``` |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +Even if all assertions are in `trait`s I highly recommend you to don't `use` these traits in your test classes. |
| 15 | +Instead you can access all assertions as static methods on the traits. |
| 16 | + |
| 17 | +```php |
| 18 | +\Astrotomic\PhpunitAssertions\StringLengthAssertions::assertSame(10, 'Astrotomic'); |
| 19 | +``` |
| 20 | + |
| 21 | +This will prevent any method name conflicts with core, your custom or other trait assertions. |
| 22 | + |
| 23 | +## Assertions |
| 24 | + |
| 25 | +### Country |
| 26 | + |
| 27 | +`composer require --dev league/iso3166` |
| 28 | + |
| 29 | +```php |
| 30 | +\Astrotomic\PhpunitAssertions\CountryAssertions::assertCountry('DE', \League\ISO3166\ISO3166::KEY_ALPHA2); |
| 31 | +\Astrotomic\PhpunitAssertions\CountryAssertions::assertCountryName('Germany'); |
| 32 | +\Astrotomic\PhpunitAssertions\CountryAssertions::assertCountryAlpha2('DE'); |
| 33 | +\Astrotomic\PhpunitAssertions\CountryAssertions::assertCountryAlpha3('DEU'); |
| 34 | +\Astrotomic\PhpunitAssertions\CountryAssertions::assertCountryNumeric('276'); |
| 35 | +``` |
| 36 | + |
| 37 | +### Email |
| 38 | + |
| 39 | +`composer require --dev egulias/email-validator` |
| 40 | + |
| 41 | +```php |
| 42 | +\Astrotomic\PhpunitAssertions\EmailAssertions::assertValidLoose('gummibeer@astrotomic.info'); |
| 43 | +\Astrotomic\PhpunitAssertions\EmailAssertions::assertValidStrict('gummibeer@astrotomic.info'); |
| 44 | +\Astrotomic\PhpunitAssertions\EmailAssertions::assertDomain('astrotomic.info', 'gummibeer@astrotomic.info'); |
| 45 | +\Astrotomic\PhpunitAssertions\EmailAssertions::assertLocalPart('gummibeer', 'gummibeer@astrotomic.info'); |
| 46 | +``` |
| 47 | + |
| 48 | +### Geographic |
| 49 | + |
| 50 | +```php |
| 51 | +\Astrotomic\PhpunitAssertions\GeographicAssertions::assertLatitude(53.551085); |
| 52 | +\Astrotomic\PhpunitAssertions\GeographicAssertions::assertLongitude(9.993682); |
| 53 | +\Astrotomic\PhpunitAssertions\GeographicAssertions::assertCoordinates([ |
| 54 | + 'lat' => 53.551085, |
| 55 | + 'lng' => 9.993682, |
| 56 | +]); |
| 57 | +``` |
| 58 | + |
| 59 | +### HashID |
| 60 | + |
| 61 | +`composer require --dev hashids/hashids` |
| 62 | + |
| 63 | +```php |
| 64 | +\Astrotomic\PhpunitAssertions\HashidAssertions::assertHashIds('3kTMd', 2, 'this is my salt'); |
| 65 | +\Astrotomic\PhpunitAssertions\HashidAssertions::assertHashId('yr8', 'this is my salt'); |
| 66 | +``` |
| 67 | + |
| 68 | +### Nullable Type |
| 69 | + |
| 70 | +```php |
| 71 | +\Astrotomic\PhpunitAssertions\NullableTypeAssertions::assertIsNullableString('Astrotomic'); |
| 72 | +\Astrotomic\PhpunitAssertions\NullableTypeAssertions::assertIsNullableInt(42); |
| 73 | +\Astrotomic\PhpunitAssertions\NullableTypeAssertions::assertIsNullableFloat(42.5); |
| 74 | +\Astrotomic\PhpunitAssertions\NullableTypeAssertions::assertIsNullableArray(['Astrotomic' => 'Gummibeer']); |
| 75 | +\Astrotomic\PhpunitAssertions\NullableTypeAssertions::assertIsNullableBool(true); |
| 76 | +``` |
| 77 | + |
| 78 | +### Phone Number |
| 79 | + |
| 80 | +`composer require --dev giggsey/libphonenumber-for-php` |
| 81 | + |
| 82 | +```php |
| 83 | +\Astrotomic\PhpunitAssertions\PhoneNumberAssertions::assertE164('+498001110550'); |
| 84 | +\Astrotomic\PhpunitAssertions\PhoneNumberAssertions::assertValid('+49 800 - 111 0 550'); |
| 85 | +\Astrotomic\PhpunitAssertions\PhoneNumberAssertions::assertValidForRegion('+49 800 - 111 0 550', 'DE'); |
| 86 | +``` |
| 87 | + |
| 88 | +### String |
| 89 | + |
| 90 | +```php |
| 91 | +\Astrotomic\PhpunitAssertions\StringLengthAssertions::assertSame(10, 'Astrotomic'); |
| 92 | +\Astrotomic\PhpunitAssertions\StringLengthAssertions::assertNotSame(8, 'Astrotomic'); |
| 93 | +\Astrotomic\PhpunitAssertions\StringLengthAssertions::assertLessThan(11, 'Astrotomic'); |
| 94 | +\Astrotomic\PhpunitAssertions\StringLengthAssertions::assertLessThanOrEqual(10, 'Astrotomic'); |
| 95 | +\Astrotomic\PhpunitAssertions\StringLengthAssertions::assertGreaterThan(9, 'Astrotomic'); |
| 96 | +\Astrotomic\PhpunitAssertions\StringLengthAssertions::assertGreaterThanOrEqual(10, 'Astrotomic'); |
| 97 | +``` |
| 98 | + |
| 99 | +### URL |
| 100 | + |
| 101 | +```php |
| 102 | +\Astrotomic\PhpunitAssertions\UrlAssertions::assertValidLoose('https://astrotomic.info'); |
| 103 | +\Astrotomic\PhpunitAssertions\UrlAssertions::assertScheme('https', 'https://astrotomic.info'); |
| 104 | +\Astrotomic\PhpunitAssertions\UrlAssertions::assertHost('astrotomic.info', 'https://astrotomic.info'); |
| 105 | +\Astrotomic\PhpunitAssertions\UrlAssertions::assertPath('/contributor/gummibeer/', 'https://astrotomic.info/contributor/gummibeer/'); |
| 106 | +\Astrotomic\PhpunitAssertions\UrlAssertions::assertComponent('gummibeer', 'https://gummibeer@astrotomic.info', PHP_URL_USER); |
| 107 | +``` |
| 108 | + |
| 109 | +### UUID |
| 110 | + |
| 111 | +`composer require --dev ramsey/uuid` |
| 112 | + |
| 113 | +```php |
| 114 | +\Astrotomic\PhpunitAssertions\UuidAssertions::assertUuid('52d08e38-ad24-4960-af02-22e0f7e0db8d'); |
| 115 | +``` |
0 commit comments