| Total Complexity | 269 |
| Total Lines | 2825 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Assert often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Assert, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 68 | abstract class Assert |
||
| 69 | { |
||
| 70 | /** |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | private static $count = 0; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Asserts that an array has a specified key. |
||
| 77 | * |
||
| 78 | * @param int|string $key |
||
| 79 | * @param array|ArrayAccess $array |
||
| 80 | * |
||
| 81 | * @throws ExpectationFailedException |
||
| 82 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 83 | */ |
||
| 84 | public static function assertArrayHasKey($key, $array, string $message = ''): void |
||
| 85 | { |
||
| 86 | if (!(\is_int($key) || \is_string($key))) { |
||
|
|
|||
| 87 | throw InvalidArgumentHelper::factory( |
||
| 88 | 1, |
||
| 89 | 'integer or string' |
||
| 90 | ); |
||
| 91 | } |
||
| 92 | |||
| 93 | if (!(\is_array($array) || $array instanceof ArrayAccess)) { |
||
| 94 | throw InvalidArgumentHelper::factory( |
||
| 95 | 2, |
||
| 96 | 'array or ArrayAccess' |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | |||
| 100 | $constraint = new ArrayHasKey($key); |
||
| 101 | |||
| 102 | static::assertThat($array, $constraint, $message); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Asserts that an array has a specified subset. |
||
| 107 | * |
||
| 108 | * @param array|ArrayAccess $subset |
||
| 109 | * @param array|ArrayAccess $array |
||
| 110 | * |
||
| 111 | * @throws ExpectationFailedException |
||
| 112 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 113 | * |
||
| 114 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3494 |
||
| 115 | */ |
||
| 116 | public static function assertArraySubset($subset, $array, bool $checkForObjectIdentity = false, string $message = ''): void |
||
| 117 | { |
||
| 118 | if (!(\is_array($subset) || $subset instanceof ArrayAccess)) { |
||
| 119 | throw InvalidArgumentHelper::factory( |
||
| 120 | 1, |
||
| 121 | 'array or ArrayAccess' |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | |||
| 125 | if (!(\is_array($array) || $array instanceof ArrayAccess)) { |
||
| 126 | throw InvalidArgumentHelper::factory( |
||
| 127 | 2, |
||
| 128 | 'array or ArrayAccess' |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | $constraint = new ArraySubset($subset, $checkForObjectIdentity); |
||
| 133 | |||
| 134 | static::assertThat($array, $constraint, $message); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Asserts that an array does not have a specified key. |
||
| 139 | * |
||
| 140 | * @param int|string $key |
||
| 141 | * @param array|ArrayAccess $array |
||
| 142 | * |
||
| 143 | * @throws ExpectationFailedException |
||
| 144 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 145 | */ |
||
| 146 | public static function assertArrayNotHasKey($key, $array, string $message = ''): void |
||
| 147 | { |
||
| 148 | if (!(\is_int($key) || \is_string($key))) { |
||
| 149 | throw InvalidArgumentHelper::factory( |
||
| 150 | 1, |
||
| 151 | 'integer or string' |
||
| 152 | ); |
||
| 153 | } |
||
| 154 | |||
| 155 | if (!(\is_array($array) || $array instanceof ArrayAccess)) { |
||
| 156 | throw InvalidArgumentHelper::factory( |
||
| 157 | 2, |
||
| 158 | 'array or ArrayAccess' |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | |||
| 162 | $constraint = new LogicalNot( |
||
| 163 | new ArrayHasKey($key) |
||
| 164 | ); |
||
| 165 | |||
| 166 | static::assertThat($array, $constraint, $message); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Asserts that a haystack contains a needle. |
||
| 171 | * |
||
| 172 | * @throws ExpectationFailedException |
||
| 173 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 174 | */ |
||
| 175 | public static function assertContains($needle, $haystack, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void |
||
| 176 | { |
||
| 177 | if (\is_array($haystack) || |
||
| 178 | (\is_object($haystack) && $haystack instanceof Traversable)) { |
||
| 179 | $constraint = new TraversableContains( |
||
| 180 | $needle, |
||
| 181 | $checkForObjectIdentity, |
||
| 182 | $checkForNonObjectIdentity |
||
| 183 | ); |
||
| 184 | } elseif (\is_string($haystack)) { |
||
| 185 | if (!\is_string($needle)) { |
||
| 186 | throw InvalidArgumentHelper::factory( |
||
| 187 | 1, |
||
| 188 | 'string' |
||
| 189 | ); |
||
| 190 | } |
||
| 191 | |||
| 192 | $constraint = new StringContains( |
||
| 193 | $needle, |
||
| 194 | $ignoreCase |
||
| 195 | ); |
||
| 196 | } else { |
||
| 197 | throw InvalidArgumentHelper::factory( |
||
| 198 | 2, |
||
| 199 | 'array, traversable or string' |
||
| 200 | ); |
||
| 201 | } |
||
| 202 | |||
| 203 | static::assertThat($haystack, $constraint, $message); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Asserts that a haystack that is stored in a static attribute of a class |
||
| 208 | * or an attribute of an object contains a needle. |
||
| 209 | * |
||
| 210 | * @param object|string $haystackClassOrObject |
||
| 211 | * |
||
| 212 | * @throws ExpectationFailedException |
||
| 213 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 214 | * |
||
| 215 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 216 | */ |
||
| 217 | public static function assertAttributeContains($needle, string $haystackAttributeName, $haystackClassOrObject, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void |
||
| 218 | { |
||
| 219 | static::assertContains( |
||
| 220 | $needle, |
||
| 221 | static::readAttribute($haystackClassOrObject, $haystackAttributeName), |
||
| 222 | $message, |
||
| 223 | $ignoreCase, |
||
| 224 | $checkForObjectIdentity, |
||
| 225 | $checkForNonObjectIdentity |
||
| 226 | ); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Asserts that a haystack does not contain a needle. |
||
| 231 | * |
||
| 232 | * @throws ExpectationFailedException |
||
| 233 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 234 | */ |
||
| 235 | public static function assertNotContains($needle, $haystack, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void |
||
| 236 | { |
||
| 237 | if (\is_array($haystack) || |
||
| 238 | (\is_object($haystack) && $haystack instanceof Traversable)) { |
||
| 239 | $constraint = new LogicalNot( |
||
| 240 | new TraversableContains( |
||
| 241 | $needle, |
||
| 242 | $checkForObjectIdentity, |
||
| 243 | $checkForNonObjectIdentity |
||
| 244 | ) |
||
| 245 | ); |
||
| 246 | } elseif (\is_string($haystack)) { |
||
| 247 | if (!\is_string($needle)) { |
||
| 248 | throw InvalidArgumentHelper::factory( |
||
| 249 | 1, |
||
| 250 | 'string' |
||
| 251 | ); |
||
| 252 | } |
||
| 253 | |||
| 254 | $constraint = new LogicalNot( |
||
| 255 | new StringContains( |
||
| 256 | $needle, |
||
| 257 | $ignoreCase |
||
| 258 | ) |
||
| 259 | ); |
||
| 260 | } else { |
||
| 261 | throw InvalidArgumentHelper::factory( |
||
| 262 | 2, |
||
| 263 | 'array, traversable or string' |
||
| 264 | ); |
||
| 265 | } |
||
| 266 | |||
| 267 | static::assertThat($haystack, $constraint, $message); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Asserts that a haystack that is stored in a static attribute of a class |
||
| 272 | * or an attribute of an object does not contain a needle. |
||
| 273 | * |
||
| 274 | * @param object|string $haystackClassOrObject |
||
| 275 | * |
||
| 276 | * @throws ExpectationFailedException |
||
| 277 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 278 | * |
||
| 279 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 280 | */ |
||
| 281 | public static function assertAttributeNotContains($needle, string $haystackAttributeName, $haystackClassOrObject, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void |
||
| 282 | { |
||
| 283 | static::assertNotContains( |
||
| 284 | $needle, |
||
| 285 | static::readAttribute($haystackClassOrObject, $haystackAttributeName), |
||
| 286 | $message, |
||
| 287 | $ignoreCase, |
||
| 288 | $checkForObjectIdentity, |
||
| 289 | $checkForNonObjectIdentity |
||
| 290 | ); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Asserts that a haystack contains only values of a given type. |
||
| 295 | * |
||
| 296 | * @throws ExpectationFailedException |
||
| 297 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 298 | */ |
||
| 299 | public static function assertContainsOnly(string $type, iterable $haystack, ?bool $isNativeType = null, string $message = ''): void |
||
| 300 | { |
||
| 301 | if ($isNativeType === null) { |
||
| 302 | $isNativeType = Type::isType($type); |
||
| 303 | } |
||
| 304 | |||
| 305 | static::assertThat( |
||
| 306 | $haystack, |
||
| 307 | new TraversableContainsOnly( |
||
| 308 | $type, |
||
| 309 | $isNativeType |
||
| 310 | ), |
||
| 311 | $message |
||
| 312 | ); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Asserts that a haystack contains only instances of a given class name. |
||
| 317 | * |
||
| 318 | * @throws ExpectationFailedException |
||
| 319 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 320 | */ |
||
| 321 | public static function assertContainsOnlyInstancesOf(string $className, iterable $haystack, string $message = ''): void |
||
| 322 | { |
||
| 323 | static::assertThat( |
||
| 324 | $haystack, |
||
| 325 | new TraversableContainsOnly( |
||
| 326 | $className, |
||
| 327 | false |
||
| 328 | ), |
||
| 329 | $message |
||
| 330 | ); |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Asserts that a haystack that is stored in a static attribute of a class |
||
| 335 | * or an attribute of an object contains only values of a given type. |
||
| 336 | * |
||
| 337 | * @param object|string $haystackClassOrObject |
||
| 338 | * @param bool $isNativeType |
||
| 339 | * |
||
| 340 | * @throws ExpectationFailedException |
||
| 341 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 342 | * |
||
| 343 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 344 | */ |
||
| 345 | public static function assertAttributeContainsOnly(string $type, string $haystackAttributeName, $haystackClassOrObject, ?bool $isNativeType = null, string $message = ''): void |
||
| 346 | { |
||
| 347 | static::assertContainsOnly( |
||
| 348 | $type, |
||
| 349 | static::readAttribute($haystackClassOrObject, $haystackAttributeName), |
||
| 350 | $isNativeType, |
||
| 351 | $message |
||
| 352 | ); |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Asserts that a haystack does not contain only values of a given type. |
||
| 357 | * |
||
| 358 | * @throws ExpectationFailedException |
||
| 359 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 360 | */ |
||
| 361 | public static function assertNotContainsOnly(string $type, iterable $haystack, ?bool $isNativeType = null, string $message = ''): void |
||
| 362 | { |
||
| 363 | if ($isNativeType === null) { |
||
| 364 | $isNativeType = Type::isType($type); |
||
| 365 | } |
||
| 366 | |||
| 367 | static::assertThat( |
||
| 368 | $haystack, |
||
| 369 | new LogicalNot( |
||
| 370 | new TraversableContainsOnly( |
||
| 371 | $type, |
||
| 372 | $isNativeType |
||
| 373 | ) |
||
| 374 | ), |
||
| 375 | $message |
||
| 376 | ); |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Asserts that a haystack that is stored in a static attribute of a class |
||
| 381 | * or an attribute of an object does not contain only values of a given |
||
| 382 | * type. |
||
| 383 | * |
||
| 384 | * @param object|string $haystackClassOrObject |
||
| 385 | * @param bool $isNativeType |
||
| 386 | * |
||
| 387 | * @throws ExpectationFailedException |
||
| 388 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 389 | * |
||
| 390 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 391 | */ |
||
| 392 | public static function assertAttributeNotContainsOnly(string $type, string $haystackAttributeName, $haystackClassOrObject, ?bool $isNativeType = null, string $message = ''): void |
||
| 393 | { |
||
| 394 | static::assertNotContainsOnly( |
||
| 395 | $type, |
||
| 396 | static::readAttribute($haystackClassOrObject, $haystackAttributeName), |
||
| 397 | $isNativeType, |
||
| 398 | $message |
||
| 399 | ); |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Asserts the number of elements of an array, Countable or Traversable. |
||
| 404 | * |
||
| 405 | * @param Countable|iterable $haystack |
||
| 406 | * |
||
| 407 | * @throws ExpectationFailedException |
||
| 408 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 409 | */ |
||
| 410 | public static function assertCount(int $expectedCount, $haystack, string $message = ''): void |
||
| 411 | { |
||
| 412 | if (!$haystack instanceof Countable && !\is_iterable($haystack)) { |
||
| 413 | throw InvalidArgumentHelper::factory(2, 'countable or iterable'); |
||
| 414 | } |
||
| 415 | |||
| 416 | static::assertThat( |
||
| 417 | $haystack, |
||
| 418 | new Count($expectedCount), |
||
| 419 | $message |
||
| 420 | ); |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Asserts the number of elements of an array, Countable or Traversable |
||
| 425 | * that is stored in an attribute. |
||
| 426 | * |
||
| 427 | * @param object|string $haystackClassOrObject |
||
| 428 | * |
||
| 429 | * @throws ExpectationFailedException |
||
| 430 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 431 | * |
||
| 432 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 433 | */ |
||
| 434 | public static function assertAttributeCount(int $expectedCount, string $haystackAttributeName, $haystackClassOrObject, string $message = ''): void |
||
| 435 | { |
||
| 436 | static::assertCount( |
||
| 437 | $expectedCount, |
||
| 438 | static::readAttribute($haystackClassOrObject, $haystackAttributeName), |
||
| 439 | $message |
||
| 440 | ); |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Asserts the number of elements of an array, Countable or Traversable. |
||
| 445 | * |
||
| 446 | * @param Countable|iterable $haystack |
||
| 447 | * |
||
| 448 | * @throws ExpectationFailedException |
||
| 449 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 450 | */ |
||
| 451 | public static function assertNotCount(int $expectedCount, $haystack, string $message = ''): void |
||
| 452 | { |
||
| 453 | if (!$haystack instanceof Countable && !\is_iterable($haystack)) { |
||
| 454 | throw InvalidArgumentHelper::factory(2, 'countable or iterable'); |
||
| 455 | } |
||
| 456 | |||
| 457 | $constraint = new LogicalNot( |
||
| 458 | new Count($expectedCount) |
||
| 459 | ); |
||
| 460 | |||
| 461 | static::assertThat($haystack, $constraint, $message); |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Asserts the number of elements of an array, Countable or Traversable |
||
| 466 | * that is stored in an attribute. |
||
| 467 | * |
||
| 468 | * @param object|string $haystackClassOrObject |
||
| 469 | * |
||
| 470 | * @throws ExpectationFailedException |
||
| 471 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 472 | * |
||
| 473 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 474 | */ |
||
| 475 | public static function assertAttributeNotCount(int $expectedCount, string $haystackAttributeName, $haystackClassOrObject, string $message = ''): void |
||
| 476 | { |
||
| 477 | static::assertNotCount( |
||
| 478 | $expectedCount, |
||
| 479 | static::readAttribute($haystackClassOrObject, $haystackAttributeName), |
||
| 480 | $message |
||
| 481 | ); |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Asserts that two variables are equal. |
||
| 486 | * |
||
| 487 | * @throws ExpectationFailedException |
||
| 488 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 489 | */ |
||
| 490 | public static function assertEquals($expected, $actual, string $message = '', float $delta = 0.0, int $maxDepth = 10, bool $canonicalize = false, bool $ignoreCase = false): void |
||
| 491 | { |
||
| 492 | $constraint = new IsEqual( |
||
| 493 | $expected, |
||
| 494 | $delta, |
||
| 495 | $maxDepth, |
||
| 496 | $canonicalize, |
||
| 497 | $ignoreCase |
||
| 498 | ); |
||
| 499 | |||
| 500 | static::assertThat($actual, $constraint, $message); |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Asserts that two variables are equal (canonicalizing). |
||
| 505 | * |
||
| 506 | * @throws ExpectationFailedException |
||
| 507 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 508 | */ |
||
| 509 | public static function assertEqualsCanonicalizing($expected, $actual, string $message = ''): void |
||
| 510 | { |
||
| 511 | $constraint = new IsEqual( |
||
| 512 | $expected, |
||
| 513 | 0.0, |
||
| 514 | 10, |
||
| 515 | true, |
||
| 516 | false |
||
| 517 | ); |
||
| 518 | |||
| 519 | static::assertThat($actual, $constraint, $message); |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Asserts that two variables are equal (ignoring case). |
||
| 524 | * |
||
| 525 | * @throws ExpectationFailedException |
||
| 526 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 527 | */ |
||
| 528 | public static function assertEqualsIgnoringCase($expected, $actual, string $message = ''): void |
||
| 529 | { |
||
| 530 | $constraint = new IsEqual( |
||
| 531 | $expected, |
||
| 532 | 0.0, |
||
| 533 | 10, |
||
| 534 | false, |
||
| 535 | true |
||
| 536 | ); |
||
| 537 | |||
| 538 | static::assertThat($actual, $constraint, $message); |
||
| 539 | } |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Asserts that two variables are equal (with delta). |
||
| 543 | * |
||
| 544 | * @throws ExpectationFailedException |
||
| 545 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 546 | */ |
||
| 547 | public static function assertEqualsWithDelta($expected, $actual, float $delta, string $message = ''): void |
||
| 548 | { |
||
| 549 | $constraint = new IsEqual( |
||
| 550 | $expected, |
||
| 551 | $delta |
||
| 552 | ); |
||
| 553 | |||
| 554 | static::assertThat($actual, $constraint, $message); |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Asserts that a variable is equal to an attribute of an object. |
||
| 559 | * |
||
| 560 | * @param object|string $actualClassOrObject |
||
| 561 | * |
||
| 562 | * @throws ExpectationFailedException |
||
| 563 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 564 | * |
||
| 565 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 566 | */ |
||
| 567 | public static function assertAttributeEquals($expected, string $actualAttributeName, $actualClassOrObject, string $message = '', float $delta = 0.0, int $maxDepth = 10, bool $canonicalize = false, bool $ignoreCase = false): void |
||
| 568 | { |
||
| 569 | static::assertEquals( |
||
| 570 | $expected, |
||
| 571 | static::readAttribute($actualClassOrObject, $actualAttributeName), |
||
| 572 | $message, |
||
| 573 | $delta, |
||
| 574 | $maxDepth, |
||
| 575 | $canonicalize, |
||
| 576 | $ignoreCase |
||
| 577 | ); |
||
| 578 | } |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Asserts that two variables are not equal. |
||
| 582 | * |
||
| 583 | * @param float $delta |
||
| 584 | * @param int $maxDepth |
||
| 585 | * @param bool $canonicalize |
||
| 586 | * @param bool $ignoreCase |
||
| 587 | * |
||
| 588 | * @throws ExpectationFailedException |
||
| 589 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 590 | */ |
||
| 591 | public static function assertNotEquals($expected, $actual, string $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false): void |
||
| 592 | { |
||
| 593 | $constraint = new LogicalNot( |
||
| 594 | new IsEqual( |
||
| 595 | $expected, |
||
| 596 | $delta, |
||
| 597 | $maxDepth, |
||
| 598 | $canonicalize, |
||
| 599 | $ignoreCase |
||
| 600 | ) |
||
| 601 | ); |
||
| 602 | |||
| 603 | static::assertThat($actual, $constraint, $message); |
||
| 604 | } |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Asserts that two variables are not equal (canonicalizing). |
||
| 608 | * |
||
| 609 | * @throws ExpectationFailedException |
||
| 610 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 611 | */ |
||
| 612 | public static function assertNotEqualsCanonicalizing($expected, $actual, string $message = ''): void |
||
| 613 | { |
||
| 614 | $constraint = new LogicalNot( |
||
| 615 | new IsEqual( |
||
| 616 | $expected, |
||
| 617 | 0.0, |
||
| 618 | 10, |
||
| 619 | true, |
||
| 620 | false |
||
| 621 | ) |
||
| 622 | ); |
||
| 623 | |||
| 624 | static::assertThat($actual, $constraint, $message); |
||
| 625 | } |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Asserts that two variables are not equal (ignoring case). |
||
| 629 | * |
||
| 630 | * @throws ExpectationFailedException |
||
| 631 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 632 | */ |
||
| 633 | public static function assertNotEqualsIgnoringCase($expected, $actual, string $message = ''): void |
||
| 634 | { |
||
| 635 | $constraint = new LogicalNot( |
||
| 636 | new IsEqual( |
||
| 637 | $expected, |
||
| 638 | 0.0, |
||
| 639 | 10, |
||
| 640 | false, |
||
| 641 | true |
||
| 642 | ) |
||
| 643 | ); |
||
| 644 | |||
| 645 | static::assertThat($actual, $constraint, $message); |
||
| 646 | } |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Asserts that two variables are not equal (with delta). |
||
| 650 | * |
||
| 651 | * @throws ExpectationFailedException |
||
| 652 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 653 | */ |
||
| 654 | public static function assertNotEqualsWithDelta($expected, $actual, float $delta, string $message = ''): void |
||
| 655 | { |
||
| 656 | $constraint = new LogicalNot( |
||
| 657 | new IsEqual( |
||
| 658 | $expected, |
||
| 659 | $delta |
||
| 660 | ) |
||
| 661 | ); |
||
| 662 | |||
| 663 | static::assertThat($actual, $constraint, $message); |
||
| 664 | } |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Asserts that a variable is not equal to an attribute of an object. |
||
| 668 | * |
||
| 669 | * @param object|string $actualClassOrObject |
||
| 670 | * |
||
| 671 | * @throws ExpectationFailedException |
||
| 672 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 673 | * |
||
| 674 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 675 | */ |
||
| 676 | public static function assertAttributeNotEquals($expected, string $actualAttributeName, $actualClassOrObject, string $message = '', float $delta = 0.0, int $maxDepth = 10, bool $canonicalize = false, bool $ignoreCase = false): void |
||
| 677 | { |
||
| 678 | static::assertNotEquals( |
||
| 679 | $expected, |
||
| 680 | static::readAttribute($actualClassOrObject, $actualAttributeName), |
||
| 681 | $message, |
||
| 682 | $delta, |
||
| 683 | $maxDepth, |
||
| 684 | $canonicalize, |
||
| 685 | $ignoreCase |
||
| 686 | ); |
||
| 687 | } |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Asserts that a variable is empty. |
||
| 691 | * |
||
| 692 | * @throws ExpectationFailedException |
||
| 693 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 694 | */ |
||
| 695 | public static function assertEmpty($actual, string $message = ''): void |
||
| 696 | { |
||
| 697 | static::assertThat($actual, static::isEmpty(), $message); |
||
| 698 | } |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Asserts that a static attribute of a class or an attribute of an object |
||
| 702 | * is empty. |
||
| 703 | * |
||
| 704 | * @param object|string $haystackClassOrObject |
||
| 705 | * |
||
| 706 | * @throws ExpectationFailedException |
||
| 707 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 708 | * |
||
| 709 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 710 | */ |
||
| 711 | public static function assertAttributeEmpty(string $haystackAttributeName, $haystackClassOrObject, string $message = ''): void |
||
| 712 | { |
||
| 713 | static::assertEmpty( |
||
| 714 | static::readAttribute($haystackClassOrObject, $haystackAttributeName), |
||
| 715 | $message |
||
| 716 | ); |
||
| 717 | } |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Asserts that a variable is not empty. |
||
| 721 | * |
||
| 722 | * @throws ExpectationFailedException |
||
| 723 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 724 | */ |
||
| 725 | public static function assertNotEmpty($actual, string $message = ''): void |
||
| 726 | { |
||
| 727 | static::assertThat($actual, static::logicalNot(static::isEmpty()), $message); |
||
| 728 | } |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Asserts that a static attribute of a class or an attribute of an object |
||
| 732 | * is not empty. |
||
| 733 | * |
||
| 734 | * @param object|string $haystackClassOrObject |
||
| 735 | * |
||
| 736 | * @throws ExpectationFailedException |
||
| 737 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 738 | * |
||
| 739 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 740 | */ |
||
| 741 | public static function assertAttributeNotEmpty(string $haystackAttributeName, $haystackClassOrObject, string $message = ''): void |
||
| 742 | { |
||
| 743 | static::assertNotEmpty( |
||
| 744 | static::readAttribute($haystackClassOrObject, $haystackAttributeName), |
||
| 745 | $message |
||
| 746 | ); |
||
| 747 | } |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Asserts that a value is greater than another value. |
||
| 751 | * |
||
| 752 | * @throws ExpectationFailedException |
||
| 753 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 754 | */ |
||
| 755 | public static function assertGreaterThan($expected, $actual, string $message = ''): void |
||
| 756 | { |
||
| 757 | static::assertThat($actual, static::greaterThan($expected), $message); |
||
| 758 | } |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Asserts that an attribute is greater than another value. |
||
| 762 | * |
||
| 763 | * @param object|string $actualClassOrObject |
||
| 764 | * |
||
| 765 | * @throws ExpectationFailedException |
||
| 766 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 767 | * |
||
| 768 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 769 | */ |
||
| 770 | public static function assertAttributeGreaterThan($expected, string $actualAttributeName, $actualClassOrObject, string $message = ''): void |
||
| 771 | { |
||
| 772 | static::assertGreaterThan( |
||
| 773 | $expected, |
||
| 774 | static::readAttribute($actualClassOrObject, $actualAttributeName), |
||
| 775 | $message |
||
| 776 | ); |
||
| 777 | } |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Asserts that a value is greater than or equal to another value. |
||
| 781 | * |
||
| 782 | * @throws ExpectationFailedException |
||
| 783 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 784 | */ |
||
| 785 | public static function assertGreaterThanOrEqual($expected, $actual, string $message = ''): void |
||
| 786 | { |
||
| 787 | static::assertThat( |
||
| 788 | $actual, |
||
| 789 | static::greaterThanOrEqual($expected), |
||
| 790 | $message |
||
| 791 | ); |
||
| 792 | } |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Asserts that an attribute is greater than or equal to another value. |
||
| 796 | * |
||
| 797 | * @param object|string $actualClassOrObject |
||
| 798 | * |
||
| 799 | * @throws ExpectationFailedException |
||
| 800 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 801 | * |
||
| 802 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 803 | */ |
||
| 804 | public static function assertAttributeGreaterThanOrEqual($expected, string $actualAttributeName, $actualClassOrObject, string $message = ''): void |
||
| 805 | { |
||
| 806 | static::assertGreaterThanOrEqual( |
||
| 807 | $expected, |
||
| 808 | static::readAttribute($actualClassOrObject, $actualAttributeName), |
||
| 809 | $message |
||
| 810 | ); |
||
| 811 | } |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Asserts that a value is smaller than another value. |
||
| 815 | * |
||
| 816 | * @throws ExpectationFailedException |
||
| 817 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 818 | */ |
||
| 819 | public static function assertLessThan($expected, $actual, string $message = ''): void |
||
| 820 | { |
||
| 821 | static::assertThat($actual, static::lessThan($expected), $message); |
||
| 822 | } |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Asserts that an attribute is smaller than another value. |
||
| 826 | * |
||
| 827 | * @param object|string $actualClassOrObject |
||
| 828 | * |
||
| 829 | * @throws ExpectationFailedException |
||
| 830 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 831 | * |
||
| 832 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 833 | */ |
||
| 834 | public static function assertAttributeLessThan($expected, string $actualAttributeName, $actualClassOrObject, string $message = ''): void |
||
| 835 | { |
||
| 836 | static::assertLessThan( |
||
| 837 | $expected, |
||
| 838 | static::readAttribute($actualClassOrObject, $actualAttributeName), |
||
| 839 | $message |
||
| 840 | ); |
||
| 841 | } |
||
| 842 | |||
| 843 | /** |
||
| 844 | * Asserts that a value is smaller than or equal to another value. |
||
| 845 | * |
||
| 846 | * @throws ExpectationFailedException |
||
| 847 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 848 | */ |
||
| 849 | public static function assertLessThanOrEqual($expected, $actual, string $message = ''): void |
||
| 850 | { |
||
| 851 | static::assertThat($actual, static::lessThanOrEqual($expected), $message); |
||
| 852 | } |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Asserts that an attribute is smaller than or equal to another value. |
||
| 856 | * |
||
| 857 | * @param object|string $actualClassOrObject |
||
| 858 | * |
||
| 859 | * @throws ExpectationFailedException |
||
| 860 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 861 | * |
||
| 862 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 863 | */ |
||
| 864 | public static function assertAttributeLessThanOrEqual($expected, string $actualAttributeName, $actualClassOrObject, string $message = ''): void |
||
| 865 | { |
||
| 866 | static::assertLessThanOrEqual( |
||
| 867 | $expected, |
||
| 868 | static::readAttribute($actualClassOrObject, $actualAttributeName), |
||
| 869 | $message |
||
| 870 | ); |
||
| 871 | } |
||
| 872 | |||
| 873 | /** |
||
| 874 | * Asserts that the contents of one file is equal to the contents of another |
||
| 875 | * file. |
||
| 876 | * |
||
| 877 | * @throws ExpectationFailedException |
||
| 878 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 879 | */ |
||
| 880 | public static function assertFileEquals(string $expected, string $actual, string $message = '', bool $canonicalize = false, bool $ignoreCase = false): void |
||
| 881 | { |
||
| 882 | static::assertFileExists($expected, $message); |
||
| 883 | static::assertFileExists($actual, $message); |
||
| 884 | |||
| 885 | static::assertEquals( |
||
| 886 | \file_get_contents($expected), |
||
| 887 | \file_get_contents($actual), |
||
| 888 | $message, |
||
| 889 | 0, |
||
| 890 | 10, |
||
| 891 | $canonicalize, |
||
| 892 | $ignoreCase |
||
| 893 | ); |
||
| 894 | } |
||
| 895 | |||
| 896 | /** |
||
| 897 | * Asserts that the contents of one file is not equal to the contents of |
||
| 898 | * another file. |
||
| 899 | * |
||
| 900 | * @throws ExpectationFailedException |
||
| 901 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 902 | */ |
||
| 903 | public static function assertFileNotEquals(string $expected, string $actual, string $message = '', bool $canonicalize = false, bool $ignoreCase = false): void |
||
| 904 | { |
||
| 905 | static::assertFileExists($expected, $message); |
||
| 906 | static::assertFileExists($actual, $message); |
||
| 907 | |||
| 908 | static::assertNotEquals( |
||
| 909 | \file_get_contents($expected), |
||
| 910 | \file_get_contents($actual), |
||
| 911 | $message, |
||
| 912 | 0, |
||
| 913 | 10, |
||
| 914 | $canonicalize, |
||
| 915 | $ignoreCase |
||
| 916 | ); |
||
| 917 | } |
||
| 918 | |||
| 919 | /** |
||
| 920 | * Asserts that the contents of a string is equal |
||
| 921 | * to the contents of a file. |
||
| 922 | * |
||
| 923 | * @throws ExpectationFailedException |
||
| 924 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 925 | */ |
||
| 926 | public static function assertStringEqualsFile(string $expectedFile, string $actualString, string $message = '', bool $canonicalize = false, bool $ignoreCase = false): void |
||
| 927 | { |
||
| 928 | static::assertFileExists($expectedFile, $message); |
||
| 929 | |||
| 930 | /** @noinspection PhpUnitTestsInspection */ |
||
| 931 | static::assertEquals( |
||
| 932 | \file_get_contents($expectedFile), |
||
| 933 | $actualString, |
||
| 934 | $message, |
||
| 935 | 0, |
||
| 936 | 10, |
||
| 937 | $canonicalize, |
||
| 938 | $ignoreCase |
||
| 939 | ); |
||
| 940 | } |
||
| 941 | |||
| 942 | /** |
||
| 943 | * Asserts that the contents of a string is not equal |
||
| 944 | * to the contents of a file. |
||
| 945 | * |
||
| 946 | * @throws ExpectationFailedException |
||
| 947 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 948 | */ |
||
| 949 | public static function assertStringNotEqualsFile(string $expectedFile, string $actualString, string $message = '', bool $canonicalize = false, bool $ignoreCase = false): void |
||
| 950 | { |
||
| 951 | static::assertFileExists($expectedFile, $message); |
||
| 952 | |||
| 953 | static::assertNotEquals( |
||
| 954 | \file_get_contents($expectedFile), |
||
| 955 | $actualString, |
||
| 956 | $message, |
||
| 957 | 0, |
||
| 958 | 10, |
||
| 959 | $canonicalize, |
||
| 960 | $ignoreCase |
||
| 961 | ); |
||
| 962 | } |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Asserts that a file/dir is readable. |
||
| 966 | * |
||
| 967 | * @throws ExpectationFailedException |
||
| 968 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 969 | */ |
||
| 970 | public static function assertIsReadable(string $filename, string $message = ''): void |
||
| 971 | { |
||
| 972 | static::assertThat($filename, new IsReadable, $message); |
||
| 973 | } |
||
| 974 | |||
| 975 | /** |
||
| 976 | * Asserts that a file/dir exists and is not readable. |
||
| 977 | * |
||
| 978 | * @throws ExpectationFailedException |
||
| 979 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 980 | */ |
||
| 981 | public static function assertNotIsReadable(string $filename, string $message = ''): void |
||
| 982 | { |
||
| 983 | static::assertThat($filename, new LogicalNot(new IsReadable), $message); |
||
| 984 | } |
||
| 985 | |||
| 986 | /** |
||
| 987 | * Asserts that a file/dir exists and is writable. |
||
| 988 | * |
||
| 989 | * @throws ExpectationFailedException |
||
| 990 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 991 | */ |
||
| 992 | public static function assertIsWritable(string $filename, string $message = ''): void |
||
| 993 | { |
||
| 994 | static::assertThat($filename, new IsWritable, $message); |
||
| 995 | } |
||
| 996 | |||
| 997 | /** |
||
| 998 | * Asserts that a file/dir exists and is not writable. |
||
| 999 | * |
||
| 1000 | * @throws ExpectationFailedException |
||
| 1001 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1002 | */ |
||
| 1003 | public static function assertNotIsWritable(string $filename, string $message = ''): void |
||
| 1004 | { |
||
| 1005 | static::assertThat($filename, new LogicalNot(new IsWritable), $message); |
||
| 1006 | } |
||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * Asserts that a directory exists. |
||
| 1010 | * |
||
| 1011 | * @throws ExpectationFailedException |
||
| 1012 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1013 | */ |
||
| 1014 | public static function assertDirectoryExists(string $directory, string $message = ''): void |
||
| 1015 | { |
||
| 1016 | static::assertThat($directory, new DirectoryExists, $message); |
||
| 1017 | } |
||
| 1018 | |||
| 1019 | /** |
||
| 1020 | * Asserts that a directory does not exist. |
||
| 1021 | * |
||
| 1022 | * @throws ExpectationFailedException |
||
| 1023 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1024 | */ |
||
| 1025 | public static function assertDirectoryNotExists(string $directory, string $message = ''): void |
||
| 1026 | { |
||
| 1027 | static::assertThat($directory, new LogicalNot(new DirectoryExists), $message); |
||
| 1028 | } |
||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Asserts that a directory exists and is readable. |
||
| 1032 | * |
||
| 1033 | * @throws ExpectationFailedException |
||
| 1034 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1035 | */ |
||
| 1036 | public static function assertDirectoryIsReadable(string $directory, string $message = ''): void |
||
| 1037 | { |
||
| 1038 | self::assertDirectoryExists($directory, $message); |
||
| 1039 | self::assertIsReadable($directory, $message); |
||
| 1040 | } |
||
| 1041 | |||
| 1042 | /** |
||
| 1043 | * Asserts that a directory exists and is not readable. |
||
| 1044 | * |
||
| 1045 | * @throws ExpectationFailedException |
||
| 1046 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1047 | */ |
||
| 1048 | public static function assertDirectoryNotIsReadable(string $directory, string $message = ''): void |
||
| 1049 | { |
||
| 1050 | self::assertDirectoryExists($directory, $message); |
||
| 1051 | self::assertNotIsReadable($directory, $message); |
||
| 1052 | } |
||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * Asserts that a directory exists and is writable. |
||
| 1056 | * |
||
| 1057 | * @throws ExpectationFailedException |
||
| 1058 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1059 | */ |
||
| 1060 | public static function assertDirectoryIsWritable(string $directory, string $message = ''): void |
||
| 1061 | { |
||
| 1062 | self::assertDirectoryExists($directory, $message); |
||
| 1063 | self::assertIsWritable($directory, $message); |
||
| 1064 | } |
||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * Asserts that a directory exists and is not writable. |
||
| 1068 | * |
||
| 1069 | * @throws ExpectationFailedException |
||
| 1070 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1071 | */ |
||
| 1072 | public static function assertDirectoryNotIsWritable(string $directory, string $message = ''): void |
||
| 1073 | { |
||
| 1074 | self::assertDirectoryExists($directory, $message); |
||
| 1075 | self::assertNotIsWritable($directory, $message); |
||
| 1076 | } |
||
| 1077 | |||
| 1078 | /** |
||
| 1079 | * Asserts that a file exists. |
||
| 1080 | * |
||
| 1081 | * @throws ExpectationFailedException |
||
| 1082 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1083 | */ |
||
| 1084 | public static function assertFileExists(string $filename, string $message = ''): void |
||
| 1085 | { |
||
| 1086 | static::assertThat($filename, new FileExists, $message); |
||
| 1087 | } |
||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * Asserts that a file does not exist. |
||
| 1091 | * |
||
| 1092 | * @throws ExpectationFailedException |
||
| 1093 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1094 | */ |
||
| 1095 | public static function assertFileNotExists(string $filename, string $message = ''): void |
||
| 1096 | { |
||
| 1097 | static::assertThat($filename, new LogicalNot(new FileExists), $message); |
||
| 1098 | } |
||
| 1099 | |||
| 1100 | /** |
||
| 1101 | * Asserts that a file exists and is readable. |
||
| 1102 | * |
||
| 1103 | * @throws ExpectationFailedException |
||
| 1104 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1105 | */ |
||
| 1106 | public static function assertFileIsReadable(string $file, string $message = ''): void |
||
| 1107 | { |
||
| 1108 | self::assertFileExists($file, $message); |
||
| 1109 | self::assertIsReadable($file, $message); |
||
| 1110 | } |
||
| 1111 | |||
| 1112 | /** |
||
| 1113 | * Asserts that a file exists and is not readable. |
||
| 1114 | * |
||
| 1115 | * @throws ExpectationFailedException |
||
| 1116 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1117 | */ |
||
| 1118 | public static function assertFileNotIsReadable(string $file, string $message = ''): void |
||
| 1119 | { |
||
| 1120 | self::assertFileExists($file, $message); |
||
| 1121 | self::assertNotIsReadable($file, $message); |
||
| 1122 | } |
||
| 1123 | |||
| 1124 | /** |
||
| 1125 | * Asserts that a file exists and is writable. |
||
| 1126 | * |
||
| 1127 | * @throws ExpectationFailedException |
||
| 1128 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1129 | */ |
||
| 1130 | public static function assertFileIsWritable(string $file, string $message = ''): void |
||
| 1131 | { |
||
| 1132 | self::assertFileExists($file, $message); |
||
| 1133 | self::assertIsWritable($file, $message); |
||
| 1134 | } |
||
| 1135 | |||
| 1136 | /** |
||
| 1137 | * Asserts that a file exists and is not writable. |
||
| 1138 | * |
||
| 1139 | * @throws ExpectationFailedException |
||
| 1140 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1141 | */ |
||
| 1142 | public static function assertFileNotIsWritable(string $file, string $message = ''): void |
||
| 1143 | { |
||
| 1144 | self::assertFileExists($file, $message); |
||
| 1145 | self::assertNotIsWritable($file, $message); |
||
| 1146 | } |
||
| 1147 | |||
| 1148 | /** |
||
| 1149 | * Asserts that a condition is true. |
||
| 1150 | * |
||
| 1151 | * @throws ExpectationFailedException |
||
| 1152 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1153 | */ |
||
| 1154 | public static function assertTrue($condition, string $message = ''): void |
||
| 1155 | { |
||
| 1156 | static::assertThat($condition, static::isTrue(), $message); |
||
| 1157 | } |
||
| 1158 | |||
| 1159 | /** |
||
| 1160 | * Asserts that a condition is not true. |
||
| 1161 | * |
||
| 1162 | * @throws ExpectationFailedException |
||
| 1163 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1164 | */ |
||
| 1165 | public static function assertNotTrue($condition, string $message = ''): void |
||
| 1166 | { |
||
| 1167 | static::assertThat($condition, static::logicalNot(static::isTrue()), $message); |
||
| 1168 | } |
||
| 1169 | |||
| 1170 | /** |
||
| 1171 | * Asserts that a condition is false. |
||
| 1172 | * |
||
| 1173 | * @throws ExpectationFailedException |
||
| 1174 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1175 | */ |
||
| 1176 | public static function assertFalse($condition, string $message = ''): void |
||
| 1177 | { |
||
| 1178 | static::assertThat($condition, static::isFalse(), $message); |
||
| 1179 | } |
||
| 1180 | |||
| 1181 | /** |
||
| 1182 | * Asserts that a condition is not false. |
||
| 1183 | * |
||
| 1184 | * @throws ExpectationFailedException |
||
| 1185 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1186 | */ |
||
| 1187 | public static function assertNotFalse($condition, string $message = ''): void |
||
| 1188 | { |
||
| 1189 | static::assertThat($condition, static::logicalNot(static::isFalse()), $message); |
||
| 1190 | } |
||
| 1191 | |||
| 1192 | /** |
||
| 1193 | * Asserts that a variable is null. |
||
| 1194 | * |
||
| 1195 | * @throws ExpectationFailedException |
||
| 1196 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1197 | */ |
||
| 1198 | public static function assertNull($actual, string $message = ''): void |
||
| 1199 | { |
||
| 1200 | static::assertThat($actual, static::isNull(), $message); |
||
| 1201 | } |
||
| 1202 | |||
| 1203 | /** |
||
| 1204 | * Asserts that a variable is not null. |
||
| 1205 | * |
||
| 1206 | * @throws ExpectationFailedException |
||
| 1207 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1208 | */ |
||
| 1209 | public static function assertNotNull($actual, string $message = ''): void |
||
| 1210 | { |
||
| 1211 | static::assertThat($actual, static::logicalNot(static::isNull()), $message); |
||
| 1212 | } |
||
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Asserts that a variable is finite. |
||
| 1216 | * |
||
| 1217 | * @throws ExpectationFailedException |
||
| 1218 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1219 | */ |
||
| 1220 | public static function assertFinite($actual, string $message = ''): void |
||
| 1221 | { |
||
| 1222 | static::assertThat($actual, static::isFinite(), $message); |
||
| 1223 | } |
||
| 1224 | |||
| 1225 | /** |
||
| 1226 | * Asserts that a variable is infinite. |
||
| 1227 | * |
||
| 1228 | * @throws ExpectationFailedException |
||
| 1229 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1230 | */ |
||
| 1231 | public static function assertInfinite($actual, string $message = ''): void |
||
| 1232 | { |
||
| 1233 | static::assertThat($actual, static::isInfinite(), $message); |
||
| 1234 | } |
||
| 1235 | |||
| 1236 | /** |
||
| 1237 | * Asserts that a variable is nan. |
||
| 1238 | * |
||
| 1239 | * @throws ExpectationFailedException |
||
| 1240 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1241 | */ |
||
| 1242 | public static function assertNan($actual, string $message = ''): void |
||
| 1243 | { |
||
| 1244 | static::assertThat($actual, static::isNan(), $message); |
||
| 1245 | } |
||
| 1246 | |||
| 1247 | /** |
||
| 1248 | * Asserts that a class has a specified attribute. |
||
| 1249 | * |
||
| 1250 | * @throws ExpectationFailedException |
||
| 1251 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1252 | */ |
||
| 1253 | public static function assertClassHasAttribute(string $attributeName, string $className, string $message = ''): void |
||
| 1254 | { |
||
| 1255 | if (!self::isValidClassAttributeName($attributeName)) { |
||
| 1256 | throw InvalidArgumentHelper::factory(1, 'valid attribute name'); |
||
| 1257 | } |
||
| 1258 | |||
| 1259 | if (!\class_exists($className)) { |
||
| 1260 | throw InvalidArgumentHelper::factory(2, 'class name', $className); |
||
| 1261 | } |
||
| 1262 | |||
| 1263 | static::assertThat($className, new ClassHasAttribute($attributeName), $message); |
||
| 1264 | } |
||
| 1265 | |||
| 1266 | /** |
||
| 1267 | * Asserts that a class does not have a specified attribute. |
||
| 1268 | * |
||
| 1269 | * @throws ExpectationFailedException |
||
| 1270 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1271 | */ |
||
| 1272 | public static function assertClassNotHasAttribute(string $attributeName, string $className, string $message = ''): void |
||
| 1273 | { |
||
| 1274 | if (!self::isValidClassAttributeName($attributeName)) { |
||
| 1275 | throw InvalidArgumentHelper::factory(1, 'valid attribute name'); |
||
| 1276 | } |
||
| 1277 | |||
| 1278 | if (!\class_exists($className)) { |
||
| 1279 | throw InvalidArgumentHelper::factory(2, 'class name', $className); |
||
| 1280 | } |
||
| 1281 | |||
| 1282 | static::assertThat( |
||
| 1283 | $className, |
||
| 1284 | new LogicalNot( |
||
| 1285 | new ClassHasAttribute($attributeName) |
||
| 1286 | ), |
||
| 1287 | $message |
||
| 1288 | ); |
||
| 1289 | } |
||
| 1290 | |||
| 1291 | /** |
||
| 1292 | * Asserts that a class has a specified static attribute. |
||
| 1293 | * |
||
| 1294 | * @throws ExpectationFailedException |
||
| 1295 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1296 | */ |
||
| 1297 | public static function assertClassHasStaticAttribute(string $attributeName, string $className, string $message = ''): void |
||
| 1298 | { |
||
| 1299 | if (!self::isValidClassAttributeName($attributeName)) { |
||
| 1300 | throw InvalidArgumentHelper::factory(1, 'valid attribute name'); |
||
| 1301 | } |
||
| 1302 | |||
| 1303 | if (!\class_exists($className)) { |
||
| 1304 | throw InvalidArgumentHelper::factory(2, 'class name', $className); |
||
| 1305 | } |
||
| 1306 | |||
| 1307 | static::assertThat( |
||
| 1308 | $className, |
||
| 1309 | new ClassHasStaticAttribute($attributeName), |
||
| 1310 | $message |
||
| 1311 | ); |
||
| 1312 | } |
||
| 1313 | |||
| 1314 | /** |
||
| 1315 | * Asserts that a class does not have a specified static attribute. |
||
| 1316 | * |
||
| 1317 | * @throws ExpectationFailedException |
||
| 1318 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1319 | */ |
||
| 1320 | public static function assertClassNotHasStaticAttribute(string $attributeName, string $className, string $message = ''): void |
||
| 1321 | { |
||
| 1322 | if (!self::isValidClassAttributeName($attributeName)) { |
||
| 1323 | throw InvalidArgumentHelper::factory(1, 'valid attribute name'); |
||
| 1324 | } |
||
| 1325 | |||
| 1326 | if (!\class_exists($className)) { |
||
| 1327 | throw InvalidArgumentHelper::factory(2, 'class name', $className); |
||
| 1328 | } |
||
| 1329 | |||
| 1330 | static::assertThat( |
||
| 1331 | $className, |
||
| 1332 | new LogicalNot( |
||
| 1333 | new ClassHasStaticAttribute($attributeName) |
||
| 1334 | ), |
||
| 1335 | $message |
||
| 1336 | ); |
||
| 1337 | } |
||
| 1338 | |||
| 1339 | /** |
||
| 1340 | * Asserts that an object has a specified attribute. |
||
| 1341 | * |
||
| 1342 | * @param object $object |
||
| 1343 | * |
||
| 1344 | * @throws ExpectationFailedException |
||
| 1345 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1346 | */ |
||
| 1347 | public static function assertObjectHasAttribute(string $attributeName, $object, string $message = ''): void |
||
| 1348 | { |
||
| 1349 | if (!self::isValidObjectAttributeName($attributeName)) { |
||
| 1350 | throw InvalidArgumentHelper::factory(1, 'valid attribute name'); |
||
| 1351 | } |
||
| 1352 | |||
| 1353 | if (!\is_object($object)) { |
||
| 1354 | throw InvalidArgumentHelper::factory(2, 'object'); |
||
| 1355 | } |
||
| 1356 | |||
| 1357 | static::assertThat( |
||
| 1358 | $object, |
||
| 1359 | new ObjectHasAttribute($attributeName), |
||
| 1360 | $message |
||
| 1361 | ); |
||
| 1362 | } |
||
| 1363 | |||
| 1364 | /** |
||
| 1365 | * Asserts that an object does not have a specified attribute. |
||
| 1366 | * |
||
| 1367 | * @param object $object |
||
| 1368 | * |
||
| 1369 | * @throws ExpectationFailedException |
||
| 1370 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1371 | */ |
||
| 1372 | public static function assertObjectNotHasAttribute(string $attributeName, $object, string $message = ''): void |
||
| 1373 | { |
||
| 1374 | if (!self::isValidObjectAttributeName($attributeName)) { |
||
| 1375 | throw InvalidArgumentHelper::factory(1, 'valid attribute name'); |
||
| 1376 | } |
||
| 1377 | |||
| 1378 | if (!\is_object($object)) { |
||
| 1379 | throw InvalidArgumentHelper::factory(2, 'object'); |
||
| 1380 | } |
||
| 1381 | |||
| 1382 | static::assertThat( |
||
| 1383 | $object, |
||
| 1384 | new LogicalNot( |
||
| 1385 | new ObjectHasAttribute($attributeName) |
||
| 1386 | ), |
||
| 1387 | $message |
||
| 1388 | ); |
||
| 1389 | } |
||
| 1390 | |||
| 1391 | /** |
||
| 1392 | * Asserts that two variables have the same type and value. |
||
| 1393 | * Used on objects, it asserts that two variables reference |
||
| 1394 | * the same object. |
||
| 1395 | * |
||
| 1396 | * @throws ExpectationFailedException |
||
| 1397 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1398 | */ |
||
| 1399 | public static function assertSame($expected, $actual, string $message = ''): void |
||
| 1400 | { |
||
| 1401 | static::assertThat( |
||
| 1402 | $actual, |
||
| 1403 | new IsIdentical($expected), |
||
| 1404 | $message |
||
| 1405 | ); |
||
| 1406 | } |
||
| 1407 | |||
| 1408 | /** |
||
| 1409 | * Asserts that a variable and an attribute of an object have the same type |
||
| 1410 | * and value. |
||
| 1411 | * |
||
| 1412 | * @param object|string $actualClassOrObject |
||
| 1413 | * |
||
| 1414 | * @throws ExpectationFailedException |
||
| 1415 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1416 | * |
||
| 1417 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 1418 | */ |
||
| 1419 | public static function assertAttributeSame($expected, string $actualAttributeName, $actualClassOrObject, string $message = ''): void |
||
| 1420 | { |
||
| 1421 | static::assertSame( |
||
| 1422 | $expected, |
||
| 1423 | static::readAttribute($actualClassOrObject, $actualAttributeName), |
||
| 1424 | $message |
||
| 1425 | ); |
||
| 1426 | } |
||
| 1427 | |||
| 1428 | /** |
||
| 1429 | * Asserts that two variables do not have the same type and value. |
||
| 1430 | * Used on objects, it asserts that two variables do not reference |
||
| 1431 | * the same object. |
||
| 1432 | * |
||
| 1433 | * @throws ExpectationFailedException |
||
| 1434 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1435 | */ |
||
| 1436 | public static function assertNotSame($expected, $actual, string $message = ''): void |
||
| 1437 | { |
||
| 1438 | if (\is_bool($expected) && \is_bool($actual)) { |
||
| 1439 | static::assertNotEquals($expected, $actual, $message); |
||
| 1440 | } |
||
| 1441 | |||
| 1442 | static::assertThat( |
||
| 1443 | $actual, |
||
| 1444 | new LogicalNot( |
||
| 1445 | new IsIdentical($expected) |
||
| 1446 | ), |
||
| 1447 | $message |
||
| 1448 | ); |
||
| 1449 | } |
||
| 1450 | |||
| 1451 | /** |
||
| 1452 | * Asserts that a variable and an attribute of an object do not have the |
||
| 1453 | * same type and value. |
||
| 1454 | * |
||
| 1455 | * @param object|string $actualClassOrObject |
||
| 1456 | * |
||
| 1457 | * @throws ExpectationFailedException |
||
| 1458 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1459 | * |
||
| 1460 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 1461 | */ |
||
| 1462 | public static function assertAttributeNotSame($expected, string $actualAttributeName, $actualClassOrObject, string $message = ''): void |
||
| 1463 | { |
||
| 1464 | static::assertNotSame( |
||
| 1465 | $expected, |
||
| 1466 | static::readAttribute($actualClassOrObject, $actualAttributeName), |
||
| 1467 | $message |
||
| 1468 | ); |
||
| 1469 | } |
||
| 1470 | |||
| 1471 | /** |
||
| 1472 | * Asserts that a variable is of a given type. |
||
| 1473 | * |
||
| 1474 | * @throws ExpectationFailedException |
||
| 1475 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1476 | */ |
||
| 1477 | public static function assertInstanceOf(string $expected, $actual, string $message = ''): void |
||
| 1478 | { |
||
| 1479 | if (!\class_exists($expected) && !\interface_exists($expected)) { |
||
| 1480 | throw InvalidArgumentHelper::factory(1, 'class or interface name'); |
||
| 1481 | } |
||
| 1482 | |||
| 1483 | static::assertThat( |
||
| 1484 | $actual, |
||
| 1485 | new IsInstanceOf($expected), |
||
| 1486 | $message |
||
| 1487 | ); |
||
| 1488 | } |
||
| 1489 | |||
| 1490 | /** |
||
| 1491 | * Asserts that an attribute is of a given type. |
||
| 1492 | * |
||
| 1493 | * @param object|string $classOrObject |
||
| 1494 | * |
||
| 1495 | * @throws ExpectationFailedException |
||
| 1496 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1497 | * |
||
| 1498 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 1499 | */ |
||
| 1500 | public static function assertAttributeInstanceOf(string $expected, string $attributeName, $classOrObject, string $message = ''): void |
||
| 1501 | { |
||
| 1502 | static::assertInstanceOf( |
||
| 1503 | $expected, |
||
| 1504 | static::readAttribute($classOrObject, $attributeName), |
||
| 1505 | $message |
||
| 1506 | ); |
||
| 1507 | } |
||
| 1508 | |||
| 1509 | /** |
||
| 1510 | * Asserts that a variable is not of a given type. |
||
| 1511 | * |
||
| 1512 | * @throws ExpectationFailedException |
||
| 1513 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1514 | */ |
||
| 1515 | public static function assertNotInstanceOf(string $expected, $actual, string $message = ''): void |
||
| 1516 | { |
||
| 1517 | if (!\class_exists($expected) && !\interface_exists($expected)) { |
||
| 1518 | throw InvalidArgumentHelper::factory(1, 'class or interface name'); |
||
| 1519 | } |
||
| 1520 | |||
| 1521 | static::assertThat( |
||
| 1522 | $actual, |
||
| 1523 | new LogicalNot( |
||
| 1524 | new IsInstanceOf($expected) |
||
| 1525 | ), |
||
| 1526 | $message |
||
| 1527 | ); |
||
| 1528 | } |
||
| 1529 | |||
| 1530 | /** |
||
| 1531 | * Asserts that an attribute is of a given type. |
||
| 1532 | * |
||
| 1533 | * @param object|string $classOrObject |
||
| 1534 | * |
||
| 1535 | * @throws ExpectationFailedException |
||
| 1536 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1537 | * |
||
| 1538 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 1539 | */ |
||
| 1540 | public static function assertAttributeNotInstanceOf(string $expected, string $attributeName, $classOrObject, string $message = ''): void |
||
| 1541 | { |
||
| 1542 | static::assertNotInstanceOf( |
||
| 1543 | $expected, |
||
| 1544 | static::readAttribute($classOrObject, $attributeName), |
||
| 1545 | $message |
||
| 1546 | ); |
||
| 1547 | } |
||
| 1548 | |||
| 1549 | /** |
||
| 1550 | * Asserts that a variable is of a given type. |
||
| 1551 | * |
||
| 1552 | * @throws ExpectationFailedException |
||
| 1553 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1554 | * |
||
| 1555 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3369 |
||
| 1556 | */ |
||
| 1557 | public static function assertInternalType(string $expected, $actual, string $message = ''): void |
||
| 1558 | { |
||
| 1559 | static::assertThat( |
||
| 1560 | $actual, |
||
| 1561 | new IsType($expected), |
||
| 1562 | $message |
||
| 1563 | ); |
||
| 1564 | } |
||
| 1565 | |||
| 1566 | /** |
||
| 1567 | * Asserts that an attribute is of a given type. |
||
| 1568 | * |
||
| 1569 | * @param object|string $classOrObject |
||
| 1570 | * |
||
| 1571 | * @throws ExpectationFailedException |
||
| 1572 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1573 | * |
||
| 1574 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 1575 | */ |
||
| 1576 | public static function assertAttributeInternalType(string $expected, string $attributeName, $classOrObject, string $message = ''): void |
||
| 1577 | { |
||
| 1578 | static::assertInternalType( |
||
| 1579 | $expected, |
||
| 1580 | static::readAttribute($classOrObject, $attributeName), |
||
| 1581 | $message |
||
| 1582 | ); |
||
| 1583 | } |
||
| 1584 | |||
| 1585 | /** |
||
| 1586 | * Asserts that a variable is of type array. |
||
| 1587 | */ |
||
| 1588 | public static function assertIsArray($actual, string $message = ''): void |
||
| 1589 | { |
||
| 1590 | static::assertThat( |
||
| 1591 | $actual, |
||
| 1592 | new IsType(IsType::TYPE_ARRAY), |
||
| 1593 | $message |
||
| 1594 | ); |
||
| 1595 | } |
||
| 1596 | |||
| 1597 | /** |
||
| 1598 | * Asserts that a variable is of type bool. |
||
| 1599 | */ |
||
| 1600 | public static function assertIsBool($actual, string $message = ''): void |
||
| 1601 | { |
||
| 1602 | static::assertThat( |
||
| 1603 | $actual, |
||
| 1604 | new IsType(IsType::TYPE_BOOL), |
||
| 1605 | $message |
||
| 1606 | ); |
||
| 1607 | } |
||
| 1608 | |||
| 1609 | /** |
||
| 1610 | * Asserts that a variable is of type float. |
||
| 1611 | */ |
||
| 1612 | public static function assertIsFloat($actual, string $message = ''): void |
||
| 1613 | { |
||
| 1614 | static::assertThat( |
||
| 1615 | $actual, |
||
| 1616 | new IsType(IsType::TYPE_FLOAT), |
||
| 1617 | $message |
||
| 1618 | ); |
||
| 1619 | } |
||
| 1620 | |||
| 1621 | /** |
||
| 1622 | * Asserts that a variable is of type int. |
||
| 1623 | */ |
||
| 1624 | public static function assertIsInt($actual, string $message = ''): void |
||
| 1625 | { |
||
| 1626 | static::assertThat( |
||
| 1627 | $actual, |
||
| 1628 | new IsType(IsType::TYPE_INT), |
||
| 1629 | $message |
||
| 1630 | ); |
||
| 1631 | } |
||
| 1632 | |||
| 1633 | /** |
||
| 1634 | * Asserts that a variable is of type numeric. |
||
| 1635 | */ |
||
| 1636 | public static function assertIsNumeric($actual, string $message = ''): void |
||
| 1637 | { |
||
| 1638 | static::assertThat( |
||
| 1639 | $actual, |
||
| 1640 | new IsType(IsType::TYPE_NUMERIC), |
||
| 1641 | $message |
||
| 1642 | ); |
||
| 1643 | } |
||
| 1644 | |||
| 1645 | /** |
||
| 1646 | * Asserts that a variable is of type object. |
||
| 1647 | */ |
||
| 1648 | public static function assertIsObject($actual, string $message = ''): void |
||
| 1649 | { |
||
| 1650 | static::assertThat( |
||
| 1651 | $actual, |
||
| 1652 | new IsType(IsType::TYPE_OBJECT), |
||
| 1653 | $message |
||
| 1654 | ); |
||
| 1655 | } |
||
| 1656 | |||
| 1657 | /** |
||
| 1658 | * Asserts that a variable is of type resource. |
||
| 1659 | */ |
||
| 1660 | public static function assertIsResource($actual, string $message = ''): void |
||
| 1661 | { |
||
| 1662 | static::assertThat( |
||
| 1663 | $actual, |
||
| 1664 | new IsType(IsType::TYPE_RESOURCE), |
||
| 1665 | $message |
||
| 1666 | ); |
||
| 1667 | } |
||
| 1668 | |||
| 1669 | /** |
||
| 1670 | * Asserts that a variable is of type string. |
||
| 1671 | */ |
||
| 1672 | public static function assertIsString($actual, string $message = ''): void |
||
| 1673 | { |
||
| 1674 | static::assertThat( |
||
| 1675 | $actual, |
||
| 1676 | new IsType(IsType::TYPE_STRING), |
||
| 1677 | $message |
||
| 1678 | ); |
||
| 1679 | } |
||
| 1680 | |||
| 1681 | /** |
||
| 1682 | * Asserts that a variable is of type scalar. |
||
| 1683 | */ |
||
| 1684 | public static function assertIsScalar($actual, string $message = ''): void |
||
| 1685 | { |
||
| 1686 | static::assertThat( |
||
| 1687 | $actual, |
||
| 1688 | new IsType(IsType::TYPE_SCALAR), |
||
| 1689 | $message |
||
| 1690 | ); |
||
| 1691 | } |
||
| 1692 | |||
| 1693 | /** |
||
| 1694 | * Asserts that a variable is of type callable. |
||
| 1695 | */ |
||
| 1696 | public static function assertIsCallable($actual, string $message = ''): void |
||
| 1697 | { |
||
| 1698 | static::assertThat( |
||
| 1699 | $actual, |
||
| 1700 | new IsType(IsType::TYPE_CALLABLE), |
||
| 1701 | $message |
||
| 1702 | ); |
||
| 1703 | } |
||
| 1704 | |||
| 1705 | /** |
||
| 1706 | * Asserts that a variable is of type iterable. |
||
| 1707 | */ |
||
| 1708 | public static function assertIsIterable($actual, string $message = ''): void |
||
| 1709 | { |
||
| 1710 | static::assertThat( |
||
| 1711 | $actual, |
||
| 1712 | new IsType(IsType::TYPE_ITERABLE), |
||
| 1713 | $message |
||
| 1714 | ); |
||
| 1715 | } |
||
| 1716 | |||
| 1717 | /** |
||
| 1718 | * Asserts that a variable is not of a given type. |
||
| 1719 | * |
||
| 1720 | * @throws ExpectationFailedException |
||
| 1721 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1722 | * |
||
| 1723 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3369 |
||
| 1724 | */ |
||
| 1725 | public static function assertNotInternalType(string $expected, $actual, string $message = ''): void |
||
| 1726 | { |
||
| 1727 | static::assertThat( |
||
| 1728 | $actual, |
||
| 1729 | new LogicalNot( |
||
| 1730 | new IsType($expected) |
||
| 1731 | ), |
||
| 1732 | $message |
||
| 1733 | ); |
||
| 1734 | } |
||
| 1735 | |||
| 1736 | /** |
||
| 1737 | * Asserts that a variable is not of type array. |
||
| 1738 | */ |
||
| 1739 | public static function assertIsNotArray($actual, string $message = ''): void |
||
| 1740 | { |
||
| 1741 | static::assertThat( |
||
| 1742 | $actual, |
||
| 1743 | new LogicalNot(new IsType(IsType::TYPE_ARRAY)), |
||
| 1744 | $message |
||
| 1745 | ); |
||
| 1746 | } |
||
| 1747 | |||
| 1748 | /** |
||
| 1749 | * Asserts that a variable is not of type bool. |
||
| 1750 | */ |
||
| 1751 | public static function assertIsNotBool($actual, string $message = ''): void |
||
| 1752 | { |
||
| 1753 | static::assertThat( |
||
| 1754 | $actual, |
||
| 1755 | new LogicalNot(new IsType(IsType::TYPE_BOOL)), |
||
| 1756 | $message |
||
| 1757 | ); |
||
| 1758 | } |
||
| 1759 | |||
| 1760 | /** |
||
| 1761 | * Asserts that a variable is not of type float. |
||
| 1762 | */ |
||
| 1763 | public static function assertIsNotFloat($actual, string $message = ''): void |
||
| 1764 | { |
||
| 1765 | static::assertThat( |
||
| 1766 | $actual, |
||
| 1767 | new LogicalNot(new IsType(IsType::TYPE_FLOAT)), |
||
| 1768 | $message |
||
| 1769 | ); |
||
| 1770 | } |
||
| 1771 | |||
| 1772 | /** |
||
| 1773 | * Asserts that a variable is not of type int. |
||
| 1774 | */ |
||
| 1775 | public static function assertIsNotInt($actual, string $message = ''): void |
||
| 1776 | { |
||
| 1777 | static::assertThat( |
||
| 1778 | $actual, |
||
| 1779 | new LogicalNot(new IsType(IsType::TYPE_INT)), |
||
| 1780 | $message |
||
| 1781 | ); |
||
| 1782 | } |
||
| 1783 | |||
| 1784 | /** |
||
| 1785 | * Asserts that a variable is not of type numeric. |
||
| 1786 | */ |
||
| 1787 | public static function assertIsNotNumeric($actual, string $message = ''): void |
||
| 1788 | { |
||
| 1789 | static::assertThat( |
||
| 1790 | $actual, |
||
| 1791 | new LogicalNot(new IsType(IsType::TYPE_NUMERIC)), |
||
| 1792 | $message |
||
| 1793 | ); |
||
| 1794 | } |
||
| 1795 | |||
| 1796 | /** |
||
| 1797 | * Asserts that a variable is not of type object. |
||
| 1798 | */ |
||
| 1799 | public static function assertIsNotObject($actual, string $message = ''): void |
||
| 1800 | { |
||
| 1801 | static::assertThat( |
||
| 1802 | $actual, |
||
| 1803 | new LogicalNot(new IsType(IsType::TYPE_OBJECT)), |
||
| 1804 | $message |
||
| 1805 | ); |
||
| 1806 | } |
||
| 1807 | |||
| 1808 | /** |
||
| 1809 | * Asserts that a variable is not of type resource. |
||
| 1810 | */ |
||
| 1811 | public static function assertIsNotResource($actual, string $message = ''): void |
||
| 1812 | { |
||
| 1813 | static::assertThat( |
||
| 1814 | $actual, |
||
| 1815 | new LogicalNot(new IsType(IsType::TYPE_RESOURCE)), |
||
| 1816 | $message |
||
| 1817 | ); |
||
| 1818 | } |
||
| 1819 | |||
| 1820 | /** |
||
| 1821 | * Asserts that a variable is not of type string. |
||
| 1822 | */ |
||
| 1823 | public static function assertIsNotString($actual, string $message = ''): void |
||
| 1824 | { |
||
| 1825 | static::assertThat( |
||
| 1826 | $actual, |
||
| 1827 | new LogicalNot(new IsType(IsType::TYPE_STRING)), |
||
| 1828 | $message |
||
| 1829 | ); |
||
| 1830 | } |
||
| 1831 | |||
| 1832 | /** |
||
| 1833 | * Asserts that a variable is not of type scalar. |
||
| 1834 | */ |
||
| 1835 | public static function assertIsNotScalar($actual, string $message = ''): void |
||
| 1836 | { |
||
| 1837 | static::assertThat( |
||
| 1838 | $actual, |
||
| 1839 | new LogicalNot(new IsType(IsType::TYPE_SCALAR)), |
||
| 1840 | $message |
||
| 1841 | ); |
||
| 1842 | } |
||
| 1843 | |||
| 1844 | /** |
||
| 1845 | * Asserts that a variable is not of type callable. |
||
| 1846 | */ |
||
| 1847 | public static function assertIsNotCallable($actual, string $message = ''): void |
||
| 1848 | { |
||
| 1849 | static::assertThat( |
||
| 1850 | $actual, |
||
| 1851 | new LogicalNot(new IsType(IsType::TYPE_CALLABLE)), |
||
| 1852 | $message |
||
| 1853 | ); |
||
| 1854 | } |
||
| 1855 | |||
| 1856 | /** |
||
| 1857 | * Asserts that a variable is not of type iterable. |
||
| 1858 | */ |
||
| 1859 | public static function assertIsNotIterable($actual, string $message = ''): void |
||
| 1860 | { |
||
| 1861 | static::assertThat( |
||
| 1862 | $actual, |
||
| 1863 | new LogicalNot(new IsType(IsType::TYPE_ITERABLE)), |
||
| 1864 | $message |
||
| 1865 | ); |
||
| 1866 | } |
||
| 1867 | |||
| 1868 | /** |
||
| 1869 | * Asserts that an attribute is of a given type. |
||
| 1870 | * |
||
| 1871 | * @param object|string $classOrObject |
||
| 1872 | * |
||
| 1873 | * @throws ExpectationFailedException |
||
| 1874 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1875 | * |
||
| 1876 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 1877 | */ |
||
| 1878 | public static function assertAttributeNotInternalType(string $expected, string $attributeName, $classOrObject, string $message = ''): void |
||
| 1879 | { |
||
| 1880 | static::assertNotInternalType( |
||
| 1881 | $expected, |
||
| 1882 | static::readAttribute($classOrObject, $attributeName), |
||
| 1883 | $message |
||
| 1884 | ); |
||
| 1885 | } |
||
| 1886 | |||
| 1887 | /** |
||
| 1888 | * Asserts that a string matches a given regular expression. |
||
| 1889 | * |
||
| 1890 | * @throws ExpectationFailedException |
||
| 1891 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1892 | */ |
||
| 1893 | public static function assertRegExp(string $pattern, string $string, string $message = ''): void |
||
| 1894 | { |
||
| 1895 | static::assertThat($string, new RegularExpression($pattern), $message); |
||
| 1896 | } |
||
| 1897 | |||
| 1898 | /** |
||
| 1899 | * Asserts that a string does not match a given regular expression. |
||
| 1900 | * |
||
| 1901 | * @throws ExpectationFailedException |
||
| 1902 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1903 | */ |
||
| 1904 | public static function assertNotRegExp(string $pattern, string $string, string $message = ''): void |
||
| 1905 | { |
||
| 1906 | static::assertThat( |
||
| 1907 | $string, |
||
| 1908 | new LogicalNot( |
||
| 1909 | new RegularExpression($pattern) |
||
| 1910 | ), |
||
| 1911 | $message |
||
| 1912 | ); |
||
| 1913 | } |
||
| 1914 | |||
| 1915 | /** |
||
| 1916 | * Assert that the size of two arrays (or `Countable` or `Traversable` objects) |
||
| 1917 | * is the same. |
||
| 1918 | * |
||
| 1919 | * @param Countable|iterable $expected |
||
| 1920 | * @param Countable|iterable $actual |
||
| 1921 | * |
||
| 1922 | * @throws ExpectationFailedException |
||
| 1923 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1924 | */ |
||
| 1925 | public static function assertSameSize($expected, $actual, string $message = ''): void |
||
| 1926 | { |
||
| 1927 | if (!$expected instanceof Countable && !\is_iterable($expected)) { |
||
| 1928 | throw InvalidArgumentHelper::factory(1, 'countable or iterable'); |
||
| 1929 | } |
||
| 1930 | |||
| 1931 | if (!$actual instanceof Countable && !\is_iterable($actual)) { |
||
| 1932 | throw InvalidArgumentHelper::factory(2, 'countable or iterable'); |
||
| 1933 | } |
||
| 1934 | |||
| 1935 | static::assertThat( |
||
| 1936 | $actual, |
||
| 1937 | new SameSize($expected), |
||
| 1938 | $message |
||
| 1939 | ); |
||
| 1940 | } |
||
| 1941 | |||
| 1942 | /** |
||
| 1943 | * Assert that the size of two arrays (or `Countable` or `Traversable` objects) |
||
| 1944 | * is not the same. |
||
| 1945 | * |
||
| 1946 | * @param Countable|iterable $expected |
||
| 1947 | * @param Countable|iterable $actual |
||
| 1948 | * |
||
| 1949 | * @throws ExpectationFailedException |
||
| 1950 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1951 | */ |
||
| 1952 | public static function assertNotSameSize($expected, $actual, string $message = ''): void |
||
| 1953 | { |
||
| 1954 | if (!$expected instanceof Countable && !\is_iterable($expected)) { |
||
| 1955 | throw InvalidArgumentHelper::factory(1, 'countable or iterable'); |
||
| 1956 | } |
||
| 1957 | |||
| 1958 | if (!$actual instanceof Countable && !\is_iterable($actual)) { |
||
| 1959 | throw InvalidArgumentHelper::factory(2, 'countable or iterable'); |
||
| 1960 | } |
||
| 1961 | |||
| 1962 | static::assertThat( |
||
| 1963 | $actual, |
||
| 1964 | new LogicalNot( |
||
| 1965 | new SameSize($expected) |
||
| 1966 | ), |
||
| 1967 | $message |
||
| 1968 | ); |
||
| 1969 | } |
||
| 1970 | |||
| 1971 | /** |
||
| 1972 | * Asserts that a string matches a given format string. |
||
| 1973 | * |
||
| 1974 | * @throws ExpectationFailedException |
||
| 1975 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1976 | */ |
||
| 1977 | public static function assertStringMatchesFormat(string $format, string $string, string $message = ''): void |
||
| 1978 | { |
||
| 1979 | static::assertThat($string, new StringMatchesFormatDescription($format), $message); |
||
| 1980 | } |
||
| 1981 | |||
| 1982 | /** |
||
| 1983 | * Asserts that a string does not match a given format string. |
||
| 1984 | * |
||
| 1985 | * @throws ExpectationFailedException |
||
| 1986 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 1987 | */ |
||
| 1988 | public static function assertStringNotMatchesFormat(string $format, string $string, string $message = ''): void |
||
| 1989 | { |
||
| 1990 | static::assertThat( |
||
| 1991 | $string, |
||
| 1992 | new LogicalNot( |
||
| 1993 | new StringMatchesFormatDescription($format) |
||
| 1994 | ), |
||
| 1995 | $message |
||
| 1996 | ); |
||
| 1997 | } |
||
| 1998 | |||
| 1999 | /** |
||
| 2000 | * Asserts that a string matches a given format file. |
||
| 2001 | * |
||
| 2002 | * @throws ExpectationFailedException |
||
| 2003 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2004 | */ |
||
| 2005 | public static function assertStringMatchesFormatFile(string $formatFile, string $string, string $message = ''): void |
||
| 2006 | { |
||
| 2007 | static::assertFileExists($formatFile, $message); |
||
| 2008 | |||
| 2009 | static::assertThat( |
||
| 2010 | $string, |
||
| 2011 | new StringMatchesFormatDescription( |
||
| 2012 | \file_get_contents($formatFile) |
||
| 2013 | ), |
||
| 2014 | $message |
||
| 2015 | ); |
||
| 2016 | } |
||
| 2017 | |||
| 2018 | /** |
||
| 2019 | * Asserts that a string does not match a given format string. |
||
| 2020 | * |
||
| 2021 | * @throws ExpectationFailedException |
||
| 2022 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2023 | */ |
||
| 2024 | public static function assertStringNotMatchesFormatFile(string $formatFile, string $string, string $message = ''): void |
||
| 2025 | { |
||
| 2026 | static::assertFileExists($formatFile, $message); |
||
| 2027 | |||
| 2028 | static::assertThat( |
||
| 2029 | $string, |
||
| 2030 | new LogicalNot( |
||
| 2031 | new StringMatchesFormatDescription( |
||
| 2032 | \file_get_contents($formatFile) |
||
| 2033 | ) |
||
| 2034 | ), |
||
| 2035 | $message |
||
| 2036 | ); |
||
| 2037 | } |
||
| 2038 | |||
| 2039 | /** |
||
| 2040 | * Asserts that a string starts with a given prefix. |
||
| 2041 | * |
||
| 2042 | * @throws ExpectationFailedException |
||
| 2043 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2044 | */ |
||
| 2045 | public static function assertStringStartsWith(string $prefix, string $string, string $message = ''): void |
||
| 2046 | { |
||
| 2047 | static::assertThat($string, new StringStartsWith($prefix), $message); |
||
| 2048 | } |
||
| 2049 | |||
| 2050 | /** |
||
| 2051 | * Asserts that a string starts not with a given prefix. |
||
| 2052 | * |
||
| 2053 | * @param string $prefix |
||
| 2054 | * @param string $string |
||
| 2055 | * |
||
| 2056 | * @throws ExpectationFailedException |
||
| 2057 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2058 | */ |
||
| 2059 | public static function assertStringStartsNotWith($prefix, $string, string $message = ''): void |
||
| 2060 | { |
||
| 2061 | static::assertThat( |
||
| 2062 | $string, |
||
| 2063 | new LogicalNot( |
||
| 2064 | new StringStartsWith($prefix) |
||
| 2065 | ), |
||
| 2066 | $message |
||
| 2067 | ); |
||
| 2068 | } |
||
| 2069 | |||
| 2070 | public static function assertStringContainsString(string $needle, string $haystack, string $message = ''): void |
||
| 2071 | { |
||
| 2072 | $constraint = new StringContains($needle, false); |
||
| 2073 | |||
| 2074 | static::assertThat($haystack, $constraint, $message); |
||
| 2075 | } |
||
| 2076 | |||
| 2077 | public static function assertStringContainsStringIgnoringCase(string $needle, string $haystack, string $message = ''): void |
||
| 2078 | { |
||
| 2079 | $constraint = new StringContains($needle, true); |
||
| 2080 | |||
| 2081 | static::assertThat($haystack, $constraint, $message); |
||
| 2082 | } |
||
| 2083 | |||
| 2084 | public static function assertStringNotContainsString(string $needle, string $haystack, string $message = ''): void |
||
| 2089 | } |
||
| 2090 | |||
| 2091 | public static function assertStringNotContainsStringIgnoringCase(string $needle, string $haystack, string $message = ''): void |
||
| 2092 | { |
||
| 2093 | $constraint = new LogicalNot(new StringContains($needle, true)); |
||
| 2094 | |||
| 2095 | static::assertThat($haystack, $constraint, $message); |
||
| 2096 | } |
||
| 2097 | |||
| 2098 | /** |
||
| 2099 | * Asserts that a string ends with a given suffix. |
||
| 2100 | * |
||
| 2101 | * @throws ExpectationFailedException |
||
| 2102 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2103 | */ |
||
| 2104 | public static function assertStringEndsWith(string $suffix, string $string, string $message = ''): void |
||
| 2105 | { |
||
| 2106 | static::assertThat($string, new StringEndsWith($suffix), $message); |
||
| 2107 | } |
||
| 2108 | |||
| 2109 | /** |
||
| 2110 | * Asserts that a string ends not with a given suffix. |
||
| 2111 | * |
||
| 2112 | * @throws ExpectationFailedException |
||
| 2113 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2114 | */ |
||
| 2115 | public static function assertStringEndsNotWith(string $suffix, string $string, string $message = ''): void |
||
| 2116 | { |
||
| 2117 | static::assertThat( |
||
| 2118 | $string, |
||
| 2119 | new LogicalNot( |
||
| 2120 | new StringEndsWith($suffix) |
||
| 2121 | ), |
||
| 2122 | $message |
||
| 2123 | ); |
||
| 2124 | } |
||
| 2125 | |||
| 2126 | /** |
||
| 2127 | * Asserts that two XML files are equal. |
||
| 2128 | * |
||
| 2129 | * @throws ExpectationFailedException |
||
| 2130 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2131 | */ |
||
| 2132 | public static function assertXmlFileEqualsXmlFile(string $expectedFile, string $actualFile, string $message = ''): void |
||
| 2133 | { |
||
| 2134 | $expected = Xml::loadFile($expectedFile); |
||
| 2135 | $actual = Xml::loadFile($actualFile); |
||
| 2136 | |||
| 2137 | static::assertEquals($expected, $actual, $message); |
||
| 2138 | } |
||
| 2139 | |||
| 2140 | /** |
||
| 2141 | * Asserts that two XML files are not equal. |
||
| 2142 | * |
||
| 2143 | * @throws ExpectationFailedException |
||
| 2144 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2145 | */ |
||
| 2146 | public static function assertXmlFileNotEqualsXmlFile(string $expectedFile, string $actualFile, string $message = ''): void |
||
| 2147 | { |
||
| 2148 | $expected = Xml::loadFile($expectedFile); |
||
| 2149 | $actual = Xml::loadFile($actualFile); |
||
| 2150 | |||
| 2151 | static::assertNotEquals($expected, $actual, $message); |
||
| 2152 | } |
||
| 2153 | |||
| 2154 | /** |
||
| 2155 | * Asserts that two XML documents are equal. |
||
| 2156 | * |
||
| 2157 | * @param DOMDocument|string $actualXml |
||
| 2158 | * |
||
| 2159 | * @throws ExpectationFailedException |
||
| 2160 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2161 | */ |
||
| 2162 | public static function assertXmlStringEqualsXmlFile(string $expectedFile, $actualXml, string $message = ''): void |
||
| 2163 | { |
||
| 2164 | $expected = Xml::loadFile($expectedFile); |
||
| 2165 | $actual = Xml::load($actualXml); |
||
| 2166 | |||
| 2167 | static::assertEquals($expected, $actual, $message); |
||
| 2168 | } |
||
| 2169 | |||
| 2170 | /** |
||
| 2171 | * Asserts that two XML documents are not equal. |
||
| 2172 | * |
||
| 2173 | * @param DOMDocument|string $actualXml |
||
| 2174 | * |
||
| 2175 | * @throws ExpectationFailedException |
||
| 2176 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2177 | */ |
||
| 2178 | public static function assertXmlStringNotEqualsXmlFile(string $expectedFile, $actualXml, string $message = ''): void |
||
| 2179 | { |
||
| 2180 | $expected = Xml::loadFile($expectedFile); |
||
| 2181 | $actual = Xml::load($actualXml); |
||
| 2182 | |||
| 2183 | static::assertNotEquals($expected, $actual, $message); |
||
| 2184 | } |
||
| 2185 | |||
| 2186 | /** |
||
| 2187 | * Asserts that two XML documents are equal. |
||
| 2188 | * |
||
| 2189 | * @param DOMDocument|string $expectedXml |
||
| 2190 | * @param DOMDocument|string $actualXml |
||
| 2191 | * |
||
| 2192 | * @throws ExpectationFailedException |
||
| 2193 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2194 | */ |
||
| 2195 | public static function assertXmlStringEqualsXmlString($expectedXml, $actualXml, string $message = ''): void |
||
| 2196 | { |
||
| 2197 | $expected = Xml::load($expectedXml); |
||
| 2198 | $actual = Xml::load($actualXml); |
||
| 2199 | |||
| 2200 | static::assertEquals($expected, $actual, $message); |
||
| 2201 | } |
||
| 2202 | |||
| 2203 | /** |
||
| 2204 | * Asserts that two XML documents are not equal. |
||
| 2205 | * |
||
| 2206 | * @param DOMDocument|string $expectedXml |
||
| 2207 | * @param DOMDocument|string $actualXml |
||
| 2208 | * |
||
| 2209 | * @throws ExpectationFailedException |
||
| 2210 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2211 | */ |
||
| 2212 | public static function assertXmlStringNotEqualsXmlString($expectedXml, $actualXml, string $message = ''): void |
||
| 2213 | { |
||
| 2214 | $expected = Xml::load($expectedXml); |
||
| 2215 | $actual = Xml::load($actualXml); |
||
| 2216 | |||
| 2217 | static::assertNotEquals($expected, $actual, $message); |
||
| 2218 | } |
||
| 2219 | |||
| 2220 | /** |
||
| 2221 | * Asserts that a hierarchy of DOMElements matches. |
||
| 2222 | * |
||
| 2223 | * @throws AssertionFailedError |
||
| 2224 | * @throws ExpectationFailedException |
||
| 2225 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2226 | */ |
||
| 2227 | public static function assertEqualXMLStructure(DOMElement $expectedElement, DOMElement $actualElement, bool $checkAttributes = false, string $message = ''): void |
||
| 2228 | { |
||
| 2229 | $expectedElement = Xml::import($expectedElement); |
||
| 2230 | $actualElement = Xml::import($actualElement); |
||
| 2231 | |||
| 2232 | static::assertSame( |
||
| 2233 | $expectedElement->tagName, |
||
| 2234 | $actualElement->tagName, |
||
| 2235 | $message |
||
| 2236 | ); |
||
| 2237 | |||
| 2238 | if ($checkAttributes) { |
||
| 2239 | static::assertSame( |
||
| 2240 | $expectedElement->attributes->length, |
||
| 2241 | $actualElement->attributes->length, |
||
| 2242 | \sprintf( |
||
| 2243 | '%s%sNumber of attributes on node "%s" does not match', |
||
| 2244 | $message, |
||
| 2245 | !empty($message) ? "\n" : '', |
||
| 2246 | $expectedElement->tagName |
||
| 2247 | ) |
||
| 2248 | ); |
||
| 2249 | |||
| 2250 | for ($i = 0; $i < $expectedElement->attributes->length; $i++) { |
||
| 2251 | /** @var \DOMAttr $expectedAttribute */ |
||
| 2252 | $expectedAttribute = $expectedElement->attributes->item($i); |
||
| 2253 | |||
| 2254 | /** @var \DOMAttr $actualAttribute */ |
||
| 2255 | $actualAttribute = $actualElement->attributes->getNamedItem( |
||
| 2256 | $expectedAttribute->name |
||
| 2257 | ); |
||
| 2258 | |||
| 2259 | if (!$actualAttribute) { |
||
| 2260 | static::fail( |
||
| 2261 | \sprintf( |
||
| 2262 | '%s%sCould not find attribute "%s" on node "%s"', |
||
| 2263 | $message, |
||
| 2264 | !empty($message) ? "\n" : '', |
||
| 2265 | $expectedAttribute->name, |
||
| 2266 | $expectedElement->tagName |
||
| 2267 | ) |
||
| 2268 | ); |
||
| 2269 | } |
||
| 2270 | } |
||
| 2271 | } |
||
| 2272 | |||
| 2273 | Xml::removeCharacterDataNodes($expectedElement); |
||
| 2274 | Xml::removeCharacterDataNodes($actualElement); |
||
| 2275 | |||
| 2276 | static::assertSame( |
||
| 2277 | $expectedElement->childNodes->length, |
||
| 2278 | $actualElement->childNodes->length, |
||
| 2279 | \sprintf( |
||
| 2280 | '%s%sNumber of child nodes of "%s" differs', |
||
| 2281 | $message, |
||
| 2282 | !empty($message) ? "\n" : '', |
||
| 2283 | $expectedElement->tagName |
||
| 2284 | ) |
||
| 2285 | ); |
||
| 2286 | |||
| 2287 | for ($i = 0; $i < $expectedElement->childNodes->length; $i++) { |
||
| 2288 | static::assertEqualXMLStructure( |
||
| 2289 | $expectedElement->childNodes->item($i), |
||
| 2290 | $actualElement->childNodes->item($i), |
||
| 2291 | $checkAttributes, |
||
| 2292 | $message |
||
| 2293 | ); |
||
| 2294 | } |
||
| 2295 | } |
||
| 2296 | |||
| 2297 | /** |
||
| 2298 | * Evaluates a PHPUnit\Framework\Constraint matcher object. |
||
| 2299 | * |
||
| 2300 | * @throws ExpectationFailedException |
||
| 2301 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2302 | */ |
||
| 2303 | public static function assertThat($value, Constraint $constraint, string $message = ''): void |
||
| 2304 | { |
||
| 2305 | self::$count += \count($constraint); |
||
| 2306 | |||
| 2307 | $constraint->evaluate($value, $message); |
||
| 2308 | } |
||
| 2309 | |||
| 2310 | /** |
||
| 2311 | * Asserts that a string is a valid JSON string. |
||
| 2312 | * |
||
| 2313 | * @throws ExpectationFailedException |
||
| 2314 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2315 | */ |
||
| 2316 | public static function assertJson(string $actualJson, string $message = ''): void |
||
| 2317 | { |
||
| 2318 | static::assertThat($actualJson, static::isJson(), $message); |
||
| 2319 | } |
||
| 2320 | |||
| 2321 | /** |
||
| 2322 | * Asserts that two given JSON encoded objects or arrays are equal. |
||
| 2323 | * |
||
| 2324 | * @throws ExpectationFailedException |
||
| 2325 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2326 | */ |
||
| 2327 | public static function assertJsonStringEqualsJsonString(string $expectedJson, string $actualJson, string $message = ''): void |
||
| 2328 | { |
||
| 2329 | static::assertJson($expectedJson, $message); |
||
| 2330 | static::assertJson($actualJson, $message); |
||
| 2331 | |||
| 2332 | static::assertThat($actualJson, new JsonMatches($expectedJson), $message); |
||
| 2333 | } |
||
| 2334 | |||
| 2335 | /** |
||
| 2336 | * Asserts that two given JSON encoded objects or arrays are not equal. |
||
| 2337 | * |
||
| 2338 | * @param string $expectedJson |
||
| 2339 | * @param string $actualJson |
||
| 2340 | * |
||
| 2341 | * @throws ExpectationFailedException |
||
| 2342 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2343 | */ |
||
| 2344 | public static function assertJsonStringNotEqualsJsonString($expectedJson, $actualJson, string $message = ''): void |
||
| 2345 | { |
||
| 2346 | static::assertJson($expectedJson, $message); |
||
| 2347 | static::assertJson($actualJson, $message); |
||
| 2348 | |||
| 2349 | static::assertThat( |
||
| 2350 | $actualJson, |
||
| 2351 | new LogicalNot( |
||
| 2352 | new JsonMatches($expectedJson) |
||
| 2353 | ), |
||
| 2354 | $message |
||
| 2355 | ); |
||
| 2356 | } |
||
| 2357 | |||
| 2358 | /** |
||
| 2359 | * Asserts that the generated JSON encoded object and the content of the given file are equal. |
||
| 2360 | * |
||
| 2361 | * @throws ExpectationFailedException |
||
| 2362 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2363 | */ |
||
| 2364 | public static function assertJsonStringEqualsJsonFile(string $expectedFile, string $actualJson, string $message = ''): void |
||
| 2365 | { |
||
| 2366 | static::assertFileExists($expectedFile, $message); |
||
| 2367 | $expectedJson = \file_get_contents($expectedFile); |
||
| 2368 | |||
| 2369 | static::assertJson($expectedJson, $message); |
||
| 2370 | static::assertJson($actualJson, $message); |
||
| 2371 | |||
| 2372 | static::assertThat($actualJson, new JsonMatches($expectedJson), $message); |
||
| 2373 | } |
||
| 2374 | |||
| 2375 | /** |
||
| 2376 | * Asserts that the generated JSON encoded object and the content of the given file are not equal. |
||
| 2377 | * |
||
| 2378 | * @throws ExpectationFailedException |
||
| 2379 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2380 | */ |
||
| 2381 | public static function assertJsonStringNotEqualsJsonFile(string $expectedFile, string $actualJson, string $message = ''): void |
||
| 2382 | { |
||
| 2383 | static::assertFileExists($expectedFile, $message); |
||
| 2384 | $expectedJson = \file_get_contents($expectedFile); |
||
| 2385 | |||
| 2386 | static::assertJson($expectedJson, $message); |
||
| 2387 | static::assertJson($actualJson, $message); |
||
| 2388 | |||
| 2389 | static::assertThat( |
||
| 2390 | $actualJson, |
||
| 2391 | new LogicalNot( |
||
| 2392 | new JsonMatches($expectedJson) |
||
| 2393 | ), |
||
| 2394 | $message |
||
| 2395 | ); |
||
| 2396 | } |
||
| 2397 | |||
| 2398 | /** |
||
| 2399 | * Asserts that two JSON files are equal. |
||
| 2400 | * |
||
| 2401 | * @throws ExpectationFailedException |
||
| 2402 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2403 | */ |
||
| 2404 | public static function assertJsonFileEqualsJsonFile(string $expectedFile, string $actualFile, string $message = ''): void |
||
| 2405 | { |
||
| 2406 | static::assertFileExists($expectedFile, $message); |
||
| 2407 | static::assertFileExists($actualFile, $message); |
||
| 2408 | |||
| 2409 | $actualJson = \file_get_contents($actualFile); |
||
| 2410 | $expectedJson = \file_get_contents($expectedFile); |
||
| 2411 | |||
| 2412 | static::assertJson($expectedJson, $message); |
||
| 2413 | static::assertJson($actualJson, $message); |
||
| 2414 | |||
| 2415 | $constraintExpected = new JsonMatches( |
||
| 2416 | $expectedJson |
||
| 2417 | ); |
||
| 2418 | |||
| 2419 | $constraintActual = new JsonMatches($actualJson); |
||
| 2420 | |||
| 2421 | static::assertThat($expectedJson, $constraintActual, $message); |
||
| 2422 | static::assertThat($actualJson, $constraintExpected, $message); |
||
| 2423 | } |
||
| 2424 | |||
| 2425 | /** |
||
| 2426 | * Asserts that two JSON files are not equal. |
||
| 2427 | * |
||
| 2428 | * @throws ExpectationFailedException |
||
| 2429 | * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
||
| 2430 | */ |
||
| 2431 | public static function assertJsonFileNotEqualsJsonFile(string $expectedFile, string $actualFile, string $message = ''): void |
||
| 2432 | { |
||
| 2433 | static::assertFileExists($expectedFile, $message); |
||
| 2434 | static::assertFileExists($actualFile, $message); |
||
| 2435 | |||
| 2436 | $actualJson = \file_get_contents($actualFile); |
||
| 2437 | $expectedJson = \file_get_contents($expectedFile); |
||
| 2438 | |||
| 2439 | static::assertJson($expectedJson, $message); |
||
| 2440 | static::assertJson($actualJson, $message); |
||
| 2441 | |||
| 2442 | $constraintExpected = new JsonMatches( |
||
| 2443 | $expectedJson |
||
| 2444 | ); |
||
| 2445 | |||
| 2446 | $constraintActual = new JsonMatches($actualJson); |
||
| 2447 | |||
| 2448 | static::assertThat($expectedJson, new LogicalNot($constraintActual), $message); |
||
| 2449 | static::assertThat($actualJson, new LogicalNot($constraintExpected), $message); |
||
| 2450 | } |
||
| 2451 | |||
| 2452 | public static function logicalAnd(): LogicalAnd |
||
| 2453 | { |
||
| 2454 | $constraints = \func_get_args(); |
||
| 2455 | |||
| 2456 | $constraint = new LogicalAnd; |
||
| 2457 | $constraint->setConstraints($constraints); |
||
| 2458 | |||
| 2459 | return $constraint; |
||
| 2460 | } |
||
| 2461 | |||
| 2462 | public static function logicalOr(): LogicalOr |
||
| 2463 | { |
||
| 2464 | $constraints = \func_get_args(); |
||
| 2465 | |||
| 2466 | $constraint = new LogicalOr; |
||
| 2467 | $constraint->setConstraints($constraints); |
||
| 2468 | |||
| 2469 | return $constraint; |
||
| 2470 | } |
||
| 2471 | |||
| 2472 | public static function logicalNot(Constraint $constraint): LogicalNot |
||
| 2473 | { |
||
| 2474 | return new LogicalNot($constraint); |
||
| 2475 | } |
||
| 2476 | |||
| 2477 | public static function logicalXor(): LogicalXor |
||
| 2478 | { |
||
| 2479 | $constraints = \func_get_args(); |
||
| 2480 | |||
| 2481 | $constraint = new LogicalXor; |
||
| 2482 | $constraint->setConstraints($constraints); |
||
| 2483 | |||
| 2484 | return $constraint; |
||
| 2485 | } |
||
| 2486 | |||
| 2487 | public static function anything(): IsAnything |
||
| 2488 | { |
||
| 2489 | return new IsAnything; |
||
| 2490 | } |
||
| 2491 | |||
| 2492 | public static function isTrue(): IsTrue |
||
| 2493 | { |
||
| 2494 | return new IsTrue; |
||
| 2495 | } |
||
| 2496 | |||
| 2497 | public static function callback(callable $callback): Callback |
||
| 2498 | { |
||
| 2499 | return new Callback($callback); |
||
| 2500 | } |
||
| 2501 | |||
| 2502 | public static function isFalse(): IsFalse |
||
| 2503 | { |
||
| 2504 | return new IsFalse; |
||
| 2505 | } |
||
| 2506 | |||
| 2507 | public static function isJson(): IsJson |
||
| 2508 | { |
||
| 2509 | return new IsJson; |
||
| 2510 | } |
||
| 2511 | |||
| 2512 | public static function isNull(): IsNull |
||
| 2513 | { |
||
| 2514 | return new IsNull; |
||
| 2515 | } |
||
| 2516 | |||
| 2517 | public static function isFinite(): IsFinite |
||
| 2518 | { |
||
| 2519 | return new IsFinite; |
||
| 2520 | } |
||
| 2521 | |||
| 2522 | public static function isInfinite(): IsInfinite |
||
| 2523 | { |
||
| 2524 | return new IsInfinite; |
||
| 2525 | } |
||
| 2526 | |||
| 2527 | public static function isNan(): IsNan |
||
| 2528 | { |
||
| 2529 | return new IsNan; |
||
| 2530 | } |
||
| 2531 | |||
| 2532 | /** |
||
| 2533 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 2534 | */ |
||
| 2535 | public static function attribute(Constraint $constraint, string $attributeName): Attribute |
||
| 2536 | { |
||
| 2537 | return new Attribute($constraint, $attributeName); |
||
| 2538 | } |
||
| 2539 | |||
| 2540 | public static function contains($value, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): TraversableContains |
||
| 2541 | { |
||
| 2542 | return new TraversableContains($value, $checkForObjectIdentity, $checkForNonObjectIdentity); |
||
| 2543 | } |
||
| 2544 | |||
| 2545 | public static function containsOnly(string $type): TraversableContainsOnly |
||
| 2546 | { |
||
| 2547 | return new TraversableContainsOnly($type); |
||
| 2548 | } |
||
| 2549 | |||
| 2550 | public static function containsOnlyInstancesOf(string $className): TraversableContainsOnly |
||
| 2551 | { |
||
| 2552 | return new TraversableContainsOnly($className, false); |
||
| 2553 | } |
||
| 2554 | |||
| 2555 | /** |
||
| 2556 | * @param int|string $key |
||
| 2557 | */ |
||
| 2558 | public static function arrayHasKey($key): ArrayHasKey |
||
| 2559 | { |
||
| 2560 | return new ArrayHasKey($key); |
||
| 2561 | } |
||
| 2562 | |||
| 2563 | public static function equalTo($value, float $delta = 0.0, int $maxDepth = 10, bool $canonicalize = false, bool $ignoreCase = false): IsEqual |
||
| 2564 | { |
||
| 2565 | return new IsEqual($value, $delta, $maxDepth, $canonicalize, $ignoreCase); |
||
| 2566 | } |
||
| 2567 | |||
| 2568 | /** |
||
| 2569 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 2570 | */ |
||
| 2571 | public static function attributeEqualTo(string $attributeName, $value, float $delta = 0.0, int $maxDepth = 10, bool $canonicalize = false, bool $ignoreCase = false): Attribute |
||
| 2572 | { |
||
| 2573 | return static::attribute( |
||
| 2574 | static::equalTo( |
||
| 2575 | $value, |
||
| 2576 | $delta, |
||
| 2577 | $maxDepth, |
||
| 2578 | $canonicalize, |
||
| 2579 | $ignoreCase |
||
| 2580 | ), |
||
| 2581 | $attributeName |
||
| 2582 | ); |
||
| 2583 | } |
||
| 2584 | |||
| 2585 | public static function isEmpty(): IsEmpty |
||
| 2586 | { |
||
| 2587 | return new IsEmpty; |
||
| 2588 | } |
||
| 2589 | |||
| 2590 | public static function isWritable(): IsWritable |
||
| 2591 | { |
||
| 2592 | return new IsWritable; |
||
| 2593 | } |
||
| 2594 | |||
| 2595 | public static function isReadable(): IsReadable |
||
| 2596 | { |
||
| 2597 | return new IsReadable; |
||
| 2598 | } |
||
| 2599 | |||
| 2600 | public static function directoryExists(): DirectoryExists |
||
| 2601 | { |
||
| 2602 | return new DirectoryExists; |
||
| 2603 | } |
||
| 2604 | |||
| 2605 | public static function fileExists(): FileExists |
||
| 2606 | { |
||
| 2607 | return new FileExists; |
||
| 2608 | } |
||
| 2609 | |||
| 2610 | public static function greaterThan($value): GreaterThan |
||
| 2611 | { |
||
| 2612 | return new GreaterThan($value); |
||
| 2613 | } |
||
| 2614 | |||
| 2615 | public static function greaterThanOrEqual($value): LogicalOr |
||
| 2616 | { |
||
| 2617 | return static::logicalOr( |
||
| 2618 | new IsEqual($value), |
||
| 2619 | new GreaterThan($value) |
||
| 2620 | ); |
||
| 2621 | } |
||
| 2622 | |||
| 2623 | public static function classHasAttribute(string $attributeName): ClassHasAttribute |
||
| 2624 | { |
||
| 2625 | return new ClassHasAttribute($attributeName); |
||
| 2626 | } |
||
| 2627 | |||
| 2628 | public static function classHasStaticAttribute(string $attributeName): ClassHasStaticAttribute |
||
| 2629 | { |
||
| 2630 | return new ClassHasStaticAttribute($attributeName); |
||
| 2631 | } |
||
| 2632 | |||
| 2633 | public static function objectHasAttribute($attributeName): ObjectHasAttribute |
||
| 2634 | { |
||
| 2635 | return new ObjectHasAttribute($attributeName); |
||
| 2636 | } |
||
| 2637 | |||
| 2638 | public static function identicalTo($value): IsIdentical |
||
| 2639 | { |
||
| 2640 | return new IsIdentical($value); |
||
| 2641 | } |
||
| 2642 | |||
| 2643 | public static function isInstanceOf(string $className): IsInstanceOf |
||
| 2644 | { |
||
| 2645 | return new IsInstanceOf($className); |
||
| 2646 | } |
||
| 2647 | |||
| 2648 | public static function isType(string $type): IsType |
||
| 2649 | { |
||
| 2650 | return new IsType($type); |
||
| 2651 | } |
||
| 2652 | |||
| 2653 | public static function lessThan($value): LessThan |
||
| 2654 | { |
||
| 2655 | return new LessThan($value); |
||
| 2656 | } |
||
| 2657 | |||
| 2658 | public static function lessThanOrEqual($value): LogicalOr |
||
| 2659 | { |
||
| 2660 | return static::logicalOr( |
||
| 2661 | new IsEqual($value), |
||
| 2662 | new LessThan($value) |
||
| 2663 | ); |
||
| 2664 | } |
||
| 2665 | |||
| 2666 | public static function matchesRegularExpression(string $pattern): RegularExpression |
||
| 2667 | { |
||
| 2668 | return new RegularExpression($pattern); |
||
| 2669 | } |
||
| 2670 | |||
| 2671 | public static function matches(string $string): StringMatchesFormatDescription |
||
| 2672 | { |
||
| 2673 | return new StringMatchesFormatDescription($string); |
||
| 2674 | } |
||
| 2675 | |||
| 2676 | public static function stringStartsWith($prefix): StringStartsWith |
||
| 2677 | { |
||
| 2678 | return new StringStartsWith($prefix); |
||
| 2679 | } |
||
| 2680 | |||
| 2681 | public static function stringContains(string $string, bool $case = true): StringContains |
||
| 2682 | { |
||
| 2683 | return new StringContains($string, $case); |
||
| 2684 | } |
||
| 2685 | |||
| 2686 | public static function stringEndsWith(string $suffix): StringEndsWith |
||
| 2687 | { |
||
| 2688 | return new StringEndsWith($suffix); |
||
| 2689 | } |
||
| 2690 | |||
| 2691 | public static function countOf(int $count): Count |
||
| 2692 | { |
||
| 2693 | return new Count($count); |
||
| 2694 | } |
||
| 2695 | |||
| 2696 | /** |
||
| 2697 | * Fails a test with the given message. |
||
| 2698 | * |
||
| 2699 | * @throws AssertionFailedError |
||
| 2700 | */ |
||
| 2701 | public static function fail(string $message = ''): void |
||
| 2702 | { |
||
| 2703 | self::$count++; |
||
| 2704 | |||
| 2705 | throw new AssertionFailedError($message); |
||
| 2706 | } |
||
| 2707 | |||
| 2708 | /** |
||
| 2709 | * Returns the value of an attribute of a class or an object. |
||
| 2710 | * This also works for attributes that are declared protected or private. |
||
| 2711 | * |
||
| 2712 | * @param object|string $classOrObject |
||
| 2713 | * |
||
| 2714 | * @throws Exception |
||
| 2715 | * |
||
| 2716 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 2717 | */ |
||
| 2718 | public static function readAttribute($classOrObject, string $attributeName) |
||
| 2719 | { |
||
| 2720 | if (!self::isValidClassAttributeName($attributeName)) { |
||
| 2721 | throw InvalidArgumentHelper::factory(2, 'valid attribute name'); |
||
| 2722 | } |
||
| 2723 | |||
| 2724 | if (\is_string($classOrObject)) { |
||
| 2725 | if (!\class_exists($classOrObject)) { |
||
| 2726 | throw InvalidArgumentHelper::factory( |
||
| 2727 | 1, |
||
| 2728 | 'class name' |
||
| 2729 | ); |
||
| 2730 | } |
||
| 2731 | |||
| 2732 | return static::getStaticAttribute( |
||
| 2733 | $classOrObject, |
||
| 2734 | $attributeName |
||
| 2735 | ); |
||
| 2736 | } |
||
| 2737 | |||
| 2738 | if (\is_object($classOrObject)) { |
||
| 2739 | return static::getObjectAttribute( |
||
| 2740 | $classOrObject, |
||
| 2741 | $attributeName |
||
| 2742 | ); |
||
| 2743 | } |
||
| 2744 | |||
| 2745 | throw InvalidArgumentHelper::factory( |
||
| 2746 | 1, |
||
| 2747 | 'class name or object' |
||
| 2748 | ); |
||
| 2749 | } |
||
| 2750 | |||
| 2751 | /** |
||
| 2752 | * Returns the value of a static attribute. |
||
| 2753 | * This also works for attributes that are declared protected or private. |
||
| 2754 | * |
||
| 2755 | * @throws Exception |
||
| 2756 | * @throws ReflectionException |
||
| 2757 | * |
||
| 2758 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 2759 | */ |
||
| 2760 | public static function getStaticAttribute(string $className, string $attributeName) |
||
| 2761 | { |
||
| 2762 | if (!\class_exists($className)) { |
||
| 2763 | throw InvalidArgumentHelper::factory(1, 'class name'); |
||
| 2764 | } |
||
| 2765 | |||
| 2766 | if (!self::isValidClassAttributeName($attributeName)) { |
||
| 2767 | throw InvalidArgumentHelper::factory(2, 'valid attribute name'); |
||
| 2768 | } |
||
| 2769 | |||
| 2770 | $class = new ReflectionClass($className); |
||
| 2771 | |||
| 2772 | while ($class) { |
||
| 2773 | $attributes = $class->getStaticProperties(); |
||
| 2774 | |||
| 2775 | if (\array_key_exists($attributeName, $attributes)) { |
||
| 2776 | return $attributes[$attributeName]; |
||
| 2777 | } |
||
| 2778 | |||
| 2779 | $class = $class->getParentClass(); |
||
| 2780 | } |
||
| 2781 | |||
| 2782 | throw new Exception( |
||
| 2783 | \sprintf( |
||
| 2784 | 'Attribute "%s" not found in class.', |
||
| 2785 | $attributeName |
||
| 2786 | ) |
||
| 2787 | ); |
||
| 2788 | } |
||
| 2789 | |||
| 2790 | /** |
||
| 2791 | * Returns the value of an object's attribute. |
||
| 2792 | * This also works for attributes that are declared protected or private. |
||
| 2793 | * |
||
| 2794 | * @param object $object |
||
| 2795 | * |
||
| 2796 | * @throws Exception |
||
| 2797 | * |
||
| 2798 | * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3338 |
||
| 2799 | */ |
||
| 2800 | public static function getObjectAttribute($object, string $attributeName) |
||
| 2836 | ) |
||
| 2837 | ); |
||
| 2838 | } |
||
| 2839 | |||
| 2840 | /** |
||
| 2841 | * Mark the test as incomplete. |
||
| 2842 | * |
||
| 2843 | * @throws IncompleteTestError |
||
| 2844 | */ |
||
| 2845 | public static function markTestIncomplete(string $message = ''): void |
||
| 2846 | { |
||
| 2847 | throw new IncompleteTestError($message); |
||
| 2848 | } |
||
| 2849 | |||
| 2850 | /** |
||
| 2851 | * Mark the test as skipped. |
||
| 2852 | * |
||
| 2853 | * @throws SkippedTestError |
||
| 2854 | */ |
||
| 2855 | public static function markTestSkipped(string $message = ''): void |
||
| 2858 | } |
||
| 2859 | |||
| 2860 | /** |
||
| 2861 | * Return the current assertion count. |
||
| 2862 | */ |
||
| 2863 | public static function getCount(): int |
||
| 2864 | { |
||
| 2865 | return self::$count; |
||
| 2866 | } |
||
| 2867 | |||
| 2868 | /** |
||
| 2869 | * Reset the assertion counter. |
||
| 2870 | */ |
||
| 2871 | public static function resetCount(): void |
||
| 2872 | { |
||
| 2873 | self::$count = 0; |
||
| 2874 | } |
||
| 2875 | |||
| 2876 | private static function isValidObjectAttributeName(string $attributeName): bool |
||
| 2877 | { |
||
| 2878 | return \preg_match('/[^\x00-\x1f\x7f-\x9f]+/', $attributeName); |
||
| 2879 | } |
||
| 2880 | |||
| 2881 | private static function isValidClassAttributeName(string $attributeName): bool |
||
| 2884 | } |
||
| 2885 | |||
| 2886 | private static function createWarning(string $warning): void |
||
| 2887 | { |
||
| 2888 | foreach (\debug_backtrace() as $step) { |
||
| 2889 | if (isset($step['object']) && $step['object'] instanceof TestCase) { |
||
| 2890 | $step['object']->addWarning($warning); |
||
| 2891 | |||
| 2892 | break; |
||
| 2893 | } |
||
| 2894 | } |
||
| 2895 | } |
||
| 2896 | } |
||
| 2897 |