TestCase.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Nesk\Puphpeteer\Tests;
  3. use Nesk\Puphpeteer\Puppeteer;
  4. use Monolog\Logger;
  5. use ReflectionClass;
  6. use Psr\Log\LogLevel;
  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. $methodName = explode(' ', $this->getName())[0] ?? '';
  18. $testMethod = new \ReflectionMethod($this, $methodName);
  19. $docComment = $testMethod->getDocComment();
  20. if (preg_match('/@dontPopulateProperties (.*)/', $docComment, $matches)) {
  21. $this->dontPopulateProperties = array_values(array_filter(explode(' ', $matches[1])));
  22. }
  23. }
  24. /**
  25. * Stops the browser and local server
  26. */
  27. public function tearDown(): void
  28. {
  29. // Close the browser.
  30. if (isset($this->browser)) {
  31. $this->browser->close();
  32. }
  33. // Shutdown the local server
  34. if (isset($this->servingProcess)) {
  35. $this->servingProcess->stop(0);
  36. }
  37. }
  38. /**
  39. * Serves the resources folder locally on port 8089
  40. */
  41. protected function serveResources(): void
  42. {
  43. // Spin up a local server to deliver the resources.
  44. $this->host = '127.0.0.1:8089';
  45. $this->url = "http://{$this->host}";
  46. $this->serverDir = __DIR__.'/resources';
  47. $this->servingProcess = new Process(['php', '-S', $this->host, '-t', $this->serverDir]);
  48. $this->servingProcess->start();
  49. }
  50. /**
  51. * Launches the PuPHPeteer-controlled browser
  52. */
  53. protected function launchBrowser(): void
  54. {
  55. /**
  56. * Chrome doesn't support Linux sandbox on many CI environments
  57. *
  58. * @see: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#chrome-headless-fails-due-to-sandbox-issues
  59. */
  60. $this->browserOptions = [
  61. 'args' => ['--no-sandbox', '--disable-setuid-sandbox'],
  62. 'headless' => 'new',
  63. ];
  64. if ($this->canPopulateProperty('browser')) {
  65. $this->browser = (new Puppeteer)->launch($this->browserOptions);
  66. }
  67. }
  68. public function canPopulateProperty(string $propertyName): bool
  69. {
  70. return !in_array($propertyName, $this->dontPopulateProperties);
  71. }
  72. public function isLogLevel(): Callback {
  73. $psrLogLevels = (new ReflectionClass(LogLevel::class))->getConstants();
  74. $monologLevels = (new ReflectionClass(Logger::class))->getConstants();
  75. $monologLevels = array_intersect_key($monologLevels, $psrLogLevels);
  76. return $this->callback(function ($level) use ($psrLogLevels, $monologLevels) {
  77. if (is_string($level)) {
  78. return in_array($level, $psrLogLevels, true);
  79. } else if (is_int($level)) {
  80. return in_array($level, $monologLevels, true);
  81. }
  82. return false;
  83. });
  84. }
  85. }