2a0f3f904f5e2aabb22b059b842f1e6d3a6099fa
[yaffs-website] / vendor / drupal-composer / drupal-scaffold / tests / PluginTest.php
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 PluginTest 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 a simple composer install without core, but adding core later.
67    */
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.');
78
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 .'"');
86       clearstatcache();
87       $mtime_after = filemtime($exampleScaffoldFile);
88       $this->assertNotEquals($mtime_after, $mtime_touched, 'Scaffold file was modified by composer update. (' . $version . ')');
89       switch ($version) {
90         case '8.0.1':
91         case '8.1.x-dev':
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');
95           break;
96         case '8.3.0':
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');
101           break;
102         case '8.5.x-dev':
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');
106           break;
107       }
108     }
109
110     // We touch a scaffold file, so we can check the file was modified after
111     // the custom commandscaffold update.
112     touch($exampleScaffoldFile);
113     clearstatcache();
114     $mtime_touched = filemtime($exampleScaffoldFile);
115     $this->composer('drupal-scaffold');
116     clearstatcache();
117     $mtime_after = filemtime($exampleScaffoldFile);
118     $this->assertNotEquals($mtime_after, $mtime_touched, 'Scaffold file was modified by custom command.');
119   }
120
121   /**
122    * Writes the default composer json to the temp direcoty.
123    */
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);
128   }
129
130   /**
131    * Writes a tag for the current commit, so we can reference it directly in the
132    * composer.json.
133    */
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'));
138   }
139
140   /**
141    * Provides the default composer.json data.
142    *
143    * @return array
144    */
145   protected function composerJSONDefaults() {
146     return array(
147       'repositories' => array(
148         array(
149           'type' => 'vcs',
150           'url' => $this->rootDir,
151         )
152       ),
153       'require' => array(
154         'drupal-composer/drupal-scaffold' => $this->tmpReleaseTag,
155         'composer/installers' => '^1.0.20',
156         'drupal/core' => '8.0.0',
157       ),
158       'scripts' => array(
159         'drupal-scaffold' =>  'DrupalComposer\\DrupalScaffold\\Plugin::scaffold'
160       ),
161       'minimum-stability' => 'dev',
162     );
163   }
164
165   /**
166    * Wrapper for the composer command.
167    *
168    * @param string $command
169    *   Composer command name, arguments and/or options
170    */
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');
176     }
177   }
178
179   /**
180    * Wrapper for git command in the root directory.
181    *
182    * @param $command
183    *   Git command name, arguments and/or options.
184    */
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');
190     }
191   }
192
193   /**
194    * Makes sure the given directory exists and has no content.
195    *
196    * @param string $directory
197    */
198   protected function ensureDirectoryExistsAndClear($directory) {
199     if (is_dir($directory)) {
200       $this->fs->removeDirectory($directory);
201     }
202     mkdir($directory, 0777, true);
203   }
204 }