|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the playwright-php/playwright package. |
| 7 | + * For the full copyright and license information, please view |
| 8 | + * the LICENSE file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace PlaywrightPHP\Tests\Unit\Browser; |
| 12 | + |
| 13 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use PlaywrightPHP\Browser\BrowserContext; |
| 16 | +use PlaywrightPHP\Transport\TransportInterface; |
| 17 | + |
| 18 | +#[CoversClass(BrowserContext::class)] |
| 19 | +final class BrowserContextDeleteCookieTest extends TestCase |
| 20 | +{ |
| 21 | + public function testDeleteCookieSendsAddCookiesWithExpiry(): void |
| 22 | + { |
| 23 | + $transport = $this->createMock(TransportInterface::class); |
| 24 | + |
| 25 | + $calledAdd = false; |
| 26 | + $that = $this; |
| 27 | + $transport->method('send')->willReturnCallback(function (array $payload) use (&$calledAdd, $that) { |
| 28 | + if (($payload['action'] ?? null) === 'context.cookies') { |
| 29 | + return [ |
| 30 | + 'cookies' => [ |
| 31 | + [ |
| 32 | + 'name' => 'foo', |
| 33 | + 'value' => '123', |
| 34 | + 'domain' => 'example.com', |
| 35 | + 'path' => '/', |
| 36 | + 'expires' => time() + 3600, |
| 37 | + 'httpOnly' => false, |
| 38 | + 'secure' => false, |
| 39 | + 'sameSite' => 'Lax', |
| 40 | + ], |
| 41 | + [ |
| 42 | + 'name' => 'bar', |
| 43 | + 'value' => 'x', |
| 44 | + 'domain' => 'example.com', |
| 45 | + 'path' => '/', |
| 46 | + 'expires' => time() + 3600, |
| 47 | + 'httpOnly' => false, |
| 48 | + 'secure' => false, |
| 49 | + 'sameSite' => 'Lax', |
| 50 | + ], |
| 51 | + ], |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | + if (($payload['action'] ?? null) === 'context.addCookies') { |
| 56 | + $that->assertArrayHasKey('cookies', $payload); |
| 57 | + $that->assertIsArray($payload['cookies']); |
| 58 | + $that->assertSame('foo', $payload['cookies'][0]['name']); |
| 59 | + $that->assertSame('example.com', $payload['cookies'][0]['domain']); |
| 60 | + $that->assertSame('/', $payload['cookies'][0]['path']); |
| 61 | + $that->assertSame(0, $payload['cookies'][0]['expires']); |
| 62 | + $calledAdd = true; |
| 63 | + |
| 64 | + return []; |
| 65 | + } |
| 66 | + |
| 67 | + return []; |
| 68 | + }); |
| 69 | + |
| 70 | + $context = new BrowserContext($transport, 'ctx'); |
| 71 | + $context->deleteCookie('foo'); |
| 72 | + |
| 73 | + $this->assertTrue($calledAdd, 'context.addCookies should be called to expire matching cookies'); |
| 74 | + } |
| 75 | +} |
0 commit comments