diff --git a/src/Browser/Browser.php b/src/Browser/Browser.php index f80b97d..1adb323 100644 --- a/src/Browser/Browser.php +++ b/src/Browser/Browser.php @@ -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; diff --git a/src/Browser/BrowserBuilder.php b/src/Browser/BrowserBuilder.php index d77bfe3..9901687 100644 --- a/src/Browser/BrowserBuilder.php +++ b/src/Browser/BrowserBuilder.php @@ -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, ) { } diff --git a/src/Browser/BrowserContext.php b/src/Browser/BrowserContext.php index 55a4d71..8c63f98 100644 --- a/src/Browser/BrowserContext.php +++ b/src/Browser/BrowserContext.php @@ -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); diff --git a/src/Page/Page.php b/src/Page/Page.php index a794b43..9fd6879 100644 --- a/src/Page/Page.php +++ b/src/Page/Page.php @@ -76,8 +76,6 @@ final class Page implements PageInterface, EventDispatcherInterface private PageEventHandlerInterface $eventHandler; - private LoggerInterface $logger; - private ?APIRequestContextInterface $apiRequestContext = null; private bool $isClosed = false; @@ -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(); @@ -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 diff --git a/src/PlaywrightFactory.php b/src/PlaywrightFactory.php index e03a601..73d3480 100644 --- a/src/PlaywrightFactory.php +++ b/src/PlaywrightFactory.php @@ -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); } } diff --git a/src/Testing/PlaywrightTestCaseTrait.php b/src/Testing/PlaywrightTestCaseTrait.php index 37cf21e..22ed27c 100644 --- a/src/Testing/PlaywrightTestCaseTrait.php +++ b/src/Testing/PlaywrightTestCaseTrait.php @@ -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 @@ -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; @@ -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)) { @@ -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); diff --git a/tests/Integration/Browser/BrowserBuilderTest.php b/tests/Integration/Browser/BrowserBuilderTest.php index 8a02171..02ff2fd 100644 --- a/tests/Integration/Browser/BrowserBuilderTest.php +++ b/tests/Integration/Browser/BrowserBuilderTest.php @@ -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; @@ -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] @@ -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); diff --git a/tests/Integration/Page/PdfIntegrationTest.php b/tests/Integration/Page/PdfIntegrationTest.php index d10f931..11d1fca 100644 --- a/tests/Integration/Page/PdfIntegrationTest.php +++ b/tests/Integration/Page/PdfIntegrationTest.php @@ -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' diff --git a/tests/Integration/Page/ScreenshotIntegrationTest.php b/tests/Integration/Page/ScreenshotIntegrationTest.php index 5cad4eb..183d500 100644 --- a/tests/Integration/Page/ScreenshotIntegrationTest.php +++ b/tests/Integration/Page/ScreenshotIntegrationTest.php @@ -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' diff --git a/tests/Integration/PlaywrightFactoryTest.php b/tests/Integration/PlaywrightFactoryTest.php index ecd4bd2..7d827f6 100644 --- a/tests/Integration/PlaywrightFactoryTest.php +++ b/tests/Integration/PlaywrightFactoryTest.php @@ -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); } diff --git a/tests/Unit/Browser/BrowserContextDeleteCookieTest.php b/tests/Unit/Browser/BrowserContextDeleteCookieTest.php index ddab5ad..2e5240a 100644 --- a/tests/Unit/Browser/BrowserContextDeleteCookieTest.php +++ b/tests/Unit/Browser/BrowserContextDeleteCookieTest.php @@ -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)] @@ -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'); diff --git a/tests/Unit/Browser/BrowserContextPopupPagesTest.php b/tests/Unit/Browser/BrowserContextPopupPagesTest.php index 580e385..ab913c1 100644 --- a/tests/Unit/Browser/BrowserContextPopupPagesTest.php +++ b/tests/Unit/Browser/BrowserContextPopupPagesTest.php @@ -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)] @@ -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()); diff --git a/tests/Unit/Page/FramePageTest.php b/tests/Unit/Page/FramePageTest.php index c739786..4990daa 100644 --- a/tests/Unit/Page/FramePageTest.php +++ b/tests/Unit/Page/FramePageTest.php @@ -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; @@ -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 diff --git a/tests/Unit/Page/PageEvaluateNormalizerTest.php b/tests/Unit/Page/PageEvaluateNormalizerTest.php index eb166bd..a3b4362 100644 --- a/tests/Unit/Page/PageEvaluateNormalizerTest.php +++ b/tests/Unit/Page/PageEvaluateNormalizerTest.php @@ -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; @@ -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); } @@ -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); } diff --git a/tests/Unit/Page/PagePauseTest.php b/tests/Unit/Page/PagePauseTest.php index 595f19b..5d44e55 100644 --- a/tests/Unit/Page/PagePauseTest.php +++ b/tests/Unit/Page/PagePauseTest.php @@ -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; @@ -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'); } diff --git a/tests/Unit/Page/PagePdfTest.php b/tests/Unit/Page/PagePdfTest.php index 3069079..9fc01a9 100644 --- a/tests/Unit/Page/PagePdfTest.php +++ b/tests/Unit/Page/PagePdfTest.php @@ -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); @@ -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.'); diff --git a/tests/Unit/Page/PagePopupTest.php b/tests/Unit/Page/PagePopupTest.php index bd843f4..12a0dcf 100644 --- a/tests/Unit/Page/PagePopupTest.php +++ b/tests/Unit/Page/PagePopupTest.php @@ -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; @@ -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 diff --git a/tests/Unit/Page/PageRequestTest.php b/tests/Unit/Page/PageRequestTest.php index 421274e..d557955 100644 --- a/tests/Unit/Page/PageRequestTest.php +++ b/tests/Unit/Page/PageRequestTest.php @@ -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; @@ -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(); diff --git a/tests/Unit/Page/PageTest.php b/tests/Unit/Page/PageTest.php index e0e6862..e9021ea 100644 --- a/tests/Unit/Page/PageTest.php +++ b/tests/Unit/Page/PageTest.php @@ -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; @@ -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 diff --git a/tests/Unit/PlaywrightFactoryTest.php b/tests/Unit/PlaywrightFactoryTest.php index cb15df5..aaa8823 100644 --- a/tests/Unit/PlaywrightFactoryTest.php +++ b/tests/Unit/PlaywrightFactoryTest.php @@ -26,25 +26,19 @@ final class PlaywrightFactoryTest extends TestCase { public function testCreateWithDefaultConfig(): void { - $factory = new PlaywrightFactory(); - - $client = $factory->create(); - - $this->assertInstanceOf(PlaywrightClient::class, $client); + $this->assertInstanceOf(PlaywrightClient::class, PlaywrightFactory::create()); } public function testCreateWithCustomConfig(): void { $config = new PlaywrightConfig( nodePath: '/usr/bin/node', - timeoutMs: 60000, headless: false, + timeoutMs: 60000, tracingEnabled: true ); - $factory = new PlaywrightFactory(); - - $client = $factory->create($config); + $client = PlaywrightFactory::create($config); $this->assertInstanceOf(PlaywrightClient::class, $client); } @@ -52,9 +46,8 @@ public function testCreateWithCustomConfig(): void public function testCreateWithLogger(): void { $logger = $this->createMock(LoggerInterface::class); - $factory = new PlaywrightFactory(); - $client = $factory->create(null, $logger); + $client = PlaywrightFactory::create(logger: $logger); $this->assertInstanceOf(PlaywrightClient::class, $client); } @@ -63,15 +56,14 @@ public function testCreateWithConfigAndLogger(): void { $config = new PlaywrightConfig( nodePath: '/opt/node/bin/node', - timeoutMs: 45000, headless: true, + timeoutMs: 45000, tracingEnabled: false ); $logger = $this->createMock(LoggerInterface::class); - $factory = new PlaywrightFactory(); - $client = $factory->create($config, $logger); + $client = PlaywrightFactory::create($config, $logger); $this->assertInstanceOf(PlaywrightClient::class, $client); }