Skip to content

Commit

Permalink
improvement test coverage (#52472)
Browse files Browse the repository at this point in the history
  • Loading branch information
saMahmoudzadeh authored Aug 14, 2024
1 parent 6f7908f commit 0b85e15
Showing 1 changed file with 59 additions and 3 deletions.
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

0 comments on commit 0b85e15

Please sign in to comment.