Skip to content

Commit 2b4d09a

Browse files
Add test for #57.
1 parent ec624c9 commit 2b4d09a

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

tests/HandlerTest.php

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \DrupalComposer\DrupalScaffold\Tests\PluginTest.
6+
*/
7+
8+
namespace DrupalComposer\DrupalScaffold\Tests;
9+
10+
use Composer\Util\Filesystem;
11+
12+
/**
13+
* Tests composer plugin functionality.
14+
*/
15+
class HandlerTest extends \PHPUnit_Framework_TestCase {
16+
17+
/**
18+
* @var \Composer\Util\Filesystem
19+
*/
20+
protected $fs;
21+
22+
/**
23+
* @var string
24+
*/
25+
protected $tmpDir;
26+
27+
/**
28+
* @var string
29+
*/
30+
protected $rootDir;
31+
32+
/**
33+
* @var string
34+
*/
35+
protected $tmpReleaseTag;
36+
37+
/**
38+
* SetUp test
39+
*/
40+
public function setUp() {
41+
$this->rootDir = realpath(realpath(__DIR__ . '/..'));
42+
43+
// Prepare temp directory.
44+
$this->fs = new Filesystem();
45+
$this->tmpDir = realpath(sys_get_temp_dir()) . DIRECTORY_SEPARATOR . 'drupal-scaffold';
46+
$this->ensureDirectoryExistsAndClear($this->tmpDir);
47+
48+
$this->writeTestReleaseTag();
49+
$this->writeComposerJSON();
50+
51+
chdir($this->tmpDir);
52+
}
53+
54+
/**
55+
* tearDown
56+
*
57+
* @return void
58+
*/
59+
public function tearDown()
60+
{
61+
$this->fs->removeDirectory($this->tmpDir);
62+
$this->git(sprintf('tag -d "%s"', $this->tmpReleaseTag));
63+
}
64+
65+
/**
66+
* Tests that files for dev environments are downloaded only in dev mode.
67+
*/
68+
public function testDevFiles() {
69+
$exampleScaffoldFile = $this->tmpDir . DIRECTORY_SEPARATOR . 'index.php';
70+
$developmentScaffoldFile = $this->tmpDir . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'development.services.yml';
71+
$this->assertFileNotExists($exampleScaffoldFile, 'Scaffold file should not exist.');
72+
$this->assertFileNotExists($developmentScaffoldFile, 'Development scaffold file should not exist.');
73+
$this->composer('install --no-dev');
74+
$this->assertFileExists($exampleScaffoldFile, 'Scaffold file should exist.');
75+
$this->assertFileNotExists($developmentScaffoldFile, 'Development scaffold file should not exist.');
76+
$this->composer('drupal-scaffold');
77+
$this->assertFileExists($exampleScaffoldFile, 'Scaffold file should exist.');
78+
$this->assertFileExists($developmentScaffoldFile, 'Development scaffold file should exist.');
79+
}
80+
81+
/**
82+
* Writes the default composer json to the temp direcoty.
83+
*/
84+
protected function writeComposerJSON() {
85+
$json = json_encode($this->composerJSONDefaults(), JSON_PRETTY_PRINT);
86+
// Write composer.json.
87+
file_put_contents($this->tmpDir . '/composer.json', $json);
88+
}
89+
90+
/**
91+
* Writes a tag for the current commit, so we can reference it directly in the
92+
* composer.json.
93+
*/
94+
protected function writeTestReleaseTag() {
95+
// Tag the current state.
96+
$this->tmpReleaseTag = '999.0.' . time();
97+
$this->git(sprintf('tag -a "%s" -m "%s"', $this->tmpReleaseTag, 'Tag for testing this exact commit'));
98+
}
99+
100+
/**
101+
* Provides the default composer.json data.
102+
*
103+
* @return array
104+
*/
105+
protected function composerJSONDefaults() {
106+
return array(
107+
'repositories' => array(
108+
array(
109+
'type' => 'vcs',
110+
'url' => $this->rootDir,
111+
)
112+
),
113+
'require' => array(
114+
'drupal-composer/drupal-scaffold' => $this->tmpReleaseTag,
115+
'composer/installers' => '^1.0.20',
116+
'drupal/core' => '8.0.0',
117+
),
118+
'scripts' => array(
119+
'drupal-scaffold' => 'DrupalComposer\\DrupalScaffold\\Plugin::scaffold'
120+
),
121+
'minimum-stability' => 'dev',
122+
'prefer-stable' => true,
123+
);
124+
}
125+
126+
/**
127+
* Wrapper for the composer command.
128+
*
129+
* @param string $command
130+
* Composer command name, arguments and/or options
131+
*/
132+
protected function composer($command) {
133+
chdir($this->tmpDir);
134+
passthru(escapeshellcmd($this->rootDir . '/vendor/bin/composer ' . $command), $exit_code);
135+
if ($exit_code !== 0) {
136+
throw new \Exception('Composer returned a non-zero exit code');
137+
}
138+
}
139+
140+
/**
141+
* Wrapper for git command in the root directory.
142+
*
143+
* @param $command
144+
* Git command name, arguments and/or options.
145+
*/
146+
protected function git($command) {
147+
chdir($this->rootDir);
148+
passthru(escapeshellcmd('git ' . $command), $exit_code);
149+
if ($exit_code !== 0) {
150+
throw new \Exception('Git returned a non-zero exit code');
151+
}
152+
}
153+
154+
/**
155+
* Makes sure the given directory exists and has no content.
156+
*
157+
* @param string $directory
158+
*/
159+
protected function ensureDirectoryExistsAndClear($directory) {
160+
if (is_dir($directory)) {
161+
$this->fs->removeDirectory($directory);
162+
}
163+
mkdir($directory, 0777, true);
164+
}
165+
}

0 commit comments

Comments
 (0)