Skip to content

Commit 260812a

Browse files
committed
Fix tests
1 parent 4a0ab82 commit 260812a

File tree

4 files changed

+384
-2
lines changed

4 files changed

+384
-2
lines changed

tests/Unit/Locator/Options/GetByRoleOptionsTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,31 @@ public function testCanBeInstantiatedDirectly(): void
7474
$this->assertTrue($result['pressed']);
7575
$this->assertSame('disabled', $result['hasNotText']);
7676
}
77+
78+
public function testFromReturnsSameInstance(): void
79+
{
80+
$options = new GetByRoleOptions(checked: true);
81+
$this->assertSame($options, GetByRoleOptions::from($options));
82+
}
83+
84+
public function testFromArrayRejectsInvalidName(): void
85+
{
86+
$this->expectExceptionMessage('getByRole option "name" must be stringable.');
87+
GetByRoleOptions::from(['name' => []]);
88+
}
89+
90+
public function testFromArrayHandlesNullValues(): void
91+
{
92+
$options = GetByRoleOptions::from([
93+
'checked' => null,
94+
'level' => null,
95+
'name' => null,
96+
]);
97+
98+
$result = $options->toArray();
99+
100+
$this->assertArrayNotHasKey('checked', $result);
101+
$this->assertArrayNotHasKey('level', $result);
102+
$this->assertArrayNotHasKey('name', $result);
103+
}
77104
}

tests/Unit/Locator/Options/LocatorOptionsTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,48 @@ public function testFromArrayRejectsInvalidStrictFlag(): void
4747

4848
LocatorOptions::from(['strict' => 'yes']);
4949
}
50+
51+
public function testFromReturnsSameInstance(): void
52+
{
53+
$options = new LocatorOptions(strict: true);
54+
$this->assertSame($options, LocatorOptions::from($options));
55+
}
56+
57+
public function testFromArrayRejectsInvalidLocator(): void
58+
{
59+
$this->expectExceptionMessage('Locator option "has" must be a Locator instance or null.');
60+
LocatorOptions::from(['has' => 'invalid']);
61+
}
62+
63+
public function testFromArrayRejectsInvalidString(): void
64+
{
65+
$this->expectExceptionMessage('Locator option "hasText" must be stringable.');
66+
LocatorOptions::from(['hasText' => []]);
67+
}
68+
69+
public function testFromArrayHandlesNullValues(): void
70+
{
71+
$options = LocatorOptions::from([
72+
'has' => null,
73+
'hasText' => null,
74+
]);
75+
76+
$result = $options->toArray();
77+
78+
$this->assertArrayNotHasKey('has', $result);
79+
$this->assertArrayNotHasKey('hasText', $result);
80+
}
81+
82+
public function testFromArrayIgnoresNonStringKeys(): void
83+
{
84+
$options = LocatorOptions::from([
85+
'valid' => 'value',
86+
0 => 'ignored',
87+
]);
88+
89+
$result = $options->toArray();
90+
91+
$this->assertArrayHasKey('valid', $result);
92+
$this->assertArrayNotHasKey(0, $result);
93+
}
5094
}

tests/Unit/Page/PageEventHandlerTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,42 @@ public function testOnResponseRegistersResponseHandler(): void
129129
$this->assertTrue($wasCalled);
130130
$this->assertSame($response, $receivedResponse);
131131
}
132+
133+
public function testOnRequestFailedRegistersRequestFailedHandler(): void
134+
{
135+
$handler = new PageEventHandler();
136+
$wasCalled = false;
137+
$receivedRequest = null;
138+
139+
$request = new Request(['url' => 'https://example.com', 'method' => 'GET', 'headers' => [], 'resourceType' => 'document', 'failure' => 'net::ERR_FAILED']);
140+
141+
$handler->onRequestFailed(function (Request $r) use (&$wasCalled, &$receivedRequest) {
142+
$wasCalled = true;
143+
$receivedRequest = $r;
144+
});
145+
146+
$handler->publicEmit('requestfailed', [$request]);
147+
148+
$this->assertTrue($wasCalled);
149+
$this->assertSame($request, $receivedRequest);
150+
}
151+
152+
public function testOnRouteRegistersRouteHandler(): void
153+
{
154+
$handler = new PageEventHandler();
155+
$wasCalled = false;
156+
$receivedRoute = null;
157+
158+
$route = new \Playwright\Network\Route($this->transport, 'route1', ['url' => 'https://example.com']);
159+
160+
$handler->onRoute(function ($r) use (&$wasCalled, &$receivedRoute) {
161+
$wasCalled = true;
162+
$receivedRoute = $r;
163+
});
164+
165+
$handler->publicEmit('route', [$route]);
166+
167+
$this->assertTrue($wasCalled);
168+
$this->assertSame($route, $receivedRoute);
169+
}
132170
}

0 commit comments

Comments
 (0)