Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Browser/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(
private readonly string $browserId,
private readonly string $defaultContextId,
private readonly string $version,
private readonly ?PlaywrightConfig $config = null,
private readonly PlaywrightConfig $config,
) {
$this->defaultContext = new BrowserContext($this->transport, $this->defaultContextId, $this->config);
$this->contexts[] = $this->defaultContext;
Expand Down
2 changes: 1 addition & 1 deletion src/Browser/BrowserBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(
private readonly string $browserType,
private readonly TransportInterface $transport,
private readonly LoggerInterface $logger,
private readonly ?PlaywrightConfig $config = null,
private readonly PlaywrightConfig $config,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Browser/BrowserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final class BrowserContext implements BrowserContextInterface, EventDispatcherIn
public function __construct(
private readonly TransportInterface $transport,
private readonly string $contextId,
private readonly ?PlaywrightConfig $config = null,
private readonly PlaywrightConfig $config,
) {
if (method_exists($this->transport, 'addEventDispatcher')) {
$this->transport->addEventDispatcher($this->contextId, $this);
Expand Down
14 changes: 3 additions & 11 deletions src/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ final class Page implements PageInterface, EventDispatcherInterface

private PageEventHandlerInterface $eventHandler;

private LoggerInterface $logger;

private ?APIRequestContextInterface $apiRequestContext = null;

private bool $isClosed = false;
Expand All @@ -91,11 +89,9 @@ public function __construct(
private readonly TransportInterface $transport,
private readonly BrowserContextInterface $context,
private readonly string $pageId,
private readonly ?PlaywrightConfig $config = null,
?LoggerInterface $logger = null,
private readonly PlaywrightConfig $config,
private readonly LoggerInterface $logger = new NullLogger(),
) {
$this->logger = $logger ?? new NullLogger();

$this->keyboard = new Keyboard($this->transport, $this->pageId);
$this->mouse = new Mouse($this->transport, $this->pageId);
$this->eventHandler = new PageEventHandler();
Expand Down Expand Up @@ -440,11 +436,7 @@ public function pdfContent(array|PdfOptions $options = []): string
*/
private function getScreenshotDirectory(): string
{
if (null !== $this->config) {
return $this->config->getScreenshotDirectory();
}

return rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'playwright';
return $this->config->getScreenshotDirectory();
}

private function getPdfDirectory(): string
Expand Down
12 changes: 3 additions & 9 deletions src/PlaywrightFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,9 @@
class PlaywrightFactory
{
public static function create(
?PlaywrightConfig $config = null,
?LoggerInterface $logger = null,
PlaywrightConfig $config = new PlaywrightConfig(),
LoggerInterface $logger = new NullLogger(),
): PlaywrightClient {
$config ??= new PlaywrightConfig();
$logger ??= new NullLogger();

$transportFactory = new TransportFactory();
$transport = $transportFactory->create($config, $logger);

return new PlaywrightClient($transport, $logger, $config);
return new PlaywrightClient((new TransportFactory())->create($config, $logger), $logger, $config);
}
}
21 changes: 14 additions & 7 deletions src/Testing/PlaywrightTestCaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Playwright\PlaywrightClient;
use Playwright\PlaywrightFactory;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Process\ExecutableFinder;

trait PlaywrightTestCaseTrait
Expand All @@ -45,16 +46,22 @@ trait PlaywrightTestCaseTrait

private bool $traceThisTest = false;

protected function setUpPlaywright(?LoggerInterface $logger = null, ?PlaywrightConfig $customConfig = null): void
private static ?string $nodePath = null;

protected function setUpPlaywright(LoggerInterface $logger = new NullLogger(), ?PlaywrightConfig $customConfig = null): void
{
$logger = $this->resolveLogger($logger);

$node = (new ExecutableFinder())->find('node');
if (null === $node) {
self::markTestSkipped('Node.js executable not found.');
if (null === self::$nodePath) {
$node = (new ExecutableFinder())->find('node');
if (null === $node) {
self::markTestSkipped('Node.js executable not found.');
}

self::$nodePath = $node;
}

$config = $this->buildConfig($node, $customConfig);
$config = $this->buildConfig(self::$nodePath, $customConfig);

if (null !== $customConfig) {
$this->usingShared = false;
Expand Down Expand Up @@ -121,7 +128,7 @@ protected function expect(LocatorInterface|PageInterface $subject): ExpectInterf
return new ExpectDecorator(new Expect($subject), $this);
}

private function resolveLogger(?LoggerInterface $logger): ?LoggerInterface
private function resolveLogger(LoggerInterface $logger): LoggerInterface
{
$loggerUrl = $_SERVER['PLAYWRIGHT_PHP_TEST_LOGGER_URL'] ?? null;
if (is_string($loggerUrl)) {
Expand All @@ -140,7 +147,7 @@ private function buildConfig(string $node, ?PlaywrightConfig $custom): Playwrigh
return $custom->withNodePath($node);
}

private function initializeShared(PlaywrightConfig $config, ?LoggerInterface $logger): void
private function initializeShared(PlaywrightConfig $config, LoggerInterface $logger): void
{
if (null === self::$sharedPlaywright) {
self::$sharedPlaywright = PlaywrightFactory::create($config, $logger);
Expand Down
7 changes: 4 additions & 3 deletions tests/Integration/Browser/BrowserBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PHPUnit\Framework\TestCase;
use Playwright\Browser\Browser;
use Playwright\Browser\BrowserBuilder;
use Playwright\Configuration\PlaywrightConfig;
use Playwright\Transport\TransportInterface;
use Psr\Log\NullLogger;

Expand All @@ -33,7 +34,7 @@ public function setUp(): void
{
$this->transport = $this->createMock(TransportInterface::class);
$this->logger = new NullLogger();
$this->builder = new BrowserBuilder('chromium', $this->transport, $this->logger);
$this->builder = new BrowserBuilder('chromium', $this->transport, $this->logger, new PlaywrightConfig());
}

#[Test]
Expand Down Expand Up @@ -133,8 +134,8 @@ public function itCanLaunchBrowserWithOptions(): void
#[Test]
public function itWorksWithDifferentBrowserTypes(): void
{
$firefoxBuilder = new BrowserBuilder('firefox', $this->transport, $this->logger);
$webkitBuilder = new BrowserBuilder('webkit', $this->transport, $this->logger);
$firefoxBuilder = new BrowserBuilder('firefox', $this->transport, $this->logger, new PlaywrightConfig());
$webkitBuilder = new BrowserBuilder('webkit', $this->transport, $this->logger, new PlaywrightConfig());

$this->assertInstanceOf(BrowserBuilder::class, $firefoxBuilder);
$this->assertInstanceOf(BrowserBuilder::class, $webkitBuilder);
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Page/PdfIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function setUp(): void
headless: true
);

$this->setUpPlaywright(null, $config);
$this->setUpPlaywright(customConfig: $config);
$this->installRouteServer($this->page, [
'/invoice.html' => <<<'HTML'
<!DOCTYPE html>
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Page/ScreenshotIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function setUp(): void
headless: true
);

$this->setUpPlaywright(null, $config);
$this->setUpPlaywright(customConfig: $config);
$this->installRouteServer($this->page, [
'/index.html' => <<<'HTML'
<!DOCTYPE html>
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/PlaywrightFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function itCanCreateClientWithCustomConfig(): void
public function itCanCreateClientWithCustomLogger(): void
{
$logger = new NullLogger();
$client = PlaywrightFactory::create(null, $logger);
$client = PlaywrightFactory::create(logger: $logger);

$this->assertInstanceOf(PlaywrightClient::class, $client);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Browser/BrowserContextDeleteCookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Playwright\Browser\BrowserContext;
use Playwright\Configuration\PlaywrightConfig;
use Playwright\Transport\TransportInterface;

#[CoversClass(BrowserContext::class)]
Expand Down Expand Up @@ -71,7 +72,7 @@ public function testDeleteCookieSendsAddCookiesWithExpiry(): void
return [];
});

$context = new BrowserContext($transport, 'ctx');
$context = new BrowserContext($transport, 'ctx', new PlaywrightConfig());
$context->deleteCookie('foo');

$this->assertTrue($calledAdd, 'context.addCookies should be called to expire matching cookies');
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Browser/BrowserContextPopupPagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Playwright\Browser\BrowserContext;
use Playwright\Configuration\PlaywrightConfig;
use Playwright\Transport\TransportInterface;

#[CoversClass(BrowserContext::class)]
Expand All @@ -25,7 +26,7 @@ final class BrowserContextPopupPagesTest extends TestCase
public function testTracksPopupPagesViaEvents(): void
{
$transport = $this->createMock(TransportInterface::class);
$context = new BrowserContext($transport, 'ctx1');
$context = new BrowserContext($transport, 'ctx1', new PlaywrightConfig());

$this->assertCount(0, $context->pages());

Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Page/FramePageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Playwright\Browser\BrowserContextInterface;
use Playwright\Configuration\PlaywrightConfig;
use Playwright\Page\Page;
use Playwright\Page\PageInterface;
use Playwright\Transport\TransportInterface;
Expand All @@ -37,7 +38,7 @@ protected function setUp(): void

private function createPage(): PageInterface
{
return new Page($this->transport, $this->context, 'page-1');
return new Page($this->transport, $this->context, 'page-1', new PlaywrightConfig());
}

public function testMainFrame(): void
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/Page/PageEvaluateNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Playwright\Browser\BrowserContextInterface;
use Playwright\Configuration\PlaywrightConfig;
use Playwright\Page\Page;
use Playwright\Transport\TransportInterface;

Expand All @@ -37,7 +38,7 @@ public function testNormalizesReturnBodyToFunction(): void
}))
->willReturn(['result' => 42]);

$page = new Page($transport, $context, 'p1');
$page = new Page($transport, $context, 'p1', new PlaywrightConfig());
$result = $page->evaluate('return 42;');
$this->assertSame(42, $result);
}
Expand All @@ -56,7 +57,7 @@ public function testLeavesPlainExpressionUntouched(): void
}))
->willReturn(['result' => 'Hello']);

$page = new Page($transport, $context, 'p1');
$page = new Page($transport, $context, 'p1', new PlaywrightConfig());
$result = $page->evaluate('document.title');
$this->assertSame('Hello', $result);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Page/PagePauseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Playwright\Browser\BrowserContextInterface;
use Playwright\Configuration\PlaywrightConfig;
use Playwright\Page\Page;
use Playwright\Transport\TransportInterface;

Expand All @@ -39,7 +40,7 @@ public function itSendsPauseCommand(): void
}))
->willReturn(['success' => true]);

$page = new Page($transport, $context, 'page_1');
$page = new Page($transport, $context, 'page_1', new PlaywrightConfig());
$page->pause();
$this->assertTrue(true, 'pause() should dispatch page.pause');
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Page/PagePdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testPdfUsesProvidedPath(): void
}))
->willReturn([]);

