Skip to content

Commit

Permalink
change to withExpiredCookie
Browse files Browse the repository at this point in the history
  • Loading branch information
Joris Vaesen committed Aug 3, 2017
1 parent 862c12f commit c5dda9c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Http/Cookie/Cookie.php
Expand Up @@ -510,7 +510,7 @@ public function withNeverExpire()
public function withExpired()
{
$new = clone $this;
$new->expiresAt = Chronos::parse('-1 year');
$new->expiresAt = Chronos::createFromTimestamp(1);

return $new;
}
Expand Down
19 changes: 8 additions & 11 deletions src/Http/Response.php
Expand Up @@ -2047,8 +2047,6 @@ public function withCookie($name, $data = '')
* ### Options
*
* - `name`: The Cookie name
* - `value`: Value of the cookie
* - `expire`: Time the cookie expires in
* - `path`: Path the cookie applies to
* - `domain`: Domain the cookie is for.
* - `secure`: Is the cookie https?
Expand All @@ -2058,26 +2056,25 @@ public function withCookie($name, $data = '')
*
* ```
* // set scalar value with defaults
* $response = $response->withoutCookie('remember_me');
* $response = $response->withExpiredCookie('remember_me');
*
* // customize cookie attributes
* $response = $response->withoutCookie('remember_me', ['path' => '/login']);
* $response = $response->withExpiredCookie('remember_me', ['path' => '/login']);
*
* // add a cookie object
* $response = $response->withoutCookie(new Cookie('remember_me', 'deleted'));
* $response = $response->withExpiredCookie(new Cookie('remember_me'));
* ```
*
* @param string|\Cake\Http\Cookie\Cookie $name The name of the cookie to expire, or a cookie object
* @param string|\Cake\Http\Cookie\CookieInterface $name The name of the cookie to expire, or a cookie object
* @param array $options An array of cookie options.
* @return static
*/
public function withoutCookie($name, $options = [])
public function withExpiredCookie($name, $options = [])
{
if ($name instanceof Cookie) {
$cookie = $name->withExpiry(new Time(1));
if ($name instanceof CookieInterface) {
$cookie = $name->withExpired();
} else {
$options += [
'value' => '',
'path' => '/',
'domain' => '',
'secure' => false,
Expand All @@ -2086,7 +2083,7 @@ public function withoutCookie($name, $options = [])

$cookie = new Cookie(
$name,
$options['value'],
'',
new Time(1),
$options['path'],
$options['domain'],
Expand Down
3 changes: 1 addition & 2 deletions tests/TestCase/Http/Cookie/CookieTest.php
Expand Up @@ -298,8 +298,7 @@ public function testWithExpired()
$this->assertNotSame($new, $cookie, 'Should clone');
$this->assertNotContains('expiry', $cookie->toHeaderValue());

$now = Chronos::parse('-1 year');
$this->assertContains($now->format('Y'), $new->toHeaderValue());
$this->assertContains('01-Jan-1970', $new->toHeaderValue());
}

/**
Expand Down
13 changes: 7 additions & 6 deletions tests/TestCase/Http/ResponseTest.php
Expand Up @@ -1491,19 +1491,19 @@ public function testWithCookieObject()
$this->assertSame($cookie, $new->getCookieCollection()->get('yay'));
}

public function testWithoutCookieScalar()
public function testWithExpiredCookieScalar()
{
$response = new Response();
$response = $response->withCookie('testing', 'abc123');
$this->assertEquals('abc123', $response->getCookie('testing')['value']);

$new = $response->withoutCookie('testing');
$new = $response->withExpiredCookie('testing');

$this->assertNull($response->getCookie('testing')['expire']);
$this->assertEquals('1', $new->getCookie('testing')['expire']);
}

public function testWithoutCookieOptions()
public function testWithExpiredCookieOptions()
{
$options = [
'name' => 'testing',
Expand All @@ -1519,20 +1519,21 @@ public function testWithoutCookieOptions()
$response = $response->withCookie('testing', $options);
$this->assertEquals($options, $response->getCookie('testing'));

$new = $response->withoutCookie('testing', $options);
$new = $response->withExpiredCookie('testing', $options);

$this->assertEquals($options['expire'], $response->getCookie('testing')['expire']);
$this->assertEquals('1', $new->getCookie('testing')['expire']);
$this->assertEquals('', $new->getCookie('testing')['value']);
}

public function testWithoutCookieObject()
public function testWithExpiredCookieObject()
{
$response = new Response();
$cookie = new Cookie('yay', 'a value');
$response = $response->withCookie($cookie);
$this->assertEquals('a value', $response->getCookie('yay')['value']);

$new = $response->withoutCookie($cookie);
$new = $response->withExpiredCookie($cookie);

$this->assertNull($response->getCookie('yay')['expire']);
$this->assertEquals('1', $new->getCookie('yay')['expire']);
Expand Down

0 comments on commit c5dda9c

Please sign in to comment.