5 * Contains \DrupalComposer\DrupalScaffold\Tests\PluginTest.
8 namespace DrupalComposer\DrupalScaffold\Tests;
10 use Composer\Util\Filesystem;
13 * Tests composer plugin functionality.
15 class PluginTest extends \PHPUnit_Framework_TestCase {
18 * @var \Composer\Util\Filesystem
35 protected $tmpReleaseTag;
40 public function setUp() {
41 $this->rootDir = realpath(realpath(__DIR__ . '/..'));
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);
48 $this->writeTestReleaseTag();
49 $this->writeComposerJSON();
59 public function tearDown()
61 $this->fs->removeDirectory($this->tmpDir);
62 $this->git(sprintf('tag -d "%s"', $this->tmpReleaseTag));
66 * Tests a simple composer install without core, but adding core later.
68 public function testComposerInstallAndUpdate() {
69 $exampleScaffoldFile = $this->tmpDir . DIRECTORY_SEPARATOR . 'index.php';
70 $this->assertFileNotExists($exampleScaffoldFile, 'Scaffold file should not be exist.');
71 $this->composer('install');
72 $this->assertFileExists($this->tmpDir . DIRECTORY_SEPARATOR . 'core', 'Drupal core is installed.');
73 $this->assertFileExists($exampleScaffoldFile, 'Scaffold file should be automatically installed.');
74 $this->fs->remove($exampleScaffoldFile);
75 $this->assertFileNotExists($exampleScaffoldFile, 'Scaffold file should not be exist.');
76 $this->composer('drupal-scaffold');
77 $this->assertFileExists($exampleScaffoldFile, 'Scaffold file should be installed by "drupal-scaffold" command.');
79 foreach (['8.0.1', '8.1.x-dev'] as $version) {
80 // We touch a scaffold file, so we can check the file was modified after
81 // the scaffold update.
82 touch($exampleScaffoldFile);
83 $mtime_touched = filemtime($exampleScaffoldFile);
84 // Requiring a newer version triggers "composer update"
85 $this->composer('require --update-with-dependencies drupal/core:"' . $version .'"');
87 $mtime_after = filemtime($exampleScaffoldFile);
88 $this->assertNotEquals($mtime_after, $mtime_touched, 'Scaffold file was modified by composer update. (' . $version . ')');
91 // We touch a scaffold file, so we can check the file was modified after
92 // the custom commandscaffold update.
93 touch($exampleScaffoldFile);
95 $mtime_touched = filemtime($exampleScaffoldFile);
96 $this->composer('drupal-scaffold');
98 $mtime_after = filemtime($exampleScaffoldFile);
99 $this->assertNotEquals($mtime_after, $mtime_touched, 'Scaffold file was modified by custom command.');
103 * Writes the default composer json to the temp direcoty.
105 protected function writeComposerJSON() {
106 $json = json_encode($this->composerJSONDefaults(), JSON_PRETTY_PRINT);
107 // Write composer.json.
108 file_put_contents($this->tmpDir . '/composer.json', $json);
112 * Writes a tag for the current commit, so we can reference it directly in the
115 protected function writeTestReleaseTag() {
116 // Tag the current state.
117 $this->tmpReleaseTag = '999.0.' . time();
118 $this->git(sprintf('tag -a "%s" -m "%s"', $this->tmpReleaseTag, 'Tag for testing this exact commit'));
122 * Provides the default composer.json data.
126 protected function composerJSONDefaults() {
128 'repositories' => array(
131 'url' => $this->rootDir,
135 'drupal-composer/drupal-scaffold' => $this->tmpReleaseTag,
136 'composer/installers' => '^1.0.20',
137 'drupal/core' => '8.0.0',
140 'drupal-scaffold' => 'DrupalComposer\\DrupalScaffold\\Plugin::scaffold'
142 'minimum-stability' => 'dev',
147 * Wrapper for the composer command.
149 * @param string $command
150 * Composer command name, arguments and/or options
152 protected function composer($command) {
153 chdir($this->tmpDir);
154 passthru(escapeshellcmd($this->rootDir . '/vendor/bin/composer ' . $command), $exit_code);
155 if ($exit_code !== 0) {
156 throw new \Exception('Composer returned a non-zero exit code');
161 * Wrapper for git command in the root directory.
164 * Git command name, arguments and/or options.
166 protected function git($command) {
167 chdir($this->rootDir);
168 passthru(escapeshellcmd('git ' . $command), $exit_code);
169 if ($exit_code !== 0) {
170 throw new \Exception('Git returned a non-zero exit code');
175 * Makes sure the given directory exists and has no content.
177 * @param string $directory
179 protected function ensureDirectoryExistsAndClear($directory) {
180 if (is_dir($directory)) {
181 $this->fs->removeDirectory($directory);
183 mkdir($directory, 0777, true);