/**
  * replace asset
  * @param sfWebRequest $request
  */
 public function executeReplaceAsset(sfWebRequest $request)
 {
     $this->forward404Unless($request->isMethod(sfRequest::POST), 'method not allowed');
     $sfAsset = $request->getParameter('sf_asset');
     $asset = sfAssetTable::getInstance()->find($sfAsset['id']);
     $this->forward404Unless($asset, 'asset not found');
     $this->form = new sfAssetReplaceForm($asset);
     $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));
     if ($this->form->isValid()) {
         // physically replace asset
         $file = $this->form->getValue('file');
         $asset->destroy();
         $asset->create($file->getTempName(), false, false);
         $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $asset)));
         $this->getUser()->setFlash('notice', 'The file has been replaced');
     }
     return $this->redirect('@sf_asset_library_edit?id=' . $asset->getId());
 }
    $t->is($asset1->getFilename(), 'raikkonen.jpg', 'create() sets the object filename according to the given asset');
    $t->is($asset1->getFilesize(), 18, 'create() sets the object filesize according to the given asset');
    $t->ok($asset1->isImage(), 'create() sets the object type according to the given asset');
    $t->is($asset1->getUrl(), $folder1->getUrl() . '/' . $asset1->getFilename(), 'create() sets the object url according to the given asset and folder object');
    $t->ok(is_file($asset1->getFullPath()), 'create() physically copies asset');
    $t->ok(is_file($asset1->getFullPath('large')), 'create() physically creates thumbnail');
    $t->diag('sfAsset::move()');
    $old_path = $asset1->getFullPath();
    $old_thumb_path = $asset1->getFullPath('large');
    $asset1->move($root, 'raikkonen2.jpg');
    $t->is($asset1->getFilename(), 'raikkonen2.jpg', 'move() changes filename');
    $t->is($asset1->getUrl(), $root->getUrl() . '/' . $asset1->getFilename(), 'move() changes the object url according to the new folder');
    $t->ok(is_file($asset1->getFullPath()), 'move() physically moves asset to the new location');
    $t->ok(!is_file($old_path), 'move() physically removes asset from the previous location');
    $t->ok(is_file($asset1->getFullPath('large')), 'move() physically moves thumbnail to the new location');
    $t->ok(!is_file($old_thumb_path), 'move() physically removes thumbnail from the previous location');
    $t->diag('sfAsset::delete()');
    $old_path = $asset1->getFullPath();
    $old_thumb_path = $asset1->getFullPath('large');
    $old_id = $asset1->getId();
    $asset1->delete();
    $t->ok(!is_file($old_path), 'delete() physically removes asset');
    $t->ok(!is_file($old_thumb_path), 'delete() physically removes thumbnail');
    $null = sfAssetTable::getInstance()->find($old_id);
    $t->ok(!$null, 'delete() removes asset from DB');
} catch (Exception $e) {
    echo $e->getMessage() . PHP_EOL;
    echo $e->getTraceAsString() . PHP_EOL;
}
// reset DB
$con->rollBack();
/**
 * get input form field
 * @param  string  $name
 * @param  integer $value   possible value of asset id
 * @param  array   $options
 * @return string
 */
