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 --no-dev --prefer-dist');
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', '8.3.0', '8.5.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 --prefer-dist --update-no-dev drupal/core:"' . $version .'"');
87 $mtime_after = filemtime($exampleScaffoldFile);
88 $this->assertNotEquals($mtime_after, $mtime_touched, 'Scaffold file was modified by composer update. (' . $version . ')');
92 $this->assertFileExists($this->tmpDir . DIRECTORY_SEPARATOR . '.eslintrc');
93 $this->assertFileNotExists($this->tmpDir . DIRECTORY_SEPARATOR . '.eslintrc.json');
94 $this->assertFileNotExists($this->tmpDir . DIRECTORY_SEPARATOR . '.ht.router.php');
97 // Note we don't clean up .eslintrc file.
98 $this->assertFileExists($this->tmpDir . DIRECTORY_SEPARATOR . '.eslintrc');
99 $this->assertFileExists($this->tmpDir . DIRECTORY_SEPARATOR . '.eslintrc.json');
100 $this->assertFileNotExists($this->tmpDir . DIRECTORY_SEPARATOR . '.ht.router.php');
103 $this->assertFileExists($this->tmpDir . DIRECTORY_SEPARATOR . '.eslintrc');
104 $this->assertFileExists($this->tmpDir . DIRECTORY_SEPARATOR . '.eslintrc.json');
105 $this->assertFileExists($this->tmpDir . DIRECTORY_SEPARATOR . '.ht.router.php');
110 // We touch a scaffold file, so we can check the file was modified after
111 // the custom commandscaffold update.
112 touch($exampleScaffoldFile);
114 $mtime_touched = filemtime($exampleScaffoldFile);
115 $this->composer('drupal-scaffold');
117 $mtime_after = filemtime($exampleScaffoldFile);
118 $this->assertNotEquals($mtime_after, $mtime_touched, 'Scaffold file was modified by custom command.');
122 * Writes the default composer json to the temp direcoty.
124 protected function writeComposerJSON() {
125 $json = json_encode($this->composerJSONDefaults(), JSON_PRETTY_PRINT);
126 // Write composer.json.
127 file_put_contents($this->tmpDir . '/composer.json', $json);
131 * Writes a tag for the current commit, so we can reference it directly in the
134 protected function writeTestReleaseTag() {
135 // Tag the current state.
136 $this->tmpReleaseTag = '999.0.' . time();
137 $this->git(sprintf('tag -a "%s" -m "%s"', $this->tmpReleaseTag, 'Tag for testing this exact commit'));
141 * Provides the default composer.json data.
145 protected function composerJSONDefaults() {
147 'repositories' => array(
150 'url' => $this->rootDir,
154 'drupal-composer/drupal-scaffold' => $this->tmpReleaseTag,
155 'composer/installers' => '^1.0.20',
156 'drupal/core' => '8.0.0',
159 'drupal-scaffold' => 'DrupalComposer\\DrupalScaffold\\Plugin::scaffold'
161 'minimum-stability' => 'dev',
166 * Wrapper for the composer command.
168 * @param string $command
169 * Composer command name, arguments and/or options
171 protected function composer($command) {
172 chdir($this->tmpDir);
173 passthru(escapeshellcmd($this->rootDir . '/vendor/bin/composer ' . $command), $exit_code);
174 if ($exit_code !== 0) {
175 throw new \Exception('Composer returned a non-zero exit code');
180 * Wrapper for git command in the root directory.
183 * Git command name, arguments and/or options.
185 protected function git($command) {
186 chdir($this->rootDir);
187 passthru(escapeshellcmd('git ' . $command), $exit_code);
188 if ($exit_code !== 0) {
189 throw new \Exception('Git returned a non-zero exit code');
194 * Makes sure the given directory exists and has no content.
196 * @param string $directory
198 protected function ensureDirectoryExistsAndClear($directory) {
199 if (is_dir($directory)) {
200 $this->fs->removeDirectory($directory);
202 mkdir($directory, 0777, true);