TestCase.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Nesk\Rialto\Tests;
  3. use Monolog\Logger;
  4. use ReflectionClass;
  5. use Psr\Log\LogLevel;
  6. use PHPUnit\Util\ErrorHandler;
  7. use Symfony\Component\Process\Process;
  8. use PHPUnit\Framework\Constraint\Callback;
  9. use PHPUnit\Framework\TestCase as BaseTestCase;
  10. use PHPUnit\Framework\MockObject\Matcher\Invocation;
  11. class TestCase extends BaseTestCase
  12. {
  13. private $dontPopulateProperties = [];
  14. public function setUp(): void
  15. {
  16. parent::setUp();
  17. $testMethod = new \ReflectionMethod($this, $this->getName());
  18. $docComment = $testMethod->getDocComment();
  19. if (preg_match('/@dontPopulateProperties (.*)/', $docComment, $matches)) {
  20. $this->dontPopulateProperties = array_values(array_filter(explode(' ', $matches[1])));
  21. }
  22. }
  23. public function canPopulateProperty(string $propertyName): bool
  24. {
  25. return !in_array($propertyName, $this->dontPopulateProperties);
  26. }
  27. public function ignoreUserDeprecation(string $messagePattern, callable $callback) {
  28. set_error_handler(
  29. function (int $errorNumber, string $errorString, string $errorFile, int $errorLine) use ($messagePattern) {
  30. if ($errorNumber !== E_USER_DEPRECATED || preg_match($messagePattern, $errorString) !== 1) {
  31. ErrorHandler::handleError($errorNumber, $errorString, $errorFile, $errorLine);
  32. }
  33. }
  34. );
  35. $value = $callback();
  36. restore_error_handler();
  37. return $value;
  38. }
  39. public function getPidsForProcessName(string $processName) {
  40. $pgrep = new Process(['pgrep', $processName]);
  41. $pgrep->run();
  42. $pids = explode("\n", $pgrep->getOutput());
  43. $pids = array_filter($pids, function ($pid) {
  44. return !empty($pid);
  45. });
  46. $pids = array_map(function ($pid) {
  47. return (int) $pid;
  48. }, $pids);
  49. return $pids;
  50. }
  51. public function loggerMock($expectations) {
  52. $loggerMock = $this->getMockBuilder(Logger::class)
  53. ->setConstructorArgs(['rialto'])
  54. ->setMethods(['log'])
  55. ->getMock();
  56. if ($expectations instanceof Invocation) {
  57. $expectations = [func_get_args()];
  58. }
  59. foreach ($expectations as $expectation) {
  60. [$matcher] = $expectation;
  61. $with = array_slice($expectation, 1);
  62. $loggerMock->expects($matcher)
  63. ->method('log')
  64. ->with(...$with);
  65. }
  66. return $loggerMock;
  67. }
  68. public function isLogLevel(): Callback {
  69. $psrLogLevels = (new ReflectionClass(LogLevel::class))->getConstants();
  70. $monologLevels = (new ReflectionClass(Logger::class))->getConstants();
  71. $monologLevels = array_intersect_key($monologLevels, $psrLogLevels);
  72. return $this->callback(function ($level) use ($psrLogLevels, $monologLevels) {
  73. if (is_string($level)) {
  74. return in_array($level, $psrLogLevels, true);
  75. } else if (is_int($level)) {
  76. return in_array($level, $monologLevels, true);
  77. }
  78. return false;
  79. });
  80. }
  81. }