Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Improve Cookie Testing Coverage #52472

Merged
merged 1 commit into from
Aug 14, 2024
Merged
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
62 changes: 59 additions & 3 deletions tests/Cookie/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Illuminate\Tests\Cookie;

use ArgumentCountError;
use Illuminate\Cookie\CookieJar;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use ReflectionObject;
use Symfony\Component\HttpFoundation\Cookie;
Expand Down Expand Up @@ -34,7 +36,7 @@ public function testCookiesAreCreatedWithProperOptions()
$this->assertTrue($c3->getExpiresTime() < time());
}

public function testCookiesAreCreatedWithProperOptionsUsingDefaultPathAndDomain()
public function testCookiesAreCreatedWithProperOptionsUsingDefaultPathAndDomain(): void
{
$cookie = $this->getCreator();
$cookie->setDefaultPathAndDomain('/path', '/domain', true, 'lax');
Expand All @@ -44,6 +46,7 @@ public function testCookiesAreCreatedWithProperOptionsUsingDefaultPathAndDomain(
$this->assertSame('/domain', $c->getDomain());
$this->assertSame('/path', $c->getPath());
$this->assertSame('lax', $c->getSameSite());
$this->assertTrue($c->isHttpOnly());
}

public function testCookiesCanSetSecureOptionUsingDefaultPathAndDomain()
Expand All @@ -58,7 +61,42 @@ public function testCookiesCanSetSecureOptionUsingDefaultPathAndDomain()
$this->assertSame('lax', $c->getSameSite());
}

public function testQueuedCookies()
public function testQueuedCookiesWithoutName(): void
{
$this->expectException(InvalidArgumentException::class);

$cookie = $this->getCreator();
$cookie->queue($cookie->make('', 'bar'));
}

public function testQueuedCookiesWithInvalidParameter(): void
{
$this->expectException(ArgumentCountError::class);

$cookie = $this->getCreator();
$cookie->queue('invalidCookie');
}

public function testQueuedCookiesWithHandlingEmptyValues(): void
{
$cookie = $this->getCreator();
$cookie->queue($cookie->make('foo', ''));
$this->assertTrue($cookie->hasQueued('foo'));
$this->assertEquals('', $cookie->queued('foo')->getValue());
}

public function testQueuedCookiesWithRepeatedValue(): void
{
$cookie = $this->getCreator();
$cookie->queue($cookie->make('foo', 'newBar'));
$this->assertTrue($cookie->hasQueued('foo'));
$this->assertEquals('newBar', $cookie->queued('foo')->getValue());

$this->expectException(ArgumentCountError::class);
$cookie->queue('invalidCookie');
}

public function testQueuedCookies(): void
{
$cookie = $this->getCreator();
$this->assertEmpty($cookie->getQueuedCookies());
Expand Down Expand Up @@ -95,9 +133,13 @@ public function testQueuedWithoutPath(): void
public function testHasQueued(): void
{
$cookieJar = $this->getCreator();
// test empty queue
$this->assertFalse($cookieJar->hasQueued('foo'));

$cookie = $cookieJar->make('foo', 'bar');
$cookieJar->queue($cookie);
$this->assertTrue($cookieJar->hasQueued('foo'));
$this->assertFalse($cookieJar->hasQueued('nonexistent'));
}

public function testHasQueuedWithPath(): void
Expand Down Expand Up @@ -128,14 +170,28 @@ public function testExpire()
$this->assertCount(1, $cookieJar->getQueuedCookies());
}

public function testUnqueue()
public function testUnqueue(): void
{
$cookie = $this->getCreator();

$cookie->unqueue('nonexistent');
$this->assertEmpty($cookie->getQueuedCookies());

$cookie->queue($cookie->make('foo', 'bar'));
$cookie->unqueue('foo');
$this->assertEmpty($cookie->getQueuedCookies());
}

public function testUnqueueMultipleCookies(): void
{
$cookie = $this->getCreator();
$cookie->queue($cookie->make('foo', 'bar'));
$cookie->queue($cookie->make('baz', 'qux'));
$cookie->unqueue('foo');
$this->assertTrue($cookie->hasQueued('baz'));
$this->assertFalse($cookie->hasQueued('foo'));
}

public function testUnqueueWithPath(): void
{
$cookieJar = $this->getCreator();
Expand Down