$page = new Page($transport, $context, 'page-unit');
$page = new Page($transport, $context, 'page-unit', new PlaywrightConfig());

$result = $page->pdf($expectedPath);

Expand Down Expand Up @@ -87,7 +87,7 @@ public function testPdfContentRejectsPathOption(): void
$transport = $this->createMock(TransportInterface::class);
$context = $this->createMock(BrowserContextInterface::class);

$page = new Page($transport, $context, 'page-unit');
$page = new Page($transport, $context, 'page-unit', new PlaywrightConfig());

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Do not provide a "path" option when requesting inline PDF content.');
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Page/PagePopupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Playwright\Browser\BrowserContextInterface;
use Playwright\Configuration\PlaywrightConfig;
use Playwright\Exception\TimeoutException;
use Playwright\Page\Page;
use Playwright\Transport\TransportInterface;
Expand All @@ -32,7 +33,7 @@ protected function setUp(): void
{
$this->transport = $this->createMock(TransportInterface::class);
$this->context = $this->createMock(BrowserContextInterface::class);
$this->page = new Page($this->transport, $this->context, 'page1');
$this->page = new Page($this->transport, $this->context, 'page1', new PlaywrightConfig());
}

public function testWaitForPopupSuccess(): void
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Page/PageRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPUnit\Framework\TestCase;
use Playwright\API\APIRequestContextInterface;
use Playwright\Browser\BrowserContextInterface;
use Playwright\Configuration\PlaywrightConfig;
use Playwright\Page\Page;
use Playwright\Transport\TransportInterface;

Expand All @@ -34,7 +35,7 @@ public function testRequestIsCached(): void
->method('request')
->willReturn($api);

$page = new Page($transport, $context, 'page-1');
$page = new Page($transport, $context, 'page-1', new PlaywrightConfig());

$first = $page->request();
$second = $page->request();
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Page/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Playwright\Browser\BrowserContextInterface;
use Playwright\Configuration\PlaywrightConfig;
use Playwright\Input\KeyboardInterface;
use Playwright\Input\MouseInterface;
use Playwright\Locator\Locator;
Expand All @@ -38,7 +39,7 @@ protected function setUp(): void
$context = $this->createMock(BrowserContextInterface::class);
$pageId = 'page-id';

$this->page = new Page($this->transport, $context, $pageId);
$this->page = new Page($this->transport, $context, $pageId, new PlaywrightConfig());
}

public function testGetKeyboard(): void
Expand Down
Loading