Commit 7799d90f authored by Thorsten Buss's avatar Thorsten Buss

* rename Adapter::keys() to keysWithPrefix(), to get PHP5.3.8 compatibility

* FIX: change Adapter::getUrl in downloadFile to self::getUrl(), so download_url worked
* add Repository::getConfig() to get the options for adapter
* add some force params
* add reset() to some test, to prevent the RepositoryAlreadyException
* FIX: testGetUrl() to work with UrlAware and normal Repos with download_url option
parent e8f20492
......@@ -43,9 +43,10 @@ interface Adapter extends \Gaufrette\Adapter {
* Returns an array of all keys (files and directories)
*
* @param null $prefix
* @param null $withDirectories show directories?
* @return array
*/
public function keys($prefix=null, $withDirectories=false);
public function keysWithPrefix($prefix, $withDirectories=false);
/**
* Returns the MimeType of the given Key
......
......@@ -172,9 +172,9 @@ class AdapterAmazonS3 extends AmazonS3 implements Adapter {
/**
* Add Directory-Prefix and remove fullPath and emptyDirLine from output for similar response to local/ftp etc
* {@inheritDoc}
* @deprecated
* @deprecated not fully compatible result @todo
*/
public function keys($prefix=null, $withDirectories=false) {
public function keysWithPrefix($prefix, $withDirectories=false) {
//@todo: implement $prefix and $withDirectories
$this->ensureBucketExists();
......
......@@ -219,7 +219,7 @@ class AdapterAmazonS3Ver2 extends AmazonS3 implements GaufretteAdapter, UrlAware
* Add Directory-Prefix and remove fullPath and emptyDirLine from output for similar response to local/ftp etc
* {@inheritDoc}
*/
public function keys($prefix = null, $withDirectories = false) {
public function keysWithPrefix($prefix = null, $withDirectories = false) {
$this->ensureBucketExists();
// add slash to beginning and remove from end
......
......@@ -97,7 +97,7 @@ class AdapterFtp extends Ftp implements Adapter {
* Add Directory-Prefix and remove fullPath and emptyDirLine from output for similar response to local/ftp etc
* {@inheritDoc}
*/
public function keys($prefix = null, $withDirectories = false) {
public function keysWithPrefix($prefix, $withDirectories = false) {
return $this->fetchKeys($prefix, $withDirectories);
}
......
......@@ -90,7 +90,7 @@ class AdapterLocal extends Local implements Adapter {
/**
* {@inheritDoc}
*/
public function keys($prefix=null, $withDirectories=false) {
public function keysWithPrefix($prefix, $withDirectories=false) {
$path = $prefix;
if (substr($path, -1) != '/') {
......
......@@ -86,7 +86,7 @@ class AdapterSftp extends Sftp implements Adapter {
/**
* {@inheritDoc}
*/
public function keys($prefix=null, $withDirectories=false) {
public function keysWithPrefix($prefix, $withDirectories=false) {
$this->initialize();
$path = $prefix;
......
......@@ -21,6 +21,11 @@ class Repository extends Filesystem {
*/
protected $adapter;
/**
* @var array settings of the adapter
*/
protected $config;
public function __construct($cfg) {
$this->config = $cfg;
......@@ -162,7 +167,7 @@ class Repository extends Filesystem {
* @return array
*/
public function keys($prefix = null, $withDirectories = false) {
return $this->adapter->keys($prefix, $withDirectories);
return $this->adapter->keysWithPrefix($prefix, $withDirectories);
}
/**
......@@ -201,7 +206,7 @@ class Repository extends Filesystem {
$options['filename'] = $downloadFileName;
if ($contentType !== null)
$options['content_type'] = $contentType;
$url = $this->adapter->getUrl($key, null, $options);
$url = $this->getUrl($key, null, $options);
header('Location: ' . $url);
} else {
$file = $this->get($key, false);
......@@ -258,7 +263,7 @@ class Repository extends Filesystem {
if (empty($url) && isset($this->config['download_url']))
$url = $this->config['download_url'];
if (empty($url))
throw new \RuntimeException('adapter dont support UrlAware - $downlodUrl needed');
throw new \RuntimeException('adapter dont support UrlAware - $download_url needed');
$url = str_replace('{FULL_PATH}', ltrim($key, '/'), $url);
$url = str_replace('{PATH}', ltrim(dirname($key), '/'), $url);
$url = str_replace('{FILENAME}', basename($key), $url);
......@@ -312,5 +317,12 @@ class Repository extends Filesystem {
return $numBytes;
}
/**
* return the adapter settings
* @return mixed
*/
public function getConfig($key = null, $default = null) {
return empty($key) ? $this->config : (@$this->config[$key] ?: $default);
}
}
......@@ -17,11 +17,13 @@ class RepositoryManager {
/**
* @param $id
* @param bool $force_create creat repo, even if is cached
* @return Repository
* @throws RepositoryNotFoundException
* @throws RepositoryTypeNotFoundException
*/
public static function getRepository($id) {
if (!array_key_exists($id, self::$repositories)) {
public static function getRepository($id, $force_create = false) {
if (!array_key_exists($id, self::$repositories) || $force_create) {
if (!array_key_exists($id, self::$config))
throw new RepositoryNotFoundException();
$cfg = self::$config[$id];
......@@ -67,23 +69,27 @@ class RepositoryManager {
/**
* @param $repositories array with repositoryConfigs
* @param bool $overwrite_existing_config
*/
public static function addRepositories($repositories) {
public static function addRepositories($repositories, $overwrite_existing_config = false) {
foreach ($repositories as $id => $repository) {
// ID entweder als Key in der Liste, oder als parameter "id"
if (isset($repository['id']))
$id = $repository['id'];
else
$repository['id'] = $id;
if (isset(self::$config[$id]) && !$overwrite_existing_config)
throw new RepositoryAlreadyExistsException('Repository with id '.$id.' already exists - use force param to overwrite');
self::$config[$id] = $repository;
}
}
/**
* @param $cfg Repository Config
* @param bool $overwrite_existing_config
*/
public static function addRepository($cfg) {
self::addRepositories(array($cfg));
public static function addRepository($cfg, $overwrite_existing_config = false) {
self::addRepositories(array($cfg), $overwrite_existing_config);
}
/**
......@@ -116,8 +122,10 @@ class RepositoryManager {
}
}
class InvalidResourceException extends \Exception {}
class NotFoundResourceException extends \Exception {}
class RepositoryNotFoundException extends \Exception {}
class RepositoryTypeNotFoundException extends \Exception {}
class ParamNotFoundException extends \Exception {}
class BnRepoException extends \Exception {}
class InvalidResourceException extends BnRepoException {}
class NotFoundResourceException extends BnRepoException {}
class RepositoryNotFoundException extends BnRepoException {}
class RepositoryAlreadyExistsException extends BnRepoException {}
class RepositoryTypeNotFoundException extends BnRepoException {}
class ParamNotFoundException extends BnRepoException {}
......@@ -4,6 +4,7 @@ namespace BNRepo\Tests\Repository;
use BNRepo\Repository\RepositoryManager;
use Symfony\Component\Yaml\Yaml;
/**
* Generated by PHPUnit_SkeletonGenerator 1.2.0 on 2013-04-13 at 13:34:33.
*/
......@@ -32,10 +33,12 @@ class RepositoryManagerTest extends \PHPUnit_Framework_TestCase {
*/
protected $object;
/**
* @covers BNRepo\Repository\RepositoryManager::getRepository
*/
public function testGetRepository() {
RepositoryManager::reset();
// Check LocalFS
RepositoryManager::addRepository($this->cfg['test']);
$local = RepositoryManager::getRepository('test');
......@@ -47,6 +50,7 @@ class RepositoryManagerTest extends \PHPUnit_Framework_TestCase {
* @covers BNRepo\Repository\RepositoryManager::importRepositoriesFromYamlFile
*/
public function testImportRepositoriesFromYamlFile() {
RepositoryManager::reset();
$yaml_string = Yaml::dump($this->cfg);
$file = self::DIR.'repositoryTest_'.uniqid().'.yaml';
file_put_contents($file, $yaml_string);
......@@ -63,8 +67,32 @@ class RepositoryManagerTest extends \PHPUnit_Framework_TestCase {
* @covers BNRepo\Repository\RepositoryManager::addRepository
*/
public function testAddRepository() {
RepositoryManager::reset();
RepositoryManager::addRepository($this->cfg['test']);
$this->assertEquals($this->cfg['test'], RepositoryManager::$config[$this->cfg['test']['id']]);
}
/**
* @covers BNRepo\Repository\RepositoryManager::addRepository
* @expectedException BNRepo\Repository\RepositoryAlreadyExistsException
*/
public function testAddRepositoryOverwriteFailed() {
RepositoryManager::reset();
RepositoryManager::addRepository($this->cfg['test']);
RepositoryManager::addRepository($this->cfg['test']);
}
/**
* @covers BNRepo\Repository\RepositoryManager::addRepository
*/
public function testAddRepositoryOverwriteForce() {
RepositoryManager::reset();
RepositoryManager::addRepository($this->cfg['test']);
$t = $this->cfg['test'];
$t['dir'] = '/tmp2';
RepositoryManager::addRepository($t, true);
$this->assertEquals($t, RepositoryManager::$config[$t['id']]);
}
}
......@@ -8,6 +8,7 @@
namespace BNRepo\Tests\Repository;
use BNRepo\Repository\Adapter\UrlAware;
use BNRepo\Repository\Repository;
use BNRepo\Repository\RepositoryManager;
use Symfony\Component\Yaml\Yaml;
......@@ -24,6 +25,7 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase {
protected $cfg_id;
public function __construct($name = NULL, array $data = array(), $dataName = '') {
RepositoryManager::reset();
parent::__construct($name, $data, $dataName);
$yml_file = __DIR__ . '/../../../../config/repositories.yml';
if (isset($_ENV['repositories']))
......@@ -51,7 +53,7 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase {
* @return Repository
*/
protected function repo() {
return RepositoryManager::getRepository($this->cfg_id);
return RepositoryManager::getRepository($this->cfg_id, true);
}
protected function tearDown() {
......@@ -68,6 +70,8 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase {
if ($repo->has('testFile.txt'))
$repo->delete('testFile.txt');
if ($repo->has('public.txt'))
$repo->delete('public.txt');
if ($repo->has('testImg.png'))
$repo->delete('testImg.png');
if ($repo->has('subdir/testFile.txt'))
......@@ -357,29 +361,52 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase {
}
protected function _testGetUrl(Repository $repo) {
$key = 'subdir/testfile.txt';
$key = 'subdir/testFile.txt';
$repo->write($key, $this->test_content);
$this->assertTrue($repo->has($key), 'check file exists');
$options = array();
$baseUrl = 'http://www.downloadtest.de/download/%s';
if (!$repo->getAdapter() instanceof UrlAware) {
$baseUrl = 'Test_%s_Test';
$downloadUrl = sprintf($baseUrl, basename($key));
$url = sprintf($baseUrl, '{FILENAME}');
$this->assertEquals($downloadUrl, $repo->getUrl($key, $url, $options), 'check DownloadUrl {FILENAME}');
// test url building
$downloadUrl = sprintf($baseUrl, basename($key));
$url = sprintf($baseUrl, '{FILENAME}');
$this->assertEquals($downloadUrl, $repo->getUrl($key, $url, $options), 'check DownloadUrl {FILENAME}');
$downloadUrl = sprintf($baseUrl, ltrim(dirname($key), '/'));
$url = sprintf($baseUrl, '{PATH}');
$this->assertEquals($downloadUrl, $repo->getUrl($key, $url, $options), 'check DownloadUrl {PATH}');
$downloadUrl = sprintf($baseUrl, ltrim(dirname($key), '/'));
$url = sprintf($baseUrl, '{PATH}');
$this->assertEquals($downloadUrl, $repo->getUrl($key, $url, $options), 'check DownloadUrl {PATH}');
$downloadUrl = sprintf($baseUrl, ltrim($key, '/'));
$url = sprintf($baseUrl, '{FULL_PATH}');
$this->assertEquals($downloadUrl, $repo->getUrl($key, $url, $options), 'check DownloadUrl {FULL_PATH}');
$downloadUrl = sprintf($baseUrl, ltrim($key, '/'));
$url = sprintf($baseUrl, '{FULL_PATH}');
$this->assertEquals($downloadUrl, $repo->getUrl($key, $url, $options), 'check DownloadUrl {FULL_PATH}');
$downloadUrl = sprintf($baseUrl, 'http');
$url = sprintf($baseUrl, '{SCHEME}');
$this->assertEquals($downloadUrl, $repo->getUrl($key, $url, $options), 'check DownloadUrl {SCHEME}');
if ($repo->getConfig('download_url')) {
$baseUrl = $repo->getConfig('download_url');
// download file
$local = self::DIR . 'trg.txt';
$url = $repo->getUrl($key, $baseUrl);
file_put_contents($local, file_get_contents($url));
$this->assertFileExists($local, 'Downloaded File exists');
$this->assertEquals($this->test_content, file_get_contents($local), 'Downloaded File Content ist Equal');
}
} else {
$local = self::DIR . 'trg.txt';
$url = $repo->getUrl($key);
file_put_contents($local, file_get_contents($url));
$this->assertFileExists($local, 'Downloaded File exists');
$this->assertEquals($this->test_content, file_get_contents($local), 'Downloaded File Content ist Equal');
}
$downloadUrl = sprintf($baseUrl, 'http');
$url = sprintf($baseUrl, '{SCHEME}');
$this->assertEquals($downloadUrl, $repo->getUrl($key, $url, $options), 'check DownloadUrl {SCHEME}');
}
protected function _testAppending(Repository $repo) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment