Skip to content

Commit

Permalink
Merge pull request #16 from derhasi/tests
Browse files Browse the repository at this point in the history
Added additional test for fixtures
  • Loading branch information
derhasi authored Feb 3, 2017
2 parents 4e0fe33 + ce14bb5 commit 351d9e4
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 19 deletions.
40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,52 @@ For configuring the paths you need to set `preserve-paths` within the `extra` of
{
"extra": {
"preserve-paths": [
"htdocs/sites/all/modules/contrib",
"htdocs/sites/all/themes/contrib",
"htdocs/sites/all/libraries",
"htdocs/sites/all/drush"
"web/sites/all/modules/contrib",
"web/sites/all/themes/contrib",
"web/sites/all/libraries",
"web/sites/all/drush"
]
}
}
```

## Example

An example composer.json using [davidbarratt/custom-installer](https://packagist.org/packages/davidbarratt/custom-installer):
An example composer.json using [composer/installers](https://packagist.org/packages/composer/installers):

```json
{
"repositories": [
{
"type": "composer",
"url": "https://packagist.drupal-composer.org/"
"url": "https://packages.drupal.org/7"
}
],
"require": {
"davidbarratt/custom-installer": "dev-master",
"composer/installers": "^1.2",
"derhasi/composer-preserve-paths": "0.1.*",
"drupal/views": "7.*",
"drupal/views": "3.*",
"drupal/drupal": "7.*"
},
"config": {
"vendor-dir": "vendor"
},
"extra": {
"custom-installer": {
"drupal-module": "htdocs/sites/all/modules/contrib/{$name}/",
"drupal-theme": "htdocs/sites/all/themes/contrib/{$name}/",
"drupal-library": "htdocs/sites/all/libraries/{$name}/",
"drupal-drush": "htdocs/sites/all/drush/{$name}/",
"drupal-profile": "htdocs/profiles/{$name}/",
"drupal-core": "htdocs/"
"installer-paths": {
"web/": ["type:drupal-core"],
"web/sites/all/modules/contrib/{$name}/": ["type:drupal-module"],
"web/sites/all/themes/contrib/{$name}/": ["type:drupal-theme"],
"web/sites/all/libraries/{$name}/": ["type:drupal-library"],
"web/sites/all/drush/{$name}/": ["type:drupal-drush"],
"web/profiles/{$name}/": ["type:drupal-profile"]
},
"preserve-paths": [
"htdocs/sites/all/modules/contrib",
"htdocs/sites/all/themes/contrib",
"htdocs/sites/all/libraries",
"htdocs/sites/all/drush"
"web/sites/all/modules/contrib",
"web/sites/all/themes/contrib",
"web/sites/all/libraries",
"web/sites/all/drush",
"web/sites/default/settings.php",
"web/sites/default/files"
]
}
}
Expand Down
126 changes: 126 additions & 0 deletions tests/FixturesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace derhasi\Composer\Tests;

use derhasi\tempdirectory\TempDirectory;

/**
* Tests for some examples.
*/
class FixturesTest extends \PHPUnit_Framework_TestCase
{

/**
* @var string
*/
protected $composerBin;

/**
* @var string
*/
protected $fixturesRoot;

/**
* @var string
*/
protected $projectRoot;

/**
* Set up test.
*/
protected function setUp()
{
$this->projectRoot = realpath(__DIR__.'/..');
$this->composerBin = realpath(__DIR__.'/../vendor/bin/composer');
$this->fixturesRoot = realpath(__DIR__.'/fixtures');
}

/**
* Test provided fixtures.
*
* @param string $folder
* Name of the folder of the fixture
* @param array $commands
* Array of composer commands to process
* @param array $files
* Array of files to check for existance
*
* @dataProvider fixturesProvider
*/
public function testFixtures($folder, $commands = array(), $files = array())
{
$workingDirectory = new TempDirectory(__METHOD__.$folder);

chdir($workingDirectory->getRoot());
copy($this->fixturesRoot.'/example/composer.json', $workingDirectory->getRoot().'/composer.json');

// Add this project as local development repository sow we work with
// the latest code.
$this->composer('config', 'repositories.dev', 'path', $this->projectRoot);

$this->composer('install');

// Run additional composer commands.
foreach ($commands as $command) {
call_user_func_array(array($this, 'composer'), $command);
}

// Check for file existance.
foreach ($files as $file) {
$this->assertFileExists($file);
}

unset($workingDirectory);
}

/**
* Provides fixtures test data.
*
* @return array
*/
public function fixturesProvider()
{
return array(
array(
'example',
// Update drupal/drupal to the newest release
array(
array('update', 'drupal/drupal'),
),
array( 'web/index.php', 'web/sites/all/modules/contrib/views/views.module'),
),
);
}

/**
* Run composer command.
*
* @param string $command
* @param string $arg,... Optional arguments
*
* @return string[]
* Array of output lines by the composer command.
*/
protected function composer($command)
{
$exec = $this->composerBin;
$exec .= ' '.escapeshellcmd($command);
$args = func_get_args();
array_shift($args);
foreach ($args as $arg) {
if (strlen($arg) > 0) {
$exec .= ' '.escapeshellarg($arg);
}
}

$output = array();
$returnCode = null;
exec("$exec 2>&1", $output, $returnCode);

if ($returnCode) {
throw new \Exception(sprintf('Composer command "%s" failed:\n%s"', $exec, implode("\n", $output)));
}

return $output;
}
}
35 changes: 35 additions & 0 deletions tests/fixtures/example/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"repositories": [
{
"type": "composer",
"url": "https://packages.drupal.org/7"
}
],
"require": {
"composer/installers": "^1.2",
"derhasi/composer-preserve-paths": "@dev",
"drupal/views": "3.*",
"drupal/drupal": "7.52"
},
"config": {
"vendor-dir": "vendor"
},
"extra": {
"installer-paths": {
"web/": ["type:drupal-core"],
"web/sites/all/modules/contrib/{$name}/": ["type:drupal-module"],
"web/sites/all/themes/contrib/{$name}/": ["type:drupal-theme"],
"web/sites/all/libraries/{$name}/": ["type:drupal-library"],
"web/sites/all/drush/{$name}/": ["type:drupal-drush"],
"web/profiles/{$name}/": ["type:drupal-profile"]
},
"preserve-paths": [
"web/sites/all/modules/contrib",
"web/sites/all/themes/contrib",
"web/sites/all/libraries",
"web/sites/all/drush",
"web/sites/default/settings.php",
"web/sites/default/files"
]
}
}

0 comments on commit 351d9e4

Please sign in to comment.