This repository was archived by the owner on Aug 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathPluginTest.php
127 lines (110 loc) · 3.2 KB
/
PluginTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* @file
* Contains \Beet|Box\Tests\PluginTest.
*/
namespace Beet\Box\Tests;
use Composer\Util\Filesystem;
use PHPUnit\Framework\TestCase;
/**
* Tests composer plugin functionality.
*/
class StackTest extends TestCase {
/**
* @var \Composer\Util\Filesystem
*/
protected $fs;
/**
* @var string
*/
protected $tmpDir;
/**
* @var string
*/
protected $rootDir;
/**
* SetUp test
*/
public function setUp() {
$this->rootDir = realpath(realpath(__DIR__ . '/..'));
// Prepare temp directory.
$this->fs = new Filesystem();
$this->tmpDir = realpath(sys_get_temp_dir()) . DIRECTORY_SEPARATOR . 'beetbox';
$this->ensureDirectoryExistsAndClear($this->tmpDir);
$this->writeComposerJSON();
chdir($this->tmpDir);
}
/**
* tearDown
*
* @return void
*/
public function tearDown()
{
$this->fs->removeDirectory($this->tmpDir);
}
/**
* Tests a composer install and update to ensure Vagrantfile is added.
*/
public function testComposerInstallAndUpdate() {
$vagrantFile = $this->tmpDir . DIRECTORY_SEPARATOR . 'Vagrantfile';
$this->assertFileNotExists($vagrantFile, 'Vagrantfile should not be exist.');
$this->composer('install');
$this->assertFileExists($vagrantFile, 'Vagrantfile should be automatically installed.');
$this->fs->remove($vagrantFile);
$this->assertFileNotExists($vagrantFile, 'Vagrantfile should not be exist.');
$this->composer('update');
$this->assertFileExists($vagrantFile, 'Vagrantfile should be automatically recreated.');
}
/**
* Writes the default composer json to the temp direcoty.
*/
protected function writeComposerJSON() {
$json = json_encode($this->composerJSONDefaults(), JSON_PRETTY_PRINT);
// Write composer.json.
file_put_contents($this->tmpDir . '/composer.json', $json);
}
/**
* Provides the default composer.json data.
*
* @return array
*/
protected function composerJSONDefaults() {
return array(
'repositories' => array(
array(
'type' => 'path',
'url' => $this->rootDir,
)
),
'require' => array(
'beet/box' => "*"
),
'minimum-stability' => 'dev'
);
}
/**
* Wrapper for the composer command.
*
* @param string $command
* Composer command name, arguments and/or options
*/
protected function composer($command) {
chdir($this->tmpDir);
passthru(escapeshellcmd($this->rootDir . '/vendor/bin/composer ' . $command), $exit_code);
if ($exit_code !== 0) {
throw new \Exception('Composer returned a non-zero exit code');
}
}
/**
* Makes sure the given directory exists and has no content.
*
* @param string $directory
*/
protected function ensureDirectoryExistsAndClear($directory) {
if (is_dir($directory)) {
$this->fs->removeDirectory($directory);
}
mkdir($directory, 0777, true);
}
}