function input_sf_asset_image_tag($name, $value = null, $options = array())
{
    $url = str_replace('&', '&', url_for('@sf_asset_library_list?dir=' . sfConfig::get('app_sfAssetsLibrary_upload_dir', 'media') . '&popup=2'));
    $asset = empty($value) ? new sfAsset() : sfAssetTable::getInstance()->find($value);
    return '<a id="sf_asset_input_image" href="#" rel="{url: \'' . $url . '\', name: \'' . $name . '\', type: \'' . $options['type'] . '\'}">' . image_tag('/sfDoctrineAssetsLibraryPlugin/images/folder_open', array('alt' => 'Insert Image', 'title' => __('Insert Image', null, 'sfAsset'))) . '</a> ' . asset_image_tag($asset, 'small', array('id' => $options['id'] . '_img'));
}
include dirname(__FILE__) . '/../../../../test/bootstrap/functional.php';
include $configuration->getSymfonyLibDir() . '/vendor/lime/lime.php';
$databaseManager = new sfDatabaseManager($configuration);
$con = Doctrine::getConnectionByTableName('sfAsset');
$con->beginTransaction();
try {
    // prepare test environment
    sfAssetFolderTable::getInstance()->createQuery()->delete()->execute();
    sfAssetTable::getInstance()->createQuery()->delete()->execute();
    sfConfig::set('app_sfAssetsLibrary_upload_dir', 'mediaTEST');
    $f = new sfAssetFolder();
    $f->setName(sfConfig::get('app_sfAssetsLibrary_upload_dir'));
    $tree = sfAssetFolderTable::getInstance()->getTree();
    $tree->createRoot($f);
    $f->save();
    $t = new lime_test(5, array('options' => new lime_output_color(), 'error_reporting' => true));
    $t->diag('sfAssetPeer');
    $t->is(sfAssetTable::getInstance()->retrieveFromUrl(sfAssetFolderTable::getInstance()->getRoot()->getRelativePath() . '/filename.jpg'), null, 'sfAssetPeer::retrieveFromUrl() returns null when a URL is not found');
    $t->is(sfAssetTable::getInstance()->exists(sfAssetFolderTable::getInstance()->getRoot()->getId(), 'filename.jpg'), false, 'sfAssetPeer::exists() returns false when an asset is not found');
    $sfAsset = new sfAsset();
    $sfAsset->setFolder(sfAssetFolderTable::getInstance()->getRoot());
    $sfAsset->setFilename('filename.jpg');
    $sfAsset->save();
    $t->is(sfAssetTable::getInstance()->retrieveFromUrl(sfAssetFolderTable::getInstance()->getRoot()->getRelativePath() . '/filename.jpg')->getId(), $sfAsset->getId(), 'sfAssetPeer::retrieveFromUrl() finds an asset from its URL');
    $t->is(sfAssetTable::getInstance()->retrieveFromUrl($sfAsset->getUrl())->getId(), $sfAsset->getId(), 'sfAssetPeer::retrieveFromUrl() finds an asset from the result of `getUrl()`');
    $t->is(sfAssetTable::getInstance()->exists(sfAssetFolderTable::getInstance()->getRoot()->getId(), 'filename.jpg'), true, 'sfAssetPeer::exists() returns true when an asset is found');
} catch (Exception $e) {
    echo $e->getMessage();
}
// reset DB
$con->rollBack();
<?php

