Skip to content
This repository has been archived by the owner on May 13, 2021. It is now read-only.

Commit

Permalink
Create separate fromString methods to prevent method call to CacheCon…
Browse files Browse the repository at this point in the history
…trol::fromString
  • Loading branch information
micheh committed Feb 17, 2016
1 parent 270de59 commit 25d045e
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Header/CacheControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class CacheControl
* @param string $string
* @return static
*/
public static function fromString($string)
protected static function createFromString($string)
{
$cacheControl = new static();

Expand Down
11 changes: 11 additions & 0 deletions src/Header/RequestCacheControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ class RequestCacheControl extends CacheControl
'only-if-cached' => 'withOnlyIfCached',
];

/**
* Create a new Request Cache-Control object from a header string.
*
* @param string $string
* @return static
*/
public static function fromString($string)
{
return static::createFromString($string);
}

/**
* Set how many seconds a stale representation is acceptable.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Header/ResponseCacheControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ class ResponseCacheControl extends CacheControl
'proxy-revalidate' => 'withProxyRevalidate',
];

/**
* Create a new Response Cache-Control object from a header string.
*
* @param string $string
* @return static
*/
public static function fromString($string)
{
return static::createFromString($string);
}

/**
* Set whether a response should be cached by shared caches. The method will automatically
* remove the private flag if it is set.
Expand Down
5 changes: 5 additions & 0 deletions test/Header/CacheControlStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class CacheControlStub extends CacheControl
'custom' => 'withCustom'
];

public static function createFromString($string)
{
return parent::createFromString($string);
}

public function withCustom($value)
{
return $value;
Expand Down
24 changes: 12 additions & 12 deletions test/Header/CacheControlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,61 +114,61 @@ public function testGetDirectiveWithoutValue()
}

/**
* @covers Micheh\Cache\Header\CacheControl::fromString
* @covers Micheh\Cache\Header\CacheControl::createFromString
* @covers Micheh\Cache\Header\CacheControl::getMethod
*/
public function testFromStringWithFlag()
{
$control = CacheControlStub::fromString('no-transform');
$control = CacheControlStub::createFromString('no-transform');
$this->assertAttributeSame(['no-transform' => true], 'directives', $control);
}

/**
* @covers Micheh\Cache\Header\CacheControl::fromString
* @covers Micheh\Cache\Header\CacheControl::createFromString
* @covers Micheh\Cache\Header\CacheControl::getMethod
*/
public function testFromStringWithToken()
{
$control = CacheControlStub::fromString('max-age=60');
$control = CacheControlStub::createFromString('max-age=60');
$this->assertAttributeSame(['max-age' => 60], 'directives', $control);
}

/**
* @covers Micheh\Cache\Header\CacheControl::fromString
* @covers Micheh\Cache\Header\CacheControl::createFromString
* @covers Micheh\Cache\Header\CacheControl::getMethod
*/
public function testFromStringWithMultiple()
{
$control = CacheControlStub::fromString('no-transform, max-age=100');
$control = CacheControlStub::createFromString('no-transform, max-age=100');
$this->assertAttributeSame(['no-transform' => true, 'max-age' => 100], 'directives', $control);
}

/**
* @covers Micheh\Cache\Header\CacheControl::fromString
* @covers Micheh\Cache\Header\CacheControl::createFromString
* @covers Micheh\Cache\Header\CacheControl::getMethod
*/
public function testFromStringWithOverrideMethod()
{
$this->assertSame('123', CacheControlStub::fromString('custom=123'));
$this->assertSame('123', CacheControlStub::createFromString('custom=123'));
}

/**
* @covers Micheh\Cache\Header\CacheControl::fromString
* @covers Micheh\Cache\Header\CacheControl::createFromString
* @covers Micheh\Cache\Header\CacheControl::getMethod
*/
public function testFromStringWithUnknownDirective()
{
$control = CacheControlStub::fromString('foo="bar"');
$control = CacheControlStub::createFromString('foo="bar"');
$this->assertAttributeSame(['foo' => 'bar'], 'directives', $control);
}

/**
* @covers Micheh\Cache\Header\CacheControl::fromString
* @covers Micheh\Cache\Header\CacheControl::createFromString
* @covers Micheh\Cache\Header\CacheControl::getMethod
*/
public function testFromStringWithUnknownDirectiveFlag()
{
$control = CacheControlStub::fromString('foo');
$control = CacheControlStub::createFromString('foo');
$this->assertAttributeSame([], 'directives', $control);
}

Expand Down
11 changes: 11 additions & 0 deletions test/Header/RequestCacheControlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MichehTest\Cache\Header;

use Micheh\Cache\Header\RequestCacheControl;

/**
* @author Michel Hunziker <[email protected]>
* @copyright Copyright (c) 2015, Michel Hunziker <[email protected]>
Expand All @@ -14,6 +16,15 @@ class RequestCacheControlTest extends CacheControlTestCase
*/
protected $controlClass = 'Micheh\Cache\Header\RequestCacheControl';

/**
* @covers Micheh\Cache\Header\RequestCacheControl::fromString
*/
public function testFromString()
{
$control = RequestCacheControl::fromString('max-age=100');
$this->assertInstanceOf($this->controlClass, $control);
}

/**
* @covers Micheh\Cache\Header\RequestCacheControl::withMaxStale
*/
Expand Down
9 changes: 9 additions & 0 deletions test/Header/ResponseCacheControlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ class ResponseCacheControlTest extends CacheControlTestCase
*/
protected $controlClass = 'Micheh\Cache\Header\ResponseCacheControl';

/**
* @covers Micheh\Cache\Header\ResponseCacheControl::fromString
*/
public function testFromString()
{
$control = ResponseCacheControl::fromString('max-age=100');
$this->assertInstanceOf($this->controlClass, $control);
}

/**
* @covers Micheh\Cache\Header\ResponseCacheControl::withPublic
* @covers Micheh\Cache\Header\ResponseCacheControl::withPublicPrivate
Expand Down

0 comments on commit 25d045e

Please sign in to comment.