|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the community-maintained Playwright PHP project. |
| 7 | + * It is not affiliated with or endorsed by Microsoft. |
| 8 | + * |
| 9 | + * (c) 2025-Present - Playwright PHP - https://github.com/playwright-php |
| 10 | + * |
| 11 | + * For the full copyright and license information, please view the LICENSE |
| 12 | + * file that was distributed with this source code. |
| 13 | + */ |
| 14 | + |
| 15 | +namespace Playwright\Tests\Unit; |
| 16 | + |
| 17 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 18 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 19 | +use PHPUnit\Framework\Attributes\Test; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use Playwright\Cookie; |
| 22 | + |
| 23 | +#[CoversClass(Cookie::class)] |
| 24 | +final class CookieTest extends TestCase |
| 25 | +{ |
| 26 | + #[DataProvider('validCookieProvider')] |
| 27 | + #[Test] |
| 28 | + public function itCreatesFromArrayValidCookie(array $cookieData, int|float $expires): void |
| 29 | + { |
| 30 | + $cookie = Cookie::fromArray($cookieData); |
| 31 | + |
| 32 | + $this->assertSame('test', $cookie->name); |
| 33 | + $this->assertSame('value', $cookie->value); |
| 34 | + $this->assertSame('example.com', $cookie->domain); |
| 35 | + $this->assertSame('/', $cookie->path); |
| 36 | + $this->assertSame($expires, $cookie->expires); |
| 37 | + $this->assertTrue($cookie->httpOnly); |
| 38 | + $this->assertTrue($cookie->secure); |
| 39 | + $this->assertSame('Strict', $cookie->sameSite); |
| 40 | + } |
| 41 | + |
| 42 | + public static function validCookieProvider(): iterable |
| 43 | + { |
| 44 | + yield 'expires with int' => [ |
| 45 | + [ |
| 46 | + 'name' => 'test', |
| 47 | + 'value' => 'value', |
| 48 | + 'domain' => 'example.com', |
| 49 | + 'path' => '/', |
| 50 | + 'expires' => 3600, |
| 51 | + 'httpOnly' => true, |
| 52 | + 'secure' => true, |
| 53 | + 'sameSite' => 'Strict', |
| 54 | + ], |
| 55 | + 3600, |
| 56 | + ]; |
| 57 | + |
| 58 | + yield 'expires with float' => [ |
| 59 | + [ |
| 60 | + 'name' => 'test', |
| 61 | + 'value' => 'value', |
| 62 | + 'domain' => 'example.com', |
| 63 | + 'path' => '/', |
| 64 | + 'expires' => 3600.5, |
| 65 | + 'httpOnly' => true, |
| 66 | + 'secure' => true, |
| 67 | + 'sameSite' => 'Strict', |
| 68 | + ], |
| 69 | + 3600.5, |
| 70 | + ]; |
| 71 | + } |
| 72 | + |
| 73 | + #[DataProvider('invalidCookieProvider')] |
| 74 | + #[Test] |
| 75 | + public function itFailsToCreateFromArrayInvalidCookie(array $cookieData): void |
| 76 | + { |
| 77 | + $this->expectException(\InvalidArgumentException::class); |
| 78 | + |
| 79 | + Cookie::fromArray($cookieData); |
| 80 | + } |
| 81 | + |
| 82 | + public static function invalidCookieProvider(): iterable |
| 83 | + { |
| 84 | + yield 'Invalid cookie data structure' => [['invalid' => 'data']]; |
| 85 | + yield 'Invalid cookie expires as random string' => [['name' => 'test', 'value' => 'value', 'domain' => 'example.com', 'path' => '/', 'expires' => 'invalid', 'httpOnly' => true, 'secure' => true, 'sameSite' => 'Strict']]; |
| 86 | + yield 'Invalid cookie expires as string' => [['name' => 'test', 'value' => 'value', 'domain' => 'example.com', 'path' => '/', 'expires' => '123', 'httpOnly' => true, 'secure' => true, 'sameSite' => 'Strict']]; |
| 87 | + yield 'Invalid cookie sameSite' => [['name' => 'test', 'value' => 'value', 'domain' => 'example.com', 'path' => '/', 'expires' => 123, 'httpOnly' => true, 'secure' => true, 'sameSite' => 'Random']]; |
| 88 | + } |
| 89 | + |
| 90 | + #[Test] |
| 91 | + public function itReturnsValidCookieArray(): void |
| 92 | + { |
| 93 | + $cookie = Cookie::fromArray([ |
| 94 | + 'name' => 'test', |
| 95 | + 'value' => 'value', |
| 96 | + 'domain' => 'example.com', |
| 97 | + 'path' => '/', |
| 98 | + 'expires' => 3600, |
| 99 | + 'httpOnly' => true, |
| 100 | + 'secure' => true, |
| 101 | + 'sameSite' => 'Strict', |
| 102 | + ]); |
| 103 | + |
| 104 | + $this->assertSame( |
| 105 | + [ |
| 106 | + 'name' => 'test', |
| 107 | + 'value' => 'value', |
| 108 | + 'domain' => 'example.com', |
| 109 | + 'path' => '/', |
| 110 | + 'expires' => 3600, |
| 111 | + 'httpOnly' => true, |
| 112 | + 'secure' => true, |
| 113 | + 'sameSite' => 'Strict', |
| 114 | + ], |
| 115 | + $cookie->toArray() |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments