From 15b491ed7459a76cbf0176dddf7ee3a66f59547e Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Fri, 16 Oct 2015 10:53:37 +1300 Subject: [PATCH] API Update for filesystem refactor --- composer.json | 2 +- .../BehatExtension/Context/BasicContext.php | 10 ++-- .../BehatExtension/Context/FixtureContext.php | 53 +++++++++++++------ 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/composer.json b/composer.json index 72d12a9..18b7a68 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "symfony/yaml": "~2.0", "symfony/finder": "~2.0", "silverstripe/testsession": "*", - "silverstripe/framework": ">=3.1.0" + "silverstripe/framework": "^4.0.0" }, "autoload": { diff --git a/src/SilverStripe/BehatExtension/Context/BasicContext.php b/src/SilverStripe/BehatExtension/Context/BasicContext.php index 31cebed..a6ee63b 100644 --- a/src/SilverStripe/BehatExtension/Context/BasicContext.php +++ b/src/SilverStripe/BehatExtension/Context/BasicContext.php @@ -2,13 +2,10 @@ namespace SilverStripe\BehatExtension\Context; -use Behat\Behat\Context\ClosuredContextInterface, - Behat\Behat\Context\TranslatedContextInterface, - Behat\Behat\Context\BehatContext, +use Behat\Behat\Context\BehatContext, Behat\Behat\Context\Step, Behat\Behat\Event\StepEvent, - Behat\Behat\Event\ScenarioEvent, - Behat\Behat\Exception\PendingException; + Behat\Behat\Event\ScenarioEvent; use Behat\Mink\Driver\Selenium2Driver; @@ -268,8 +265,9 @@ JS; */ public function cleanAssetsAfterScenario(ScenarioEvent $event) { foreach(\File::get() as $file) { - if(file_exists($file->getFullPath())) $file->delete(); + $file->delete(); } + \Filesystem::removeFolder(ASSETS_PATH, true); } public function takeScreenshot(StepEvent $event) { diff --git a/src/SilverStripe/BehatExtension/Context/FixtureContext.php b/src/SilverStripe/BehatExtension/Context/FixtureContext.php index 64b8ffe..dc81e56 100644 --- a/src/SilverStripe/BehatExtension/Context/FixtureContext.php +++ b/src/SilverStripe/BehatExtension/Context/FixtureContext.php @@ -2,17 +2,11 @@ namespace SilverStripe\BehatExtension\Context; -use Behat\Behat\Context\ClosuredContextInterface, - Behat\Behat\Context\TranslatedContextInterface, - Behat\Behat\Context\BehatContext, - Behat\Behat\Context\Step, - Behat\Behat\Event\StepEvent, - Behat\Behat\Event\FeatureEvent, +use Behat\Behat\Context\BehatContext, Behat\Behat\Event\ScenarioEvent, - Behat\Behat\Exception\PendingException, - Behat\Mink\Driver\Selenium2Driver, Behat\Gherkin\Node\PyStringNode, - Behat\Gherkin\Node\TableNode; + Behat\Gherkin\Node\TableNode, + SilverStripe\Filesystem\Storage\AssetStore; // PHPUnit require_once 'PHPUnit/Autoload.php'; @@ -548,13 +542,13 @@ class FixtureContext extends BehatContext protected function prepareAsset($class, $identifier, $data = null) { if(!$data) $data = array(); $relativeTargetPath = (isset($data['Filename'])) ? $data['Filename'] : $identifier; - $relativeTargetPath = preg_replace('/^' . ASSETS_DIR . '/', '', $relativeTargetPath); - $targetPath = $this->joinPaths(ASSETS_PATH, $relativeTargetPath); + $relativeTargetPath = preg_replace('/^' . ASSETS_DIR . '\/?/', '', $relativeTargetPath); $sourcePath = $this->joinPaths($this->getFilesPath(), basename($relativeTargetPath)); // Create file or folder on filesystem + $parent = \Folder::find_or_make(dirname($relativeTargetPath)); if($class == 'Folder' || is_subclass_of($class, 'Folder')) { - $parent = \Folder::find_or_make($relativeTargetPath); + $targetPath = $this->joinPaths(ASSETS_PATH, $relativeTargetPath); } else { if(!file_exists($sourcePath)) { throw new \InvalidArgumentException(sprintf( @@ -563,18 +557,43 @@ class FixtureContext extends BehatContext $sourcePath )); } - $parent = \Folder::find_or_make(dirname($relativeTargetPath)); - copy($sourcePath, $targetPath); + // Load file into APL and retrieve tuple + $asset = $this->getAssetStore()->setFromLocalFile( + $sourcePath, + $relativeTargetPath, + null, + null, + AssetStore::CONFLICT_OVERWRITE + ); + $data['FileFilename'] = $asset['Filename']; + $data['FileHash'] = $asset['Hash']; + $data['FileVariant'] = $asset['Variant']; + + // Strip base from url to get dir relative to base + $url = $this->getAssetStore()->getAsURL($asset['Filename'], $asset['Hash'], $asset['Variant']); + $targetPath = $this->joinPaths(BASE_PATH, substr($url, strlen(\Director::baseURL()))); + } + unset($data['Filename']); + if(!isset($data['Name'])) { + $data['Name'] = basename($relativeTargetPath); + } + if($parent) { + $data['ParentID'] = $parent->ID; } - $data['Filename'] = $this->joinPaths(ASSETS_DIR, $relativeTargetPath); - if(!isset($data['Name'])) $data['Name'] = basename($relativeTargetPath); - if($parent) $data['ParentID'] = $parent->ID; $this->createdFilesPaths[] = $targetPath; return $data; } + /** + * + * @return AssetStore + */ + protected function getAssetStore() { + return singleton('AssetStore'); + } + /** * Converts a natural language class description to an actual class name. * Respects {@link DataObject::$singular_name} variations.