Skip to content

Commit ee07826

Browse files
Refactor tests to have a base Class.
1 parent c5a2ef6 commit ee07826

File tree

3 files changed

+147
-260
lines changed

3 files changed

+147
-260
lines changed

tests/BaseTest.php

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

tests/HandlerTest.php

Lines changed: 6 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,15 @@
22

33
/**
44
* @file
5-
* Contains \DrupalComposer\DrupalScaffold\Tests\PluginTest.
5+
* Contains \DrupalComposer\DrupalScaffold\Tests\HandlerTest.
66
*/
77

88
namespace DrupalComposer\DrupalScaffold\Tests;
99

10-
use Composer\Util\Filesystem;
11-
1210
/**
1311
* Tests composer plugin functionality.
1412
*/
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-
}
13+
class HandlerTest extends BaseTest {
6414

6515
/**
6616
* Tests that files for dev environments are downloaded only in dev mode.
@@ -79,87 +29,14 @@ public function testDevFiles() {
7929
}
8030

8131
/**
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.
32+
* Add prefer-stable true to speed up tests.
10233
*
10334
* @return array
10435
*/
10536
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-
);
37+
$composerJsonDefault = parent::composerJSONDefaults();
38+
$composerJsonDefault['prefer-stable'] = true;
39+
return $composerJsonDefault;
12440
}
12541

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-
}
16542
}

0 commit comments

Comments
 (0)