Add check to Store::unlock to ensure file exists

Fix indentation and shorten code
This commit is contained in:
Jonathan Ingram 2012-08-29 16:23:59 +10:00
parent 462f93ad12
commit a094f7e2a5
3 changed files with 31 additions and 1 deletions

View File

@ -86,10 +86,14 @@ class Store implements StoreInterface
* Releases the lock for the given Request. * Releases the lock for the given Request.
* *
* @param Request $request A Request instance * @param Request $request A Request instance
*
* @return Boolean False if the lock file does not exist or cannot be unlocked, true otherwise
*/ */
public function unlock(Request $request) public function unlock(Request $request)
{ {
return @unlink($this->getPath($this->getCacheKey($request).'.lck')); $file = $this->getPath($this->getCacheKey($request).'.lck');
return is_file($file) ? @unlink($file) : false;
} }
/** /**

View File

@ -66,6 +66,8 @@ interface StoreInterface
* Releases the lock for the given Request. * Releases the lock for the given Request.
* *
* @param Request $request A Request instance * @param Request $request A Request instance
*
* @return Boolean False if the lock file does not exist or cannot be unlocked, true otherwise
*/ */
public function unlock(Request $request); public function unlock(Request $request);

View File

@ -19,8 +19,19 @@ use Symfony\Component\HttpKernel\HttpCache\Store;
class StoreTest extends \PHPUnit_Framework_TestCase class StoreTest extends \PHPUnit_Framework_TestCase
{ {
/**
* @var Request
*/
protected $request; protected $request;
/**
* @var Response
*/
protected $response; protected $response;
/**
* @var Store
*/
protected $store; protected $store;
protected function setUp() protected function setUp()
@ -47,6 +58,19 @@ class StoreTest extends \PHPUnit_Framework_TestCase
$this->assertEmpty($this->getStoreMetadata('/nothing')); $this->assertEmpty($this->getStoreMetadata('/nothing'));
} }
public function testUnlockFileThatDoesExist()
{
$cacheKey = $this->storeSimpleEntry();
$this->store->lock($this->request);
$this->assertTrue($this->store->unlock($this->request));
}
public function testUnlockFileThatDoesNotExist()
{
$this->assertFalse($this->store->unlock($this->request));
}
public function testRemovesEntriesForKeyWithPurge() public function testRemovesEntriesForKeyWithPurge()
{ {
$request = Request::create('/foo'); $request = Request::create('/foo');