Skip to content
This repository has been archived by the owner on Jan 1, 2023. It is now read-only.

Commit

Permalink
Rework tests for browser logs
Browse files Browse the repository at this point in the history
Better decoupling and compatible with PHPUnit 9
  • Loading branch information
nesk committed Nov 30, 2020
1 parent c1ea4db commit 7779e00
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 42 deletions.
74 changes: 54 additions & 20 deletions tests/PuphpeteerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Nesk\Rialto\Data\JsFunction;
use PHPUnit\Framework\ExpectationFailedException;
use Nesk\Puphpeteer\Resources\ElementHandle;
use Psr\Log\LoggerInterface;

class PuphpeteerTest extends TestCase
{
Expand Down Expand Up @@ -156,30 +157,63 @@ public function resourceProvider(): \Generator
}
}

private function createBrowserLogger(callable $onBrowserLog): LoggerInterface
{
$logger = $this->createMock(LoggerInterface::class);
$logger->expects(self::atLeastOnce())
->method('log')
->willReturn(self::returnCallback(function (string $level, string $message) use ($onBrowserLog) {
if (\strpos($message, "Received a Browser log:") === 0) {
$onBrowserLog();
}

return null;
}));

return $logger;
}

/**
* @test
* @dontPopulateProperties browser
*/
public function browser_console_calls_are_logged()
public function browser_console_calls_are_logged_if_enabled()
{
$setups = [
[false, function ($browser) { return $browser->newPage(); }, 'Received data from the port'],
[true, function ($browser) { return $browser->newPage(); }, 'Received a Browser log:'],
[true, function ($browser) { return $browser->pages()[0]; }, 'Received a Browser log:'],
];

foreach ($setups as [$shoulLogBrowserConsole, $pageFactory, $startsWith]) {
$puppeteer = new Puppeteer([
'log_browser_console' => $shoulLogBrowserConsole,
'logger' => $this->loggerMock(
$this->at(9),
$this->isLogLevel(),
$this->stringStartsWith($startsWith)
),
]);

$this->browser = $puppeteer->launch($this->browserOptions);
$pageFactory($this->browser)->goto($this->url);
}
$browserLogOccured = false;
$logger = $this->createBrowserLogger(function () use (&$browserLogOccured) {
$browserLogOccured = true;
});

$puppeteer = new Puppeteer([
'log_browser_console' => true,
'logger' => $logger,
]);

$this->browser = $puppeteer->launch($this->browserOptions);
$this->browser->pages()[0]->goto($this->url);

static::assertTrue($browserLogOccured);
}

/**
* @test
* @dontPopulateProperties browser
*/
public function browser_console_calls_are_not_logged_if_disabled()
{
$browserLogOccured = false;
$logger = $this->createBrowserLogger(function () use (&$browserLogOccured) {
$browserLogOccured = true;
});

$puppeteer = new Puppeteer([
'log_browser_console' => false,
'logger' => $logger,
]);

$this->browser = $puppeteer->launch($this->browserOptions);
$this->browser->pages()[0]->goto($this->url);

static::assertFalse($browserLogOccured);
}
}
22 changes: 0 additions & 22 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,6 @@ public function canPopulateProperty(string $propertyName): bool
return !in_array($propertyName, $this->dontPopulateProperties);
}

public function loggerMock($expectations) {
$loggerMock = $this->getMockBuilder(Logger::class)
->setConstructorArgs(['rialto'])
->setMethods(['log'])
->getMock();

if ($expectations instanceof Invocation) {
$expectations = [func_get_args()];
}

foreach ($expectations as $expectation) {
[$matcher] = $expectation;
$with = array_slice($expectation, 1);

$loggerMock->expects($matcher)
->method('log')
->with(...$with);
}

return $loggerMock;
}

public function isLogLevel(): Callback {
$psrLogLevels = (new ReflectionClass(LogLevel::class))->getConstants();
$monologLevels = (new ReflectionClass(Logger::class))->getConstants();
Expand Down

0 comments on commit 7779e00

Please sign in to comment.