VerifyDeprecations.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Deprecations\PHPUnit;
  4. use Doctrine\Deprecations\Deprecation;
  5. use function sprintf;
  6. trait VerifyDeprecations
  7. {
  8. /** @var array<string,int> */
  9. private $doctrineDeprecationsExpectations = [];
  10. /** @var array<string,int> */
  11. private $doctrineNoDeprecationsExpectations = [];
  12. public function expectDeprecationWithIdentifier(string $identifier): void
  13. {
  14. $this->doctrineDeprecationsExpectations[$identifier] = Deprecation::getTriggeredDeprecations()[$identifier] ?? 0;
  15. }
  16. public function expectNoDeprecationWithIdentifier(string $identifier): void
  17. {
  18. $this->doctrineNoDeprecationsExpectations[$identifier] = Deprecation::getTriggeredDeprecations()[$identifier] ?? 0;
  19. }
  20. /**
  21. * @before
  22. */
  23. public function enableDeprecationTracking(): void
  24. {
  25. Deprecation::enableTrackingDeprecations();
  26. }
  27. /**
  28. * @after
  29. */
  30. public function verifyDeprecationsAreTriggered(): void
  31. {
  32. foreach ($this->doctrineDeprecationsExpectations as $identifier => $expectation) {
  33. $actualCount = Deprecation::getTriggeredDeprecations()[$identifier] ?? 0;
  34. $this->assertTrue(
  35. $actualCount > $expectation,
  36. sprintf(
  37. "Expected deprecation with identifier '%s' was not triggered by code executed in test.",
  38. $identifier
  39. )
  40. );
  41. }
  42. foreach ($this->doctrineNoDeprecationsExpectations as $identifier => $expectation) {
  43. $actualCount = Deprecation::getTriggeredDeprecations()[$identifier] ?? 0;
  44. $this->assertTrue(
  45. $actualCount === $expectation,
  46. sprintf(
  47. "Expected deprecation with identifier '%s' was triggered by code executed in test, but expected not to.",
  48. $identifier
  49. )
  50. );
  51. }
  52. }
  53. }