Yaffs site version 1.1
[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');
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'] 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 .'"');
86       clearstatcache();
87       $mtime_after = filemtime($exampleScaffoldFile);
88       $this->assertNotEquals($mtime_after, $mtime_touched, 'Scaffold file was modified by composer update. (' . $version . ')');
89     }
90
91     // We touch a scaffold file, so we can check the file was modified after
92     // the custom commandscaffold update.
93     touch($exampleScaffoldFile);
94     clearstatcache();
95     $mtime_touched = filemtime($exampleScaffoldFile);
96     $this->composer('drupal-scaffold');
97     clearstatcache();
98     $mtime_after = filemtime($exampleScaffoldFile);
99     $this->assertNotEquals($mtime_after, $mtime_touched, 'Scaffold file was modified by custom command.');
100   }
101
102   /**
103    * Writes the default composer json to the temp direcoty.
104    */
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);
109   }
110
111   /**
112    * Writes a tag for the current commit, so we can reference it directly in the
113    * composer.json.
114    */
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'));
119   }
120
121   /**
122    * Provides the default composer.json data.
123    *
124    * @return array
125    */
126   protected function composerJSONDefaults() {
127     return array(
128       'repositories' => array(
129         array(
130           'type' => 'vcs',
131           'url' => $this->rootDir,
132         )
133       ),
134       'require' => array(
135         'drupal-composer/drupal-scaffold' => $this->tmpReleaseTag,
136         'composer/installers' => '^1.0.20',
137         'drupal/core' => '8.0.0',
138       ),
139       'scripts' => array(
140         'drupal-scaffold' =>  'DrupalComposer\\DrupalScaffold\\Plugin::scaffold'
141       ),
142       'minimum-stability' => 'dev',
143     );
144   }
145
146   /**
147    * Wrapper for the composer command.
148    *
149    * @param string $command
150    *   Composer command name, arguments and/or options
151    */
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');
157     }
158   }
159
160   /**
161    * Wrapper for git command in the root directory.
162    *
163    * @param $command
164    *   Git command name, arguments and/or options.
165    */
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');
171     }
172   }
173
174   /**
175    * Makes sure the given directory exists and has no content.
176    *
177    * @param string $directory
178    */
179   protected function ensureDirectoryExistsAndClear($directory) {
180     if (is_dir($directory)) {
181       $this->fs->removeDirectory($directory);
182     }
183     mkdir($directory, 0777, true);
184   }
185 }