Skip to content

Commit

Permalink
Write some tests!
Browse files Browse the repository at this point in the history
  • Loading branch information
dhensby committed Mar 6, 2018
1 parent 0d4b4cf commit d8f9f31
Show file tree
Hide file tree
Showing 4 changed files with 485 additions and 15 deletions.
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: php

php:
- 7.1
- 7.2
- nightly

matrix:
fast_finish: true
allow_failures:
- php: nightly

before_script:
- composer validate
- composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile

script:
- vendor/bin/phpunit tests/
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"SilverStripe\\RecipePlugin\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"SilverStripe\\Test\\RecipePlugin\\": "src/"
}
},
"extra": {
"class": "SilverStripe\\RecipePlugin\\RecipePlugin",
"branch-alias": {
Expand All @@ -24,6 +29,7 @@
"composer-plugin-api": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^7",
"composer/composer": "^1.2"
},
"minimum-stability": "dev"
Expand Down
64 changes: 49 additions & 15 deletions src/RecipeInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Composer\IO\IOInterface;
use Composer\Json\JsonFile;
use Composer\Package\PackageInterface;
use Composer\Util\Filesystem;
use FilesystemIterator;
use Iterator;
use RecursiveDirectoryIterator;
Expand All @@ -16,8 +17,16 @@

class RecipeInstaller extends LibraryInstaller {

public function __construct(IOInterface $io, Composer $composer) {
parent::__construct($io, $composer, null);
/**
* RecipeInstaller constructor.
*
* @param IOInterface $io
* @param Composer $composer
* @param string $type
* @param Filesystem $filesystem
*/
public function __construct(IOInterface $io, Composer $composer, $type = null, Filesystem $filesystem = null) {
parent::__construct($io, $composer, $type, $filesystem);
}

/**
Expand All @@ -32,23 +41,23 @@ public function __construct(IOInterface $io, Composer $composer) {
*/
protected function installProjectFiles($recipe, $sourceRoot, $destinationRoot, $filePatterns, $registrationKey, $name = 'project')
{
// load composer json data
$composerFile = new JsonFile(Factory::getComposerFile(), null, $this->io);
$composerData = $composerFile->read();
$installedFiles = isset($composerData['extra'][$registrationKey])
? $composerData['extra'][$registrationKey]
: [];
// fetch the installed files from the json data
$installedFiles = $this->getInstalledFiles($registrationKey);

// Load all project files
$fileIterator = $this->getFileIterator($sourceRoot, $filePatterns);
$any = false;
foreach($fileIterator as $path => $info) {
$destination = $destinationRoot . substr($path, strlen($sourceRoot));
$extension = pathinfo($destination, PATHINFO_EXTENSION);
if ($extension === 'tmpl') {
$destination = substr($destination, -5);
$destinationExt = pathinfo($destination, PATHINFO_EXTENSION);
if ($destinationExt === 'tmpl') {
$destination = substr($destination, 0, -5);
}
$relativePath = substr($path, strlen($sourceRoot) + 1); // Name path without leading '/'
$relativePathExt = pathinfo($relativePath, PATHINFO_EXTENSION);
if ($relativePathExt === 'tmpl') {
$relativePath = substr($relativePath, 0, -5);
}

// Write header
if (!$any) {
Expand All @@ -57,8 +66,8 @@ protected function installProjectFiles($recipe, $sourceRoot, $destinationRoot, $
}

// Check if file exists
if (file_exists($destination)) {
if (file_get_contents($destination) === file_get_contents($path)) {
if ($this->fileExists($destination)) {
if ($this->fileGetContents($destination) === $this->fileGetContents($path)) {
$this->io->write(
" - Skipping <info>$relativePath</info> (<comment>existing, but unchanged</comment>)"
);
Expand All @@ -76,7 +85,7 @@ protected function installProjectFiles($recipe, $sourceRoot, $destinationRoot, $
$any++;
$this->io->write(" - Copying <info>$relativePath</info>");
$this->filesystem->ensureDirectoryExists(dirname($destination));
copy($path, $destination);
$this->filesystem->copy($path, $destination);
}

// Add file to installed (even if already exists)
Expand All @@ -92,10 +101,35 @@ protected function installProjectFiles($recipe, $sourceRoot, $destinationRoot, $
$composerData['extra'] = [];
}
$composerData['extra'][$registrationKey] = $installedFiles;
$composerFile->write($composerData);
$this->getComposerFile()->write($composerData);
}
}

public function fileExists($filename)
{
return file_exists($filename);
}

public function fileGetContents($filename, $use_include_path = false, $context = null, $offset = 0, $maxlen = null)
{
return file_get_contents($filename, $use_include_path, $context, $offset, $maxlen);
}

protected function getComposerFile()
{
return new JsonFile(Factory::getComposerFile(), null, $this->io);
}

protected function getInstalledFiles($registrationKey)
{
// load composer json data
$composerFile = $this->getComposerFile();
$composerData = $composerFile->read();
return isset($composerData['extra'][$registrationKey])
? $composerData['extra'][$registrationKey]
: [];
}

/**
* Get iterator of matching source files to copy
*
Expand Down
Loading

0 comments on commit d8f9f31

Please sign in to comment.