├── .gitignore ├── .travis.yml ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src ├── Assert.php ├── Assertions │ ├── CollectionAssertions.php │ ├── DateTimeAssertions.php │ ├── FileUploadAssertions.php │ ├── FloatAssertions.php │ ├── GenericAssertions.php │ ├── Helpers │ │ └── StringHelpers.php │ ├── IntegerAssertions.php │ ├── ObjectAssertions.php │ └── StringAssertions.php └── Exceptions │ ├── AssertionException.php │ └── FileUploadException.php └── tests ├── AssertCollectionTest.php ├── AssertDateTimeTest.php ├── AssertFileUploadTest.php ├── AssertFloatTest.php ├── AssertGenericTest.php ├── AssertIntegerTest.php ├── AssertObjectTest.php ├── AssertStringTest.php ├── AssertTest.php ├── Dummy.php ├── DummyInterface.php └── resources ├── phpGpKMlf └── sample.png /.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | /build/ 3 | /vendor/ 4 | .php_cs.cache 5 | composer.lock 6 | .coveralls.yml 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: php 3 | 4 | php: 5 | - 5.6 6 | - 7.0 7 | 8 | matrix: 9 | fast_finish: true 10 | 11 | before_install: 12 | - /home/travis/.phpenv/versions/$(phpenv version-name)/bin/composer self-update 13 | 14 | before_script: 15 | - composer install 16 | 17 | script: 18 | - bin/phpunit --coverage-text 19 | 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Assert 2 | 3 | [![Build Status](https://travis-ci.org/nilportugues/php-assert.svg)](https://travis-ci.org/nilportugues/php-assert) [![Coverage Status](https://coveralls.io/repos/nilportugues/php-assert/badge.svg?branch=master)](https://coveralls.io/r/nilportugues/php-assert?branch=master) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/c37360c4-7280-4e08-bff1-0f0053103003/mini.png?)](https://insight.sensiolabs.com/projects/c37360c4-7280-4e08-bff1-0f0053103003) [![Latest Stable Version](https://poser.pugx.org/nilportugues/assert/v/stable)](https://packagist.org/packages/nilportugues/assert) [![Total Downloads](https://poser.pugx.org/nilportugues/assert/downloads)](https://packagist.org/packages/nilportugues/assert) [![License](https://poser.pugx.org/nilportugues/assert/license)](https://packagist.org/packages/nilportugues/assert) 4 | [![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://paypal.me/nilportugues) 5 | 6 | While I know there are out there other assertion libraries I was unhappy with them, so I wrote my own. 7 | 8 | ## Usage 9 | 10 | Assertion library is really straight forward. Pass input to the method call and it will just run or throw an `\Exception` if it doesn't meet the requirements. 11 | 12 | 13 | ```php 14 | use NilPortugues\Assert\Assert; 15 | 16 | try { 17 | Assert::nullOrIsLowercase(null); 18 | Assert::isUppercase('THIS IS GREAT'); 19 | Assert::isNight(new \DateTime('now')); 20 | 21 | } catch (Exception $e) { 22 | echo 'Did not meet the requirements, too bad!'; 23 | } 24 | ``` 25 | 26 | ## Assertion Methods 27 | 28 | All assertion methods can be used against different data types (integer, double, objects..). 29 | 30 | If you try to validate data for an unexpected data type, an Exception will be thrown making your assertion fail as expected. 31 | 32 | Most methods can be called using the "nullOr" prefix instead of the "is" prefix. For instance: 33 | 34 | 35 | ```php 36 | Assert::nullOrIsLowercase('lowercase'); //OK 37 | Assert::isLowercase(null); //raises Exception 38 | ``` 39 | 40 | ## Assertion List 41 | 42 | All methods allow specifying a custom `$message` value. If not passed in, a library-defined default will be used. 43 | 44 | Those accepting a `callable` as an argument, expect an Assert statement to be used inside the callable. 45 | 46 | #### Generic Assertions 47 | 48 | ```php 49 | Assert::isRequired($value, $message = ''); 50 | Assert::isNotNull($value, $message = ''); 51 | Assert::notEquals($property, $value, $message = ''); 52 | Assert::greaterThanOrEqual($property, $value, $message = ''); 53 | Assert::greaterThan($property, $value, $message = ''); 54 | Assert::lessThanOrEqual($property, $value, $message = ''); 55 | Assert::lessThan($property, $value, $message = ''); 56 | Assert::isScalar($value, $message = ''); 57 | 58 | // nullOr assertions 59 | Assert::nullOrNotEquals($property, $value, $message = ''); 60 | Assert::nullOrGreaterThanOrEqual($property, $value, $message = ''); 61 | Assert::nullOrGreaterThan($property, $value, $message = ''); 62 | Assert::nullOrLessThanOrEqual($property, $value, $message = ''); 63 | Assert::nullOrLessThan($property, $value, $message = ''); 64 | Assert::nullOrIsScalar($value, $message = ''); 65 | ``` 66 | 67 | #### String Assertions 68 | 69 | ```php 70 | Assert::isString($value, $message = '') 71 | Assert::isAlphanumeric($value, $message = '') 72 | Assert::isAlpha($value, $message = '') 73 | Assert::isBetween($value, $min, $max, $inclusive = false, $message = '') 74 | Assert::isCharset($value, $charset, $message = '') 75 | Assert::isAllConsonants($value, $message = '') 76 | Assert::contains($value, $contains, $identical = false, $message = '') 77 | Assert::isControlCharacters($value, $message = '') 78 | Assert::isDigit($value, $message = '') 79 | Assert::endsWith($value, $contains, $identical = false, $message = '') 80 | Assert::equals($value, $comparedValue, $identical = false, $message = '') 81 | Assert::in($value, $haystack, $identical = false, $message = '') 82 | Assert::hasGraphicalCharsOnly($value, $message = '') 83 | Assert::hasLength($value, $length, $message = '') 84 | Assert::isLowercase($value, $message = '') 85 | Assert::notEmpty($value, $message = '') 86 | Assert::noWhitespace($value, $message = '') 87 | Assert::hasPrintableCharsOnly($value, $message = '') 88 | Assert::isPunctuation($value, $message = '') 89 | Assert::matchesRegex($value, $regex, $message = '') 90 | Assert::isSlug($value, $message = '') 91 | Assert::isSpace($value, $message = '') 92 | Assert::startsWith($value, $contains, $identical = false, $message = '') 93 | Assert::isUppercase($value, $message = '') 94 | Assert::isVersion($value, $message = '') 95 | Assert::isVowel($value, $message = '') 96 | Assert::isHexDigit($value, $message = '') 97 | Assert::hasLowercase($value, $amount = null, $message = '') 98 | Assert::hasUppercase($value, $amount = null, $message = '') 99 | Assert::hasNumeric($value, $amount = null, $message = '') 100 | Assert::hasSpecialCharacters($value, $amount = null, $message = '') 101 | Assert::isEmail($value, $message = '') 102 | Assert::isUrl($value, $message = '') 103 | Assert::isUUID($value, $strict = true, $message = '') 104 | Assert::isLatitude($latitude, $message = '') 105 | Assert::isLongitude($lontitude, $message = '') 106 | Assert::isTimeString($value, $message = '') 107 | Assert::isDateString($value, $message = '') 108 | Assert::isHexColor($value, $message = '') 109 | Assert::isIpAddress($value, $message = '') 110 | Assert::isIpv4Address($value, $message = '') 111 | Assert::isIpv6Address($value, $message = '') 112 | Assert::isJson($value, $message = '') 113 | Assert::isCreditCard($value, $message = '') 114 | Assert::isPalindrome($value, $message = '') 115 | Assert::isUnderScore($value, $message = '') 116 | Assert::isTitleCase($value, $message = '') 117 | 118 | // nullOr assertions 119 | Assert::nullOrIsString($value, $message = '') 120 | Assert::nullOrIsAlphanumeric($value, $message = '') 121 | Assert::nullOrIsAlpha($value, $message = '') 122 | Assert::nullOrIsBetween($value, $min, $max, $inclusive = false, $message = '') 123 | Assert::nullOrIsCharset($value, $charset, $message = '') 124 | Assert::nullOrIsAllConsonants($value, $message = '') 125 | Assert::nullOrContains($value, $contains, $identical = false, $message = '') 126 | Assert::nullOrIsControlCharacters($value, $message = '') 127 | Assert::nullOrIsDigit($value, $message = '') 128 | Assert::nullOrEndsWith($value, $contains, $identical = false, $message = '') 129 | Assert::nullOrEquals($value, $comparedValue, $identical = false, $message = '') 130 | Assert::nullOrIn($value, $haystack, $identical = false, $message = '') 131 | Assert::nullOrHasGraphicalCharsOnly($value, $message = '') 132 | Assert::nullOrHasLength($value, $length, $message = '') 133 | Assert::nullOrIsLowercase($value, $message = '') 134 | Assert::nullOrNotEmpty($value, $message = '') 135 | Assert::nullOrNoWhitespace($value, $message = '') 136 | Assert::nullOrHasPrintableCharsOnly($value, $message = '') 137 | Assert::nullOrIsPunctuation($value, $message = '') 138 | Assert::nullOrMatchesRegex($value, $regex, $message = '') 139 | Assert::nullOrIsSlug($value, $message = '') 140 | Assert::nullOrIsSpace($value, $message = '') 141 | Assert::nullOrStartsWith($value, $contains, $identical = false, $message = '') 142 | Assert::nullOrIsUppercase($value, $message = '') 143 | Assert::nullOrIsVersion($value, $message = '') 144 | Assert::nullOrIsVowel($value, $message = '') 145 | Assert::nullOrIsHexDigit($value, $message = '') 146 | Assert::nullOrHasLowercase($value, $amount = null, $message = '') 147 | Assert::nullOrHasUppercase($value, $amount = null, $message = '') 148 | Assert::nullOrHasNumeric($value, $amount = null, $message = '') 149 | Assert::nullOrHasSpecialCharacters($value, $amount = null, $message = '') 150 | Assert::nullOrIsEmail($value, $message = '') 151 | Assert::nullOrIsUrl($value, $message = '') 152 | Assert::nullOrIsUUID($value, $strict = true, $message = '') 153 | Assert::nullOrIsLatitude($latitude, $message = '') 154 | Assert::nullOrIsLongitude($lontitude, $message = '') 155 | Assert::nullOrIsTimeString($value, $message = '') 156 | Assert::nullOrIsDateString($value, $message = '') 157 | Assert::nullOrIsHexColor($value, $message = '') 158 | Assert::nullOrIsIpAddress($value, $message = '') 159 | Assert::nullOrIsIpv4Address($value, $message = '') 160 | Assert::nullOrIsIpv6Address($value, $message = '') 161 | Assert::nullOrIsJson($value, $message = '') 162 | Assert::nullOrIsCreditCard($value, $message = '') 163 | Assert::nullOrIsPalindrome($value, $message = '') 164 | Assert::nullOrIsUnderScore($value, $message = '') 165 | Assert::nullOrIsTitleCase($value, $message = '') 166 | 167 | ``` 168 | 169 | #### Integer Assertions 170 | 171 | ```php 172 | Assert::isInteger($value, $message = '') 173 | Assert::isNotZero($value, $message = '') 174 | Assert::isPositiveOrZero($value, $message = '') 175 | Assert::isPositive($value, $message = '') 176 | Assert::isNegativeOrZero($value, $message = '') 177 | Assert::isNegative($value, $message = '') 178 | Assert::isBetween($value, $min, $max, $inclusive = false, $message = '') 179 | Assert::isOdd($value, $message = '') 180 | Assert::isEven($value, $message = '') 181 | Assert::isMultiple($value, $multiple, $message = '') 182 | 183 | // nullOr assertions 184 | Assert::nullOrIsInteger($value, $message = '') 185 | Assert::nullOrIsNotZero($value, $message = '') 186 | Assert::nullOrIsPositiveOrZero($value, $message = '') 187 | Assert::nullOrIsPositive($value, $message = '') 188 | Assert::nullOrIsNegativeOrZero($value, $message = '') 189 | Assert::nullOrIsNegative($value, $message = '') 190 | Assert::nullOrIsBetween($value, $min, $max, $inclusive = false, $message = '') 191 | Assert::nullOrIsOdd($value, $message = '') 192 | Assert::nullOrIsEven($value, $message = '') 193 | Assert::nullOrIsMultiple($value, $multiple, $message = '') 194 | ``` 195 | 196 | #### Float & Double Assertions 197 | 198 | ```php 199 | Assert::isFloat($value, $message = '') 200 | Assert::isNotZero($value, $message = '') 201 | Assert::isPositiveOrZero($value, $message = '') 202 | Assert::isPositive($value, $message = '') 203 | Assert::isNegativeOrZero($value, $message = '') 204 | Assert::isNegative($value, $message = '') 205 | Assert::isBetween($value, $min, $max, $inclusive = false, $message = '') 206 | Assert::isOdd($value, $message = '') 207 | Assert::isEven($value, $message = '') 208 | Assert::isMultiple($value, $multiple, $message = '') 209 | 210 | // nullOr assertions 211 | Assert::nullOrIsFloat($value, $message = '') 212 | Assert::nullOrIsNotZero($value, $message = '') 213 | Assert::nullOrIsPositiveOrZero($value, $message = '') 214 | Assert::nullOrIsPositive($value, $message = '') 215 | Assert::nullOrIsNegativeOrZero($value, $message = '') 216 | Assert::nullOrIsNegative($value, $message = '') 217 | Assert::nullOrIsBetween($value, $min, $max, $inclusive = false, $message = '') 218 | Assert::nullOrIsOdd($value, $message = '') 219 | Assert::nullOrIsEven($value, $message = '') 220 | Assert::nullOrIsMultiple($value, $multiple, $message = '') 221 | ``` 222 | 223 | #### Array & Collection Assertions 224 | 225 | ```php 226 | Assert::isArray($value, $message = '') 227 | Assert::each($value, callable $valueValidator, callable $keyValidator = null, $message = '') 228 | Assert::hasKeyFormat($value, callable $keyValidator, $message = '') 229 | Assert::endsWith($haystack, $needle, $strict = false, $message = '') 230 | Assert::contains($haystack, $needle, $strict = false, $message = '') 231 | Assert::hasKey($value, $keyName, $message = '') 232 | Assert::hasLength($value, $length, $message = '') 233 | Assert::isNotEmpty($value, $message = '') 234 | Assert::startsWith($haystack, $needle, $strict = false, $message = '') 235 | 236 | // nullOr assertions 237 | Assert::nullOrIsArray($value, $message = '') 238 | Assert::nullOrEach($value, callable $valueValidator, callable $keyValidator = null, $message = '') 239 | Assert::nullOrHasKeyFormat($value, callable $keyValidator, $message = '') 240 | Assert::nullOrEndsWith($haystack, $needle, $strict = false, $message = '') 241 | Assert::nullOrContains($haystack, $needle, $strict = false, $message = '') 242 | Assert::nullOrHasKey($value, $keyName, $message = '') 243 | Assert::nullOrHasLength($value, $length, $message = '') 244 | Assert::nullOrIsNotEmpty($value, $message = '') 245 | Assert::nullOrStartsWith($haystack, $needle, $strict = false, $message = '') 246 | ``` 247 | 248 | #### DateTime Assertions 249 | 250 | ```php 251 | Assert::isDateTime($value, $message = '') 252 | Assert::isAfter($value, $limit, $inclusive = false, $message = '') 253 | Assert::isBefore($value, $limit, $inclusive = false, $message = '') 254 | Assert::isDateRange($value, $minDate, $maxDate, $inclusive = false, $message = '') 255 | Assert::isWeekend($value, $message = '') 256 | Assert::isWeekday($value, $message = '') 257 | Assert::isMonday($value, $message = '') 258 | Assert::isTuesday($value, $message = '') 259 | Assert::isWednesday($value, $message = '') 260 | Assert::isThursday($value, $message = '') 261 | Assert::isFriday($value, $message = '') 262 | Assert::isSaturday($value, $message = '') 263 | Assert::isSunday($value, $message = '') 264 | Assert::isToday($value, $message = '') 265 | Assert::isYesterday($value, $message = '') 266 | Assert::isTomorrow($value, $message = '') 267 | Assert::isLeapYear($value, $message = '') 268 | Assert::isMorning($value, $message = '') 269 | Assert::isAfternoon($value, $message = '') 270 | Assert::isEvening($value, $message = '') 271 | Assert::isNight($value, $message = '') 272 | Assert::isFutureDate($value, $message = '') 273 | Assert::isPastDate($value, $message = '') 274 | Assert::isInNextWeek($value, $message = '') 275 | Assert::isInWeek($value, $weekNumber, $message) 276 | Assert::isInLastWeek($value, $message = '') 277 | Assert::isInLastMonth($value, $message = '') 278 | Assert::isInMonth($value, $monthNumber, $message) 279 | Assert::isInNextMonth($value, $message = '') 280 | Assert::isInLastYear($value, $message = '') 281 | Assert::isInYear($value, $yearNumber, $message) 282 | Assert::isInNextYear($value, $message = '') 283 | Assert::isFirstHalfOfYear($value, $message = '') 284 | Assert::isSecondHalfOfYear($value, $message = '') 285 | Assert::isTrimesterOfYear($value, $trimester, $message = '') 286 | Assert::isQuarterOfYear($value, $quarter, $message = '') 287 | Assert::isDayLightSavingTime($value, \DateTimeZone $timezone, $message = '') 288 | 289 | // nullOr assertions 290 | Assert::nullOrIsDateTime($value, $message = '') 291 | Assert::nullOrIsAfter($value, $limit, $inclusive = false, $message = '') 292 | Assert::nullOrIsBefore($value, $limit, $inclusive = false, $message = '') 293 | Assert::nullOrIsDateRange($value, $minDate, $maxDate, $inclusive = false, $message = '') 294 | Assert::nullOrIsWeekend($value, $message = '') 295 | Assert::nullOrIsWeekday($value, $message = '') 296 | Assert::nullOrIsMonday($value, $message = '') 297 | Assert::nullOrIsTuesday($value, $message = '') 298 | Assert::nullOrIsWednesday($value, $message = '') 299 | Assert::nullOrIsThursday($value, $message = '') 300 | Assert::nullOrIsFriday($value, $message = '') 301 | Assert::nullOrIsSaturday($value, $message = '') 302 | Assert::nullOrIsSunday($value, $message = '') 303 | Assert::nullOrIsToday($value, $message = '') 304 | Assert::nullOrIsYesterday($value, $message = '') 305 | Assert::nullOrIsTomorrow($value, $message = '') 306 | Assert::nullOrIsLeapYear($value, $message = '') 307 | Assert::nullOrIsMorning($value, $message = '') 308 | Assert::nullOrIsAfternoon($value, $message = '') 309 | Assert::nullOrIsEvening($value, $message = '') 310 | Assert::nullOrIsNight($value, $message = '') 311 | Assert::nullOrIsFutureDate($value, $message = '') 312 | Assert::nullOrIsPastDate($value, $message = '') 313 | Assert::nullOrIsInNextWeek($value, $message = '') 314 | Assert::nullOrIsInWeek($value, $weekNumber, $message) 315 | Assert::nullOrIsInLastWeek($value, $message = '') 316 | Assert::nullOrIsInLastMonth($value, $message = '') 317 | Assert::nullOrIsInMonth($value, $monthNumber, $message) 318 | Assert::nullOrIsInNextMonth($value, $message = '') 319 | Assert::nullOrIsInLastYear($value, $message = '') 320 | Assert::nullOrIsInYear($value, $yearNumber, $message) 321 | Assert::nullOrIsInNextYear($value, $message = '') 322 | Assert::nullOrIsFirstHalfOfYear($value, $message = '') 323 | Assert::nullOrIsSecondHalfOfYear($value, $message = '') 324 | Assert::nullOrIsTrimesterOfYear($value, $trimester, $message = '') 325 | Assert::nullOrIsQuarterOfYear($value, $quarter, $message = '') 326 | Assert::nullOrIsDayLightSavingTime($value, \DateTimeZone $timezone, $message = '') 327 | ``` 328 | 329 | #### Object Assertions 330 | 331 | ```php 332 | Assert::isObject($value, $message = '') 333 | Assert::isInstanceOf($value, $instanceOf, $message = '') 334 | Assert::hasProperty($value, $property, $message = '') 335 | Assert::hasMethod($value, $method, $message = '') 336 | Assert::hasParentClass($value, $message = '') 337 | Assert::isChildOf($value, $parentClass, $message = '') 338 | Assert::inheritsFrom($value, $inheritsClass, $message = '') 339 | Assert::hasInterface($value, $interface, $message = '') 340 | 341 | // nullOr assertions 342 | Assert::nullOrIsObject($value, $message = '') 343 | Assert::nullOrIsInstanceOf($value, $instanceOf, $message = '') 344 | Assert::nullOrHasProperty($value, $property, $message = '') 345 | Assert::nullOrHasMethod($value, $method, $message = '') 346 | Assert::nullOrHasParentClass($value, $message = '') 347 | Assert::nullOrIsChildOf($value, $parentClass, $message = '') 348 | Assert::nullOrInheritsFrom($value, $inheritsClass, $message = '') 349 | Assert::nullOrHasInterface($value, $interface, $message = '') 350 | ``` 351 | 352 | #### FileUpload Assertions 353 | 354 | FileUpload Assertions look into the `$_FILES` global variable. 355 | 356 | ```php 357 | Assert::isFileUploaded($uploadName, $message = '') 358 | Assert::isFileUploadedBetweenFileSize($uploadName, $minSize, $maxSize, $format='B', $inclusive = false, $message = '') 359 | Assert::hasFileUploadedFileNameFormat($uploadName, callable $assertion, $message = '') 360 | Assert::hasFileUploadedValidUploadDirectory($uploadName, $uploadDir, $message = '') 361 | Assert::isFileUploadedNotOverwritingExistingFile($uploadName, $uploadDir, $message = '') 362 | Assert::hasFileUploadedFileNameLength($uploadName, $size, $message = '') 363 | Assert::isFileUploadedImage($uploadName, $message = '') 364 | Assert::isFileUploadedMimeType($uploadName, array $allowedTypes, $message = '') 365 | 366 | // nullOr assertions 367 | Assert::nullOrIsFileUploaded($uploadName, $message = '') 368 | Assert::nullOrIsFileUploadedBetweenFileSize($uploadName, $minSize, $maxSize, $format='B', $inclusive=false, $message='') 369 | Assert::nullOrHasFileUploadedFileNameFormat($uploadName, callable $assertion, $message = '') 370 | Assert::nullOrHasFileUploadedValidUploadDirectory($uploadName, $uploadDir, $message = '') 371 | Assert::nullOrIsFileUploadedNotOverwritingExistingFile($uploadName, $uploadDir, $message = '') 372 | Assert::nullOrHasFileUploadedFileNameLength($uploadName, $size, $message = '') 373 | Assert::nullOrIsFileUploadedImage($uploadName, $message = '') 374 | Assert::nullOrIsFileUploadedMimeType($uploadName, array $allowedTypes, $message = '') 375 | ``` 376 | 377 | ## Contribute 378 | 379 | Contributions to the package are always welcome! 380 | 381 | * Report any bugs or issues you find on the [issue tracker](https://github.com/nilportugues/php-assert/issues/new). 382 | * You can grab the source code at the package's [Git repository](https://github.com/nilportugues/php-assert). 383 | 384 | 385 | ## Support 386 | 387 | Get in touch with me using one of the following means: 388 | 389 | - Emailing me at 390 | - Opening an [Issue](https://github.com/nilportugues/php-assert/issues/new) 391 | 392 | ## Authors 393 | 394 | * [Nil Portugués Calderó](http://nilportugues.com) 395 | * [The Community Contributors](https://github.com/nilportugues/php-assert/graphs/contributors) 396 | 397 | 398 | ## License 399 | The code base is licensed under the [MIT license](LICENSE). 400 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nilportugues/assert", 3 | "description": "A simple and elegant assertion and guard methods library for input validation.", 4 | "keywords": ["assert", "assertion", "validation", "input", "data", "validator", "nil portugues", "nilportugues"], 5 | "type": "library", 6 | "homepage": "http://nilportugues.com", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Nil Portugués", 11 | "email": "contact@nilportugues.com" 12 | } 13 | ], 14 | "require-dev": { 15 | "phpunit/phpunit": "~4.8", 16 | "fabpot/php-cs-fixer": "1.9.*", 17 | "satooshi/php-coveralls": "^1.0" 18 | }, 19 | "autoload": { 20 | "psr-4": { 21 | "NilPortugues\\Assert\\": "src/" 22 | } 23 | }, 24 | "autoload-dev": { 25 | "psr-4": { 26 | "NilPortugues\\Tests\\Assert\\": "tests/" 27 | } 28 | }, 29 | "config": { 30 | "bin-dir": "bin/" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | ./tests 31 | 32 | 33 | 34 | 35 | 36 | ./ 37 | 38 | ./tests 39 | ./vendor/ 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /src/Assert.php: -------------------------------------------------------------------------------- 1 | 5 | * Date: 6/30/15 6 | * Time: 10:39 PM. 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | namespace NilPortugues\Assert; 12 | 13 | use Exception; 14 | use NilPortugues\Assert\Exceptions\AssertionException; 15 | use RuntimeException; 16 | 17 | /** 18 | * METHODSTART. 19 | * 20 | * @method static void isArray($value, $message = '') 21 | * @method static void each($value, callable $valueValidator, callable $keyValidator = null, $message = '') 22 | * @method static void hasKeyFormat($value, callable $keyValidator, $message = '') 23 | * @method static void endsWith($haystack, $needle, $strict = false, $message = '') 24 | * @method static void contains($haystack, $needle, $strict = false, $message = '') 25 | * @method static void hasKey($value, $keyName, $message = '') 26 | * @method static void hasLength($value, $length, $message = '') 27 | * @method static void isNotEmpty($value, $message = '') 28 | * @method static void startsWith($haystack, $needle, $strict = false, $message = '') 29 | * @method static void isFileUploaded($uploadName, $message = '') 30 | * @method static void isFileUploadedBetweenFileSize($uploadName, $minSize, $maxSize, $format = 'B', $inclusive = false, $message = '') 31 | * @method static void hasFileUploadedFileNameFormat($uploadName, callable $assertion, $message = '') 32 | * @method static void hasFileUploadedValidUploadDirectory($uploadName, $uploadDir, $message = '') 33 | * @method static void isFileUploadedNotOverwritingExistingFile($uploadName, $uploadDir, $message = '') 34 | * @method static void hasFileUploadedFileNameLength($uploadName, $size, $message = '') 35 | * @method static void isFileUploadedImage($uploadName, $message = '') 36 | * @method static void isFileUploadedMimeType($uploadName, array $allowedTypes, $message = '') 37 | * @method static void isDateTime($value, $message = '') 38 | * @method static void isAfter($value, $limit, $inclusive = false, $message = '') 39 | * @method static void isBefore($value, $limit, $inclusive = false, $message = '') 40 | * @method static void isDateRange($value, $minDate, $maxDate, $inclusive = false, $message = '') 41 | * @method static void isWeekend($value, $message = '') 42 | * @method static void isWeekday($value, $message = '') 43 | * @method static void isMonday($value, $message = '') 44 | * @method static void isTuesday($value, $message = '') 45 | * @method static void isWednesday($value, $message = '') 46 | * @method static void isThursday($value, $message = '') 47 | * @method static void isFriday($value, $message = '') 48 | * @method static void isSaturday($value, $message = '') 49 | * @method static void isSunday($value, $message = '') 50 | * @method static void isToday($value, $message = '') 51 | * @method static void isYesterday($value, $message = '') 52 | * @method static void isTomorrow($value, $message = '') 53 | * @method static void isLeapYear($value, $message = '') 54 | * @method static void isMorning($value, $message = '') 55 | * @method static void isAfternoon($value, $message = '') 56 | * @method static void isEvening($value, $message = '') 57 | * @method static void isNight($value, $message = '') 58 | * @method static void isFloat($value, $message = '') 59 | * @method static void isNotZero($value, $message = '') 60 | * @method static void isPositiveOrZero($value, $message = '') 61 | * @method static void isPositive($value, $message = '') 62 | * @method static void isNegativeOrZero($value, $message = '') 63 | * @method static void isNegative($value, $message = '') 64 | * @method static void isBetween($value, $min, $max, $inclusive = false, $message = '') 65 | * @method static void isOdd($value, $message = '') 66 | * @method static void isEven($value, $message = '') 67 | * @method static void isMultiple($value, $multiple, $message = '') 68 | * @method static void isInteger($value, $message = '') 69 | * @method static void isObject($value, $message = '') 70 | * @method static void isInstanceOf($value, $instanceOf, $message = '') 71 | * @method static void hasProperty($value, $property, $message = '') 72 | * @method static void hasMethod($value, $method, $message = '') 73 | * @method static void hasParentClass($value, $message = '') 74 | * @method static void isChildOf($value, $parentClass, $message = '') 75 | * @method static void inheritsFrom($value, $inheritsClass, $message = '') 76 | * @method static void hasInterface($value, $interface, $message = '') 77 | * @method static void isString($value, $message = '') 78 | * @method static void isAlphanumeric($value, $message = '') 79 | * @method static void isAlpha($value, $message = '') 80 | * @method static void isCharset($value, $charset, $message = '') 81 | * @method static void isAllConsonants($value, $message = '') 82 | * @method static void isControlCharacters($value, $message = '') 83 | * @method static void isDigit($value, $message = '') 84 | * @method static void equals($value, $comparedValue, $identical = false, $message = '') 85 | * @method static void in($value, $haystack, $identical = false, $message = '') 86 | * @method static void hasGraphicalCharsOnly($value, $message = '') 87 | * @method static void isLowercase($value, $message = '') 88 | * @method static void notEmpty($value, $message = '') 89 | * @method static void noWhitespace($value, $message = '') 90 | * @method static void hasPrintableCharsOnly($value, $message = '') 91 | * @method static void isPunctuation($value, $message = '') 92 | * @method static void matchesRegex($value, $regex, $message = '') 93 | * @method static void isSlug($value, $message = '') 94 | * @method static void isSpace($value, $message = '') 95 | * @method static void isUppercase($value, $message = '') 96 | * @method static void isVersion($value, $message = '') 97 | * @method static void isVowel($value, $message = '') 98 | * @method static void isHexDigit($value, $message = '') 99 | * @method static void hasLowercase($value, $amount = null, $message = '') 100 | * @method static void hasUppercase($value, $amount = null, $message = '') 101 | * @method static void hasNumeric($value, $amount = null, $message = '') 102 | * @method static void hasSpecialCharacters($value, $amount = null, $message = '') 103 | * @method static void isEmail($value, $message = '') 104 | * @method static void isUrl($value, $message = '') 105 | * @method static void isUUID($value, $strict = true, $message = '') 106 | * @method static void isRequired($value, $message = '') 107 | * @method static void isNotNull($value, $message = '') 108 | * @method static void notEquals($property, $value, $message = '') 109 | * @method static void greaterThanOrEqual($property, $value, $message = '') 110 | * @method static void greaterThan($property, $value, $message = '') 111 | * @method static void lessThanOrEqual($property, $value, $message = '') 112 | * @method static void lessThan($property, $value, $message = '') 113 | * @method static void nullOrEach($value, callable $valueValidator, callable $keyValidator = null, $message = '') 114 | * @method static void nullOrHasKeyFormat($value, callable $keyValidator, $message = '') 115 | * @method static void nullOrEndsWith($haystack, $needle, $strict = false, $message = '') 116 | * @method static void nullOrContains($haystack, $needle, $strict = false, $message = '') 117 | * @method static void nullOrHasKey($value, $keyName, $message = '') 118 | * @method static void nullOrHasLength($value, $length, $message = '') 119 | * @method static void nullOrIsNotEmpty($value, $message = '') 120 | * @method static void nullOrStartsWith($haystack, $needle, $strict = false, $message = '') 121 | * @method static void nullOrIsFileUploaded($uploadName, $message = '') 122 | * @method static void nullOrIsFileUploadedBetweenFileSize($uploadName, $minSize, $maxSize, $format = 'B', $inclusive = false, $message = '') 123 | * @method static void nullOrHasFileUploadedFileNameFormat($uploadName, callable $assertion, $message = '') 124 | * @method static void nullOrHasFileUploadedValidUploadDirectory($uploadName, $uploadDir, $message = '') 125 | * @method static void nullOrIsFileUploadedNotOverwritingExistingFile($uploadName, $uploadDir, $message = '') 126 | * @method static void nullOrHasFileUploadedFileNameLength($uploadName, $size, $message = '') 127 | * @method static void nullOrIsFileUploadedImage($uploadName, $message = '') 128 | * @method static void nullOrIsFileUploadedMimeType($uploadName, array $allowedTypes, $message = '') 129 | * @method static void nullOrIsDateTime($value, $message = '') 130 | * @method static void nullOrIsAfter($value, $limit, $inclusive = false, $message = '') 131 | * @method static void nullOrIsBefore($value, $limit, $inclusive = false, $message = '') 132 | * @method static void nullOrIsDateRange($value, $minDate, $maxDate, $inclusive = false, $message = '') 133 | * @method static void nullOrIsWeekend($value, $message = '') 134 | * @method static void nullOrIsWeekday($value, $message = '') 135 | * @method static void nullOrIsMonday($value, $message = '') 136 | * @method static void nullOrIsTuesday($value, $message = '') 137 | * @method static void nullOrIsWednesday($value, $message = '') 138 | * @method static void nullOrIsThursday($value, $message = '') 139 | * @method static void nullOrIsFriday($value, $message = '') 140 | * @method static void nullOrIsSaturday($value, $message = '') 141 | * @method static void nullOrIsSunday($value, $message = '') 142 | * @method static void nullOrIsToday($value, $message = '') 143 | * @method static void nullOrIsYesterday($value, $message = '') 144 | * @method static void nullOrIsTomorrow($value, $message = '') 145 | * @method static void nullOrIsLeapYear($value, $message = '') 146 | * @method static void nullOrIsMorning($value, $message = '') 147 | * @method static void nullOrIsAfternoon($value, $message = '') 148 | * @method static void nullOrIsEvening($value, $message = '') 149 | * @method static void nullOrIsNight($value, $message = '') 150 | * @method static void nullOrIsFloat($value, $message = '') 151 | * @method static void nullOrIsNotZero($value, $message = '') 152 | * @method static void nullOrIsPositiveOrZero($value, $message = '') 153 | * @method static void nullOrIsPositive($value, $message = '') 154 | * @method static void nullOrIsNegativeOrZero($value, $message = '') 155 | * @method static void nullOrIsNegative($value, $message = '') 156 | * @method static void nullOrIsBetween($value, $min, $max, $inclusive = false, $message = '') 157 | * @method static void nullOrIsOdd($value, $message = '') 158 | * @method static void nullOrIsEven($value, $message = '') 159 | * @method static void nullOrIsMultiple($value, $multiple, $message = '') 160 | * @method static void nullOrIsInteger($value, $message = '') 161 | * @method static void nullOrIsObject($value, $message = '') 162 | * @method static void nullOrIsInstanceOf($value, $instanceOf, $message = '') 163 | * @method static void nullOrHasProperty($value, $property, $message = '') 164 | * @method static void nullOrHasMethod($value, $method, $message = '') 165 | * @method static void nullOrHasParentClass($value, $message = '') 166 | * @method static void nullOrIsChildOf($value, $parentClass, $message = '') 167 | * @method static void nullOrInheritsFrom($value, $inheritsClass, $message = '') 168 | * @method static void nullOrHasInterface($value, $interface, $message = '') 169 | * @method static void nullOrIsString($value, $message = '') 170 | * @method static void nullOrIsAlphanumeric($value, $message = '') 171 | * @method static void nullOrIsAlpha($value, $message = '') 172 | * @method static void nullOrIsCharset($value, $charset, $message = '') 173 | * @method static void nullOrIsAllConsonants($value, $message = '') 174 | * @method static void nullOrIsControlCharacters($value, $message = '') 175 | * @method static void nullOrIsDigit($value, $message = '') 176 | * @method static void nullOrEquals($value, $comparedValue, $identical = false, $message = '') 177 | * @method static void nullOrIn($value, $haystack, $identical = false, $message = '') 178 | * @method static void nullOrHasGraphicalCharsOnly($value, $message = '') 179 | * @method static void nullOrIsLowercase($value, $message = '') 180 | * @method static void nullOrNotEmpty($value, $message = '') 181 | * @method static void nullOrNoWhitespace($value, $message = '') 182 | * @method static void nullOrHasPrintableCharsOnly($value, $message = '') 183 | * @method static void nullOrIsPunctuation($value, $message = '') 184 | * @method static void nullOrMatchesRegex($value, $regex, $message = '') 185 | * @method static void nullOrIsSlug($value, $message = '') 186 | * @method static void nullOrIsSpace($value, $message = '') 187 | * @method static void nullOrIsUppercase($value, $message = '') 188 | * @method static void nullOrIsVersion($value, $message = '') 189 | * @method static void nullOrIsVowel($value, $message = '') 190 | * @method static void nullOrIsHexDigit($value, $message = '') 191 | * @method static void nullOrHasLowercase($value, $amount = null, $message = '') 192 | * @method static void nullOrHasUppercase($value, $amount = null, $message = '') 193 | * @method static void nullOrHasNumeric($value, $amount = null, $message = '') 194 | * @method static void nullOrHasSpecialCharacters($value, $amount = null, $message = '') 195 | * @method static void nullOrIsEmail($value, $message = '') 196 | * @method static void nullOrIsUrl($value, $message = '') 197 | * @method static void nullOrIsUUID($value, $strict = true, $message = '') 198 | * @method static void nullOrIsRequired($value, $message = '') 199 | * @method static void nullOrIsNotNull($value, $message = '') 200 | * @method static void nullOrNotEquals($property, $value, $message = '') 201 | * @method static void nullOrGreaterThanOrEqual($property, $value, $message = '') 202 | * @method static void nullOrGreaterThan($property, $value, $message = '') 203 | * @method static void nullOrLessThanOrEqual($property, $value, $message = '') 204 | * @method static void nullOrLessThan($property, $value, $message = '') 205 | * @method static void nullOrIsLatitude($latitude, $message = '') 206 | * @method static void nullOrIsLongitude($lontitude, $message = '') 207 | * @method static void isLatitude($latitude, $message = '') 208 | * @method static void isLongitude($lontitude, $message = '') 209 | * @method static void isScalar($value, $message = '') 210 | * @method static void nullOrIsScalar($value, $message = '') 211 | * @method static void isFutureDate($value, $message = '') 212 | * @method static void isPastDate($value, $message = '') 213 | * @method static void nullOrIsFutureDate($value, $message = '') 214 | * @method static void nullOrIsPastDate($value, $message = '') 215 | * @method static void isTimeString($value, $message = '') 216 | * @method static void isDateString($value, $message = '') 217 | * @method static void isHexColor($value, $message = '') 218 | * @method static void isIpAddress($value, $message = '') 219 | * @method static void isIpv4Address($value, $message = '') 220 | * @method static void isIpv6Address($value, $message = '') 221 | * @method static void nullOrIsTimeString($value, $message = '') 222 | * @method static void nullOrIsDateString($value, $message = '') 223 | * @method static void nullOrIsHexColor($value, $message = '') 224 | * @method static void nullOrIsIpAddress($value, $message = '') 225 | * @method static void nullOrIsIpv4Address($value, $message = '') 226 | * @method static void nullOrIsIpv6Address($value, $message = '') 227 | * @method static void isInNextWeek($value, $message = '') 228 | * @method static void isInWeek($value, $weekNumber, $message = '') 229 | * @method static void isInLastWeek($value, $message = '') 230 | * @method static void isInLastMonth($value, $message = '') 231 | * @method static void isInMonth($value, $monthNumber, $message = '') 232 | * @method static void isInNextMonth($value, $message = '') 233 | * @method static void isInLastYear($value, $message = '') 234 | * @method static void isInYear($value, $yearNumber, $message = '') 235 | * @method static void isInNextYear($value, $message = '') 236 | * @method static void isFirstHalfOfYear($value, $message = '') 237 | * @method static void isSecondHalfOfYear($value, $message = '') 238 | * @method static void isTrimesterOfYear($value, $trimester, $message = '') 239 | * @method static void isQuarterOfYear($value, $quarter, $message = '') 240 | * @method static void isDayLightSavingTime($value, $message = '') 241 | * @method static void nullOrIsInNextWeek($value, $message = '') 242 | * @method static void nullOrIsInWeek($value, $weekNumber, $message = '') 243 | * @method static void nullOrIsInLastWeek($value, $message = '') 244 | * @method static void nullOrIsInLastMonth($value, $message = '') 245 | * @method static void nullOrIsInMonth($value, $monthNumber, $message = '') 246 | * @method static void nullOrIsInNextMonth($value, $message = '') 247 | * @method static void nullOrIsInLastYear($value, $message = '') 248 | * @method static void nullOrIsInYear($value, $yearNumber, $message = '') 249 | * @method static void nullOrIsInNextYear($value, $message = '') 250 | * @method static void nullOrIsFirstHalfOfYear($value, $message = '') 251 | * @method static void nullOrIsSecondHalfOfYear($value, $message = '') 252 | * @method static void nullOrIsTrimesterOfYear($value, $trimester, $message = '') 253 | * @method static void nullOrIsQuarterOfYear($value, $quarter, $message = '') 254 | * @method static void nullOrIsDayLightSavingTime($value, $message = '') 255 | * @method static void isJson($value, $message = '') 256 | * @method static void isCreditCard($value, $message = '') 257 | * @method static void nullOrIsJson($value, $message = '') 258 | * @method static void nullOrIsCreditCard($value, $message = '') 259 | * @method static void nullOrIsPalindrome($value, $message = '') 260 | * @method static void isPalindrome($value, $message = '') 261 | * @method static void isUnderScore($value, $message = '') 262 | * @method static void nullOrIsUnderScore($value, $message = '') 263 | * @method static void isCamelCase($value, $message = '') 264 | * @method static void nullOrIsCamelCase($value, $message = '') 265 | * @method static void isTitleCase($value, $message = '') 266 | * @method static void nullOrIsTitleCase($value, $message = '') 267 | * 268 | * METHODEND 269 | */ 270 | class Assert 271 | { 272 | /** 273 | * @var array 274 | */ 275 | protected static $classMap = [ 276 | 'Collection' => '\NilPortugues\Assert\Assertions\CollectionAssertions', 277 | 'String' => '\NilPortugues\Assert\Assertions\StringAssertions', 278 | 'Object' => '\NilPortugues\Assert\Assertions\ObjectAssertions', 279 | 'Integer' => '\NilPortugues\Assert\Assertions\IntegerAssertions', 280 | 'Float' => '\NilPortugues\Assert\Assertions\FloatAssertions', 281 | 'DateTime' => '\NilPortugues\Assert\Assertions\DateTimeAssertions', 282 | 'FileUpload' => '\NilPortugues\Assert\Assertions\FileUploadAssertions', 283 | 'Generic' => '\NilPortugues\Assert\Assertions\GenericAssertions', 284 | ]; 285 | 286 | protected static $filterByType = [ 287 | 'NULL' => ['Generic', 'String', 'Integer', 'Float', 'Object', 'DateTime', 'Collection'], 288 | 'integer' => ['Integer', 'Generic', 'String'], 289 | 'double' => ['Float', 'Generic', 'String'], 290 | 'string' => ['String', 'DateTime', 'Generic', 'FileUpload'], 291 | 'object' => ['DateTime', 'Object', 'Generic', 'Collection'], 292 | 'array' => ['Collection', 'Generic'], 293 | ]; 294 | 295 | /** 296 | * @var array 297 | */ 298 | protected static $methods = [ 299 | 'istitlecase' => ['String'], 300 | 'iscamelcase' => ['String'], 301 | 'isunderscore' => ['String'], 302 | 'ispalindrome' => ['String'], 303 | 'isjson' => ['String'], 304 | 'iscreditcard' => ['String'], 305 | 'isinnextweek' => ['DateTime'], 306 | 'isinweek' => ['DateTime'], 307 | 'isinlastweek' => ['DateTime'], 308 | 'isinlastmonth' => ['DateTime'], 309 | 'isinmonth' => ['DateTime'], 310 | 'isinnextmonth' => ['DateTime'], 311 | 'isinlastyear' => ['DateTime'], 312 | 'isinyear' => ['DateTime'], 313 | 'isinnextyear' => ['DateTime'], 314 | 'isfirsthalfofyear' => ['DateTime'], 315 | 'issecondhalfofyear' => ['DateTime'], 316 | 'istrimesterofyear' => ['DateTime'], 317 | 'isquarterofyear' => ['DateTime'], 318 | 'isdaylightsavingtime' => ['DateTime'], 319 | 'isfuturedate' => ['DateTime'], 320 | 'ispastdate' => ['DateTime'], 321 | 'islatitude' => ['String'], 322 | 'islongitude' => ['String'], 323 | 'isrequired' => ['Generic'], 324 | 'isnotnull' => ['Generic'], 325 | 'notequals' => ['Generic'], 326 | 'greaterthanorequal' => ['Generic'], 327 | 'greaterthan' => ['Generic'], 328 | 'lessthanorequal' => ['Generic'], 329 | 'lessthan' => ['Generic'], 330 | 'isstring' => ['String'], 331 | 'isalphanumeric' => ['String'], 332 | 'isalpha' => ['String'], 333 | 'isbetween' => ['DateTime', 'String', 'Integer', 'Float'], 334 | 'isdaterange' => ['DateTime'], 335 | 'ischarset' => ['String'], 336 | 'isallconsonants' => ['String'], 337 | 'contains' => ['String', 'Collection'], 338 | 'iscontrolcharacters' => ['String'], 339 | 'isdigit' => ['String'], 340 | 'endswith' => ['String', 'Collection'], 341 | 'equals' => ['String'], 342 | 'in' => ['String'], 343 | 'hasgraphicalcharsonly' => ['String'], 344 | 'haslength' => ['String', 'Collection'], 345 | 'islowercase' => ['String'], 346 | 'notempty' => ['String'], 347 | 'nowhitespace' => ['String'], 348 | 'hasprintablecharsonly' => ['String'], 349 | 'ispunctuation' => ['String'], 350 | 'matchesregex' => ['String'], 351 | 'isslug' => ['String'], 352 | 'isspace' => ['String'], 353 | 'startswith' => ['String', 'Collection'], 354 | 'isuppercase' => ['String'], 355 | 'isversion' => ['String'], 356 | 'isvowel' => ['String'], 357 | 'ishexdigit' => ['String'], 358 | 'haslowercase' => ['String'], 359 | 'hasuppercase' => ['String'], 360 | 'hasnumeric' => ['String'], 361 | 'hasspecialcharacters' => ['String'], 362 | 'isemail' => ['String'], 363 | 'isurl' => ['String'], 364 | 'isuuid' => ['String'], 365 | 'isobject' => ['Object'], 366 | 'isinstanceof' => ['Object'], 367 | 'hasproperty' => ['Object'], 368 | 'hasmethod' => ['Object'], 369 | 'hasparentclass' => ['Object'], 370 | 'ischildof' => ['Object'], 371 | 'inheritsfrom' => ['Object'], 372 | 'hasinterface' => ['Object'], 373 | 'isinteger' => ['Integer'], 374 | 'isfloat' => ['Float'], 375 | 'isnotzero' => ['Integer', 'Float'], 376 | 'ispositiveorzero' => ['Integer', 'Float'], 377 | 'ispositive' => ['Integer', 'Float'], 378 | 'isnegativeorzero' => ['Integer', 'Float'], 379 | 'isnegative' => ['Integer', 'Float'], 380 | 'isodd' => ['Integer', 'Float'], 381 | 'iseven' => ['Integer', 'Float'], 382 | 'ismultiple' => ['Integer', 'Float'], 383 | 'isfileuploaded' => ['FileUpload'], 384 | 'isfileuploadedbetweenfilesize' => ['FileUpload'], 385 | 'hasfileuploadedfilenameformat' => ['FileUpload'], 386 | 'hasfileuploadedvaliduploaddirectory' => ['FileUpload'], 387 | 'isfileuploadednotoverwritingexistingfile' => ['FileUpload'], 388 | 'hasfileuploadedfilenamelength' => ['FileUpload'], 389 | 'isfileuploadedimage' => ['FileUpload'], 390 | 'isfileuploadedmimetype' => ['FileUpload'], 391 | 'isdatetime' => ['DateTime'], 392 | 'isafter' => ['DateTime'], 393 | 'isbefore' => ['DateTime'], 394 | 'isweekend' => ['DateTime'], 395 | 'isweekday' => ['DateTime'], 396 | 'ismonday' => ['DateTime'], 397 | 'istuesday' => ['DateTime'], 398 | 'iswednesday' => ['DateTime'], 399 | 'isthursday' => ['DateTime'], 400 | 'isfriday' => ['DateTime'], 401 | 'issaturday' => ['DateTime'], 402 | 'issunday' => ['DateTime'], 403 | 'istoday' => ['DateTime'], 404 | 'isyesterday' => ['DateTime'], 405 | 'istomorrow' => ['DateTime'], 406 | 'isleapyear' => ['DateTime'], 407 | 'ismorning' => ['DateTime'], 408 | 'isafternoon' => ['DateTime'], 409 | 'isevening' => ['DateTime'], 410 | 'isnight' => ['DateTime'], 411 | 'isarray' => ['Collection'], 412 | 'each' => ['Collection'], 413 | 'haskeyformat' => ['Collection'], 414 | 'haskey' => ['Collection'], 415 | 'isnotempty' => ['Collection'], 416 | 'isscalar' => ['Generic'], 417 | 'nullorisscalar' => ['Generic'], 418 | 'ishexcolor' => ['String'], 419 | 'isipaddress' => ['String'], 420 | 'isipv4address' => ['String'], 421 | 'isipv6address' => ['String'], 422 | 'istimestring' => ['String'], 423 | 'isdatestring' => ['String'], 424 | ]; 425 | 426 | /** 427 | * @param $method 428 | * @param $args 429 | * 430 | * @throws Exception 431 | */ 432 | public static function __callStatic($method, $args) 433 | { 434 | $normalized = ltrim(strtolower($method), 'nullor'); 435 | 436 | if (self::isDefinedMethod($method, $normalized)) { 437 | throw new RuntimeException(sprintf('Method %s does not exist.', $method)); 438 | } 439 | 440 | $message = 'Method not found'; 441 | $normalized = self::resolveName($method, $normalized); 442 | $classNames = self::bestAssertionMethod($normalized, $args); 443 | 444 | foreach ($classNames as $className) { 445 | try { 446 | if (self::isNullOrMethod($method, $args)) { 447 | return; 448 | } 449 | 450 | call_user_func_array(self::$classMap[$className].'::'.$normalized, $args); 451 | 452 | return; 453 | } catch (AssertionException $e) { 454 | $message = $e->getMessage(); 455 | } 456 | } 457 | 458 | throw new Exception($message); 459 | } 460 | 461 | /** 462 | * @param string $method 463 | * @param string $normalized 464 | * 465 | * @return bool 466 | */ 467 | protected static function isDefinedMethod($method, $normalized) 468 | { 469 | return empty(self::$methods[strtolower($method)]) && empty(self::$methods[$normalized]); 470 | } 471 | 472 | /** 473 | * @param string $method 474 | * @param string $normalized 475 | * 476 | * @return string 477 | */ 478 | protected static function resolveName($method, $normalized) 479 | { 480 | return empty(self::$methods[strtolower($method)]) ? $normalized : strtolower($method); 481 | } 482 | 483 | /** 484 | * @param string $method 485 | * @param array $args 486 | * 487 | * @return array 488 | */ 489 | protected static function bestAssertionMethod($method, array &$args) 490 | { 491 | $classNames = array_intersect(self::$filterByType[gettype($args[0])], self::$methods[$method]); 492 | 493 | if (count($classNames) > 1) { 494 | $classNames = [array_shift($classNames)]; 495 | } 496 | 497 | return $classNames; 498 | } 499 | 500 | /** 501 | * @param string $method 502 | * @param array $args 503 | * 504 | * @return bool 505 | */ 506 | protected static function isNullOrMethod($method, array $args) 507 | { 508 | return 'nullor' === strtolower(substr($method, 0, 6)) && null === $args[0]; 509 | } 510 | } 511 | -------------------------------------------------------------------------------- /src/Assertions/CollectionAssertions.php: -------------------------------------------------------------------------------- 1 | 5 | * Date: 9/16/14 6 | * Time: 10:19 PM. 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | namespace NilPortugues\Assert\Assertions; 12 | 13 | use Exception; 14 | use NilPortugues\Assert\Exceptions\AssertionException; 15 | 16 | class CollectionAssertions 17 | { 18 | const ASSERT_IS_ARRAY = 'Value must be an array.'; 19 | const ASSERT_KEY_FORMAT = 'Value array key format is not valid.'; 20 | const ASSERT_ENDS_WITH = 'Value array does not end as expected.'; 21 | const ASSERT_CONTAINS = 'Value was not found.'; 22 | const ASSERT_HAS_KEY = 'Value array has no %s.'; 23 | const ASSERT_HAS_LENGTH = 'Value must contain %s items.'; 24 | const ASSERT_IS_NOT_EMPTY = 'Value must have at least 1 item.'; 25 | const ASSERT_STARTS_WITH = 'Value array does not start as expected.'; 26 | 27 | /** 28 | * @param $value 29 | * @param string $message 30 | * 31 | * @throws AssertionException 32 | */ 33 | public static function isArray($value, $message = '') 34 | { 35 | $result = is_array($value) 36 | || (is_object($value) && $value instanceof \ArrayObject) 37 | || (is_object($value) && $value instanceof \SplFixedArray); 38 | 39 | if (false === $result) { 40 | throw new AssertionException( 41 | ($message) ? $message : self::ASSERT_IS_ARRAY 42 | ); 43 | } 44 | } 45 | 46 | /** 47 | * @param $value 48 | * @param callable $valueValidator 49 | * @param callable|null $keyValidator 50 | * @param string $message 51 | * 52 | * @throws AssertionException 53 | */ 54 | public static function each($value, callable $valueValidator, callable $keyValidator = null, $message = '') 55 | { 56 | foreach ($value as $key => $data) { 57 | try { 58 | $keyValidator($key); 59 | $valueValidator($data); 60 | } catch (\Exception $e) { 61 | throw new AssertionException( 62 | ($message) ? $message : $e->getMessage() 63 | ); 64 | } 65 | } 66 | } 67 | 68 | /** 69 | * @param $value 70 | * @param callable $keyValidator 71 | * @param string $message 72 | * 73 | * @throws AssertionException 74 | */ 75 | public static function hasKeyFormat($value, callable $keyValidator, $message = '') 76 | { 77 | if ($value instanceof \ArrayObject) { 78 | $value = $value->getArrayCopy(); 79 | } 80 | 81 | if ($value instanceof \SplFixedArray) { 82 | $value = $value->toArray(); 83 | } 84 | 85 | $arrayKeys = array_keys($value); 86 | 87 | foreach ($arrayKeys as $key) { 88 | try { 89 | $keyValidator($key); 90 | } catch (Exception $e) { 91 | throw new AssertionException( 92 | ($message) ? $message : self::ASSERT_KEY_FORMAT 93 | ); 94 | } 95 | } 96 | } 97 | 98 | /** 99 | * @param $haystack 100 | * @param $needle 101 | * @param bool $strict 102 | * @param string $message 103 | * 104 | * @throws AssertionException 105 | */ 106 | public static function endsWith($haystack, $needle, $strict = false, $message = '') 107 | { 108 | $last = end($haystack); 109 | settype($strict, 'bool'); 110 | 111 | if (false === $strict) { 112 | if ($last != $needle) { 113 | throw new AssertionException( 114 | ($message) ? $message : self::ASSERT_ENDS_WITH 115 | ); 116 | } 117 | 118 | return; 119 | } 120 | 121 | if ($last !== $needle) { 122 | throw new AssertionException( 123 | ($message) ? $message : self::ASSERT_ENDS_WITH 124 | ); 125 | } 126 | } 127 | 128 | /** 129 | * @param $haystack 130 | * @param $needle 131 | * @param bool $strict 132 | * @param string $message 133 | * 134 | * @throws AssertionException 135 | */ 136 | public static function contains($haystack, $needle, $strict = false, $message = '') 137 | { 138 | if ($haystack instanceof \ArrayObject) { 139 | $haystack = $haystack->getArrayCopy(); 140 | } 141 | 142 | if ($haystack instanceof \SplFixedArray) { 143 | $haystack = $haystack->toArray(); 144 | } 145 | 146 | settype($strict, 'bool'); 147 | 148 | if (false === $strict) { 149 | if (false === (in_array($needle, $haystack, false))) { 150 | throw new AssertionException( 151 | ($message) ? $message : self::ASSERT_CONTAINS 152 | ); 153 | } 154 | 155 | return; 156 | } 157 | 158 | if (false === (in_array($needle, $haystack, true))) { 159 | throw new AssertionException( 160 | ($message) ? $message : self::ASSERT_CONTAINS 161 | ); 162 | } 163 | } 164 | 165 | /** 166 | * @param $value 167 | * @param $keyName 168 | * @param string $message 169 | * 170 | * @throws AssertionException 171 | */ 172 | public static function hasKey($value, $keyName, $message = '') 173 | { 174 | if (false === array_key_exists($keyName, $value)) { 175 | throw new AssertionException( 176 | ($message) ? $message : sprintf(self::ASSERT_HAS_KEY, $keyName) 177 | ); 178 | } 179 | } 180 | 181 | /** 182 | * @param $value 183 | * @param $length 184 | * @param string $message 185 | * 186 | * @throws AssertionException 187 | */ 188 | public static function hasLength($value, $length, $message = '') 189 | { 190 | settype($length, 'int'); 191 | 192 | if (false === (count($value) === $length)) { 193 | throw new AssertionException( 194 | ($message) ? $message : sprintf(self::ASSERT_HAS_LENGTH, $length) 195 | ); 196 | } 197 | } 198 | 199 | /** 200 | * @param $value 201 | * @param string $message 202 | * 203 | * @throws AssertionException 204 | */ 205 | public static function isNotEmpty($value, $message = '') 206 | { 207 | if (false === (count($value) > 0)) { 208 | throw new AssertionException( 209 | ($message) ? $message : self::ASSERT_IS_NOT_EMPTY 210 | ); 211 | } 212 | } 213 | 214 | /** 215 | * @param $haystack 216 | * @param $needle 217 | * @param bool $strict 218 | * @param string $message 219 | * 220 | * @throws AssertionException 221 | */ 222 | public static function startsWith($haystack, $needle, $strict = false, $message = '') 223 | { 224 | $first = reset($haystack); 225 | settype($strict, 'bool'); 226 | 227 | if (false === $strict) { 228 | if ($first != $needle) { 229 | throw new AssertionException( 230 | ($message) ? $message : self::ASSERT_STARTS_WITH 231 | ); 232 | } 233 | 234 | return; 235 | } 236 | 237 | if ($first !== $needle) { 238 | throw new AssertionException( 239 | ($message) ? $message : self::ASSERT_STARTS_WITH 240 | ); 241 | } 242 | } 243 | } 244 | -------------------------------------------------------------------------------- /src/Assertions/DateTimeAssertions.php: -------------------------------------------------------------------------------- 1 | 5 | * Date: 9/21/14 6 | * Time: 8:18 PM. 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | namespace NilPortugues\Assert\Assertions; 12 | 13 | use DateTime; 14 | use DateTimeImmutable; 15 | use DateTimeInterface; 16 | use DateTimeZone; 17 | use NilPortugues\Assert\Exceptions\AssertionException; 18 | 19 | class DateTimeAssertions 20 | { 21 | const ASSERT_DATE_TIME = 'Value is not a valid date.'; 22 | const ASSERT_IS_MORNING = 'Time provided is not morning.'; 23 | const ASSERT_IS_AFTERNOON = 'Time provided is not afternoon.'; 24 | const ASSERT_IS_EVENING = 'Time provided is not evening.'; 25 | const ASSERT_IS_NIGHT = 'Time provided is not night.'; 26 | const ASSERT_IS_BETWEEN = 'Date provided must be between %s and %s.'; 27 | const ASSERT_IS_WEEKEND = 'Day provided is not a weekend day.'; 28 | const ASSERT_IS_WEEKDAY = 'Day provided is not a weekday.'; 29 | const ASSERT_IS_MONDAY = 'Day provided is not Monday.'; 30 | const ASSERT_IS_TUESDAY = 'Day provided is not Tuesday.'; 31 | const ASSERT_IS_WEDNESDAY = 'Day provided is not Wednesday.'; 32 | const ASSERT_IS_THURSDAY = 'Day provided is not Thursday.'; 33 | const ASSERT_IS_FRIDAY = 'Day provided is not Friday.'; 34 | const ASSERT_IS_SATURDAY = 'Day provided is not Saturday.'; 35 | const ASSERT_IS_SUNDAY = 'Day provided is not Sunday.'; 36 | const ASSERT_IS_TODAY = 'Day provided is not today.'; 37 | const ASSERT_IS_YESTERDAY = 'Day provided is not yesterday.'; 38 | const ASSERT_IS_TOMORROW = 'Day provided is not tomorrow.'; 39 | const ASSERT_IS_LEAP_YEAR = 'Year provided is not a leap year.'; 40 | const ASSERT_IS_AFTER = 'Date provided must be a after %s.'; 41 | const ASSERT_IS_BEFORE = 'Date provided must be a before %s.'; 42 | const ASSERT_FUTURE_DATE = 'Date provided is not set in the future.'; 43 | const ASSERT_PAST_DATE = 'Date provided is not set in the past.'; 44 | const ASSERT_IS_QUARTER = 'Date provided does not belong to the quarter %s of the year.'; 45 | const ASSERT_IS_TRIMESTER = 'Date provided does not belong to the trimester %s of the year.'; 46 | const ASSERT_IS_SECOND_FIRST_YEAR = 'Date provided does not belong to the second half of the year.'; 47 | const ASSERT_IS_SECOND_HALF_YEAR = 'Date provided does not belong to the second half of the year.'; 48 | const ASSERT_IS_IN_YEAR = 'Date provided does not belong to year %s.'; 49 | const ASSERT_IS_IN_NEXT_YEAR = 'Date provided does not belong to next year (%s).'; 50 | const ASSERT_IS_IN_PAST_YEAR = 'Date provided does not belong to past year (%s).'; 51 | const ASSERT_IS_IN_MONTH = 'Date provided does not belong to month (%s).'; 52 | const ASSERT_IS_IN_NEXT_MONTH = 'Date provided does not belong to next month (%s).'; 53 | const ASSERT_IS_IN_PAST_MONTH = 'Date provided does not belong to last month (%s).'; 54 | const ASSERT_IS_IN_WEEK = 'Date provided does not belong to week %s.'; 55 | const ASSERT_IS_NEXT_WEEK = 'Date provided does not belong to next week.'; 56 | const ASSERT_IS_LAST_WEEK = 'Date provided does not belong to last week.'; 57 | const ASSERT_IS_DST = 'Date provided is not in DST.'; 58 | 59 | /** 60 | * Checks if a value is a a valid datetime format. 61 | * 62 | * @param string|DateTimeInterface $value 63 | * @param string $message 64 | * 65 | * @throws AssertionException 66 | */ 67 | public static function isDateTime($value, $message = '') 68 | { 69 | if ($value instanceof DateTimeInterface) { 70 | return; 71 | } 72 | 73 | try { 74 | $date = new DateTimeImmutable($value); 75 | $errors = $date->getLastErrors(); 76 | } catch (\Exception $e) { 77 | $errors['warning_count'] = 1; 78 | $errors['error_count'] = 1; 79 | } 80 | 81 | if (false === (0 == $errors['warning_count'] && 0 == $errors['error_count'])) { 82 | throw new AssertionException( 83 | ($message) ? $message : sprintf(self::ASSERT_DATE_TIME, gettype($value)) 84 | ); 85 | } 86 | } 87 | 88 | /** 89 | * Checks if a given date is happening after the given limiting date. 90 | * 91 | * @param string|DateTimeInterface $value 92 | * @param string|DateTimeInterface $limit 93 | * @param bool $inclusive 94 | * @param string $message 95 | * 96 | * @throws AssertionException 97 | */ 98 | public static function isAfter($value, $limit, $inclusive = false, $message = '') 99 | { 100 | $value = self::convertToDateTime($value); 101 | $limit = self::convertToDateTime($limit); 102 | 103 | if (false === $inclusive) { 104 | if (!(strtotime($value->format('Y-m-d H:i:s')) > strtotime($limit->format('Y-m-d H:i:s')))) { 105 | throw new AssertionException( 106 | ($message) ? $message : sprintf(self::ASSERT_IS_AFTER, $limit->format('Y-m-d H:i:s')) 107 | ); 108 | } 109 | } 110 | 111 | if (!(strtotime($value->format('Y-m-d H:i:s')) >= strtotime($limit->format('Y-m-d H:i:s')))) { 112 | throw new AssertionException( 113 | ($message) ? $message : sprintf(self::ASSERT_IS_AFTER, $limit->format('Y-m-d H:i:s')) 114 | ); 115 | } 116 | } 117 | 118 | /** 119 | * @param string|DateTimeInterface $value 120 | * 121 | * @return DateTimeImmutable 122 | */ 123 | private static function convertToDateTime($value) 124 | { 125 | if ($value instanceof DateTimeInterface) { 126 | return new DateTimeImmutable($value->format(DATE_ATOM)); 127 | } 128 | 129 | return new DateTimeImmutable($value); 130 | } 131 | 132 | /** 133 | * Checks if a given date is happening before the given limiting date. 134 | * 135 | * @param string|DateTimeInterface $value 136 | * @param string|DateTimeInterface $limit 137 | * @param bool $inclusive 138 | * @param string $message 139 | * 140 | * @throws AssertionException 141 | */ 142 | public static function isBefore($value, $limit, $inclusive = false, $message = '') 143 | { 144 | $value = self::convertToDateTime($value); 145 | $limit = self::convertToDateTime($limit); 146 | 147 | if (false === $inclusive) { 148 | if (!(strtotime($value->format('Y-m-d H:i:s')) < strtotime($limit->format('Y-m-d H:i:s')))) { 149 | throw new AssertionException( 150 | ($message) ? $message : sprintf(self::ASSERT_IS_BEFORE, $limit->format('Y-m-d H:i:s')) 151 | ); 152 | } 153 | } 154 | 155 | if (!(strtotime($value->format('Y-m-d H:i:s')) <= strtotime($limit->format('Y-m-d H:i:s')))) { 156 | throw new AssertionException( 157 | ($message) ? $message : sprintf(self::ASSERT_IS_BEFORE, $limit->format('Y-m-d H:i:s')) 158 | ); 159 | } 160 | } 161 | 162 | /** 163 | * Checks if a given date is in a given range of dates. 164 | * 165 | * @param string|DateTimeInterface $value 166 | * @param bool $inclusive 167 | * @param string $minDate 168 | * @param string $maxDate 169 | * @param string $message 170 | * 171 | * @throws AssertionException 172 | */ 173 | public static function isDateRange($value, $minDate, $maxDate, $inclusive = false, $message = '') 174 | { 175 | $value = self::convertToDateTime($value); 176 | $minDate = self::convertToDateTime($minDate); 177 | $maxDate = self::convertToDateTime($maxDate); 178 | 179 | if (!$inclusive && !($value > $minDate && $value < $maxDate)) { 180 | $minDate = $minDate->format('Y-m-d H:i:s'); 181 | $maxDate = $maxDate->format('Y-m-d H:i:s'); 182 | throw new AssertionException( 183 | ($message) ? $message : sprintf(self::ASSERT_IS_BETWEEN, $minDate, $maxDate) 184 | ); 185 | } 186 | 187 | if ($inclusive && !($value >= $minDate && $value <= $maxDate)) { 188 | $minDate = $minDate->format('Y-m-d H:i:s'); 189 | $maxDate = $maxDate->format('Y-m-d H:i:s'); 190 | throw new AssertionException( 191 | ($message) ? $message : sprintf(self::ASSERT_IS_BETWEEN, $minDate, $maxDate) 192 | ); 193 | } 194 | } 195 | 196 | /** 197 | * @param string|DateTimeInterface $value 198 | * @param string $message 199 | * 200 | * @throws AssertionException 201 | */ 202 | public static function isWeekend($value, $message = '') 203 | { 204 | $value = self::convertToDateTime($value); 205 | 206 | if ('0' != $value->format('w') && '6' != $value->format('w')) { 207 | throw new AssertionException( 208 | ($message) ? $message : self::ASSERT_IS_WEEKEND 209 | ); 210 | } 211 | } 212 | 213 | /** 214 | * @param string|DateTimeInterface $value 215 | * @param string $message 216 | * 217 | * @throws AssertionException 218 | */ 219 | public static function isWeekday($value, $message = '') 220 | { 221 | $value = self::convertToDateTime($value); 222 | 223 | if ($value->format('w') == 0 || $value->format('w') == 6) { 224 | throw new AssertionException( 225 | ($message) ? $message : self::ASSERT_IS_WEEKDAY 226 | ); 227 | } 228 | } 229 | 230 | /** 231 | * @param string|DateTimeInterface $value 232 | * @param string $message 233 | * 234 | * @throws AssertionException 235 | */ 236 | public static function isMonday($value, $message = '') 237 | { 238 | $value = self::convertToDateTime($value); 239 | 240 | if (!('1' == $value->format('w'))) { 241 | throw new AssertionException( 242 | ($message) ? $message : self::ASSERT_IS_MONDAY 243 | ); 244 | } 245 | } 246 | 247 | /** 248 | * @param string|DateTimeInterface $value 249 | * @param string $message 250 | * 251 | * @throws AssertionException 252 | */ 253 | public static function isTuesday($value, $message = '') 254 | { 255 | $value = self::convertToDateTime($value); 256 | 257 | if (!('2' == $value->format('w'))) { 258 | throw new AssertionException( 259 | ($message) ? $message : self::ASSERT_IS_TUESDAY 260 | ); 261 | } 262 | } 263 | 264 | /** 265 | * @param string|DateTimeInterface $value 266 | * @param string $message 267 | * 268 | * @throws AssertionException 269 | */ 270 | public static function isWednesday($value, $message = '') 271 | { 272 | $value = self::convertToDateTime($value); 273 | 274 | if (!('3' == $value->format('w'))) { 275 | throw new AssertionException( 276 | ($message) ? $message : self::ASSERT_IS_WEDNESDAY 277 | ); 278 | } 279 | } 280 | 281 | /** 282 | * @param string|DateTimeInterface $value 283 | * @param string $message 284 | * 285 | * @throws AssertionException 286 | */ 287 | public static function isThursday($value, $message = '') 288 | { 289 | $value = self::convertToDateTime($value); 290 | 291 | if (!('4' == $value->format('w'))) { 292 | throw new AssertionException( 293 | ($message) ? $message : self::ASSERT_IS_THURSDAY 294 | ); 295 | } 296 | } 297 | 298 | /** 299 | * @param string|DateTimeInterface $value 300 | * @param string $message 301 | * 302 | * @throws AssertionException 303 | */ 304 | public static function isFriday($value, $message = '') 305 | { 306 | $value = self::convertToDateTime($value); 307 | 308 | if (!('5' == $value->format('w'))) { 309 | throw new AssertionException( 310 | ($message) ? $message : self::ASSERT_IS_FRIDAY 311 | ); 312 | } 313 | } 314 | 315 | /** 316 | * @param string|DateTimeInterface $value 317 | * @param string $message 318 | * 319 | * @throws AssertionException 320 | */ 321 | public static function isSaturday($value, $message = '') 322 | { 323 | $value = self::convertToDateTime($value); 324 | 325 | if (!('6' == $value->format('w'))) { 326 | throw new AssertionException( 327 | ($message) ? $message : self::ASSERT_IS_SATURDAY 328 | ); 329 | } 330 | } 331 | 332 | /** 333 | * @param string|DateTimeInterface $value 334 | * @param string $message 335 | * 336 | * @throws AssertionException 337 | */ 338 | public static function isSunday($value, $message = '') 339 | { 340 | $value = self::convertToDateTime($value); 341 | 342 | if (!('0' == $value->format('w'))) { 343 | throw new AssertionException( 344 | ($message) ? $message : self::ASSERT_IS_SUNDAY 345 | ); 346 | } 347 | } 348 | 349 | /** 350 | * @param string|DateTimeInterface $value 351 | * @param string $message 352 | * 353 | * @throws AssertionException 354 | */ 355 | public static function isToday($value, $message = '') 356 | { 357 | $value = self::convertToDateTime($value); 358 | 359 | $date = new DateTime('now'); 360 | 361 | if (!($date->format('Y-m-d') === $value->format('Y-m-d'))) { 362 | throw new AssertionException( 363 | ($message) ? $message : self::ASSERT_IS_TODAY 364 | ); 365 | } 366 | } 367 | 368 | /** 369 | * @param string|DateTimeInterface $value 370 | * @param string $message 371 | * 372 | * @throws AssertionException 373 | */ 374 | public static function isYesterday($value, $message = '') 375 | { 376 | $value = self::convertToDateTime($value); 377 | 378 | $date = new DateTime('now - 1 day'); 379 | 380 | if (!($date->format('Y-m-d') === $value->format('Y-m-d'))) { 381 | throw new AssertionException( 382 | ($message) ? $message : self::ASSERT_IS_YESTERDAY 383 | ); 384 | } 385 | } 386 | 387 | /** 388 | * @param string|DateTimeInterface $value 389 | * @param string $message 390 | * 391 | * @throws AssertionException 392 | */ 393 | public static function isTomorrow($value, $message = '') 394 | { 395 | $value = self::convertToDateTime($value); 396 | 397 | $date = new DateTime('now + 1 day'); 398 | 399 | if (!($date->format('Y-m-d') === $value->format('Y-m-d'))) { 400 | throw new AssertionException( 401 | ($message) ? $message : self::ASSERT_IS_TOMORROW 402 | ); 403 | } 404 | } 405 | 406 | /** 407 | * Determines if the instance is a leap year. 408 | * 409 | * 410 | * @param string|DateTimeInterface $value 411 | * @param string $message 412 | * 413 | * @throws AssertionException 414 | */ 415 | public static function isLeapYear($value, $message = '') 416 | { 417 | $value = self::convertToDateTime($value); 418 | 419 | if (!(('1' == $value->format('L')))) { 420 | throw new AssertionException( 421 | ($message) ? $message : self::ASSERT_IS_LEAP_YEAR 422 | ); 423 | } 424 | } 425 | 426 | /** 427 | * @param string|DateTimeInterface $value 428 | * @param string $message 429 | * 430 | * @throws AssertionException 431 | */ 432 | public static function isMorning($value, $message = '') 433 | { 434 | $value = self::convertToDateTime($value); 435 | $date = strtotime($value->format('H:i:s')); 436 | 437 | if (!($date >= strtotime($value->format('06:00:00')) && $date <= strtotime($value->format('11:59:59')))) { 438 | throw new AssertionException( 439 | ($message) ? $message : self::ASSERT_IS_MORNING 440 | ); 441 | } 442 | } 443 | 444 | /** 445 | * @param string|DateTimeInterface $value 446 | * @param string $message 447 | * 448 | * @throws AssertionException 449 | */ 450 | public static function isAfternoon($value, $message = '') 451 | { 452 | $value = self::convertToDateTime($value); 453 | $date = strtotime($value->format('H:i:s')); 454 | 455 | if (!($date >= strtotime($value->format('12:00:00')) && $date <= strtotime($value->format('17:59:59')))) { 456 | throw new AssertionException( 457 | ($message) ? $message : self::ASSERT_IS_AFTERNOON 458 | ); 459 | } 460 | } 461 | 462 | /** 463 | * @param string|DateTimeInterface $value 464 | * @param string $message 465 | * 466 | * @throws AssertionException 467 | */ 468 | public static function isEvening($value, $message = '') 469 | { 470 | $value = self::convertToDateTime($value); 471 | $date = strtotime($value->format('H:i:s')); 472 | 473 | if (!($date >= strtotime($value->format('18:00:00')) && $date <= strtotime($value->format('23:59:59')))) { 474 | throw new AssertionException( 475 | ($message) ? $message : self::ASSERT_IS_EVENING 476 | ); 477 | } 478 | } 479 | 480 | /** 481 | * @param string|DateTimeInterface $value 482 | * @param string $message 483 | * 484 | * @throws AssertionException 485 | */ 486 | public static function isNight($value, $message = '') 487 | { 488 | $value = self::convertToDateTime($value); 489 | $date = strtotime($value->format('H:i:s')); 490 | 491 | if (!($date >= strtotime($value->format('00:00:00')) && $date <= strtotime($value->format('05:59:59')))) { 492 | throw new AssertionException( 493 | ($message) ? $message : self::ASSERT_IS_NIGHT 494 | ); 495 | } 496 | } 497 | 498 | /** 499 | * @param string|DateTimeInterface $value 500 | * @param string $message 501 | * 502 | * @throws AssertionException 503 | */ 504 | public static function isFutureDate($value, $message = '') 505 | { 506 | $value = self::convertToDateTime($value); 507 | 508 | if (false === ($value > new DateTime())) { 509 | throw new AssertionException( 510 | ($message) ? $message : self::ASSERT_FUTURE_DATE 511 | ); 512 | } 513 | } 514 | 515 | /** 516 | * @param string|DateTimeInterface $value 517 | * @param string $message 518 | * 519 | * @throws AssertionException 520 | */ 521 | public static function isPastDate($value, $message = '') 522 | { 523 | $value = self::convertToDateTime($value); 524 | 525 | if (false === ($value < new DateTime())) { 526 | throw new AssertionException( 527 | ($message) ? $message : self::ASSERT_PAST_DATE 528 | ); 529 | } 530 | } 531 | 532 | /** 533 | * @param string|DateTimeInterface $value 534 | * @param string $message 535 | * 536 | * @throws AssertionException 537 | */ 538 | public static function isInNextWeek($value, $message = '') 539 | { 540 | $nextWeek = (new DateTime('now +1 week'))->format('W'); 541 | 542 | self::isInWeek( 543 | $value, 544 | $nextWeek, 545 | ($message) ? $message : sprintf(self::ASSERT_IS_NEXT_WEEK, $nextWeek) 546 | ); 547 | } 548 | 549 | /** 550 | * @param string|DateTimeInterface $value 551 | * @param $weekNumber 552 | * @param string $message 553 | */ 554 | public static function isInWeek($value, $weekNumber, $message = '') 555 | { 556 | $weekValue = (int) self::convertToDateTime($value)->format('W'); 557 | $weekNumber = (int) $weekNumber; 558 | 559 | if (false === ($weekValue == $weekNumber)) { 560 | throw new AssertionException( 561 | ($message) ? $message : sprintf(self::ASSERT_IS_IN_WEEK, $weekNumber) 562 | ); 563 | } 564 | } 565 | 566 | /** 567 | * @param string|DateTimeInterface $value 568 | * @param string $message 569 | * 570 | * @throws AssertionException 571 | */ 572 | public static function isInLastWeek($value, $message = '') 573 | { 574 | $lastWeek = (int) (new DateTime('now -1 week'))->format('W'); 575 | 576 | self::isInWeek( 577 | $value, 578 | $lastWeek, 579 | ($message) ? $message : sprintf(self::ASSERT_IS_LAST_WEEK, $lastWeek) 580 | ); 581 | } 582 | 583 | /** 584 | * @param string|DateTimeInterface $value 585 | * @param string $message 586 | * 587 | * @throws AssertionException 588 | */ 589 | public static function isInLastMonth($value, $message = '') 590 | { 591 | $date = strtotime('-1 month'); 592 | $past = DateTime::createFromFormat('U', $date); 593 | 594 | self::isInMonth( 595 | $value, 596 | $past->format('m'), 597 | ($message) ? $message : sprintf(self::ASSERT_IS_IN_PAST_MONTH, $past->format('F')) 598 | ); 599 | } 600 | 601 | /** 602 | * @param string|DateTimeInterface $value 603 | * @param $monthNumber 604 | * @param string $message 605 | */ 606 | public static function isInMonth($value, $monthNumber, $message = '') 607 | { 608 | $value = self::convertToDateTime($value); 609 | $currentYear = date('Y'); 610 | 611 | 612 | if ($currentYear != $value->format('Y') || 613 | $monthNumber != $value->format('n') 614 | ) { 615 | throw new AssertionException( 616 | ($message) ? $message : sprintf(self::ASSERT_IS_IN_MONTH, (new DateTime('1970-'.$monthNumber.'-01'))->format('F')) 617 | ); 618 | } 619 | } 620 | 621 | /** 622 | * @param string|DateTimeInterface $value 623 | * @param string $message 624 | * 625 | * @throws AssertionException 626 | */ 627 | public static function isInNextMonth($value, $message = '') 628 | { 629 | $date = strtotime('+1 month'); 630 | $next = DateTime::createFromFormat('U', $date); 631 | 632 | self::isInMonth( 633 | $value, 634 | $next->format('m'), 635 | ($message) ? $message : sprintf(self::ASSERT_IS_IN_NEXT_MONTH, $next->format('F')) 636 | ); 637 | } 638 | 639 | /** 640 | * @param string|DateTimeInterface $value 641 | * @param string $message 642 | * 643 | * @throws AssertionException 644 | */ 645 | public static function isInLastYear($value, $message = '') 646 | { 647 | $pastYear = (new DateTime('now -1 year'))->format('Y'); 648 | 649 | self::isInYear( 650 | $value, 651 | $pastYear, 652 | ($message) ? $message : sprintf(self::ASSERT_IS_IN_PAST_YEAR, $pastYear) 653 | ); 654 | } 655 | 656 | /** 657 | * @param string|DateTimeInterface $value 658 | * @param string $yearNumber 659 | * @param string $message 660 | */ 661 | public static function isInYear($value, $yearNumber, $message = '') 662 | { 663 | $value = self::convertToDateTime($value); 664 | 665 | if ($value->format('Y') != $yearNumber) { 666 | throw new AssertionException( 667 | ($message) ? $message : sprintf(self::ASSERT_IS_IN_YEAR, $yearNumber) 668 | ); 669 | } 670 | } 671 | 672 | /** 673 | * @param string|DateTimeInterface $value 674 | * @param string $message 675 | * 676 | * @throws AssertionException 677 | */ 678 | public static function isInNextYear($value, $message = '') 679 | { 680 | $next = (new DateTime('now +1 year'))->format('Y'); 681 | 682 | self::isInYear( 683 | $value, 684 | $next, 685 | ($message) ? $message : sprintf(self::ASSERT_IS_IN_NEXT_YEAR, $next) 686 | ); 687 | } 688 | 689 | /** 690 | * @param string|DateTimeInterface $value 691 | * @param string $message 692 | * 693 | * @throws AssertionException 694 | */ 695 | public static function isFirstHalfOfYear($value, $message = '') 696 | { 697 | $value = self::convertToDateTime($value); 698 | $half = (int) (($value->format('n') - 1) / 6); 699 | 700 | if (1 == $half) { 701 | throw new AssertionException( 702 | ($message) ? $message : self::ASSERT_IS_SECOND_HALF_YEAR 703 | ); 704 | } 705 | } 706 | 707 | /** 708 | * @param string|DateTimeInterface $value 709 | * @param string $message 710 | * 711 | * @throws AssertionException 712 | */ 713 | public static function isSecondHalfOfYear($value, $message = '') 714 | { 715 | $value = self::convertToDateTime($value); 716 | $half = (int) (($value->format('n') - 1) / 6); 717 | 718 | if (0 == $half) { 719 | throw new AssertionException( 720 | ($message) ? $message : self::ASSERT_IS_SECOND_HALF_YEAR 721 | ); 722 | } 723 | } 724 | 725 | /** 726 | * @param string|DateTimeInterface $value 727 | * @param string $trimester 728 | * @param string $message 729 | * 730 | * @throws AssertionException 731 | */ 732 | public static function isTrimesterOfYear($value, $trimester, $message = '') 733 | { 734 | $trimester = (int) $trimester; 735 | 736 | if ($trimester < 1 || $trimester > 4) { 737 | throw new AssertionException('Provided trimester value must range from 1 to 4'); 738 | } 739 | 740 | $value = self::convertToDateTime($value); 741 | $currentTrimester = (int) (($value->format('n') - 1) / 3); 742 | 743 | if ($currentTrimester != ($trimester - 1)) { 744 | throw new AssertionException( 745 | ($message) ? $message : sprintf(self::ASSERT_IS_TRIMESTER, $trimester) 746 | ); 747 | } 748 | } 749 | 750 | /** 751 | * @param string|DateTimeInterface $value 752 | * @param string $quarter 753 | * @param string $message 754 | * 755 | * @throws AssertionException 756 | */ 757 | public static function isQuarterOfYear($value, $quarter, $message = '') 758 | { 759 | $quarter = (int) $quarter; 760 | if ($quarter < 1 || $quarter > 3) { 761 | throw new AssertionException('Provided quarter value must range from 1 to 3'); 762 | } 763 | 764 | $value = self::convertToDateTime($value); 765 | $currentQuarter = (($value->format('n')) / 4); 766 | 767 | if ($currentQuarter != $quarter) { 768 | throw new AssertionException( 769 | ($message) ? $message : sprintf(self::ASSERT_IS_QUARTER, $quarter) 770 | ); 771 | } 772 | } 773 | 774 | /** 775 | * @param string|DateTimeInterface $value 776 | * @param DateTimeZone $timeZone 777 | * @param string $message 778 | */ 779 | public static function isDayLightSavingTime($value, DateTimeZone $timeZone, $message = '') 780 | { 781 | $value = self::convertToDateTime($value); 782 | $transitions = timezone_transitions_get($timeZone, $value->getTimestamp(), $value->getTimestamp()); 783 | 784 | if (is_array($transitions) && !empty($transitions)) { 785 | foreach ($transitions as $transition) { 786 | if (0 == $transition['isdst']) { 787 | throw new AssertionException(($message) ? $message : self::ASSERT_IS_DST); 788 | } 789 | } 790 | } 791 | } 792 | } 793 | -------------------------------------------------------------------------------- /src/Assertions/FileUploadAssertions.php: -------------------------------------------------------------------------------- 1 | 5 | * Date: 9/24/14 6 | * Time: 1:12 PM. 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | namespace NilPortugues\Assert\Assertions; 12 | 13 | use Exception; 14 | use NilPortugues\Assert\Exceptions\AssertionException; 15 | use NilPortugues\Assert\Exceptions\FileUploadException; 16 | 17 | class FileUploadAssertions 18 | { 19 | const ASSERT_FILE_UPLOAD = 'Value has no files.'; 20 | const ASSERT_IS_BETWEEN = 'Value must be between %s and %s %s.'; 21 | const ASSERT_IS_MIME_TYPE = 'Value is not a valid file format.'; 22 | const ASSERT_HAS_FILE_NAME_FORMAT = 'Value file name format is not valid.'; 23 | const ASSERT_HAS_VALID_UPLOAD_DIRECTORY = 'Value upload directory is not valid.'; 24 | const ASSERT_NOT_OVERWRITING_EXISTING_FILE = 'Value upload will overwrite an existing file.'; 25 | 26 | const ASSERT_IS_IMAGE = 'Value must be a valid image file.'; 27 | const ASSERT_HAS_LENGTH = 'Value must be %s files.'; 28 | 29 | /** 30 | * @var array 31 | */ 32 | private static $byte = [ 33 | 'K' => 1000, 34 | 'KB' => 1000, 35 | 'M' => 1000000, 36 | 'MB' => 1000000, 37 | 'G' => 1000000000, 38 | 'GB' => 1000000000, 39 | 'T' => 1000000000000, 40 | 'TB' => 1000000000000, 41 | ]; 42 | 43 | /** 44 | * Validates if the given data is a file that was uploaded. 45 | * 46 | * @param string $uploadName 47 | * @param string $message 48 | * 49 | * @throws AssertionException 50 | */ 51 | public static function isFileUploaded($uploadName, $message = '') 52 | { 53 | if (false === array_key_exists($uploadName, $_FILES)) { 54 | throw new AssertionException(($message) ? $message : self::ASSERT_FILE_UPLOAD); 55 | } 56 | } 57 | 58 | /** 59 | * @param string $uploadName 60 | * @param string $minSize 61 | * @param string $maxSize 62 | * @param string $format 63 | * @param bool $inclusive 64 | * @param string $message 65 | * 66 | * @throws AssertionException 67 | */ 68 | public static function isFileUploadedBetweenFileSize( 69 | $uploadName, 70 | $minSize, 71 | $maxSize, 72 | $format = 'B', 73 | $inclusive = false, 74 | $message = '' 75 | ) { 76 | $multiplier = 1; 77 | if (array_key_exists(strtoupper($format), self::$byte)) { 78 | $multiplier = self::$byte[$format]; 79 | } 80 | 81 | $minSize = $minSize * $multiplier; 82 | $maxSize = $maxSize * $multiplier; 83 | $maxSize = min(self::getMaxServerFileSize(), $maxSize); 84 | 85 | if (isset($_FILES[$uploadName]['size']) && is_array($_FILES[$uploadName]['size'])) { 86 | foreach ($_FILES[$uploadName]['size'] as $size) { 87 | self::checkIfMaximumUploadFileSizeHasBeenExceeded($uploadName, $maxSize, $size); 88 | IntegerAssertions::isBetween( 89 | $size, 90 | $minSize, 91 | $maxSize, 92 | $inclusive, 93 | sprintf(self::ASSERT_IS_BETWEEN, $minSize / $multiplier, $maxSize / $multiplier, $format) 94 | ); 95 | } 96 | 97 | return; 98 | } 99 | 100 | if (!isset($_FILES[$uploadName]['size'])) { 101 | throw new AssertionException( 102 | ($message) ? $message : sprintf(self::ASSERT_IS_BETWEEN, $minSize / $multiplier, $maxSize / $multiplier, $format) 103 | ); 104 | } 105 | 106 | self::checkIfMaximumUploadFileSizeHasBeenExceeded($uploadName, $maxSize, $_FILES[$uploadName]['size']); 107 | 108 | IntegerAssertions::isBetween( 109 | $_FILES[$uploadName]['size'], 110 | $minSize, 111 | $maxSize, 112 | $inclusive, 113 | ($message) ? $message : sprintf(self::ASSERT_IS_BETWEEN, $minSize / $multiplier, $maxSize / $multiplier, $format) 114 | ); 115 | } 116 | 117 | /** 118 | * @return int 119 | */ 120 | private static function getMaxServerFileSize() 121 | { 122 | $maxFileSize = min(ini_get('post_max_size'), ini_get('upload_max_filesize')); 123 | $maxFileSizeUnit = preg_replace('/\d/', '', $maxFileSize); 124 | 125 | $finalMaxFileSize = 0; 126 | if (array_key_exists(strtoupper($maxFileSizeUnit), self::$byte)) { 127 | $multiplier = self::$byte[$maxFileSizeUnit]; 128 | $finalMaxFileSize = preg_replace('/[^0-9,.]/', '', $maxFileSize); 129 | $finalMaxFileSize = $finalMaxFileSize * $multiplier; 130 | } 131 | 132 | return (int) $finalMaxFileSize; 133 | } 134 | 135 | /** 136 | * @param string $uploadName 137 | * @param string $size 138 | * @param string $maxSize 139 | * 140 | * @throws FileUploadException 141 | */ 142 | private static function checkIfMaximumUploadFileSizeHasBeenExceeded($uploadName, $size, $maxSize) 143 | { 144 | if ($size < $maxSize) { 145 | throw new FileUploadException($uploadName); 146 | } 147 | } 148 | 149 | /** 150 | * @param string $uploadName 151 | * @param callable $assertion 152 | * @param string $message 153 | * 154 | * @throws AssertionException 155 | */ 156 | public static function hasFileUploadedFileNameFormat($uploadName, callable $assertion, $message = '') 157 | { 158 | if (isset($_FILES[$uploadName]['name']) && is_array($_FILES[$uploadName]['name'])) { 159 | foreach ($_FILES[$uploadName]['name'] as $name) { 160 | try { 161 | $assertion($name); 162 | } catch (Exception $e) { 163 | throw new AssertionException(($message) ? $message : self::ASSERT_HAS_FILE_NAME_FORMAT); 164 | } 165 | } 166 | 167 | return; 168 | } 169 | 170 | try { 171 | $assertion($_FILES[$uploadName]['name']); 172 | } catch (Exception $e) { 173 | throw new AssertionException(($message) ? $message : self::ASSERT_HAS_FILE_NAME_FORMAT); 174 | } 175 | } 176 | 177 | /** 178 | * @param string $uploadName 179 | * @param string $uploadDir 180 | * @param string $message 181 | * 182 | * @throws AssertionException 183 | */ 184 | public static function hasFileUploadedValidUploadDirectory($uploadName, $uploadDir, $message = '') 185 | { 186 | if (isset($_FILES[$uploadName]['name']) && (!file_exists($uploadDir) || !is_dir($uploadDir) || !is_writable($uploadDir))) { 187 | throw new AssertionException( 188 | ($message) ? $message : self::ASSERT_HAS_VALID_UPLOAD_DIRECTORY 189 | ); 190 | } 191 | } 192 | 193 | /** 194 | * @param string $uploadName 195 | * @param string $uploadDir 196 | * @param string $message 197 | * 198 | * @throws AssertionException 199 | */ 200 | public static function isFileUploadedNotOverwritingExistingFile($uploadName, $uploadDir, $message = '') 201 | { 202 | if (isset($_FILES[$uploadName]['name']) && is_array($_FILES[$uploadName]['name'])) { 203 | foreach ($_FILES[$uploadName]['name'] as $name) { 204 | if (file_exists($uploadDir.DIRECTORY_SEPARATOR.$name)) { 205 | throw new AssertionException( 206 | ($message) ? $message : self::ASSERT_NOT_OVERWRITING_EXISTING_FILE 207 | ); 208 | } 209 | } 210 | 211 | return; 212 | } 213 | 214 | if (isset($_FILES[$uploadName]['name']) && file_exists($uploadDir.DIRECTORY_SEPARATOR.$_FILES[$uploadName]['name'])) { 215 | throw new AssertionException( 216 | ($message) ? $message : self::ASSERT_NOT_OVERWRITING_EXISTING_FILE 217 | ); 218 | } 219 | } 220 | 221 | /** 222 | * @param string $uploadName 223 | * @param IntegerAssertions $size 224 | * @param string $message 225 | * 226 | * @throws AssertionException 227 | */ 228 | public static function hasFileUploadedFileNameLength($uploadName, $size, $message = '') 229 | { 230 | settype($size, 'int'); 231 | 232 | if (isset($_FILES[$uploadName]['name']) && is_array($_FILES[$uploadName]['name']) && $size >= 0) { 233 | if ($size != count($_FILES[$uploadName]['name'])) { 234 | throw new AssertionException( 235 | ($message) ? $message : sprintf(self::ASSERT_HAS_LENGTH, $size) 236 | ); 237 | } 238 | 239 | return; 240 | } 241 | 242 | if (false === (1 == $size && isset($_FILES[$uploadName]['name']))) { 243 | throw new AssertionException( 244 | ($message) ? $message : sprintf(self::ASSERT_HAS_LENGTH, $size) 245 | ); 246 | } 247 | } 248 | 249 | /** 250 | * @param string $uploadName 251 | * @param string $message 252 | * 253 | * @throws AssertionException 254 | */ 255 | public static function isFileUploadedImage($uploadName, $message = '') 256 | { 257 | self::isFileUploadedMimeType($uploadName, ['image/gif', 'image/jpeg', 'image/png'], $message); 258 | } 259 | 260 | /** 261 | * @param string $uploadName 262 | * @param string[] $allowedTypes 263 | * @param string $message 264 | * 265 | * @throws AssertionException 266 | */ 267 | public static function isFileUploadedMimeType($uploadName, array $allowedTypes, $message = '') 268 | { 269 | if (isset($_FILES[$uploadName]['tmp_name']) && is_array($_FILES[$uploadName]['tmp_name'])) { 270 | array_filter($_FILES[$uploadName]['tmp_name']); 271 | foreach ($_FILES[$uploadName]['tmp_name'] as $name) { 272 | if (false === in_array(self::getMimeType($name), $allowedTypes, true)) { 273 | throw new AssertionException(($message) ? $message : self::ASSERT_IS_MIME_TYPE); 274 | } 275 | } 276 | 277 | return; 278 | } 279 | 280 | if (!isset($_FILES[$uploadName]['tmp_name'])) { 281 | throw new AssertionException(($message) ? $message : self::ASSERT_IS_MIME_TYPE); 282 | } 283 | 284 | if (false === in_array(self::getMimeType($_FILES[$uploadName]['tmp_name']), $allowedTypes, true)) { 285 | throw new AssertionException(($message) ? $message : self::ASSERT_IS_MIME_TYPE); 286 | } 287 | } 288 | 289 | /** 290 | * @param $filePath 291 | * 292 | * @return string 293 | */ 294 | private static function getMimeType($filePath) 295 | { 296 | $currentErrorReporting = error_reporting(); 297 | error_reporting(0); 298 | 299 | $mimeType = ''; 300 | $fileInfo = finfo_open(FILEINFO_MIME_TYPE); 301 | if (false !== $fileInfo) { 302 | $mimeType = (string) finfo_file($fileInfo, $filePath); 303 | finfo_close($fileInfo); 304 | } 305 | error_reporting($currentErrorReporting); 306 | 307 | return $mimeType; 308 | } 309 | } 310 | -------------------------------------------------------------------------------- /src/Assertions/FloatAssertions.php: -------------------------------------------------------------------------------- 1 | 5 | * Date: 9/16/14 6 | * Time: 10:19 PM. 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | namespace NilPortugues\Assert\Assertions; 12 | 13 | use NilPortugues\Assert\Exceptions\AssertionException; 14 | 15 | class FloatAssertions 16 | { 17 | const ASSERT_FLOAT = 'Value must be a float.'; 18 | const ASSERT_IS_NOT_ZERO = 'Value must not be zero.'; 19 | const ASSERT_IS_POSITIVE = 'Value must be a positive value.'; 20 | const ASSERT_IS_POSITIVE_OR_ZERO = 'Value must be a positive value or zero.'; 21 | const ASSERT_IS_NEGATIVE = 'Value must be a negative value.'; 22 | const ASSERT_IS_NEGATIVE_OR_ZERO = 'Value must be a negative value or zero'; 23 | const ASSERT_IS_BETWEEN = 'Value must be between %s and %s.'; 24 | const ASSERT_IS_ODD = 'Value must be divisible by 3.'; 25 | const ASSERT_IS_EVEN = 'Value must be divisible by 2.'; 26 | const ASSERT_IS_MULTIPLE = 'Value must be multiple of %s.'; 27 | 28 | /** 29 | * @param $value 30 | * @param string $message 31 | * 32 | * @return AssertionException 33 | */ 34 | public static function isFloat($value, $message = '') 35 | { 36 | if (false === is_float($value)) { 37 | throw new AssertionException( 38 | ($message) ? $message : self::ASSERT_FLOAT 39 | ); 40 | } 41 | } 42 | 43 | /** 44 | * @param $value 45 | * @param string $message 46 | * 47 | * @return AssertionException 48 | */ 49 | public static function isNotZero($value, $message = '') 50 | { 51 | settype($value, 'float'); 52 | 53 | if (false === (0 != $value)) { 54 | throw new AssertionException( 55 | ($message) ? $message : self::ASSERT_IS_NOT_ZERO 56 | ); 57 | } 58 | } 59 | 60 | /** 61 | * @param float $value 62 | * @param string $message 63 | * 64 | * @return AssertionException 65 | */ 66 | public static function isPositiveOrZero($value, $message = '') 67 | { 68 | settype($value, 'float'); 69 | 70 | if (false === (0 <= $value)) { 71 | throw new AssertionException( 72 | ($message) ? $message : self::ASSERT_IS_POSITIVE_OR_ZERO 73 | ); 74 | } 75 | } 76 | 77 | /** 78 | * @param float $value 79 | * @param string $message 80 | * 81 | * @return AssertionException 82 | */ 83 | public static function isPositive($value, $message = '') 84 | { 85 | settype($value, 'float'); 86 | 87 | if (false === (0 < $value)) { 88 | throw new AssertionException( 89 | ($message) ? $message : self::ASSERT_IS_POSITIVE 90 | ); 91 | } 92 | } 93 | 94 | /** 95 | * @param float $value 96 | * @param string $message 97 | * 98 | * @return AssertionException 99 | */ 100 | public static function isNegativeOrZero($value, $message = '') 101 | { 102 | settype($value, 'float'); 103 | 104 | if (false === (0 >= $value)) { 105 | throw new AssertionException( 106 | ($message) ? $message : self::ASSERT_IS_NEGATIVE_OR_ZERO 107 | ); 108 | } 109 | } 110 | 111 | /** 112 | * @param float $value 113 | * @param string $message 114 | * 115 | * @return AssertionException 116 | */ 117 | public static function isNegative($value, $message = '') 118 | { 119 | settype($value, 'float'); 120 | 121 | if (false === (0 > $value)) { 122 | throw new AssertionException( 123 | ($message) ? $message : self::ASSERT_IS_NEGATIVE 124 | ); 125 | } 126 | } 127 | 128 | /** 129 | * @param float $value 130 | * @param float $min 131 | * @param float $max 132 | * @param bool $inclusive 133 | * @param string $message 134 | * 135 | * @throws \InvalidArgumentException 136 | * 137 | * @return AssertionException 138 | */ 139 | public static function isBetween($value, $min, $max, $inclusive = false, $message = '') 140 | { 141 | settype($value, 'float'); 142 | settype($min, 'float'); 143 | settype($max, 'float'); 144 | 145 | if ($min > $max) { 146 | throw new \InvalidArgumentException(sprintf('%s cannot be less than %s for validation', $min, $max)); 147 | } 148 | 149 | if (false === $inclusive) { 150 | if (false === (($min < $value) && ($value < $max))) { 151 | throw new AssertionException( 152 | ($message) ? $message : sprintf(self::ASSERT_IS_BETWEEN, $min, $max) 153 | ); 154 | } 155 | 156 | return; 157 | } 158 | 159 | if (false === (($min <= $value) && ($value <= $max))) { 160 | throw new AssertionException( 161 | ($message) ? $message : sprintf(self::ASSERT_IS_BETWEEN, $min, $max) 162 | ); 163 | } 164 | } 165 | 166 | /** 167 | * @param float $value 168 | * @param string $message 169 | * 170 | * @return AssertionException 171 | */ 172 | public static function isOdd($value, $message = '') 173 | { 174 | settype($value, 'int'); 175 | 176 | if (false === (1 == ($value % 2))) { 177 | throw new AssertionException( 178 | ($message) ? $message : self::ASSERT_IS_ODD 179 | ); 180 | } 181 | } 182 | 183 | /** 184 | * @param float $value 185 | * @param string $message 186 | * 187 | * @return AssertionException 188 | */ 189 | public static function isEven($value, $message = '') 190 | { 191 | settype($value, 'int'); 192 | 193 | if (false === (0 == ($value % 2))) { 194 | throw new AssertionException( 195 | ($message) ? $message : self::ASSERT_IS_EVEN 196 | ); 197 | } 198 | } 199 | 200 | /** 201 | * @param float $value 202 | * @param float $multiple 203 | * @param string $message 204 | * 205 | * @return AssertionException 206 | */ 207 | public static function isMultiple($value, $multiple, $message = '') 208 | { 209 | settype($value, 'float'); 210 | settype($multiple, 'float'); 211 | 212 | if (false === ((float) 0 == fmod($value, $multiple))) { 213 | throw new AssertionException( 214 | ($message) ? $message : sprintf(self::ASSERT_IS_MULTIPLE, $multiple) 215 | ); 216 | } 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /src/Assertions/GenericAssertions.php: -------------------------------------------------------------------------------- 1 | 5 | * Date: 9/20/14 6 | * Time: 11:46 AM. 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | namespace NilPortugues\Assert\Assertions; 12 | 13 | use NilPortugues\Assert\Exceptions\AssertionException; 14 | 15 | class GenericAssertions 16 | { 17 | const ASSERT_IS_REQUIRED = 'Value field is required.'; 18 | const ASSERT_IS_NOT_NULL = 'Value must be a non null value.'; 19 | const ASSERT_NOT_EQUALS = 'Values are not equal'; 20 | const ASSERT_GREATER_THAN_OR_EQUAL = 'Value is not greater or equal than the provided value.'; 21 | const ASSERT_GREATER = 'Value is not greater than the provided value.'; 22 | const ASSERT_LESS_THAN_OR_EQUAL = 'Value is not less or equal than the provided value.'; 23 | const ASSERT_LESS = 'Value is not less than the provided value.'; 24 | const ASSERT_IS_SCALAR = 'Value must be a scalar.'; 25 | 26 | /** 27 | * @param $value 28 | * @param string $message 29 | */ 30 | public static function isScalar($value, $message = '') 31 | { 32 | if (false === is_scalar($value)) { 33 | throw new AssertionException( 34 | ($message) ? $message : self::ASSERT_IS_SCALAR 35 | ); 36 | } 37 | } 38 | 39 | /** 40 | * @param string|null $value 41 | * @param string $message 42 | * 43 | * @return AssertionException 44 | */ 45 | public static function isRequired($value, $message = '') 46 | { 47 | if (false === (isset($value) === true && is_null($value) === false && empty($value) === false)) { 48 | throw new AssertionException( 49 | ($message) ? $message : self::ASSERT_IS_REQUIRED 50 | ); 51 | } 52 | } 53 | 54 | /** 55 | * @param string|null $value 56 | * @param string $message 57 | * 58 | * @return AssertionException 59 | */ 60 | public static function isNotNull($value, $message = '') 61 | { 62 | if (false === ($value !== null && $value !== '')) { 63 | throw new AssertionException( 64 | ($message) ? $message : self::ASSERT_IS_NOT_NULL 65 | ); 66 | } 67 | } 68 | 69 | /** 70 | * @param string $property 71 | * @param string|int|float $value 72 | * @param string $message 73 | * 74 | * @return \Closure 75 | */ 76 | public static function notEquals($property, $value, $message = '') 77 | { 78 | if (false === ($property != $value)) { 79 | throw new AssertionException( 80 | ($message) ? $message : self::ASSERT_NOT_EQUALS 81 | ); 82 | } 83 | } 84 | 85 | /** 86 | * @param string $property 87 | * @param string|int|float $value 88 | * @param string $message 89 | * 90 | * @return \Closure 91 | */ 92 | public static function greaterThanOrEqual($property, $value, $message = '') 93 | { 94 | if (false === ($property >= $value)) { 95 | throw new AssertionException( 96 | ($message) ? $message : self::ASSERT_GREATER_THAN_OR_EQUAL 97 | ); 98 | } 99 | } 100 | 101 | /** 102 | * @param string $property 103 | * @param string|int|float $value 104 | * @param string $message 105 | * 106 | * @return \Closure 107 | */ 108 | public static function greaterThan($property, $value, $message = '') 109 | { 110 | if (false === ($property > $value)) { 111 | throw new AssertionException( 112 | ($message) ? $message : self::ASSERT_GREATER 113 | ); 114 | } 115 | } 116 | 117 | /** 118 | * @param string $property 119 | * @param string|int|float $value 120 | * @param string $message 121 | * 122 | * @return \Closure 123 | */ 124 | public static function lessThanOrEqual($property, $value, $message = '') 125 | { 126 | if (false === ($property <= $value)) { 127 | throw new AssertionException( 128 | ($message) ? $message : self::ASSERT_LESS_THAN_OR_EQUAL 129 | ); 130 | } 131 | } 132 | 133 | /** 134 | * @param string $property 135 | * @param string|int|float $value 136 | * @param string $message 137 | * 138 | * @return \Closure 139 | */ 140 | public static function lessThan($property, $value, $message = '') 141 | { 142 | if (false === ($property < $value)) { 143 | throw new AssertionException( 144 | ($message) ? $message : self::ASSERT_LESS 145 | ); 146 | } 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /src/Assertions/Helpers/StringHelpers.php: -------------------------------------------------------------------------------- 1 | 5 | * Date: 6/02/16 6 | * Time: 12:02. 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | namespace NilPortugues\Assert\Assertions\Helpers; 12 | 13 | class StringHelpers 14 | { 15 | /** 16 | * Transforms a given string from camelCase to under_score style. 17 | * 18 | * @param string $camel 19 | * @param string $splitter 20 | * 21 | * @return string 22 | */ 23 | public static function camelCaseToUnderscore($camel, $splitter = '_') 24 | { 25 | $camel = preg_replace( 26 | '/(?!^)[[:upper:]][[:lower:]]/', 27 | '$0', 28 | preg_replace('/(?!^)[[:upper:]]+/', $splitter.'$0', $camel) 29 | ); 30 | 31 | return strtolower($camel); 32 | } 33 | 34 | /** 35 | * Converts a underscore string to camelCase. 36 | * 37 | * @param string $string 38 | * 39 | * @return string 40 | */ 41 | public static function underscoreToCamelCase($string) 42 | { 43 | return str_replace(' ', '', ucwords(strtolower(str_replace(['_', '-'], ' ', $string)))); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Assertions/IntegerAssertions.php: -------------------------------------------------------------------------------- 1 | 5 | * Date: 9/16/14 6 | * Time: 9:44 PM. 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | namespace NilPortugues\Assert\Assertions; 12 | 13 | use NilPortugues\Assert\Exceptions\AssertionException; 14 | 15 | class IntegerAssertions 16 | { 17 | const ASSERT_INTEGER = 'Value must be an integer.'; 18 | const ASSERT_IS_NOT_ZERO = 'Value must not be zero.'; 19 | const ASSERT_IS_POSITIVE = 'Value must be a positive value.'; 20 | const ASSERT_IS_POSITIVE_OR_ZERO = 'Value must be a positive value or zero.'; 21 | const ASSERT_IS_NEGATIVE = 'Value must be a negative value.'; 22 | const ASSERT_IS_NEGATIVE_OR_ZERO = 'Value must be a negative value or zero'; 23 | const ASSERT_IS_BETWEEN = 'Integer %s must be between %s and %s.'; 24 | const ASSERT_IS_ODD = 'Value must be divisible by 3.'; 25 | const ASSERT_IS_EVEN = 'Value must be divisible by 2'; 26 | const ASSERT_IS_MULTIPLE = 'Value must be multiple of %s.'; 27 | 28 | /** 29 | * @param $value 30 | * @param string $message 31 | * 32 | * @throws AssertionException 33 | */ 34 | public static function isInteger($value, $message = '') 35 | { 36 | if (false === is_integer($value)) { 37 | throw new AssertionException( 38 | ($message) ? $message : self::ASSERT_INTEGER 39 | ); 40 | } 41 | } 42 | 43 | /** 44 | * @param int $value 45 | * @param string $message 46 | * 47 | * @throws AssertionException 48 | */ 49 | public static function isNotZero($value, $message = '') 50 | { 51 | settype($value, 'int'); 52 | 53 | if (0 === $value) { 54 | throw new AssertionException( 55 | ($message) ? $message : self::ASSERT_IS_NOT_ZERO 56 | ); 57 | } 58 | } 59 | 60 | /** 61 | * @param int $value 62 | * @param string $message 63 | * 64 | * @throws AssertionException 65 | */ 66 | public static function isPositiveOrZero($value, $message = '') 67 | { 68 | settype($value, 'int'); 69 | 70 | if (false === 0 <= $value) { 71 | throw new AssertionException( 72 | ($message) ? $message : self::ASSERT_IS_POSITIVE_OR_ZERO 73 | ); 74 | } 75 | } 76 | 77 | /** 78 | * @param int $value 79 | * @param string $message 80 | * 81 | * @throws AssertionException 82 | */ 83 | public static function isPositive($value, $message = '') 84 | { 85 | settype($value, 'int'); 86 | 87 | if (false === 0 < $value) { 88 | throw new AssertionException( 89 | ($message) ? $message : self::ASSERT_IS_POSITIVE 90 | ); 91 | } 92 | } 93 | 94 | /** 95 | * @param int $value 96 | * @param string $message 97 | * 98 | * @throws AssertionException 99 | */ 100 | public static function isNegativeOrZero($value, $message = '') 101 | { 102 | settype($value, 'int'); 103 | 104 | if (false === 0 >= $value) { 105 | throw new AssertionException( 106 | ($message) ? $message : self::ASSERT_IS_NEGATIVE_OR_ZERO 107 | ); 108 | } 109 | } 110 | 111 | /** 112 | * @param int $value 113 | * @param string $message 114 | * 115 | * @throws AssertionException 116 | */ 117 | public static function isNegative($value, $message = '') 118 | { 119 | settype($value, 'int'); 120 | 121 | if (false === 0 > $value) { 122 | throw new AssertionException( 123 | ($message) ? $message : self::ASSERT_IS_NEGATIVE 124 | ); 125 | } 126 | } 127 | 128 | /** 129 | * @param int $value 130 | * @param int $min 131 | * @param int $max 132 | * @param bool $inclusive 133 | * @param string $message 134 | * 135 | * @throws AssertionException 136 | */ 137 | public static function isBetween($value, $min, $max, $inclusive = false, $message = '') 138 | { 139 | settype($value, 'int'); 140 | settype($min, 'int'); 141 | settype($max, 'int'); 142 | 143 | if ($min > $max) { 144 | throw new AssertionException(sprintf('%s cannot be less than %s.', $min, $max)); 145 | } 146 | 147 | if ($inclusive && !($value >= $min && $value <= $max)) { 148 | throw new AssertionException( 149 | ($message) ? $message : sprintf(self::ASSERT_IS_BETWEEN, $value, $min, $max) 150 | ); 151 | } 152 | 153 | if (!$inclusive && !($value > $min && $value < $max)) { 154 | throw new AssertionException( 155 | ($message) ? $message : sprintf(self::ASSERT_IS_BETWEEN, $value, $min, $max) 156 | ); 157 | } 158 | } 159 | 160 | /** 161 | * @param int $value 162 | * @param string $message 163 | * 164 | * @throws AssertionException 165 | */ 166 | public static function isOdd($value, $message = '') 167 | { 168 | settype($value, 'int'); 169 | 170 | if (false === (0 == ($value % 3))) { 171 | throw new AssertionException( 172 | ($message) ? $message : self::ASSERT_IS_ODD 173 | ); 174 | } 175 | } 176 | 177 | /** 178 | * @param int $value 179 | * @param string $message 180 | * 181 | * @throws AssertionException 182 | */ 183 | public static function isEven($value, $message = '') 184 | { 185 | settype($value, 'int'); 186 | 187 | if (false === (0 == ($value % 2))) { 188 | throw new AssertionException( 189 | ($message) ? $message : self::ASSERT_IS_EVEN 190 | ); 191 | } 192 | } 193 | 194 | /** 195 | * @param int $value 196 | * @param int $multiple 197 | * @param string $message 198 | * 199 | * @throws AssertionException 200 | */ 201 | public static function isMultiple($value, $multiple, $message = '') 202 | { 203 | settype($value, 'int'); 204 | settype($multiple, 'int'); 205 | 206 | if (false === (0 == ($value % $multiple))) { 207 | throw new AssertionException( 208 | ($message) ? $message : sprintf(self::ASSERT_IS_MULTIPLE, $multiple) 209 | ); 210 | } 211 | } 212 | } 213 | -------------------------------------------------------------------------------- /src/Assertions/ObjectAssertions.php: -------------------------------------------------------------------------------- 1 | 5 | * Date: 9/16/14 6 | * Time: 10:19 PM. 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | namespace NilPortugues\Assert\Assertions; 12 | 13 | use NilPortugues\Assert\Exceptions\AssertionException; 14 | 15 | class ObjectAssertions 16 | { 17 | const ASSERT_OBJECT = 'Value is not a valid object.'; 18 | const ASSERT_IS_INSTANCE_OF = 'Value is not an instance of %s.'; 19 | const ASSERT_HAS_PROPERTY = 'Value property %s is not valid.'; 20 | const ASSERT_HAS_METHOD = 'Value method %s is not valid.'; 21 | const ASSERT_HAS_PARENT_CLASS = 'Value has no parent class.'; 22 | const ASSERT_IS_CHILD_OF = 'Value is not child of class %s.'; 23 | const ASSERT_INHERITS_FROM = 'Value does not inherit from class %s.'; 24 | const ASSERT_HAS_INTERFACE = 'Value does not implement interface %s.'; 25 | 26 | /** 27 | * @param mixed $value 28 | * @param string $message 29 | * 30 | * @return AssertionException 31 | */ 32 | public static function isObject($value, $message = '') 33 | { 34 | if (false === is_object($value)) { 35 | throw new AssertionException( 36 | ($message) ? $message : self::ASSERT_OBJECT 37 | ); 38 | } 39 | } 40 | 41 | /** 42 | * @param mixed $value 43 | * @param string $instanceOf 44 | * @param string $message 45 | * 46 | * @return AssertionException 47 | */ 48 | public static function isInstanceOf($value, $instanceOf, $message = '') 49 | { 50 | if (false === ($value instanceof $instanceOf)) { 51 | throw new AssertionException( 52 | ($message) ? $message : sprintf(self::ASSERT_IS_INSTANCE_OF, $instanceOf) 53 | ); 54 | } 55 | } 56 | 57 | /** 58 | * @param mixed $value 59 | * @param string $property 60 | * @param string $message 61 | * 62 | * @return AssertionException 63 | */ 64 | public static function hasProperty($value, $property, $message = '') 65 | { 66 | if (false === (is_object($value) && property_exists(get_class($value), $property))) { 67 | throw new AssertionException( 68 | ($message) ? $message : sprintf(self::ASSERT_HAS_PROPERTY, gettype($value)) 69 | ); 70 | } 71 | } 72 | 73 | /** 74 | * @param mixed $value 75 | * @param string $method 76 | * @param string $message 77 | * 78 | * @return AssertionException 79 | */ 80 | public static function hasMethod($value, $method, $message = '') 81 | { 82 | if (false === (is_object($value) && method_exists(get_class($value), $method))) { 83 | throw new AssertionException( 84 | ($message) ? $message : sprintf(self::ASSERT_HAS_METHOD, $method) 85 | ); 86 | } 87 | } 88 | 89 | /** 90 | * @param mixed $value 91 | * @param string $message 92 | * 93 | * @return AssertionException 94 | */ 95 | public static function hasParentClass($value, $message = '') 96 | { 97 | if (false === (is_object($value) && get_parent_class($value) !== false)) { 98 | throw new AssertionException( 99 | ($message) ? $message : self::ASSERT_HAS_PARENT_CLASS 100 | ); 101 | } 102 | } 103 | 104 | /** 105 | * @param mixed $value 106 | * @param string $parentClass 107 | * @param string $message 108 | * 109 | * @return AssertionException 110 | */ 111 | public static function isChildOf($value, $parentClass, $message = '') 112 | { 113 | if (false === (is_object($value) && get_parent_class($value) === $parentClass)) { 114 | throw new AssertionException( 115 | ($message) ? $message : sprintf(self::ASSERT_IS_CHILD_OF, $parentClass) 116 | ); 117 | } 118 | } 119 | 120 | /** 121 | * @param mixed $value 122 | * @param string $inheritsClass 123 | * @param string $message 124 | * 125 | * @return AssertionException 126 | */ 127 | public static function inheritsFrom($value, $inheritsClass, $message = '') 128 | { 129 | if (false === (is_object($value) && is_subclass_of($value, $inheritsClass))) { 130 | throw new AssertionException( 131 | ($message) ? $message : sprintf(self::ASSERT_INHERITS_FROM, $inheritsClass) 132 | ); 133 | } 134 | } 135 | 136 | /** 137 | * @param mixed $value 138 | * @param string $interface 139 | * @param string $message 140 | * 141 | * @return AssertionException 142 | */ 143 | public static function hasInterface($value, $interface, $message = '') 144 | { 145 | if (false === (is_object($value) && in_array($interface, class_implements($value)))) { 146 | throw new AssertionException( 147 | ($message) ? $message : sprintf(self::ASSERT_HAS_INTERFACE, $interface) 148 | ); 149 | } 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /src/Exceptions/AssertionException.php: -------------------------------------------------------------------------------- 1 | 5 | * Date: 6/30/15 6 | * Time: 10:49 PM. 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | namespace NilPortugues\Assert\Exceptions; 12 | 13 | use InvalidArgumentException; 14 | 15 | class AssertionException extends InvalidArgumentException 16 | { 17 | } 18 | -------------------------------------------------------------------------------- /src/Exceptions/FileUploadException.php: -------------------------------------------------------------------------------- 1 | 5 | * Date: 9/25/14 6 | * Time: 10:53 PM. 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | namespace NilPortugues\Assert\Exceptions; 12 | 13 | class FileUploadException extends AssertionException 14 | { 15 | const ASSERT_UPLOAD_ERR_INI_SIZE = 'Value upload exceeds the maximum file size allowed by the server.'; 16 | const ASSERT_UPLOAD_ERR_FORM_SIZE = 'Value upload exceeds the maximum file size specified in the form.'; 17 | const ASSERT_UPLOAD_ERR_PARTIAL = 'Value was only partially uploaded.'; 18 | const ASSERT_UPLOAD_ERR_NO_FILE = 'No %s file was uploaded.'; 19 | const ASSERT_UPLOAD_ERR_NO_TMP_DIR = 'Upload failed. Missing a temporary upload folder.'; 20 | const ASSERT_UPLOAD_ERR_CANT_WRITE = 'Upload failed. Failed to write file to disk.'; 21 | const ASSERT_UPLOAD_ERR_EXTENSION = 'Upload failed. File upload was stopped.'; 22 | 23 | /** 24 | * @var array 25 | */ 26 | private $errorMessages = [ 27 | UPLOAD_ERR_INI_SIZE => self::ASSERT_UPLOAD_ERR_INI_SIZE, 28 | UPLOAD_ERR_FORM_SIZE => self::ASSERT_UPLOAD_ERR_FORM_SIZE, 29 | UPLOAD_ERR_PARTIAL => self::ASSERT_UPLOAD_ERR_PARTIAL, 30 | UPLOAD_ERR_NO_FILE => self::ASSERT_UPLOAD_ERR_NO_FILE, 31 | UPLOAD_ERR_NO_TMP_DIR => self::ASSERT_UPLOAD_ERR_NO_TMP_DIR, 32 | UPLOAD_ERR_CANT_WRITE => self::ASSERT_UPLOAD_ERR_CANT_WRITE, 33 | UPLOAD_ERR_EXTENSION => self::ASSERT_UPLOAD_ERR_EXTENSION, 34 | ]; 35 | 36 | /** 37 | * @param string $uploadName 38 | */ 39 | public function __construct($uploadName) 40 | { 41 | $error = 4; 42 | 43 | if (!empty($_FILES[$uploadName]['error']) && is_int($_FILES[$uploadName]['error'])) { 44 | $error = $_FILES[$uploadName]['error']; 45 | } 46 | 47 | if (!empty($_FILES[$uploadName]['error']) 48 | && is_array($_FILES[$uploadName]['error']) 49 | && is_int($_FILES[$uploadName]['error'][0]) 50 | ) { 51 | $error = reset($_FILES[$uploadName]['error']); 52 | } 53 | 54 | $this->message = $this->errorMessages[$error]; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /tests/AssertCollectionTest.php: -------------------------------------------------------------------------------- 1 | 5 | * Date: 1/02/16 6 | * Time: 0:36. 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | namespace NilPortugues\Tests\Assert; 12 | 13 | use Exception; 14 | use NilPortugues\Assert\Assert; 15 | use NilPortugues\Assert\Assertions\CollectionAssertions; 16 | 17 | class AssertCollectionTest extends \PHPUnit_Framework_TestCase 18 | { 19 | public function testIsArray() 20 | { 21 | Assert::isArray([]); 22 | } 23 | 24 | public function testIsArrayThrowsException() 25 | { 26 | $this->setExpectedException(Exception::class); 27 | CollectionAssertions::isArray('a'); 28 | } 29 | 30 | public function testIsEach() 31 | { 32 | $array = ['some-value']; 33 | 34 | $valueAssert = function () {}; 35 | $keyAssert = function () {}; 36 | Assert::each($array, $valueAssert, $keyAssert); 37 | } 38 | 39 | public function testIsEachThrowsException() 40 | { 41 | $array = ['some-value']; 42 | 43 | $valueAssert = function () {}; 44 | $keyAssert = function ($key) { 45 | Assert::isUUID($key, false); 46 | }; 47 | 48 | $this->setExpectedException(Exception::class); 49 | Assert::each($array, $valueAssert, $keyAssert); 50 | } 51 | 52 | public function testHasKeyFormat() 53 | { 54 | $array = ['some-value']; 55 | $keyAssert = function () {}; 56 | Assert::hasKeyFormat($array, $keyAssert); 57 | } 58 | 59 | public function testHasKeyFormatWithArrayObject() 60 | { 61 | $array = new \ArrayObject(); 62 | $array[] = 'some-value'; 63 | 64 | $keyAssert = function () {}; 65 | Assert::hasKeyFormat($array, $keyAssert); 66 | } 67 | 68 | public function testHasKeyFormatWithSplFixedArray() 69 | { 70 | $array = new \SplFixedArray(1); 71 | $array[0] = 'some-value'; 72 | 73 | $keyAssert = function () {}; 74 | Assert::hasKeyFormat($array, $keyAssert); 75 | } 76 | 77 | public function testHasKeyFormatThrowsException() 78 | { 79 | $array = ['some-value']; 80 | $keyAssert = function ($key) { 81 | Assert::isUUID($key, false); 82 | }; 83 | 84 | $this->setExpectedException(Exception::class); 85 | Assert::hasKeyFormat($array, $keyAssert); 86 | } 87 | 88 | public function testItEndsWithStrict() 89 | { 90 | $array = new \SplFixedArray(2); 91 | $array[0] = 'some-value'; 92 | $array[1] = 'last-value'; 93 | 94 | Assert::endsWith($array, 'last-value', true); 95 | } 96 | public function testItEndsWithNotStrict() 97 | { 98 | $array = new \SplFixedArray(2); 99 | $array[0] = 'some-value'; 100 | $array[1] = 1337; 101 | 102 | Assert::endsWith($array, '1337'); 103 | } 104 | 105 | public function testItEndsWithStrictThrowsException() 106 | { 107 | $array = new \SplFixedArray(2); 108 | $array[0] = 'some-value'; 109 | $array[1] = 'last-value'; 110 | 111 | $this->setExpectedException(Exception::class); 112 | Assert::endsWith($array, 'not-there', true); 113 | } 114 | 115 | public function testItEndsWithNotStrictThrowsException() 116 | { 117 | $array = new \SplFixedArray(2); 118 | $array[0] = 'some-value'; 119 | $array[1] = 1337; 120 | 121 | $this->setExpectedException(Exception::class); 122 | Assert::endsWith($array, '1338'); 123 | } 124 | 125 | public function testItContainsStrictArrayObject() 126 | { 127 | $array = new \ArrayObject(); 128 | $array[0] = 'some-value'; 129 | $array[1] = 'last-value'; 130 | 131 | Assert::contains($array, 'last-value', true); 132 | } 133 | 134 | public function testItContainsStrictSplFixedArray() 135 | { 136 | $array = new \SplFixedArray(2); 137 | $array[0] = 'some-value'; 138 | $array[1] = 'last-value'; 139 | 140 | Assert::contains($array, 'last-value', true); 141 | } 142 | 143 | public function testItContainsNotStrict() 144 | { 145 | $array = new \SplFixedArray(2); 146 | $array[0] = 'some-value'; 147 | $array[1] = 1337; 148 | 149 | Assert::contains($array, '1337'); 150 | } 151 | 152 | public function testItContainsStrictThrowsException() 153 | { 154 | $array = new \SplFixedArray(2); 155 | $array[0] = 'some-value'; 156 | $array[1] = 'last-value'; 157 | 158 | $this->setExpectedException(Exception::class); 159 | Assert::contains($array, 'not-there', true); 160 | } 161 | 162 | public function testItContainsNotStrictThrowsException() 163 | { 164 | $array = new \SplFixedArray(2); 165 | $array[0] = 'some-value'; 166 | $array[1] = 1337; 167 | 168 | $this->setExpectedException(Exception::class); 169 | Assert::contains($array, '1338'); 170 | } 171 | 172 | public function testItHasKey() 173 | { 174 | $array = new \SplFixedArray(2); 175 | $array[0] = 'some-value'; 176 | $array[1] = 'last-value'; 177 | 178 | Assert::hasKey($array, 1); 179 | } 180 | 181 | public function testItHasKeyThrowsException() 182 | { 183 | $array = new \SplFixedArray(2); 184 | $array[0] = 'some-value'; 185 | $array[1] = 'last-value'; 186 | 187 | $this->setExpectedException(Exception::class); 188 | Assert::hasKey($array, 100); 189 | } 190 | 191 | public function testItHasLength() 192 | { 193 | $array = new \SplFixedArray(2); 194 | $array[0] = 'some-value'; 195 | $array[1] = 'last-value'; 196 | 197 | Assert::hasLength($array, 2); 198 | } 199 | 200 | public function testItHasLengthThrowsException() 201 | { 202 | $array = new \SplFixedArray(2); 203 | $array[0] = 'some-value'; 204 | $array[1] = 'last-value'; 205 | 206 | $this->setExpectedException(Exception::class); 207 | Assert::hasLength($array, 100); 208 | } 209 | 210 | public function testIsNotEmpty() 211 | { 212 | $array = new \SplFixedArray(2); 213 | $array[0] = 'some-value'; 214 | $array[1] = 'last-value'; 215 | 216 | Assert::isNotEmpty($array); 217 | } 218 | 219 | public function testIsNotEmptyThrowsException() 220 | { 221 | $this->setExpectedException(Exception::class); 222 | Assert::isNotEmpty([]); 223 | } 224 | 225 | public function testItStartsWithStrict() 226 | { 227 | $array = new \SplFixedArray(2); 228 | $array[0] = 'some-value'; 229 | $array[1] = 'last-value'; 230 | 231 | Assert::startsWith($array, 'some-value', true); 232 | } 233 | public function testItStartsWithNotStrict() 234 | { 235 | $array = new \SplFixedArray(2); 236 | $array[0] = 'some-value'; 237 | $array[1] = 1337; 238 | 239 | Assert::startsWith($array, 'some-value'); 240 | } 241 | 242 | public function testItStartsWithStrictThrowsException() 243 | { 244 | $array = new \SplFixedArray(2); 245 | $array[0] = 'some-value'; 246 | $array[1] = 'last-value'; 247 | 248 | $this->setExpectedException(Exception::class); 249 | Assert::startsWith($array, 'not-there', true); 250 | } 251 | 252 | public function testItStartsWithNotStrictThrowsException() 253 | { 254 | $array = new \SplFixedArray(2); 255 | $array[0] = 'some-value'; 256 | $array[1] = 1337; 257 | 258 | $this->setExpectedException(Exception::class); 259 | Assert::startsWith($array, '1338'); 260 | } 261 | } 262 | -------------------------------------------------------------------------------- /tests/AssertDateTimeTest.php: -------------------------------------------------------------------------------- 1 | setExpectedException(Exception::class); 25 | $date1 = '2012-13-13 00:00:00'; 26 | DateTimeAssertions::isDateTime($date1); 27 | } 28 | 29 | public function testItShouldCheckIfDateIsBefore() 30 | { 31 | $date1 = '2012-01-01 00:00:00'; 32 | $date2 = new DateTime($date1); 33 | $limit1 = '2013-12-31 23:59:59'; 34 | 35 | Assert::isBefore($date1, $limit1, false); 36 | Assert::isBefore($date2, $limit1, false); 37 | Assert::isBefore($date1, $date1, true); 38 | Assert::isBefore($date2, $date2, true); 39 | } 40 | 41 | public function testItShouldCheckIfDateIsBeforeThrowsException1() 42 | { 43 | $date1 = '2012-01-01 00:00:00'; 44 | $limit2 = '2010-01-01 00:00:00'; 45 | 46 | $this->setExpectedException(Exception::class); 47 | Assert::isBefore($date1, $limit2); 48 | } 49 | 50 | public function testItShouldCheckIfDateIsBeforeThrowsException2() 51 | { 52 | $date1 = '2012-01-01 00:00:00'; 53 | $date2 = new DateTime($date1); 54 | $limit2 = '2010-01-01 00:00:00'; 55 | 56 | $this->setExpectedException(Exception::class); 57 | Assert::isBefore($date2, $limit2, true); 58 | } 59 | 60 | public function testItShouldCheckIfDateIsAfter() 61 | { 62 | $date1 = '2014-01-01 00:00:00'; 63 | $date2 = new DateTime($date1); 64 | 65 | $limit1 = '2013-12-31 23:59:59'; 66 | 67 | Assert::isAfter($date1, $limit1, false); 68 | Assert::isAfter($date2, $limit1, false); 69 | 70 | Assert::isAfter($date1, $date1, true); 71 | Assert::isAfter($date2, $date2, true); 72 | } 73 | 74 | public function testItShouldCheckIfDateIsAfterThrowsException1() 75 | { 76 | $date1 = '2014-01-01 00:00:00'; 77 | $limit2 = '2015-01-01 00:00:00'; 78 | 79 | $this->setExpectedException(Exception::class); 80 | Assert::isAfter($date1, $limit2); 81 | } 82 | 83 | public function testItShouldCheckIfDateIsAfterThrowsException2() 84 | { 85 | $date1 = '2014-01-01 00:00:00'; 86 | $date2 = new DateTime($date1); 87 | $limit2 = '2015-01-01 00:00:00'; 88 | 89 | $this->setExpectedException(Exception::class); 90 | Assert::isAfter($date2, $limit2, true); 91 | } 92 | 93 | public function testItShouldCheckIfDateisDateRange() 94 | { 95 | $date1 = '2014-01-01 00:00:00'; 96 | $date2 = new DateTime($date1); 97 | 98 | $minDate = '2013-01-01 00:00:00'; 99 | $maxDate = '2015-01-01 00:00:00'; 100 | 101 | Assert::isDateRange($date1, $minDate, $maxDate, false); 102 | Assert::isDateRange($date2, $minDate, $maxDate, false); 103 | 104 | Assert::isDateRange($date1, $minDate, $maxDate, true); 105 | Assert::isDateRange($date2, $minDate, $maxDate, true); 106 | } 107 | 108 | public function testItShouldCheckIfDateisDateRangeThrowsException1() 109 | { 110 | $date1 = '2014-01-01 00:00:00'; 111 | $minDate = '2013-12-01 00:00:00'; 112 | $maxDate = '2013-12-30 00:00:00'; 113 | 114 | $this->setExpectedException(Exception::class); 115 | Assert::isDateRange($date1, $minDate, $maxDate, false); 116 | } 117 | 118 | public function testItShouldCheckIfDateisDateRangeThrowsException2() 119 | { 120 | $date1 = '2014-01-01 00:00:00'; 121 | $minDate = '2013-12-01 00:00:00'; 122 | $maxDate = '2013-12-30 00:00:00'; 123 | 124 | $this->setExpectedException(Exception::class); 125 | Assert::isDateRange($date1, $minDate, $maxDate, true); 126 | } 127 | 128 | public function testItShouldCheckIfIsMonday() 129 | { 130 | Assert::isMonday('2014-09-22'); 131 | } 132 | 133 | public function testItShouldCheckIfIsMondayThrowsException() 134 | { 135 | $this->setExpectedException(Exception::class); 136 | Assert::isMonday('2014-09-23'); 137 | } 138 | 139 | public function testItShouldCheckIfIsTuesday() 140 | { 141 | Assert::isTuesday('2014-09-23'); 142 | } 143 | 144 | public function testItShouldCheckIfIsTuesdayThrowsException() 145 | { 146 | $this->setExpectedException(Exception::class); 147 | Assert::isTuesday('2014-09-24'); 148 | } 149 | 150 | public function testItShouldCheckIfIsWednesday() 151 | { 152 | Assert::isWednesday('2014-09-24'); 153 | } 154 | 155 | public function testItShouldCheckIfIsWednesdayThrowsException() 156 | { 157 | $this->setExpectedException(Exception::class); 158 | Assert::isWednesday('2014-09-25'); 159 | } 160 | 161 | public function testItShouldCheckIfIsThursday() 162 | { 163 | Assert::isThursday('2014-09-25'); 164 | } 165 | 166 | public function testItShouldCheckIfIsThursdayThrowsException() 167 | { 168 | $this->setExpectedException(Exception::class); 169 | Assert::isThursday('2014-09-26'); 170 | } 171 | 172 | public function testItShouldCheckIfIsFriday() 173 | { 174 | Assert::isFriday('2014-09-26'); 175 | } 176 | 177 | public function testItShouldCheckIfIsFridayThrowsException() 178 | { 179 | $this->setExpectedException(Exception::class); 180 | Assert::isFriday('2014-09-27'); 181 | } 182 | 183 | public function testItShouldCheckIfIsSaturday() 184 | { 185 | Assert::isSaturday('2014-09-27'); 186 | } 187 | 188 | public function testItShouldCheckIfIsSaturdayThrowsException() 189 | { 190 | $this->setExpectedException(Exception::class); 191 | Assert::isSaturday('2014-09-28'); 192 | } 193 | 194 | public function testItShouldCheckIfIsSunday() 195 | { 196 | Assert::isSunday('2014-09-28'); 197 | } 198 | 199 | public function testItShouldCheckIfIsSundayThrowsException() 200 | { 201 | $this->setExpectedException(Exception::class); 202 | Assert::isSunday('2014-09-29'); 203 | } 204 | 205 | public function testItShouldCheckIfIsToday() 206 | { 207 | $date = new DateTime('now'); 208 | Assert::isToday($date); 209 | } 210 | 211 | public function testItShouldCheckIfIsTodayThrowsException() 212 | { 213 | $this->setExpectedException(Exception::class); 214 | $date = new DateTime('now -1day'); 215 | Assert::isToday($date); 216 | } 217 | 218 | public function testItShouldCheckIfIsYesterday() 219 | { 220 | $date = new DateTime('now -1 day'); 221 | Assert::isYesterday($date); 222 | } 223 | 224 | public function testItShouldCheckIfIsYesterdayThrowsException() 225 | { 226 | $this->setExpectedException(Exception::class); 227 | $date = new DateTime('now'); 228 | Assert::isYesterday($date); 229 | } 230 | 231 | public function testItShouldCheckIfIsTomorrow() 232 | { 233 | $date = new DateTime('now +1 day'); 234 | Assert::isTomorrow($date); 235 | } 236 | 237 | public function testItShouldCheckIfIsTomorrowThrowsException() 238 | { 239 | $this->setExpectedException(Exception::class); 240 | $date = new DateTime('now'); 241 | Assert::isTomorrow($date); 242 | } 243 | 244 | public function testItShouldCheckIfIsLeapYear() 245 | { 246 | $date = new DateTime('2016-01-01'); 247 | Assert::isLeapYear($date); 248 | } 249 | 250 | public function testItShouldCheckIfIsLeapYearThrowsException() 251 | { 252 | $this->setExpectedException(Exception::class); 253 | $date = new DateTime('2015-01-01'); 254 | Assert::isLeapYear($date); 255 | } 256 | 257 | public function testItShouldCheckIfIsWeekend() 258 | { 259 | Assert::isWeekend('2014-09-20'); 260 | 261 | $this->setExpectedException(Exception::class); 262 | Assert::isWeekend('2014-09-22'); 263 | } 264 | 265 | public function testItShouldCheckIfIsWeekday() 266 | { 267 | Assert::isWeekday('2016-01-29'); 268 | } 269 | 270 | public function testItShouldCheckIfIsWeekdayThrowsException() 271 | { 272 | $this->setExpectedException(Exception::class); 273 | Assert::isWeekday('2016-01-30'); 274 | } 275 | 276 | public function testItShouldCheckIfIsMorning() 277 | { 278 | Assert::isMorning('07:20:15'); 279 | } 280 | 281 | public function testItShouldCheckIfIsMorningThrowsException() 282 | { 283 | $this->setExpectedException(Exception::class); 284 | Assert::isMorning('20:15:00'); 285 | } 286 | 287 | public function testItShouldCheckIfIsAfternoon() 288 | { 289 | Assert::isAfternoon('12:00:00'); 290 | } 291 | 292 | public function testItShouldCheckIfIsAfternoonThrowsException() 293 | { 294 | $this->setExpectedException(Exception::class); 295 | Assert::isAfternoon('20:15:00'); 296 | } 297 | 298 | public function testItShouldCheckIfIsEvening() 299 | { 300 | Assert::isEvening('18:00:00'); 301 | } 302 | 303 | public function testItShouldCheckIfIsEveningThrowsException() 304 | { 305 | $this->setExpectedException(Exception::class); 306 | Assert::isEvening('07:15:00'); 307 | } 308 | 309 | public function testItShouldCheckIfIsNight() 310 | { 311 | Assert::isNight('01:00:00'); 312 | } 313 | 314 | public function testItShouldCheckIfIsNightThrowsException() 315 | { 316 | $this->setExpectedException(Exception::class); 317 | Assert::isNight('12:15:00'); 318 | } 319 | 320 | public function testItShouldCheckIfIsPast() 321 | { 322 | Assert::isPastDate(new DateTime('now -1 day')); 323 | } 324 | 325 | public function testItShouldCheckIfIsPastThrowsException() 326 | { 327 | $this->setExpectedException(Exception::class); 328 | Assert::isPastDate(new DateTime('now')); 329 | } 330 | 331 | public function testItShouldCheckIfIsFuture() 332 | { 333 | Assert::isFutureDate(new DateTime('now +1 day')); 334 | } 335 | 336 | public function testItShouldCheckIfIsFutureThrowsException() 337 | { 338 | $this->setExpectedException(Exception::class); 339 | Assert::isFutureDate(new DateTime('now')); 340 | } 341 | 342 | public function testItIsInNextWeek() 343 | { 344 | Assert::isInNextWeek(new DateTime('now +1 week')); 345 | } 346 | 347 | public function testItIsInNextWeekThrowsException() 348 | { 349 | $this->setExpectedException(Exception::class); 350 | Assert::isInNextWeek(new DateTime('now -1 week')); 351 | } 352 | 353 | public function testItIsInWeek() 354 | { 355 | $value = new DateTime('07-02-2016'); 356 | $weekNumber = $value->format('W'); 357 | Assert::isInWeek($value, $weekNumber); 358 | } 359 | 360 | public function testItIsInWeekThrowsException() 361 | { 362 | $this->setExpectedException(Exception::class); 363 | $value = new DateTime('07-02-2016'); 364 | $weekNumber = (int) $value->format('W'); 365 | Assert::isInWeek($value, $weekNumber + 1); 366 | } 367 | 368 | public function testItIsInLastWeek() 369 | { 370 | $dateTime = new DateTime(); 371 | $lastWeek = $dateTime->modify('-7 days'); 372 | Assert::isInLastWeek($lastWeek); 373 | } 374 | 375 | public function testItIsInLastWeekThrowsException() 376 | { 377 | $this->setExpectedException(Exception::class); 378 | $value = new DateTime('07-02-2015'); 379 | Assert::isInLastWeek($value); 380 | } 381 | 382 | public function testItIsInLastMonth() 383 | { 384 | $lastMonth = new DateTime('now -20 days'); 385 | Assert::isInLastMonth($lastMonth); 386 | } 387 | 388 | public function testItIsInLastMonthThrowsException() 389 | { 390 | $this->setExpectedException(Exception::class); 391 | $value = new DateTime('07-02-2015'); 392 | Assert::isInLastMonth($value); 393 | } 394 | 395 | public function testItIsInMonth() 396 | { 397 | $next = new DateTime('01-02-'.date('Y')); 398 | Assert::isInMonth($next, 2); 399 | } 400 | 401 | public function testItIsInMonthThrowsException() 402 | { 403 | $this->setExpectedException(Exception::class); 404 | $next = new DateTime('07-02-2015'); 405 | Assert::isInMonth($next, 3); 406 | } 407 | 408 | public function testItIsInNextMonth() 409 | { 410 | $next = new DateTime('now +1 month'); 411 | Assert::isInNextMonth($next); 412 | } 413 | 414 | public function testItIsInNextMonthThrowsException() 415 | { 416 | $this->setExpectedException(Exception::class); 417 | $next = new DateTime('07-02-2015'); 418 | Assert::isInNextMonth($next); 419 | } 420 | 421 | public function testItIsInLastYear() 422 | { 423 | $next = new DateTime('now -1 year'); 424 | Assert::isInLastYear($next); 425 | } 426 | 427 | public function testItIsInLastYearThrowsException() 428 | { 429 | $this->setExpectedException(Exception::class); 430 | $next = new DateTime('07-02-2013'); 431 | Assert::isInLastYear($next); 432 | } 433 | 434 | public function testItIsInYear() 435 | { 436 | $next = new DateTime('05-02-2016'); 437 | Assert::isInYear($next, 2016); 438 | } 439 | 440 | public function testItIsInYearThrowsException() 441 | { 442 | $this->setExpectedException(Exception::class); 443 | $next = new DateTime('05-02-2016'); 444 | Assert::isInYear($next, 2017); 445 | } 446 | 447 | public function testItIsInNextYear() 448 | { 449 | $next = new DateTime('now +1 year'); 450 | Assert::isInNextYear($next); 451 | } 452 | 453 | public function testItIsInNextYearThrowsException() 454 | { 455 | $this->setExpectedException(Exception::class); 456 | $next = new DateTime('now +2 year'); 457 | Assert::isInNextYear($next); 458 | } 459 | 460 | public function testItIsFirstHalfOfYear() 461 | { 462 | $date = new DateTime('05-02-2016'); 463 | Assert::isFirstHalfOfYear($date); 464 | } 465 | 466 | public function testItIsFirstHalfOfYearThrowsException() 467 | { 468 | $this->setExpectedException(Exception::class); 469 | $date = new DateTime('05-12-2016'); 470 | Assert::isFirstHalfOfYear($date); 471 | } 472 | 473 | public function testItIsSecondHalfOfYear() 474 | { 475 | $date = new DateTime('05-12-2016'); 476 | Assert::isSecondHalfOfYear($date); 477 | } 478 | 479 | public function testItIsSecondHalfOfYearThrowsException() 480 | { 481 | $this->setExpectedException(Exception::class); 482 | $date = new DateTime('05-02-2016'); 483 | Assert::isSecondHalfOfYear($date); 484 | } 485 | 486 | public function testItIsTrimesterOfYear() 487 | { 488 | $date = new DateTime('05-12-2016'); 489 | Assert::isTrimesterOfYear($date, 4); 490 | } 491 | 492 | public function testItIsTrimesterOfYearThrowsException() 493 | { 494 | $this->setExpectedException(Exception::class); 495 | $date = new DateTime('05-12-2016'); 496 | Assert::isTrimesterOfYear($date, 3); 497 | } 498 | 499 | public function testItIsTrimesterOfYearWrongBoundsThrowsException() 500 | { 501 | $this->setExpectedException(Exception::class); 502 | $date = new DateTime('05-12-2016'); 503 | Assert::isTrimesterOfYear($date, 100); 504 | } 505 | 506 | public function testItIsQuarterOfYearWrongBoundsThrowsException() 507 | { 508 | $this->setExpectedException(Exception::class); 509 | $date = new DateTime('05-12-2016'); 510 | Assert::isQuarterOfYear($date, 100); 511 | } 512 | 513 | public function testItIsQuarterOfYear() 514 | { 515 | $date = new DateTime('05-12-2016'); 516 | Assert::isQuarterOfYear($date, 3); 517 | } 518 | 519 | public function testItIsQuarterOfYearThrowsException() 520 | { 521 | $this->setExpectedException(Exception::class); 522 | $date = new DateTime('05-12-2016'); 523 | Assert::isQuarterOfYear($date, 1); 524 | } 525 | 526 | public function testItIsDayLightSavingTime() 527 | { 528 | $value = new DateTime('28-03-2016', new DateTimeZone('Europe/Madrid')); 529 | Assert::isDayLightSavingTime($value, $value->getTimezone()); 530 | } 531 | 532 | public function testItIsDayLightSavingTimeThrowsException() 533 | { 534 | $this->setExpectedException(Exception::class); 535 | $value = new DateTime('22-03-2016', new DateTimeZone('Europe/Madrid')); 536 | Assert::isDayLightSavingTime($value, $value->getTimezone()); 537 | } 538 | } 539 | -------------------------------------------------------------------------------- /tests/AssertFileUploadTest.php: -------------------------------------------------------------------------------- 1 | [ 16 | 'name' => 'sample.png', 17 | 'type' => 'image/png', 18 | 'tmp_name' => realpath(dirname(__FILE__)).'/resources/phpGpKMlf', 19 | 'error' => '0', 20 | 'size' => '203868', 21 | ], 22 | 'image_set' => [ 23 | 'name' => ['sample.png', 'sample.png', 'sample.png'], 24 | 'type' => ['image/png', 'image/png', 'image/png'], 25 | 'tmp_name' => [ 26 | \realpath(\dirname(__FILE__)).'/resources/phpGpKMlf', 27 | \realpath(\dirname(__FILE__)).'/resources/phpGpKMlf', 28 | \realpath(\dirname(__FILE__)).'/resources/phpGpKMlf', 29 | ], 30 | 'error' => [], 31 | 'size' => [203868, 203868, 203868], 32 | ], 33 | ]; 34 | } 35 | public function testIsFileUploaded() 36 | { 37 | FileUploadAssertions::isFileUploaded('image_one'); 38 | } 39 | 40 | public function testIsFileUploadedThrowsException() 41 | { 42 | $this->setExpectedException(Exception::class); 43 | FileUploadAssertions::isFileUploaded('no'); 44 | } 45 | 46 | public function testItShouldCheckIfHasLength() 47 | { 48 | Assert::hasFileUploadedFileNameLength('image_one', 1); 49 | } 50 | 51 | public function testItShouldCheckIfHasLengthThrowsException() 52 | { 53 | $this->setExpectedException(Exception::class); 54 | Assert::hasFileUploadedFileNameLength('image_one', 2000); 55 | } 56 | 57 | public function testItShouldCheckIfHasLengthUploadMany() 58 | { 59 | Assert::hasFileUploadedFileNameLength('image_set', 3); 60 | } 61 | 62 | public function testItShouldCheckIfHasLengthUploadManyThrowsException() 63 | { 64 | $this->setExpectedException(Exception::class); 65 | Assert::hasFileUploadedFileNameLength('image_set', 2000); 66 | } 67 | 68 | public function testItShouldCheckIfIsBetween() 69 | { 70 | Assert::isFileUploadedBetweenFileSize('image_one', 0, 2, 'MB'); 71 | } 72 | 73 | public function testItShouldCheckIfIsBetweenInclusive() 74 | { 75 | Assert::isFileUploadedBetweenFileSize('image_one', 0, 1, 'MB', true); 76 | } 77 | 78 | public function testItShouldCheckIfIsBetweenThrowsExceptionifMinIsAboveMaxFileSize() 79 | { 80 | $this->setExpectedException(Exception::class); 81 | Assert::isFileUploadedBetweenFileSize('image_one', 2, 0, 'MB'); 82 | } 83 | 84 | public function testItShouldCheckIfIsBetweenThrowsExceptionWhenNotExists() 85 | { 86 | $this->setExpectedException(Exception::class); 87 | Assert::isFileUploadedBetweenFileSize('image_one0', 20, 30, 'MB'); 88 | } 89 | 90 | public function testItShouldCheckIfIsBetweenUploadMany() 91 | { 92 | Assert::isFileUploadedBetweenFileSize('image_set', 0, 2, 'MB', true); 93 | } 94 | 95 | public function testItShouldCheckIfIsBetweenUploadManyThrowsException() 96 | { 97 | $this->setExpectedException(Exception::class); 98 | Assert::isFileUploadedBetweenFileSize('image_set', 2000, 3000, 'MB', true); 99 | } 100 | 101 | public function testItShouldCheckIfIsMimeType() 102 | { 103 | Assert::isFileUploadedMimeType('image_one', ['image/png', 'image/gif', 'image/jpg']); 104 | } 105 | 106 | public function testItShouldCheckIfIsMimeTypeThrowsException() 107 | { 108 | $this->setExpectedException(Exception::class); 109 | Assert::isFileUploadedMimeType('image_one', ['image/bmp']); 110 | } 111 | 112 | public function testItShouldCheckIfIsMimeTypeThrowsExceptionIfTmpNameNotSet() 113 | { 114 | unset($_FILES['image_one']['tmp_name']); 115 | $this->setExpectedException(Exception::class); 116 | Assert::isFileUploadedMimeType('image_one', ['image/png']); 117 | } 118 | 119 | public function testItShouldCheckIfIsMimeTypeUploadMany() 120 | { 121 | Assert::isFileUploadedMimeType('image_set', ['image/png', 'image/gif', 'image/jpg']); 122 | } 123 | 124 | public function testItShouldCheckIfIsMimeTypeUploadManyThrowsException() 125 | { 126 | $this->setExpectedException(Exception::class); 127 | Assert::isFileUploadedMimeType('image_set', ['image/bmp']); 128 | } 129 | 130 | public function testItShouldCheckIfIsImage() 131 | { 132 | Assert::isFileUploadedImage('image_one'); 133 | } 134 | 135 | public function testItShouldCheckIfHasFileNameFormat() 136 | { 137 | $validator = function ($value) { 138 | Assert::isLowercase($value); 139 | }; 140 | Assert::hasFileUploadedFileNameFormat('image_one', $validator); 141 | } 142 | 143 | public function testItShouldCheckIfHasFileNameFormatThrowsException() 144 | { 145 | $validator = function ($value) { 146 | Assert::isAlpha($value); 147 | }; 148 | $_FILES['image_one']['name'] = '@sample.png'; 149 | 150 | $this->setExpectedException(Exception::class); 151 | Assert::hasFileUploadedFileNameFormat('image_one', $validator); 152 | } 153 | 154 | public function testItShouldCheckIfHasFileNameFormatUploadMany() 155 | { 156 | $validator = function ($value) { 157 | Assert::isLowercase($value); 158 | }; 159 | Assert::hasFileUploadedFileNameFormat('image_set', $validator); 160 | } 161 | 162 | public function testItShouldCheckIfHasFileNameFormatUploadManyThrowsException() 163 | { 164 | $validator = function ($value) { 165 | Assert::isAlpha($value); 166 | }; 167 | 168 | $_FILES['image_set']['name'][0] = '@sample.png'; 169 | $this->setExpectedException(Exception::class); 170 | Assert::hasFileUploadedFileNameFormat('image_set', $validator); 171 | } 172 | 173 | public function testItShouldCheckIfHasValidUploadDirectory() 174 | { 175 | Assert::hasFileUploadedValidUploadDirectory('image_one', realpath(dirname(__FILE__))); 176 | } 177 | 178 | public function testItShouldCheckIfHasValidUploadDirectoryThrowsException() 179 | { 180 | $this->setExpectedException(Exception::class); 181 | Assert::hasFileUploadedValidUploadDirectory('image_one', realpath(dirname(__FILE__)).'/not/'); 182 | } 183 | 184 | public function testItShouldCheckIfHasValidUploadDirectoryUploadMany() 185 | { 186 | Assert::hasFileUploadedValidUploadDirectory('image_set', realpath(dirname(__FILE__)).'/resources/'); 187 | } 188 | 189 | public function testItShouldCheckIfHasValidUploadDirectoryUploadManyThrowsException() 190 | { 191 | $this->setExpectedException(Exception::class); 192 | Assert::hasFileUploadedValidUploadDirectory('image_set', realpath(dirname(__FILE__)).'/not/'); 193 | } 194 | 195 | public function testItShouldCheckIfNotOverwritingExistingFile() 196 | { 197 | $_FILES['image_one']['name'] = 'a.png'; 198 | Assert::isFileUploadedNotOverwritingExistingFile('image_one', realpath(dirname(__FILE__)).'/resources'); 199 | } 200 | 201 | public function testItShouldCheckIfNotOverwritingExistingFileThrowsException() 202 | { 203 | $this->setExpectedException(Exception::class); 204 | Assert::isFileUploadedNotOverwritingExistingFile('image_one', realpath(dirname(__FILE__)).'/resources'); 205 | } 206 | 207 | public function testItShouldCheckIfNotOverwritingExistingFileUploadMany() 208 | { 209 | $_FILES['image_set']['name'][0] = 'a.png'; 210 | $_FILES['image_set']['name'][1] = 'b.png'; 211 | $_FILES['image_set']['name'][2] = 'c.png'; 212 | Assert::isFileUploadedNotOverwritingExistingFile('image_set', \realpath(\dirname(__FILE__)).'/resources'); 213 | } 214 | 215 | public function testItShouldCheckIfNotOverwritingExistingFileUploadManyThrowsException() 216 | { 217 | $this->setExpectedException(Exception::class); 218 | Assert::isFileUploadedNotOverwritingExistingFile('image_set', \realpath(\dirname(__FILE__)).'/resources'); 219 | } 220 | 221 | public function testItShouldReturnErrorForNonMultipleFilesArray() 222 | { 223 | $_FILES = [ 224 | 'image' => [ 225 | 'name' => 'sample.png', 226 | 'type' => 'image/png', 227 | 'tmp_name' => \realpath(\dirname(__FILE__)).'/resources/phpGpKMlf', 228 | 'error' => 1, 229 | 'size' => '203868', 230 | ], 231 | ]; 232 | $this->setExpectedException(Exception::class); 233 | throw new FileUploadException('image'); 234 | } 235 | 236 | public function testItShouldReturnErrorForMultipleFilesArray() 237 | { 238 | $_FILES = [ 239 | 'image' => [ 240 | 'name' => ['sample.png', 'sample.png', 'sample.png'], 241 | 'type' => ['image/png', 'image/png', 'image/png'], 242 | 'tmp_name' => [ 243 | \realpath(\dirname(__FILE__)).'/resources/phpGpKMlf', 244 | \realpath(\dirname(__FILE__)).'/resources/phpGpKMlf', 245 | \realpath(\dirname(__FILE__)).'/resources/phpGpKMlf', 246 | ], 247 | 'error' => [1, 1, 1], 248 | 'size' => [203868, 203868, 203868], 249 | ], 250 | ]; 251 | 252 | $this->setExpectedException(Exception::class); 253 | throw new FileUploadException('image'); 254 | } 255 | } 256 | -------------------------------------------------------------------------------- /tests/AssertFloatTest.php: -------------------------------------------------------------------------------- 1 | setExpectedException(Exception::class); 19 | FloatAssertions::isFloat(3); 20 | } 21 | 22 | public function testItShouldCheckIfItIsNotZero() 23 | { 24 | Assert::isNotZero(3.14); 25 | Assert::isNotZero(0.00000001); 26 | } 27 | 28 | public function testItShouldCheckIfItIsNotZeroThrowsException1() 29 | { 30 | $this->setExpectedException(Exception::class); 31 | Assert::isNotZero(0.00); 32 | } 33 | 34 | public function testItShouldCheckIfItIsNotZeroThrowsException2() 35 | { 36 | $this->setExpectedException(Exception::class); 37 | Assert::isNotZero(0); 38 | } 39 | 40 | public function testItShouldCheckIfItIsPositive() 41 | { 42 | Assert::isPositive(3.14); 43 | } 44 | 45 | public function testItShouldCheckIfItIsPositiveThrowsException1() 46 | { 47 | $this->setExpectedException(Exception::class); 48 | Assert::isPositive(0); 49 | } 50 | 51 | public function testItShouldCheckIfItIsPositiveThrowsException2() 52 | { 53 | $this->setExpectedException(Exception::class); 54 | Assert::isPositive(-3.14); 55 | } 56 | 57 | public function testItShouldCheckIfItIsPositiveOrZero() 58 | { 59 | Assert::isPositiveOrZero(3.14); 60 | Assert::isPositiveOrZero(0); 61 | } 62 | 63 | public function testItShouldCheckIfItIsPositiveOrZeroThrowsException1() 64 | { 65 | $this->setExpectedException(Exception::class); 66 | Assert::isPositiveOrZero(-3.14); 67 | } 68 | 69 | public function testItShouldCheckIfItIsNegative() 70 | { 71 | Assert::isNegative(-3.14); 72 | } 73 | 74 | public function testItShouldCheckIfItIsNegativeThrowsException1() 75 | { 76 | $this->setExpectedException(Exception::class); 77 | Assert::isNegative(0); 78 | } 79 | 80 | public function testItShouldCheckIfItIsNegativeThrowsException2() 81 | { 82 | $this->setExpectedException(Exception::class); 83 | Assert::isNegative(3.14); 84 | } 85 | 86 | public function testItShouldCheckIfItIsNegativeOrZero() 87 | { 88 | Assert::isNegativeOrZero(-3.14); 89 | Assert::isNegativeOrZero(0); 90 | } 91 | 92 | public function testItShouldCheckIfItIsNegativeOrZeroThrowsException() 93 | { 94 | $this->setExpectedException(Exception::class); 95 | Assert::isNegativeOrZero(3.14); 96 | } 97 | 98 | public function testItShouldCheckIfItIsBetween() 99 | { 100 | Assert::isBetween(3.14, 1.2, 5.6, false); 101 | Assert::isBetween(3.14, 1.2, 3.14, true); 102 | } 103 | 104 | public function testItShouldCheckIfItIsBetweenThrowsException1() 105 | { 106 | $this->setExpectedException(Exception::class); 107 | Assert::isBetween(13.14, 1.2, 5.6, false); 108 | } 109 | 110 | public function testItShouldCheckIfItIsBetweenThrowsException2() 111 | { 112 | $this->setExpectedException(Exception::class); 113 | Assert::isBetween(13.14, 1.2, 3.14, true); 114 | } 115 | 116 | public function testItShouldCheckStringIsBetweenThrowsException() 117 | { 118 | $this->setExpectedException(Exception::class); 119 | Assert::isBetween(3.14, 12.3, 4.2, false); 120 | } 121 | 122 | public function testItShouldCheckIfItIsOdd() 123 | { 124 | Assert::isOdd(1.1); 125 | } 126 | 127 | public function testItShouldCheckIfItIsOddThrowsException() 128 | { 129 | $this->setExpectedException(Exception::class); 130 | Assert::isOdd(6.5); 131 | } 132 | 133 | public function testItShouldCheckIfItIsOddWithIntegers() 134 | { 135 | FloatAssertions::isOdd(1); 136 | } 137 | 138 | public function testItShouldCheckIfItIsOddWithIntegersThrowsException() 139 | { 140 | $this->setExpectedException(Exception::class); 141 | FloatAssertions::isOdd(6); 142 | } 143 | 144 | public function testItShouldCheckIfItIsEven() 145 | { 146 | Assert::isEven(2.2); 147 | } 148 | 149 | public function testItShouldCheckIfItIsEvenThrowsException() 150 | { 151 | $this->setExpectedException(Exception::class); 152 | Assert::isEven(5.4); 153 | } 154 | 155 | public function testItShouldCheckIfItIsEvenWithIntegers() 156 | { 157 | FloatAssertions::isEven(2); 158 | } 159 | 160 | public function testItShouldCheckIfItIsEvenWithIntegersThrowsException() 161 | { 162 | $this->setExpectedException(Exception::class); 163 | FloatAssertions::isEven(5); 164 | } 165 | 166 | public function testItShouldCheckIfItIsMultiple() 167 | { 168 | Assert::isMultiple(5.00, 2.50); 169 | } 170 | 171 | public function testItShouldCheckIfItIsMultipleThrowsException() 172 | { 173 | $this->setExpectedException(Exception::class); 174 | Assert::isMultiple(3.14, 1); 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /tests/AssertGenericTest.php: -------------------------------------------------------------------------------- 1 | setExpectedException(Exception::class); 19 | $value = []; 20 | Assert::isScalar($value); 21 | } 22 | 23 | public function testItShouldCheckIfIsRequired() 24 | { 25 | $value = 'asdsdadds'; 26 | Assert::isRequired($value); 27 | } 28 | 29 | public function testItShouldCheckIfIsRequiredThrowsException1() 30 | { 31 | $this->setExpectedException(Exception::class); 32 | $value = null; 33 | Assert::isRequired($value); 34 | } 35 | 36 | public function testItShouldCheckIsNotNull() 37 | { 38 | $value = 'asdsdadds'; 39 | Assert::isNotNull($value); 40 | } 41 | 42 | public function testItShouldCheckIsNotNullThrowsException1() 43 | { 44 | $this->setExpectedException(Exception::class); 45 | $value = null; 46 | Assert::isNotNull($value); 47 | } 48 | 49 | public function testItShouldCheckIsNotNullThrowsException2() 50 | { 51 | $this->setExpectedException(Exception::class); 52 | $value = ''; 53 | Assert::isNotNull($value); 54 | } 55 | 56 | public function testItShouldCheckLessThanOrEqual() 57 | { 58 | Assert::lessThanOrEqual(10, 10); 59 | } 60 | 61 | public function testItShouldCheckLessThanOrEqualThrowsException() 62 | { 63 | $this->setExpectedException(Exception::class); 64 | Assert::lessThanOrEqual(15, 10); 65 | } 66 | 67 | public function testItShouldCheckLessThan() 68 | { 69 | Assert::lessThan(5, 10); 70 | } 71 | 72 | public function testItShouldCheckLessThanThrowsException() 73 | { 74 | $this->setExpectedException(Exception::class); 75 | Assert::lessThan(15, 10); 76 | } 77 | 78 | public function testItShouldCheckGreaterThan() 79 | { 80 | Assert::greaterThan(100, 10); 81 | } 82 | 83 | public function testItShouldCheckGreaterThanThrowsException() 84 | { 85 | $this->setExpectedException(Exception::class); 86 | Assert::greaterThan(5, 10); 87 | } 88 | 89 | public function testItShouldCheckGreaterThanOrEqual() 90 | { 91 | Assert::greaterThanOrEqual(10, 10); 92 | } 93 | 94 | public function testItShouldCheckGreaterThanOrEqualThrowsException() 95 | { 96 | $this->setExpectedException(Exception::class); 97 | Assert::greaterThanOrEqual(5, 10); 98 | } 99 | 100 | public function testItShouldCheckNotEquals() 101 | { 102 | Assert::notEquals(6, 10); 103 | } 104 | 105 | public function testItShouldCheckNotEqualsThrowsException() 106 | { 107 | $this->setExpectedException(Exception::class); 108 | Assert::notEquals(10, 10); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /tests/AssertIntegerTest.php: -------------------------------------------------------------------------------- 1 | setExpectedException(Exception::class); 19 | IntegerAssertions::isInteger(3.14); 20 | } 21 | 22 | public function testItShouldCheckIfItIsNotZero() 23 | { 24 | Assert::isNotZero(3); 25 | } 26 | 27 | public function testItShouldCheckIfItIsNotZeroThrowsException() 28 | { 29 | $this->setExpectedException(Exception::class); 30 | Assert::isNotZero(0); 31 | } 32 | 33 | public function testItShouldCheckIfItIsPositive() 34 | { 35 | Assert::isPositive(3); 36 | } 37 | 38 | public function testItShouldCheckIfItIsPositiveThrowsException1() 39 | { 40 | $this->setExpectedException(Exception::class); 41 | Assert::isPositive(-3); 42 | } 43 | 44 | public function testItShouldCheckIfItIsPositiveThrowsException2() 45 | { 46 | $this->setExpectedException(Exception::class); 47 | Assert::isPositive(0); 48 | } 49 | 50 | public function testItShouldCheckIfItIsPositiveOrZero() 51 | { 52 | Assert::isPositiveOrZero(3); 53 | Assert::isPositiveOrZero(0); 54 | } 55 | 56 | public function testItShouldCheckIfItIsPositiveOrZeroThrowsException() 57 | { 58 | $this->setExpectedException(Exception::class); 59 | Assert::isPositiveOrZero(-3); 60 | } 61 | 62 | public function testItShouldCheckIfItIsNegativeOrZero() 63 | { 64 | Assert::isNegativeOrZero(-3); 65 | Assert::isNegativeOrZero(0); 66 | } 67 | 68 | public function testItShouldCheckIfItIsNegativeOrZeroThrowsException() 69 | { 70 | $this->setExpectedException(Exception::class); 71 | Assert::isNegativeOrZero(3); 72 | } 73 | 74 | public function testItShouldCheckIfItIsNegative() 75 | { 76 | Assert::isNegative(-3); 77 | } 78 | 79 | public function testItShouldCheckIfItIsNegativeThrowsException() 80 | { 81 | $this->setExpectedException(Exception::class); 82 | Assert::isNegative(3); 83 | } 84 | 85 | public function testItShouldCheckIfItIsBetween() 86 | { 87 | Assert::isBetween(3, 1, 5, false); 88 | } 89 | 90 | public function testItShouldCheckIfItIsBetweenInclusive() 91 | { 92 | Assert::isBetween(3, 1, 3, true); 93 | } 94 | 95 | public function testItShouldCheckIfItIsBetweenThrowsException1() 96 | { 97 | $this->setExpectedException(Exception::class); 98 | Assert::isBetween(13, 1, 5, false); 99 | } 100 | 101 | public function testItShouldCheckIfItIsBetweenThrowsException2() 102 | { 103 | $this->setExpectedException(Exception::class); 104 | Assert::isBetween(13, 1, 3, true); 105 | } 106 | 107 | public function testItShouldCheckStringIsBetweenException() 108 | { 109 | $this->setExpectedException(Exception::class); 110 | Assert::isBetween(3, 12, 4, false); 111 | } 112 | 113 | public function testItShouldCheckIfItIsOdd() 114 | { 115 | Assert::isOdd(3); 116 | } 117 | 118 | public function testItShouldCheckIfItIsOddThrowsException() 119 | { 120 | $this->setExpectedException(Exception::class); 121 | Assert::isOdd(4); 122 | } 123 | 124 | public function testItShouldCheckIfItIsEven() 125 | { 126 | Assert::isEven(4); 127 | } 128 | 129 | public function testItShouldCheckIfItIsEvenThrowsException() 130 | { 131 | $this->setExpectedException(Exception::class); 132 | Assert::isEven(3); 133 | } 134 | 135 | public function testItShouldCheckIfItIsMultiple() 136 | { 137 | Assert::isMultiple(6, 3); 138 | } 139 | 140 | public function testItShouldCheckIfItIsMultipleThrowsException() 141 | { 142 | $this->setExpectedException(Exception::class); 143 | Assert::isMultiple(13, 7); 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /tests/AssertObjectTest.php: -------------------------------------------------------------------------------- 1 | setExpectedException(Exception::class); 19 | ObjectAssertions::isObject('a'); 20 | } 21 | 22 | public function testItShouldCheckIfIsInstanceOf() 23 | { 24 | Assert::isInstanceOf(new \DateTime(), 'DateTime'); 25 | } 26 | 27 | public function testItShouldCheckIfIsInstanceOfThrowsException1() 28 | { 29 | $this->setExpectedException(Exception::class); 30 | Assert::isInstanceOf(new \stdClass(), 'DateTime'); 31 | } 32 | 33 | public function testItShouldCheckIfIsInstanceOfThrowsException2() 34 | { 35 | $this->setExpectedException(Exception::class); 36 | Assert::isInstanceOf('a', 'DateTime'); 37 | } 38 | 39 | public function testItShouldCheckIfHasProperty() 40 | { 41 | $dummy = new Dummy(); 42 | Assert::hasProperty($dummy, 'userName'); 43 | } 44 | 45 | public function testItShouldCheckIfHasPropertyThrowException() 46 | { 47 | $dummy = new Dummy(); 48 | $this->setExpectedException(Exception::class); 49 | Assert::hasProperty($dummy, 'password'); 50 | } 51 | 52 | public function testItShouldCheckIfHasMethod() 53 | { 54 | $dummy = new Dummy(); 55 | Assert::hasMethod($dummy, 'getUserName'); 56 | } 57 | 58 | public function testItShouldCheckIfHasMethodThrowException() 59 | { 60 | $dummy = new Dummy(); 61 | $this->setExpectedException(Exception::class); 62 | Assert::hasMethod($dummy, 'getPassword'); 63 | } 64 | 65 | public function testItShouldCheckIfHasParentClass() 66 | { 67 | Assert::hasParentClass(new Dummy()); 68 | } 69 | 70 | public function testItShouldCheckIfHasParentClassThrowException() 71 | { 72 | $this->setExpectedException(Exception::class); 73 | Assert::hasParentClass(new \stdClass()); 74 | } 75 | 76 | public function testItShouldCheckIfIsChildOf() 77 | { 78 | $dummy = new Dummy(); 79 | Assert::isChildOf($dummy, 'DateTime'); 80 | } 81 | 82 | public function testItShouldCheckIfIsChildOfThrowException() 83 | { 84 | $dummy = new Dummy(); 85 | $this->setExpectedException(Exception::class); 86 | Assert::isChildOf($dummy, 'DateTimeZone'); 87 | } 88 | 89 | public function testItShouldCheckIfInheritsFrom() 90 | { 91 | $dummy = new Dummy(); 92 | Assert::inheritsFrom($dummy, 'DateTime'); 93 | } 94 | 95 | public function testItShouldCheckIfInheritsFromThrowException() 96 | { 97 | $dummy = new Dummy(); 98 | $this->setExpectedException(Exception::class); 99 | Assert::inheritsFrom($dummy, 'DateTimeZone'); 100 | } 101 | 102 | public function testItShouldCheckHasInterface() 103 | { 104 | $dummy = new Dummy(); 105 | Assert::hasInterface($dummy, DummyInterface::class); 106 | } 107 | 108 | public function testItShouldCheckHasInterfaceFromThrowException() 109 | { 110 | $this->setExpectedException(Exception::class); 111 | Assert::hasInterface(new \stdClass(), DummyInterface::class); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /tests/AssertStringTest.php: -------------------------------------------------------------------------------- 1 | assertTrue(true); 17 | } 18 | 19 | public function testItShouldCheckStringIsStringThrowsException() 20 | { 21 | $value = new stdClass(); 22 | 23 | $this->setExpectedException(Exception::class); 24 | StringAssertions::isString($value); 25 | } 26 | 27 | public function testItShouldCheckStringIsAlphanumeric() 28 | { 29 | $value = 'Qwerty1234'; 30 | Assert::isAlphanumeric($value); 31 | $this->assertTrue(true); 32 | } 33 | 34 | public function testIsAlphanumericWillThrowException() 35 | { 36 | $value = ''; 37 | $this->setExpectedException(Exception::class); 38 | Assert::isAlphanumeric($value); 39 | } 40 | 41 | public function testItShouldCheckStringIsAlpha() 42 | { 43 | $value = 'querty'; 44 | Assert::isAlpha($value); 45 | $this->assertTrue(true); 46 | } 47 | 48 | public function testIsAlphaWillThrowException() 49 | { 50 | $value = 'querty123'; 51 | $this->setExpectedException(Exception::class); 52 | Assert::isAlpha($value); 53 | } 54 | 55 | public function testItShouldCheckStringIsBetween() 56 | { 57 | $value = 'Nil'; 58 | Assert::isBetween($value, 2, 4, false); 59 | $this->assertTrue(true); 60 | } 61 | 62 | public function testItShouldCheckStringIsBetweenInclusive() 63 | { 64 | $value = 'Nilo'; 65 | Assert::isBetween($value, 2, 4, true); 66 | $this->assertTrue(true); 67 | } 68 | 69 | public function testIsBetweenWillThrowException1() 70 | { 71 | $this->setExpectedException(Exception::class); 72 | Assert::isBetween('Nil', 12, 4, false); 73 | } 74 | 75 | public function testIsBetweenWillThrowException2() 76 | { 77 | $this->setExpectedException(Exception::class); 78 | Assert::isBetween('Nil', 5, 8, false); 79 | } 80 | 81 | public function testIsBetweenWillThrowException3() 82 | { 83 | $this->setExpectedException(Exception::class); 84 | Assert::isBetween('Nil', 5, 8, true); 85 | } 86 | 87 | public function testItShouldCheckStringIsCharset() 88 | { 89 | $value = 'Portugués'; 90 | Assert::isCharset($value, 'UTF-8'); 91 | $this->assertTrue(true); 92 | } 93 | 94 | public function testIsCharsetWillThrowException() 95 | { 96 | $this->setExpectedException(Exception::class); 97 | $value = '日本語'; 98 | Assert::isCharset($value, 'iso-8859-15'); 99 | } 100 | 101 | public function testItShouldCheckStringIsAllConsonants() 102 | { 103 | $value = 'bs'; 104 | Assert::isAllConsonants($value); 105 | $this->assertTrue(true); 106 | } 107 | 108 | public function testIsAllConsonantsWillThrowException() 109 | { 110 | $this->setExpectedException(Exception::class); 111 | $value = 'a'; 112 | Assert::isAllConsonants($value); 113 | } 114 | 115 | public function testItShouldCheckStringIsContains() 116 | { 117 | $value = 'AAAAAAA123A'; 118 | $contains = 123; 119 | $identical = false; 120 | Assert::contains($value, $contains, $identical); 121 | } 122 | 123 | public function testItShouldCheckStringIsContainsIdentical() 124 | { 125 | $value = 'AAAAAAAaaaA'; 126 | $contains = 'aaa'; 127 | $identical = true; 128 | Assert::contains($value, $contains, $identical); 129 | } 130 | 131 | public function testContainsWillThrowException() 132 | { 133 | $this->setExpectedException(Exception::class); 134 | $value = 'AAAAAAAaaaA'; 135 | $contains = 'B'; 136 | $identical = true; 137 | Assert::contains($value, $contains, $identical); 138 | } 139 | 140 | public function testContainsWillThrowException2() 141 | { 142 | $this->setExpectedException(Exception::class); 143 | $value = 'AAAAAAAaa1aA'; 144 | $contains = 2; 145 | $identical = false; 146 | Assert::contains($value, $contains, $identical); 147 | } 148 | 149 | public function testItShouldCheckStringIsControlCharacters() 150 | { 151 | $value = "\n\t"; 152 | Assert::isControlCharacters($value); 153 | $this->assertTrue(true); 154 | } 155 | 156 | public function testIsControlCharactersWillThrowException() 157 | { 158 | $this->setExpectedException(Exception::class); 159 | $value = "\nHello\tWorld"; 160 | Assert::isControlCharacters($value); 161 | } 162 | 163 | public function testIsDigit() 164 | { 165 | $value = '145'; 166 | Assert::isDigit($value); 167 | $this->assertTrue(true); 168 | } 169 | 170 | public function testIsDigitWillThrowException() 171 | { 172 | $this->setExpectedException(Exception::class); 173 | $value = 145.6; 174 | Assert::isDigit($value); 175 | } 176 | public function testItShouldCheckStringIsEndsWith() 177 | { 178 | $value = 'AAAAAAA123'; 179 | $contains = 123; 180 | $identical = false; 181 | Assert::endsWith($value, $contains, $identical); 182 | $this->assertTrue(true); 183 | } 184 | 185 | public function testItShouldCheckStringIsEndsWithIdentical() 186 | { 187 | $value = 'AAAAAAAaaaA'; 188 | $contains = 'aaaA'; 189 | $identical = true; 190 | Assert::endsWith($value, $contains, $identical); 191 | $this->assertTrue(true); 192 | } 193 | 194 | public function testEndsWithWillThrowException1() 195 | { 196 | $this->setExpectedException(Exception::class); 197 | $value = 'AAAAAAAaaaA1'; 198 | $contains = '2'; 199 | $identical = false; 200 | Assert::endsWith($value, $contains, $identical); 201 | } 202 | 203 | public function testEndsWithWillThrowException2() 204 | { 205 | $this->setExpectedException(Exception::class); 206 | $value = 'AAAAAAAaaaA1'; 207 | $contains = 2; 208 | $identical = true; 209 | Assert::endsWith($value, $contains, $identical); 210 | } 211 | 212 | public function testItShouldCheckStringIsEquals() 213 | { 214 | $value = '1'; 215 | $comparedValue = 1; 216 | $identical = false; 217 | Assert::equals($value, $comparedValue, $identical); 218 | 219 | $this->assertTrue(true); 220 | } 221 | 222 | public function testItShouldCheckStringIsEqualsIdentical() 223 | { 224 | $value = 'hello'; 225 | $comparedValue = 'hello'; 226 | $identical = true; 227 | Assert::equals($value, $comparedValue, $identical); 228 | $this->assertTrue(true); 229 | } 230 | 231 | public function testEqualsWillThrowException1() 232 | { 233 | $this->setExpectedException(Exception::class); 234 | $value = '1'; 235 | $comparedValue = 1; 236 | $identical = true; 237 | Assert::equals($value, $comparedValue, $identical); 238 | } 239 | 240 | public function testEqualsWillThrowException2() 241 | { 242 | $this->setExpectedException(Exception::class); 243 | $value = '1'; 244 | $comparedValue = '2'; 245 | $identical = false; 246 | Assert::equals($value, $comparedValue, $identical); 247 | } 248 | 249 | public function testItShouldCheckStringIsIn() 250 | { 251 | $haystack = 'a12245 asdhsjasd 63-211'; 252 | $value = 122; 253 | $identical = false; 254 | Assert::in($value, $haystack, $identical); 255 | } 256 | 257 | public function testItShouldCheckStringIsInStrict() 258 | { 259 | $haystack = 'a12245 asdhsjasd 63-211'; 260 | $value = '5 asd'; 261 | $identical = true; 262 | Assert::in($value, $haystack, $identical); 263 | } 264 | 265 | public function testInWillThrowException() 266 | { 267 | $this->setExpectedException(Exception::class); 268 | $haystack = 'a12245 asdhsjasd 63-211'; 269 | $value = '@@@@'; 270 | $identical = true; 271 | Assert::in($value, $haystack, $identical); 272 | } 273 | 274 | public function testInWillThrowException2() 275 | { 276 | $this->setExpectedException(Exception::class); 277 | $haystack = 'a12245 asdhsjasd 63-211'; 278 | $value = 122; 279 | $identical = true; 280 | Assert::in($value, $haystack, $identical); 281 | } 282 | 283 | public function testInWillThrowException3() 284 | { 285 | $this->setExpectedException(Exception::class); 286 | $haystack = 'a12245 asdhsjasd 63-211'; 287 | $value = 1227; 288 | $identical = false; 289 | Assert::in($value, $haystack, $identical); 290 | } 291 | 292 | public function testItShouldCheckStringIsGraph() 293 | { 294 | $value = 'arf12'; 295 | Assert::hasGraphicalCharsOnly($value); 296 | } 297 | 298 | public function testHasGraphicalCharsOnlyWillThrowException() 299 | { 300 | $this->setExpectedException(Exception::class); 301 | $value = "asdf\n\r\t"; 302 | Assert::hasGraphicalCharsOnly($value); 303 | } 304 | 305 | public function testItShouldCheckStringIsLength() 306 | { 307 | $value = 'abcdefgh'; 308 | $length = 8; 309 | Assert::hasLength($value, $length); 310 | } 311 | 312 | public function testHasLengthWillThrowException() 313 | { 314 | $this->setExpectedException(Exception::class); 315 | $value = 'abcdefgh'; 316 | $length = 5; 317 | Assert::hasLength($value, $length); 318 | } 319 | 320 | public function testItShouldCheckStringIsLowercase() 321 | { 322 | $value = 'strtolower'; 323 | Assert::isLowercase($value); 324 | } 325 | 326 | public function testIsLowercaseWillThrowException() 327 | { 328 | $this->setExpectedException(Exception::class); 329 | $value = 'STRTOLOWER'; 330 | Assert::isLowercase($value); 331 | } 332 | 333 | public function testItShouldCheckStringIsNotEmpty() 334 | { 335 | $value = 'a'; 336 | Assert::notEmpty($value); 337 | } 338 | 339 | public function testNotEmptyWillThrowException() 340 | { 341 | $this->setExpectedException(Exception::class); 342 | $value = ''; 343 | Assert::notEmpty($value); 344 | } 345 | 346 | public function testItShouldCheckStringIsNoWhitespace() 347 | { 348 | $value = 'aaaaa'; 349 | Assert::noWhitespace($value); 350 | } 351 | 352 | public function testNoWhitespaceWillThrowException() 353 | { 354 | $this->setExpectedException(Exception::class); 355 | $value = 'lorem ipsum'; 356 | Assert::noWhitespace($value); 357 | } 358 | 359 | public function testItShouldCheckStringIsPrintable() 360 | { 361 | $value = 'LMKA0$% _123'; 362 | Assert::hasPrintableCharsOnly($value); 363 | } 364 | 365 | public function testHasPrintableCharsOnlyWillThrowException() 366 | { 367 | $this->setExpectedException(Exception::class); 368 | $value = "LMKA0$%\t_123"; 369 | Assert::hasPrintableCharsOnly($value); 370 | } 371 | 372 | public function testItShouldCheckStringIsPunctuation() 373 | { 374 | $value = '&,.;[]'; 375 | Assert::isPunctuation($value); 376 | } 377 | 378 | public function testIsPunctuationWillThrowException() 379 | { 380 | $this->setExpectedException(Exception::class); 381 | $value = 'a'; 382 | Assert::isPunctuation($value); 383 | } 384 | 385 | public function testItShouldCheckStringIsRegex() 386 | { 387 | $value = 'a'; 388 | $regex = '/[a-z]/'; 389 | Assert::matchesRegex($value, $regex); 390 | } 391 | 392 | public function testMatchesRegexWillThrowException() 393 | { 394 | $this->setExpectedException(Exception::class); 395 | $value = 'A'; 396 | $regex = '/[a-z]/'; 397 | Assert::matchesRegex($value, $regex); 398 | } 399 | 400 | public function testItShouldCheckStringIsSlug() 401 | { 402 | $value = 'hello-world-yeah'; 403 | Assert::isSlug($value); 404 | } 405 | 406 | public function testIsSlugWillThrowExceptionVariation1() 407 | { 408 | $this->setExpectedException(Exception::class); 409 | $value = '-hello-world-yeah'; 410 | Assert::isSlug($value); 411 | } 412 | 413 | public function testIsSlugWillThrowExceptionVariation2() 414 | { 415 | $this->setExpectedException(Exception::class); 416 | $value = 'hello-world-yeah-'; 417 | Assert::isSlug($value); 418 | } 419 | 420 | public function testIsSlugWillThrowExceptionVariation3() 421 | { 422 | $this->setExpectedException(Exception::class); 423 | $value = 'hello-world----yeah'; 424 | Assert::isSlug($value); 425 | } 426 | 427 | public function testItShouldCheckStringIsSpace() 428 | { 429 | $value = ' '; 430 | Assert::isSpace($value); 431 | } 432 | 433 | public function testIsSpaceWillThrowException() 434 | { 435 | $this->setExpectedException(Exception::class); 436 | $value = 'e e'; 437 | Assert::isSpace($value); 438 | } 439 | 440 | public function testItShouldCheckStringIsStartsWith() 441 | { 442 | $value = 'aaaAAAAAAAA'; 443 | $contains = 'aaaA'; 444 | $identical = true; 445 | Assert::startsWith($value, $contains, $identical); 446 | 447 | $value = '123AAAAAAA'; 448 | $contains = 123; 449 | $identical = false; 450 | Assert::startsWith($value, $contains, $identical); 451 | } 452 | 453 | public function testStartsWithWillThrowExceptionVariation1() 454 | { 455 | $this->setExpectedException(Exception::class); 456 | $value = '123AAAAAAA'; 457 | $contains = 134; 458 | $identical = false; 459 | Assert::startsWith($value, $contains, $identical); 460 | } 461 | 462 | public function testStartsWithWillThrowExceptionVariation2() 463 | { 464 | $this->setExpectedException(Exception::class); 465 | $value = '123AAAAAAA'; 466 | $contains = '134'; 467 | $identical = true; 468 | Assert::startsWith($value, $contains, $identical); 469 | } 470 | 471 | public function testItShouldCheckStringIsUppercase() 472 | { 473 | $value = 'AAAAAA'; 474 | Assert::isUppercase($value); 475 | } 476 | 477 | public function testIsUppercaseWillThrowException() 478 | { 479 | $this->setExpectedException(Exception::class); 480 | $value = 'aaaa'; 481 | Assert::isUppercase($value); 482 | } 483 | 484 | public function testItShouldCheckStringIsVersion() 485 | { 486 | $value = '1.0.2'; 487 | Assert::isVersion($value); 488 | 489 | $value = '1.0.2-beta'; 490 | Assert::isVersion($value); 491 | 492 | $value = '1.0'; 493 | Assert::isVersion($value); 494 | } 495 | 496 | public function testIsVersionWillThrowException() 497 | { 498 | $this->setExpectedException(Exception::class); 499 | $value = '1.0.2 beta'; 500 | Assert::isVersion($value); 501 | } 502 | 503 | public function testItShouldCheckStringIsVowel() 504 | { 505 | $value = 'aeA'; 506 | Assert::isVowel($value); 507 | } 508 | 509 | public function testIsVowelWillThrowException() 510 | { 511 | $this->setExpectedException(Exception::class); 512 | $value = 'cds'; 513 | Assert::isVowel($value); 514 | } 515 | 516 | public function testItShouldCheckStringIsHexDigit() 517 | { 518 | $value = '100'; 519 | Assert::isHexDigit($value); 520 | } 521 | 522 | public function testIsHexDigitWillThrowException() 523 | { 524 | $this->setExpectedException(Exception::class); 525 | $value = 'h0000'; 526 | Assert::isHexDigit($value); 527 | } 528 | 529 | public function testItShouldCheckIfHasLowercase() 530 | { 531 | Assert::hasLowercase('HELLOWOrLD'); 532 | Assert::hasLowercase('HeLLoWOrLD', 3); 533 | } 534 | 535 | public function testHasLowercaseWillThrowException() 536 | { 537 | $this->setExpectedException(Exception::class); 538 | Assert::hasLowercase('HELLOWORLD'); 539 | } 540 | 541 | public function testHasLowercaseWithCountWillThrowException() 542 | { 543 | $this->setExpectedException(Exception::class); 544 | Assert::hasLowercase('el', 3); 545 | } 546 | 547 | public function testItShouldCheckIfHasLowercaseAndUppercase() 548 | { 549 | Assert::hasUppercase('hello World'); 550 | Assert::hasUppercase('Hello World', 2); 551 | } 552 | 553 | public function testHasUppercaseWillThrowException() 554 | { 555 | $this->setExpectedException(Exception::class); 556 | Assert::hasUppercase('hello world'); 557 | } 558 | 559 | public function testHasUppercaseWithCountWillThrowException() 560 | { 561 | $this->setExpectedException(Exception::class); 562 | Assert::hasUppercase('helloWorld', 2); 563 | } 564 | 565 | public function testItShouldCheckIfHasNumeric() 566 | { 567 | Assert::hasNumeric('hell0 W0rld'); 568 | Assert::hasNumeric('H3ll0 W0rld', 3); 569 | } 570 | 571 | public function testHasNumericWillThrowException() 572 | { 573 | $this->setExpectedException(Exception::class); 574 | Assert::hasNumeric('hello world'); 575 | } 576 | 577 | public function testHasNumericWithCountWillThrowException() 578 | { 579 | $this->setExpectedException(Exception::class); 580 | Assert::hasNumeric('h3lloWorld', 2); 581 | } 582 | 583 | public function testItShouldCheckIfHasSpecialCharacters() 584 | { 585 | Assert::hasSpecialCharacters('hell0@W0rld'); 586 | Assert::hasSpecialCharacters('H3ll0@W0@rld', 2); 587 | } 588 | 589 | public function testHasSpecialCharactersWillThrowException() 590 | { 591 | $this->setExpectedException(Exception::class); 592 | Assert::hasSpecialCharacters('hello world'); 593 | } 594 | 595 | public function testHasSpecialCharactersWithCountWillThrowException() 596 | { 597 | $this->setExpectedException(Exception::class); 598 | Assert::hasSpecialCharacters('h3llo@World', 2); 599 | } 600 | 601 | public function testItShouldCheckIfIsEmail() 602 | { 603 | Assert::isEmail('hello@world.com'); 604 | Assert::isEmail('hello.earth@world.com'); 605 | Assert::isEmail('hello.earth+moon@world.com'); 606 | Assert::isEmail('hello@subdomain.world.com'); 607 | Assert::isEmail('hello.earth@subdomain.world.com'); 608 | Assert::isEmail('hello.earth+moon@subdomain.world.com'); 609 | Assert::isEmail('hello.earth+moon@127.0.0.1'); 610 | } 611 | 612 | public function testIsEmailWillThrowException() 613 | { 614 | $this->setExpectedException(Exception::class); 615 | Assert::isEmail('hello.earth+moon@localhost'); 616 | } 617 | 618 | public function testIsUrl() 619 | { 620 | Assert::isUrl('http://google.com'); 621 | Assert::isUrl('http://google.com/robots.txt'); 622 | Assert::isUrl('https://google.com'); 623 | Assert::isUrl('https://google.com/robots.txt'); 624 | Assert::isUrl('//google.com'); 625 | Assert::isUrl('//google.com/robots.txt'); 626 | } 627 | 628 | public function testIsUrlWillThrowException() 629 | { 630 | $this->setExpectedException(Exception::class); 631 | Assert::isUrl('hello'); 632 | } 633 | 634 | public function testItShouldValidateUUID() 635 | { 636 | Assert::isUUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8'); 637 | Assert::isUUID('6ba7b811-9dad-11d1-80b4-00c04fd430c8'); 638 | Assert::isUUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8'); 639 | Assert::isUUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8'); 640 | Assert::isUUID('00000000-0000-0000-0000-000000000000'); 641 | 642 | Assert::isUUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8', false); 643 | Assert::isUUID('6ba7b811-9dad-11d1-80b4-00c04fd430c8', false); 644 | Assert::isUUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8', false); 645 | Assert::isUUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8', false); 646 | Assert::isUUID('00000000-0000-0000-0000-000000000000', false); 647 | 648 | Assert::isUUID('{6ba7b810-9dad-11d1-80b4-00c04fd430c8}', false); 649 | Assert::isUUID('216f-ff40-98d9-11e3-a5e2-0800-200c-9a66', false); 650 | Assert::isUUID('{216fff40-98d9-11e3-a5e2-0800200c9a66}', false); 651 | Assert::isUUID('216fff4098d911e3a5e20800200c9a66', false); 652 | } 653 | 654 | public function testIsUUIDWillThrowExceptionVariation1() 655 | { 656 | $this->setExpectedException(Exception::class); 657 | Assert::isUUID('{6ba7b810-9dad-11d1-80b4-00c04fd430c8}'); 658 | } 659 | 660 | public function testIsUUIDWillThrowExceptionVariation2() 661 | { 662 | $this->setExpectedException(Exception::class); 663 | Assert::isUUID('216f-ff40-98d9-11e3-a5e2-0800-200c-9a66'); 664 | } 665 | 666 | public function testIsUUIDWillThrowExceptionVariation3() 667 | { 668 | $this->setExpectedException(Exception::class); 669 | Assert::isUUID('{216fff40-98d9-11e3-a5e2-0800200c9a66}'); 670 | } 671 | 672 | public function testIsUUIDWillThrowExceptionVariation4() 673 | { 674 | $this->setExpectedException(Exception::class); 675 | Assert::isUUID('216fff4098d911e3a5e20800200c9a66'); 676 | } 677 | 678 | public function testIsLongitude() 679 | { 680 | Assert::isLongitude(37.4895); 681 | } 682 | 683 | public function testIsLongitudeThrowsException() 684 | { 685 | $this->setExpectedException(Exception::class); 686 | Assert::isLongitude(250.4559); 687 | } 688 | 689 | public function testIsLatitude() 690 | { 691 | Assert::isLatitude(20.4578); 692 | } 693 | 694 | public function testIsLatitudeThrowsException() 695 | { 696 | $this->setExpectedException(Exception::class); 697 | Assert::isLatitude(120.4545); 698 | } 699 | 700 | public static function testItIsHexColor() 701 | { 702 | Assert::isHexColor('#000'); 703 | Assert::isHexColor('#000000'); 704 | } 705 | 706 | public static function testItIsIpAddress() 707 | { 708 | Assert::isIpAddress('127.0.0.1'); 709 | } 710 | 711 | public static function testItIsIpv4Address() 712 | { 713 | Assert::isIpv4Address('127.0.0.1'); 714 | } 715 | 716 | public static function testItIsIpv6Address() 717 | { 718 | Assert::isIpv6Address('::1'); 719 | } 720 | public function testIsTimeString() 721 | { 722 | Assert::isTimeString('12:13:14'); 723 | } 724 | public function testIsDateString() 725 | { 726 | Assert::isDateString('2015-12-11'); 727 | } 728 | 729 | public function testItIsCreditCard() 730 | { 731 | Assert::isCreditCard('378282246310005'); 732 | } 733 | 734 | public function testItIsJson() 735 | { 736 | Assert::isJson('{"hello": 1}'); 737 | } 738 | 739 | public function testItIsJsonThrowsException() 740 | { 741 | $this->setExpectedException(Exception::class); 742 | Assert::isJson('aaaaaaaaaaaaaaaaa'); 743 | } 744 | 745 | public function testItIsPalindrome() 746 | { 747 | Assert::isPalindrome('girafarig'); 748 | } 749 | 750 | public function testItIsPalindromeThrowsException() 751 | { 752 | $this->setExpectedException(Exception::class); 753 | Assert::isPalindrome('Pikachu'); 754 | } 755 | 756 | public function testItIsUnderScore() 757 | { 758 | Assert::isUnderScore('thunder_bolt'); 759 | } 760 | 761 | public function testItIsUnderScoreThrowsException() 762 | { 763 | $this->setExpectedException(Exception::class); 764 | Assert::isUnderScore('thunder_Bolt'); 765 | } 766 | 767 | public function testItIsCamelCase() 768 | { 769 | Assert::isCamelCase('ThunderBolt'); 770 | } 771 | 772 | public function testItIsCamelCaseThrowsException() 773 | { 774 | $this->setExpectedException(Exception::class); 775 | Assert::isCamelCase('thunderBolt'); 776 | } 777 | 778 | public function testItIsTitleCase() 779 | { 780 | Assert::isTitleCase('The Star Wars'); 781 | } 782 | 783 | public function testItIsTitleCaseThrowsException() 784 | { 785 | $this->setExpectedException(Exception::class); 786 | Assert::isTitleCase('The Star wars'); 787 | } 788 | } 789 | -------------------------------------------------------------------------------- /tests/AssertTest.php: -------------------------------------------------------------------------------- 1 | assertTrue(true); 14 | } 15 | 16 | public function testItWillAllowNullOrFailsIfDataIsInvalid() 17 | { 18 | $this->setExpectedException(\Exception::class); 19 | Assert::nullOrIsEmail('not-a-mail'); 20 | } 21 | 22 | public function testItWillThrowRuntimeExceptionWhenNonExistentMethodIsCalled() 23 | { 24 | $this->setExpectedException(RuntimeException::class); 25 | Assert::hola(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/Dummy.php: -------------------------------------------------------------------------------- 1 | 5 | * Date: 31/01/16 6 | * Time: 20:52. 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | namespace NilPortugues\Tests\Assert; 12 | 13 | /** 14 | * Class Dummy. 15 | */ 16 | class Dummy extends \DateTime implements DummyInterface 17 | { 18 | /** 19 | * @var string 20 | */ 21 | private $userName = 'username'; 22 | 23 | /** 24 | * @return string 25 | */ 26 | public function getUserName() 27 | { 28 | return $this->userName; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/DummyInterface.php: -------------------------------------------------------------------------------- 1 | 5 | * Date: 31/01/16 6 | * Time: 20:52. 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | namespace NilPortugues\Tests\Assert; 12 | 13 | interface DummyInterface 14 | { 15 | } 16 | -------------------------------------------------------------------------------- /tests/resources/phpGpKMlf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nilportugues/php-assert/2af2fde2d43ecbf266d126e4d7ed29d122c0e37d/tests/resources/phpGpKMlf -------------------------------------------------------------------------------- /tests/resources/sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nilportugues/php-assert/2af2fde2d43ecbf266d126e4d7ed29d122c0e37d/tests/resources/sample.png --------------------------------------------------------------------------------