TestCase.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests;
  13. use PHPUnit\Framework\TestCase as BaseTestCase;
  14. /**
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. *
  17. * @internal
  18. */
  19. abstract class TestCase extends BaseTestCase
  20. {
  21. /** @var null|callable */
  22. private $previouslyDefinedErrorHandler;
  23. /** @var list<string> */
  24. private array $expectedDeprecations = [];
  25. /** @var list<string> */
  26. private array $actualDeprecations = [];
  27. protected function tearDown(): void
  28. {
  29. if (null !== $this->previouslyDefinedErrorHandler) {
  30. $this->actualDeprecations = array_unique($this->actualDeprecations);
  31. sort($this->actualDeprecations);
  32. $this->expectedDeprecations = array_unique($this->expectedDeprecations);
  33. sort($this->expectedDeprecations);
  34. self::assertSame($this->expectedDeprecations, $this->actualDeprecations);
  35. restore_error_handler();
  36. }
  37. parent::tearDown();
  38. }
  39. final public function testNotDefiningConstructor(): void
  40. {
  41. $reflection = new \ReflectionObject($this);
  42. self::assertNotSame(
  43. $reflection->getConstructor()->getDeclaringClass()->getName(),
  44. $reflection->getName(),
  45. );
  46. }
  47. /**
  48. * Mark test to expect given deprecation. Order or repetition count of expected vs actual deprecation usage can vary, but result sets must be identical.
  49. *
  50. * @TODO change access to protected and pass the parameter when PHPUnit 9 support is dropped
  51. */
  52. public function expectDeprecation(/* string $message */): void
  53. {
  54. $this->expectedDeprecations[] = func_get_arg(0);
  55. if (null === $this->previouslyDefinedErrorHandler) {
  56. $this->previouslyDefinedErrorHandler = set_error_handler(
  57. function (
  58. int $code,
  59. string $message
  60. ) {
  61. if (E_USER_DEPRECATED === $code || E_DEPRECATED === $code) {
  62. $this->actualDeprecations[] = $message;
  63. }
  64. return true;
  65. }
  66. );
  67. }
  68. }
  69. }