├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── src ├── ArrayAssertions.php ├── CountryAssertions.php ├── EmailAssertions.php ├── GeographicAssertions.php ├── HashidAssertions.php ├── LanguageAssertions.php ├── Laravel ├── BladeAssertions.php ├── CollectionAssertions.php ├── HashidAssertions.php └── ModelAssertions.php ├── NullableTypeAssertions.php ├── PathAssertions.php ├── PhoneNumberAssertions.php ├── StringLengthAssertions.php ├── UrlAssertions.php └── UuidAssertions.php /.gitattributes: -------------------------------------------------------------------------------- 1 | /.github export-ignore 2 | /.styleci.yml export-ignore 3 | /phpunit.xml.dist export-ignore 4 | /tests export-ignore -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | /composer.lock 3 | /.phpunit.result.cache -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Astrotomic 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHPUnit Assertions 2 | 3 | [](https://packagist.org/packages/astrotomic/phpunit-assertions) 4 | [](https://github.com/Astrotomic/phpunit-assertions/blob/master/LICENSE) 5 | [](https://forest.astrotomic.info) 6 | [](https://larabelles.com) 7 | 8 | [](https://github.com/Astrotomic/phpunit-assertions/actions?query=workflow%3Aphpunit) 9 | [](https://packagist.org/packages/astrotomic/phpunit-assertions) 10 | [](https://forest.astrotomic.info) 11 | [](https://forest.astrotomic.info) 12 | 13 | This package provides a set of common [PHPUnit](https://phpunit.de/) custom assertions. 14 | Some require optional packages - check the `composer.json[suggest]` section for more details. 15 | 16 | ## Installation 17 | 18 | ```bash 19 | composer require --dev astrotomic/phpunit-assertions 20 | ``` 21 | 22 | ## Usage 23 | 24 | Even if all assertions are in `trait`s I highly recommend you to don't `use` these traits in your test classes. 25 | Instead you can access all assertions as static methods on the traits. 26 | 27 | ```php 28 | \Astrotomic\PhpunitAssertions\StringLengthAssertions::assertSame(10, 'Astrotomic'); 29 | ``` 30 | 31 | This will prevent any method name conflicts with core, your custom or other trait assertions. 32 | 33 | ## Assertions 34 | 35 | ### Array 36 | 37 | ```php 38 | \Astrotomic\PhpunitAssertions\ArrayAssertions::assertIndexed(['foo', 'bar']); 39 | \Astrotomic\PhpunitAssertions\ArrayAssertions::assertAssociative(['foo' => 'bar']); 40 | \Astrotomic\PhpunitAssertions\ArrayAssertions::assertEquals(['foo', 'bar'], ['bar', 'foo']); 41 | \Astrotomic\PhpunitAssertions\ArrayAssertions::assertSubset(['foo' => 'bar'], ['baz' => 'foo', 'foo' => 'bar']); 42 | \Astrotomic\PhpunitAssertions\ArrayAssertions::assertContainsAll(['foo', 'bar'], ['baz', 'foo', 'lorem', 'ipsum', 'bar']); 43 | ``` 44 | 45 | ### Country 46 | 47 | `composer require --dev league/iso3166:^3.0` 48 | 49 | ```php 50 | \Astrotomic\PhpunitAssertions\CountryAssertions::assertName('Germany'); 51 | \Astrotomic\PhpunitAssertions\CountryAssertions::assertAlpha2('DE'); 52 | \Astrotomic\PhpunitAssertions\CountryAssertions::assertAlpha3('DEU'); 53 | \Astrotomic\PhpunitAssertions\CountryAssertions::assertNumeric('276'); 54 | ``` 55 | 56 | ### Email 57 | 58 | `composer require --dev egulias/email-validator:^3.0` 59 | 60 | ```php 61 | \Astrotomic\PhpunitAssertions\EmailAssertions::assertValidLoose('gummibeer@astrotomic.info'); 62 | \Astrotomic\PhpunitAssertions\EmailAssertions::assertValidStrict('gummibeer@astrotomic.info'); 63 | \Astrotomic\PhpunitAssertions\EmailAssertions::assertDomain('astrotomic.info', 'gummibeer@astrotomic.info'); 64 | \Astrotomic\PhpunitAssertions\EmailAssertions::assertLocalPart('gummibeer', 'gummibeer@astrotomic.info'); 65 | \Astrotomic\PhpunitAssertions\EmailAssertions::assertPlusMailbox('gummibeer', 'gummibeer+news@astrotomic.info'); 66 | \Astrotomic\PhpunitAssertions\EmailAssertions::assertPlusAlias('news', 'gummibeer+news@astrotomic.info'); 67 | ``` 68 | 69 | ### Geographic 70 | 71 | ```php 72 | \Astrotomic\PhpunitAssertions\GeographicAssertions::assertLatitude(53.551085); 73 | \Astrotomic\PhpunitAssertions\GeographicAssertions::assertLongitude(9.993682); 74 | \Astrotomic\PhpunitAssertions\GeographicAssertions::assertCoordinates([ 75 | 'lat' => 53.551085, 76 | 'lng' => 9.993682, 77 | ]); 78 | ``` 79 | 80 | ### HashID 81 | 82 | `composer require --dev hashids/hashids:^4.0` 83 | 84 | ```php 85 | \Astrotomic\PhpunitAssertions\HashidAssertions::assertHashIds('3kTMd', 2, 'this is my salt'); 86 | \Astrotomic\PhpunitAssertions\HashidAssertions::assertHashId('yr8', 'this is my salt'); 87 | ``` 88 | 89 | ### Language 90 | 91 | `composer require --dev astrotomic/iso639:^1.0` 92 | 93 | ```php 94 | \Astrotomic\PhpunitAssertions\LanguageAssertions::assertName('German'); 95 | \Astrotomic\PhpunitAssertions\LanguageAssertions::assertAlpha2('de'); 96 | ``` 97 | 98 | ### Nullable Type 99 | 100 | ```php 101 | \Astrotomic\PhpunitAssertions\NullableTypeAssertions::assertIsNullableString('Astrotomic'); 102 | \Astrotomic\PhpunitAssertions\NullableTypeAssertions::assertIsNullableInt(42); 103 | \Astrotomic\PhpunitAssertions\NullableTypeAssertions::assertIsNullableFloat(42.5); 104 | \Astrotomic\PhpunitAssertions\NullableTypeAssertions::assertIsNullableArray(['Astrotomic' => 'Gummibeer']); 105 | \Astrotomic\PhpunitAssertions\NullableTypeAssertions::assertIsNullableBool(true); 106 | ``` 107 | 108 | ### Phone Number 109 | 110 | `composer require --dev giggsey/libphonenumber-for-php:^8.12` 111 | 112 | ```php 113 | \Astrotomic\PhpunitAssertions\PhoneNumberAssertions::assertE164('+498001110550'); 114 | \Astrotomic\PhpunitAssertions\PhoneNumberAssertions::assertValid('+49 800 - 111 0 550'); 115 | \Astrotomic\PhpunitAssertions\PhoneNumberAssertions::assertValidForRegion('+49 800 - 111 0 550', 'DE'); 116 | ``` 117 | 118 | ### String 119 | 120 | ```php 121 | \Astrotomic\PhpunitAssertions\StringLengthAssertions::assertSame(10, 'Astrotomic'); 122 | \Astrotomic\PhpunitAssertions\StringLengthAssertions::assertNotSame(8, 'Astrotomic'); 123 | \Astrotomic\PhpunitAssertions\StringLengthAssertions::assertLessThan(11, 'Astrotomic'); 124 | \Astrotomic\PhpunitAssertions\StringLengthAssertions::assertLessThanOrEqual(10, 'Astrotomic'); 125 | \Astrotomic\PhpunitAssertions\StringLengthAssertions::assertGreaterThan(9, 'Astrotomic'); 126 | \Astrotomic\PhpunitAssertions\StringLengthAssertions::assertGreaterThanOrEqual(10, 'Astrotomic'); 127 | ``` 128 | 129 | ### URL 130 | 131 | ```php 132 | \Astrotomic\PhpunitAssertions\UrlAssertions::assertValidLoose('https://astrotomic.info'); 133 | \Astrotomic\PhpunitAssertions\UrlAssertions::assertScheme('https', 'https://astrotomic.info'); 134 | \Astrotomic\PhpunitAssertions\UrlAssertions::assertHost('astrotomic.info', 'https://astrotomic.info'); 135 | \Astrotomic\PhpunitAssertions\UrlAssertions::assertPath('/contributor/gummibeer/', 'https://astrotomic.info/contributor/gummibeer/'); 136 | \Astrotomic\PhpunitAssertions\UrlAssertions::assertQuery(['_' => '123', 'q' => 'search'], 'https://astrotomic.info?q=search&_=123'); 137 | \Astrotomic\PhpunitAssertions\UrlAssertions::assertComponent('gummibeer', 'https://gummibeer@astrotomic.info', PHP_URL_USER); 138 | ``` 139 | 140 | ### Path 141 | 142 | ```php 143 | \Astrotomic\PhpunitAssertions\PathAssertions::assertDirname('/foo/bar', '/foo/bar/image.jpg'); 144 | \Astrotomic\PhpunitAssertions\PathAssertions::assertBasename('image.jpg', '/foo/bar/image.jpg'); 145 | \Astrotomic\PhpunitAssertions\PathAssertions::assertFilename('image', '/foo/bar/image.jpg'); 146 | \Astrotomic\PhpunitAssertions\PathAssertions::assertExtension('jpg', '/foo/bar/image.jpg'); 147 | ``` 148 | 149 | ### UUID 150 | 151 | `composer require --dev ramsey/uuid:^4.0` 152 | 153 | ```php 154 | \Astrotomic\PhpunitAssertions\UuidAssertions::assertUuid('52d08e38-ad24-4960-af02-22e0f7e0db8d'); 155 | ``` 156 | 157 | ## Laravel Assertions 158 | 159 | ### Collection 160 | 161 | ```php 162 | \Astrotomic\PhpunitAssertions\Laravel\CollectionAssertions::assertContains($collection, 'Astrotomic'); 163 | ``` 164 | 165 | ### HashID 166 | 167 | `composer require --dev vinkla/hashids:^9.0` 168 | 169 | ```php 170 | \Astrotomic\PhpunitAssertions\Laravel\HashidAssertions::assertHashIds('3kTMd', 2); 171 | \Astrotomic\PhpunitAssertions\Laravel\HashidAssertions::assertHashId('yr8'); 172 | ``` 173 | 174 | ### Model 175 | 176 | ```php 177 | \Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertExists($model); 178 | \Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertSame($model, \App\Models\User::first()); 179 | \Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertRelated($post, 'comments', $comment); 180 | \Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertRelated( 181 | $post, 182 | 'comments', 183 | \App\Models\Comment::class, 184 | \Illuminate\Database\Eloquent\Relations\HasMany::class 185 | ); 186 | ``` 187 | 188 | ### Blade 189 | 190 | `composer require --dev gajus/dindent:^2.0` 191 | 192 | ```php 193 | \Astrotomic\PhpunitAssertions\Laravel\BladeAssertions::assertRenderEquals( 194 | "
Price: 99.99 €
Price: {{ number_format($price, 2) }} €