TestCase.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Nesk\Puphpeteer\Tests;
  3. use Monolog\Logger;
  4. use ReflectionClass;
  5. use Psr\Log\LogLevel;
  6. use PHPUnit\Framework\Constraint\Callback;
  7. use PHPUnit\Framework\TestCase as BaseTestCase;
  8. use PHPUnit\Framework\MockObject\Matcher\Invocation;
  9. class TestCase extends BaseTestCase
  10. {
  11. private $dontPopulateProperties = [];
  12. public function setUp(): void
  13. {
  14. parent::setUp();
  15. $testMethod = new \ReflectionMethod($this, $this->getName());
  16. $docComment = $testMethod->getDocComment();
  17. if (preg_match('/@dontPopulateProperties (.*)/', $docComment, $matches)) {
  18. $this->dontPopulateProperties = array_values(array_filter(explode(' ', $matches[1])));
  19. }
  20. }
  21. public function canPopulateProperty(string $propertyName): bool
  22. {
  23. return !in_array($propertyName, $this->dontPopulateProperties);
  24. }
  25. public function loggerMock($expectations) {
  26. $loggerMock = $this->getMockBuilder(Logger::class)
  27. ->setConstructorArgs(['rialto'])
  28. ->setMethods(['log'])
  29. ->getMock();
  30. if ($expectations instanceof Invocation) {
  31. $expectations = [func_get_args()];
  32. }
  33. foreach ($expectations as $expectation) {
  34. [$matcher] = $expectation;
  35. $with = array_slice($expectation, 1);
  36. $loggerMock->expects($matcher)
  37. ->method('log')
  38. ->with(...$with);
  39. }
  40. return $loggerMock;
  41. }
  42. public function isLogLevel(): Callback {
  43. $psrLogLevels = (new ReflectionClass(LogLevel::class))->getConstants();
  44. $monologLevels = (new ReflectionClass(Logger::class))->getConstants();
  45. $monologLevels = array_intersect_key($monologLevels, $psrLogLevels);
  46. return $this->callback(function ($level) use ($psrLogLevels, $monologLevels) {
  47. if (is_string($level)) {
  48. return in_array($level, $psrLogLevels, true);
  49. } else if (is_int($level)) {
  50. return in_array($level, $monologLevels, true);
  51. }
  52. return false;
  53. });
  54. }
  55. }