Skip to content

Commit 351d9e4

Browse files
authored
Merge pull request #16 from derhasi/tests
Added additional test for fixtures
2 parents 4e0fe33 + ce14bb5 commit 351d9e4

File tree

3 files changed

+182
-19
lines changed

3 files changed

+182
-19
lines changed

README.md

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,50 +22,52 @@ For configuring the paths you need to set `preserve-paths` within the `extra` of
2222
{
2323
"extra": {
2424
"preserve-paths": [
25-
"htdocs/sites/all/modules/contrib",
26-
"htdocs/sites/all/themes/contrib",
27-
"htdocs/sites/all/libraries",
28-
"htdocs/sites/all/drush"
25+
"web/sites/all/modules/contrib",
26+
"web/sites/all/themes/contrib",
27+
"web/sites/all/libraries",
28+
"web/sites/all/drush"
2929
]
3030
}
3131
}
3232
```
3333

3434
## Example
3535

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

3838
```json
3939
{
4040
"repositories": [
4141
{
4242
"type": "composer",
43-
"url": "https://packagist.drupal-composer.org/"
43+
"url": "https://packages.drupal.org/7"
4444
}
4545
],
4646
"require": {
47-
"davidbarratt/custom-installer": "dev-master",
47+
"composer/installers": "^1.2",
4848
"derhasi/composer-preserve-paths": "0.1.*",
49-
"drupal/views": "7.*",
49+
"drupal/views": "3.*",
5050
"drupal/drupal": "7.*"
5151
},
5252
"config": {
5353
"vendor-dir": "vendor"
5454
},
5555
"extra": {
56-
"custom-installer": {
57-
"drupal-module": "htdocs/sites/all/modules/contrib/{$name}/",
58-
"drupal-theme": "htdocs/sites/all/themes/contrib/{$name}/",
59-
"drupal-library": "htdocs/sites/all/libraries/{$name}/",
60-
"drupal-drush": "htdocs/sites/all/drush/{$name}/",
61-
"drupal-profile": "htdocs/profiles/{$name}/",
62-
"drupal-core": "htdocs/"
56+
"installer-paths": {
57+
"web/": ["type:drupal-core"],
58+
"web/sites/all/modules/contrib/{$name}/": ["type:drupal-module"],
59+
"web/sites/all/themes/contrib/{$name}/": ["type:drupal-theme"],
60+
"web/sites/all/libraries/{$name}/": ["type:drupal-library"],
61+
"web/sites/all/drush/{$name}/": ["type:drupal-drush"],
62+
"web/profiles/{$name}/": ["type:drupal-profile"]
6363
},
6464
"preserve-paths": [
65-
"htdocs/sites/all/modules/contrib",
66-
"htdocs/sites/all/themes/contrib",
67-
"htdocs/sites/all/libraries",
68-
"htdocs/sites/all/drush"
65+
"web/sites/all/modules/contrib",
66+
"web/sites/all/themes/contrib",
67+
"web/sites/all/libraries",
68+
"web/sites/all/drush",
69+
"web/sites/default/settings.php",
70+
"web/sites/default/files"
6971
]
7072
}
7173
}

tests/FixturesTest.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
namespace derhasi\Composer\Tests;
4+
5+
use derhasi\tempdirectory\TempDirectory;
6+
7+
/**
8+
* Tests for some examples.
9+
*/
10+
class FixturesTest extends \PHPUnit_Framework_TestCase
11+
{
12+
13+
/**
14+
* @var string
15+
*/
16+
protected $composerBin;
17+
18+
/**
19+
* @var string
20+
*/
21+
protected $fixturesRoot;
22+
23+
/**
24+
* @var string
25+
*/
26+
protected $projectRoot;
27+
28+
/**
29+
* Set up test.
30+
*/
31+
protected function setUp()
32+
{
33+
$this->projectRoot = realpath(__DIR__.'/..');
34+
$this->composerBin = realpath(__DIR__.'/../vendor/bin/composer');
35+
$this->fixturesRoot = realpath(__DIR__.'/fixtures');
36+
}
37+
38+
/**
39+
* Test provided fixtures.
40+
*
41+
* @param string $folder
42+
* Name of the folder of the fixture
43+
* @param array $commands
44+
* Array of composer commands to process
45+
* @param array $files
46+
* Array of files to check for existance
47+
*
48+
* @dataProvider fixturesProvider
49+
*/
50+
public function testFixtures($folder, $commands = array(), $files = array())
51+
{
52+
$workingDirectory = new TempDirectory(__METHOD__.$folder);
53+
54+
chdir($workingDirectory->getRoot());
55+
copy($this->fixturesRoot.'/example/composer.json', $workingDirectory->getRoot().'/composer.json');
56+
57+
// Add this project as local development repository sow we work with
58+
// the latest code.
59+
$this->composer('config', 'repositories.dev', 'path', $this->projectRoot);
60+
61+
$this->composer('install');
62+
63+
// Run additional composer commands.
64+
foreach ($commands as $command) {
65+
call_user_func_array(array($this, 'composer'), $command);
66+
}
67+
68+
// Check for file existance.
69+
foreach ($files as $file) {
70+
$this->assertFileExists($file);
71+
}
72+
73+
unset($workingDirectory);
74+
}
75+
76+
/**
77+
* Provides fixtures test data.
78+
*
79+
* @return array
80+
*/
81+
public function fixturesProvider()
82+
{
83+
return array(
84+
array(
85+
'example',
86+
// Update drupal/drupal to the newest release
87+
array(
88+
array('update', 'drupal/drupal'),
89+
),
90+
array( 'web/index.php', 'web/sites/all/modules/contrib/views/views.module'),
91+
),
92+
);
93+
}
94+
95+
/**
96+
* Run composer command.
97+
*
98+
* @param string $command
99+
* @param string $arg,... Optional arguments
100+
*
101+
* @return string[]
102+
* Array of output lines by the composer command.
103+
*/
104+
protected function composer($command)
105+
{
106+
$exec = $this->composerBin;
107+
$exec .= ' '.escapeshellcmd($command);
108+
$args = func_get_args();
109+
array_shift($args);
110+
foreach ($args as $arg) {
111+
if (strlen($arg) > 0) {
112+
$exec .= ' '.escapeshellarg($arg);
113+
}
114+
}
115+
116+
$output = array();
117+
$returnCode = null;
118+
exec("$exec 2>&1", $output, $returnCode);
119+
120+
if ($returnCode) {
121+
throw new \Exception(sprintf('Composer command "%s" failed:\n%s"', $exec, implode("\n", $output)));
122+
}
123+
124+
return $output;
125+
}
126+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"repositories": [
3+
{
4+
"type": "composer",
5+
"url": "https://packages.drupal.org/7"
6+
}
7+
],
8+
"require": {
9+
"composer/installers": "^1.2",
10+
"derhasi/composer-preserve-paths": "@dev",
11+
"drupal/views": "3.*",
12+
"drupal/drupal": "7.52"
13+
},
14+
"config": {
15+
"vendor-dir": "vendor"
16+
},
17+
"extra": {
18+
"installer-paths": {
19+
"web/": ["type:drupal-core"],
20+
"web/sites/all/modules/contrib/{$name}/": ["type:drupal-module"],
21+
"web/sites/all/themes/contrib/{$name}/": ["type:drupal-theme"],
22+
"web/sites/all/libraries/{$name}/": ["type:drupal-library"],
23+
"web/sites/all/drush/{$name}/": ["type:drupal-drush"],
24+
"web/profiles/{$name}/": ["type:drupal-profile"]
25+
},
26+
"preserve-paths": [
27+
"web/sites/all/modules/contrib",
28+
"web/sites/all/themes/contrib",
29+
"web/sites/all/libraries",
30+
"web/sites/all/drush",
31+
"web/sites/default/settings.php",
32+
"web/sites/default/files"
33+
]
34+
}
35+
}

0 commit comments

Comments
 (0)