Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Add getFilename() to Zend\Cache\Pattern\CaptureCache #3747

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions library/Zend/Cache/Pattern/CaptureCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,23 @@ protected function createDirectoryStructure($pathname)

ErrorHandler::stop();
}

/**
* Returns the generated file name.
*
* @param null|string $pageId
* @return string
*/
public function getFilename($pageId = null)
{
if ($pageId === null) {
$pageId = $this->detectPageId();
}

$publicDir = $this->getOptions()->getPublicDir();
$path = $this->pageId2Path($pageId);
$file = $path . \DIRECTORY_SEPARATOR . $this->pageId2Filename($pageId);

return $publicDir . $file;
}
}
49 changes: 49 additions & 0 deletions tests/ZendTest/Cache/Pattern/CaptureCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ class CaptureCacheTest extends CommonPatternTest

protected $_tmpCacheDir;
protected $_umask;
protected $_bufferedServerSuperGlobal;

public function setUp()
{
$this->_bufferedServerSuperGlobal = $_SERVER;
$this->_umask = umask();

$this->_tmpCacheDir = @tempnam(sys_get_temp_dir(), 'zend_cache_test_');
Expand All @@ -51,6 +53,8 @@ public function setUp()

public function tearDown()
{
$_SERVER = $this->_bufferedServerSuperGlobal;

$this->_removeRecursive($this->_tmpCacheDir);

if ($this->_umask != umask()) {
Expand Down Expand Up @@ -129,4 +133,49 @@ public function testRemoveThrowsLogicExceptionOnMissingPublicDir()
$this->setExpectedException('Zend\Cache\Exception\LogicException');
$captureCache->remove('/pageId');
}

public function testGetFilenameWithoutPublicDir()
{
$captureCache = new Cache\Pattern\CaptureCache();

$this->assertEquals('/index.html', $captureCache->getFilename('/'));
$this->assertEquals('/dir1/test', $captureCache->getFilename('/dir1/test'));
$this->assertEquals('/dir1/test.html', $captureCache->getFilename('/dir1/test.html'));
$this->assertEquals('/dir1/dir2/test.html', $captureCache->getFilename('/dir1/dir2/test.html'));
}

public function testGetFilenameWithoutPublicDirAndNoPageId()
{
$_SERVER['REQUEST_URI'] = '/dir1/test.html';
$captureCache = new Cache\Pattern\CaptureCache();
$this->assertEquals('/dir1/test.html', $captureCache->getFilename());
}

public function testGetFilenameWithPublicDir()
{
$options = new Cache\Pattern\PatternOptions(array(
'public_dir' => $this->_tmpCacheDir
));

$captureCache = new Cache\Pattern\CaptureCache();
$captureCache->setOptions($options);

$this->assertEquals($this->_tmpCacheDir . '/index.html', $captureCache->getFilename('/'));
$this->assertEquals($this->_tmpCacheDir . '/dir1/test', $captureCache->getFilename('/dir1/test'));
$this->assertEquals($this->_tmpCacheDir . '/dir1/test.html', $captureCache->getFilename('/dir1/test.html'));
$this->assertEquals($this->_tmpCacheDir . '/dir1/dir2/test.html', $captureCache->getFilename('/dir1/dir2/test.html'));
}

public function testGetFilenameWithPublicDirAndNoPageId()
{
$_SERVER['REQUEST_URI'] = '/dir1/test.html';

$options = new Cache\Pattern\PatternOptions(array(
'public_dir' => $this->_tmpCacheDir
));
$captureCache = new Cache\Pattern\CaptureCache();
$captureCache->setOptions($options);

$this->assertEquals($this->_tmpCacheDir . '/dir1/test.html', $captureCache->getFilename());
}
}