$app = 'frontend';
include dirname(__FILE__) . '/../../../../test/bootstrap/functional.php';
include $configuration->getSymfonyLibDir() . '/vendor/lime/lime.php';
$databaseManager = new sfDatabaseManager($configuration);
$con = Doctrine::getConnectionByTableName('sfAssetFolder');
$con->beginTransaction();
try {
    // prepare test environment
    sfAssetFolderTable::getInstance()->createQuery()->delete()->execute();
    sfAssetTable::getInstance()->createQuery()->delete()->execute();
    sfConfig::set('app_sfAssetsLibrary_upload_dir', 'mediaTEST');
    $f = new sfAssetFolder();
    $f->setName(sfConfig::get('app_sfAssetsLibrary_upload_dir'));
    $tree = sfAssetFolderTable::getInstance()->getTree();
    $tree->createRoot($f);
    $f->save();
    // run the test
    $t = new lime_test(13, array('options' => new lime_output_color(), 'error_reporting' => true));
    $t->diag('sfAssetFolderTable');
    $sfAssetFolder = sfAssetFolderTable::getInstance()->retrieveByPath(sfConfig::get('app_sfAssetsLibrary_upload_dir', 'mediaTEST'));
    $t->ok($sfAssetFolder->isRoot(), 'retrieveByPath() retrieves root from app_sfAssetsLibrary_upload_dir string: ' . sfConfig::get('app_sfAssetsLibrary_upload_dir', 'mediaTEST'));
    $sfAssetFolder = sfAssetFolderTable::getInstance()->retrieveByPath();
    $t->ok($sfAssetFolder->isRoot(), 'retrieveByPath() retrieves root from empty string');
    $sfAssetFolder = sfAssetFolderTable::getInstance()->createFromPath(sfConfig::get('app_sfAssetsLibrary_upload_dir', 'mediaTEST') . DIRECTORY_SEPARATOR . 'simple');
    $t->isa_ok($sfAssetFolder, 'sfAssetFolder', 'createFromPath() creates a sfAssetFolder from simple string');
    $t->isa_ok($sfAssetFolder->getParent(), 'sfAssetFolder', 'createFromPath() from simple string has a parent');
    $t->ok($sfAssetFolder->getParent()->isRoot(), 'createFromPath() creates a root child from simple string');
    $sfAssetFolder2 = sfAssetFolderTable::getInstance()->createFromPath(sfConfig::get('app_sfAssetsLibrary_upload_dir', 'mediaTEST') . DIRECTORY_SEPARATOR . 'simple' . DIRECTORY_SEPARATOR . 'subfolder');
    $t->isa_ok($sfAssetFolder2, 'sfAssetFolder', 'createFromPath() creates a sfAssetFolder from simple string');
 /**
  * @return array
  */
 public function getAssetsWithFilenames()
 {
     return sfAssetTable::getInstance()->getAssetsWithFilenames($this->getId());
 }
    #   Test_Sub-directory/         sfAssetFolder2
    #     Test_Sub-sub-directory/   sfAssetFolder3
    #       toto
    #     raikkonen.jpg
    // Bug in Propel instance pooling + NestedSets...
    //  sfAssetFolderPeer::clearInstancePool(); clear()
    $root = sfAssetFolderTable::getInstance()->find($rootId);
    $sfAssetFolder2 = sfAssetFolderTable::getInstance()->find($id2);
    $sfAssetFolder3 = sfAssetFolderTable::getInstance()->find($id3);
    $t->is($sfAssetFolder2->getParent()->getId(), $root->getId(), 'move() gives the correct parent');
    $t->is($sfAssetFolder3->getParent()->getParent()->getId(), $root->getId(), 'move() also moves children');
    $t->is($sfAssetFolder2->getRelativePath(), $root->getRelativePath() . '/' . $sfAssetFolder2->getName(), 'move() changes descendants relative paths');
    $t->is($sfAssetFolder3->getRelativePath(), $sfAssetFolder2->getRelativePath() . '/' . $sfAssetFolder3->getName(), 'move() changes descendants relative paths');
    //  sfAssetPeer::clearInstancePool();
    $sfAsset = sfAssetTable::getInstance()->find($sf_asset_id);
    $sfAsset2 = sfAssetTable::getInstance()->find($sf_asset2_id);
    $t->ok(is_file($sfAsset->getFullPath()), 'move() also moves assets under the moved folder');
    $t->ok(is_file($sfAsset2->getFullPath()), 'move() also moves assets under the moved folder');
} catch (Exception $e) {
    echo 'errore: ' . $e->getMessage() . PHP_EOL;
    echo $e->getTraceAsString() . PHP_EOL;
}
// reset DB
$con->rollBack();
function debugTree()
{
    $treeObject = Doctrine_Core::getTable('sfAssetFolder')->getTree()->fetchTree();
    $tree = $treeObject->fetchTree();
    //
    //  foreach ($tree as $folder) {
    //      echo str_repeat('    ', $folder->getLevel()) . $folder->getName() . "\n";
 /**
  * Change asset directory and/or name
  *
  * @param sfAssetFolder $new_folder
  */
 private function move()
 {
     $new_folder = $this->sfAssetFolder;
     $old_folder_id = $this->old_folder_id;
     if ($old_folder_id == $this->sfAssetFolder->id) {
         return;
     }
     if (sfAssetTable::presents($this->sfAssetFolder->id, $this->getFilename())) {
         throw new sfAssetException('The target folder "%folder%" already contains an asset named "%name%". The asset has not been moved.', array('%folder%' => $new_folder->getName(), '%name%' => $this->getFilename()));
     }
     $this->setFolderId($old_folder_id);
     $this->refreshRelated('sfAssetFolder');
     $old_filepaths = $this->getFilepaths();
     $this->sfAssetFolder = $new_folder;
     $success = true;
     foreach ($old_filepaths as $type => $filepath) {
         if (!is_file($filepath)) {
             continue;
         }
         $success = rename($filepath, $this->getFullPath($type)) && $success;
     }
     if (!$success) {
         throw new sfAssetException('Some or all of the file operations failed. It is possible that the moved asset or its thumbnails are missing.');
     }
 }
 /**
  * Change asset directory and/or name
  *
  * @param sfAssetFolder $newFolder
  * @param string        $newFilename
  */
 public function move(sfAssetFolder $newFolder, $newFilename = null)
 {
     if (sfAssetTable::getInstance()->exists($newFolder->getId(), $newFilename ? $newFilename : $this->getFilename())) {
         throw new sfAssetException('The target folder "%folder%" already contains an asset named "%name%". The asset has not been moved.', array('%folder%' => $newFolder->getName(), '%name%' => $newFilename ? $newFilename : $this->getFilename()));
     }
     $oldFilepaths = $this->getFilepaths();
     if ($newFilename) {
         if (sfAssetsLibraryTools::sanitizeName($newFilename) != $newFilename) {
             throw new sfAssetException('The filename "%name%" contains incorrect characters. The asset has not be altered.', array('%name%' => $newFilename));
         }
         $this->setFilename($newFilename);
     }
     $this->setFolder($newFolder);
     $success = true;
     foreach ($oldFilepaths as $type => $filepath) {
         $success = rename($filepath, $this->getFullPath($type)) && $success;
     }
     if (!$success) {
         throw new sfAssetException('Some or all of the file operations failed. It is possible that the moved asset or its thumbnails are missing.');
     }
 }