TestCase.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2023 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * A compatibility shim for PHPUnit's TestCase, so we can use modern-ish exception expectations on
  12. * older PHPUnit versions.
  13. *
  14. * @todo Remove shims whenever we update minimum PHPUnit versions.
  15. */
  16. namespace Psy\Test;
  17. // PHPUnit <= 9
  18. trait AssertMatchesRegularExpressionForwardCompatibility
  19. {
  20. public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = '')
  21. {
  22. static::assertRegExp($pattern, $string, $message);
  23. }
  24. }
  25. // PHPUnit <= 8
  26. trait ExpectExceptionMessagMatchesForwardCompatibility
  27. {
  28. public function expectExceptionMessageMatches($regularExpression)
  29. {
  30. if (\method_exists(\PHPUnit\Framework\TestCase::class, 'expectExceptionMessageRegExp')) {
  31. parent::expectExceptionMessageRegExp($regularExpression);
  32. } else {
  33. $this->setExpectedExceptionRegExp($this->getExpectedException(), $regularExpression);
  34. }
  35. }
  36. }
  37. // PHPUnit <= 7
  38. trait AssertContainsForwardCompatibility
  39. {
  40. public static function assertStringContainsString($needle, $haystack, $message = '')
  41. {
  42. static::assertContains((string) $needle, (string) $haystack, $message);
  43. }
  44. public static function assertStringContainsStringIgnoringCase($needle, $haystack, $message = '')
  45. {
  46. static::assertContains((string) $needle, (string) $haystack, $message, true);
  47. }
  48. public static function assertStringNotContainsString($needle, $haystack, $message = '')
  49. {
  50. static::assertNotContains((string) $needle, (string) $haystack, $message);
  51. }
  52. public static function assertStringNotContainsStringIgnoringCase($needle, $haystack, $message = '')
  53. {
  54. static::assertNotContains((string) $needle, (string) $haystack, $message, true);
  55. }
  56. }
  57. if (\method_exists(\PHPUnit\Framework\TestCase::class, 'assertStringContainsString')) {
  58. if (\method_exists(\PHPUnit\Framework\TestCase::class, 'expectExceptionMessageMatches')) {
  59. if (\method_exists(\PHPUnit\Framework\TestCase::class, 'assertMatchesRegularExpression')) {
  60. abstract class TestCase extends \PHPUnit\Framework\TestCase
  61. {
  62. // No forward compatibility needed!
  63. }
  64. } else {
  65. abstract class TestCase extends \PHPUnit\Framework\TestCase
  66. {
  67. use AssertMatchesRegularExpressionForwardCompatibility;
  68. }
  69. }
  70. } else {
  71. abstract class TestCase extends \PHPUnit\Framework\TestCase
  72. {
  73. use AssertMatchesRegularExpressionForwardCompatibility;
  74. use ExpectExceptionMessagMatchesForwardCompatibility;
  75. }
  76. }
  77. } else {
  78. abstract class TestCase extends \PHPUnit\Framework\TestCase
  79. {
  80. use AssertContainsForwardCompatibility;
  81. use AssertMatchesRegularExpressionForwardCompatibility;
  82. use ExpectExceptionMessagMatchesForwardCompatibility;
  83. }
  84. }