Skip to content

Commit df490fc

Browse files
Refactor tests to have a base Class.
1 parent 2b4d09a commit df490fc

File tree

3 files changed

+155
-263
lines changed

3 files changed

+155
-263
lines changed

tests/BaseTest.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \DrupalComposer\DrupalScaffold\Tests\BaseTest.
6+
*/
7+
8+
namespace DrupalComposer\DrupalScaffold\Tests;
9+
10+
use Composer\Util\Filesystem;
11+
12+
/**
13+
* Base test Class for drupal-scaffold features.
14+
*/
15+
abstract class BaseTest 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+
* Writes the default composer json to the temp direcoty.
67+
*/
68+
protected function writeComposerJSON() {
69+
$json = json_encode($this->composerJSONDefaults(), JSON_PRETTY_PRINT);
70+
// Write composer.json.
71+
file_put_contents($this->tmpDir . '/composer.json', $json);
72+
}
73+
74+
/**
75+
* Writes a tag for the current commit, so we can reference it directly in the
76+
* composer.json.
77+
*/
78+
protected function writeTestReleaseTag() {
79+
// Tag the current state.
80+
$this->tmpReleaseTag = '999.0.' . time();
81+
$this->git(sprintf('tag -a "%s" -m "%s"', $this->tmpReleaseTag, 'Tag for testing this exact commit'));
82+
}
83+
84+
/**
85+
* Provides the default composer.json data.
86+
*
87+
* @return array
88+
*/
89+
protected function composerJSONDefaults() {
90+
return array(
91+
'repositories' => array(
92+
array(
93+
'type' => 'vcs',
94+
'url' => $this->rootDir,
95+
)
96+
),
97+
'require' => array(
98+
'drupal-composer/drupal-scaffold' => $this->tmpReleaseTag,
99+
'composer/installers' => '^1.0.20',
100+
'drupal/core' => '8.0.0',
101+
),
102+
'scripts' => array(
103+
'drupal-scaffold' => 'DrupalComposer\\DrupalScaffold\\Plugin::scaffold'
104+
),
105+
'minimum-stability' => 'dev',
106+
);
107+
}
108+
109+
/**
110+
* Wrapper for the composer command.
111+
*
112+
* @param string $command
113+
* Composer command name, arguments and/or options
114+
*/
115+
protected function composer($command) {
116+
chdir($this->tmpDir);
117+
passthru(escapeshellcmd($this->rootDir . '/vendor/bin/composer ' . $command), $exit_code);
118+
if ($exit_code !== 0) {
119+
throw new \Exception('Composer returned a non-zero exit code');
120+
}
121+
}
122+
123+
/**
124+
* Wrapper for git command in the root directory.
125+
*
126+
* @param $command
127+
* Git command name, arguments and/or options.
128+
*/
129+
protected function git($command) {
130+
chdir($this->rootDir);
131+
passthru(escapeshellcmd('git ' . $command), $exit_code);
132+
if ($exit_code !== 0) {
133+
throw new \Exception('Git returned a non-zero exit code');
134+
}
135+
}
136+
137+
/**
138+
* Makes sure the given directory exists and has no content.
139+
*
140+
* @param string $directory
141+
*/
142+
protected function ensureDirectoryExistsAndClear($directory) {
143+
if (is_dir($directory)) {
144+
$this->fs->removeDirectory($directory);
145+
}
146+
mkdir($directory, 0777, true);
147+
}
148+
}

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)