-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
PlayNiceWithScriptsTest.php
183 lines (162 loc) · 6.32 KB
/
PlayNiceWithScriptsTest.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* This file is part of the Dealerdirect PHP_CodeSniffer Standards
* Composer Installer Plugin package.
*
* @copyright 2022 PHPCodeSniffer Composer Installer Contributors
* @license MIT
*/
namespace Dealerdirect\Composer\Plugin\Installers\PHPCodeSniffer\Tests\IntegrationTest;
use Dealerdirect\Composer\Plugin\Installers\PHPCodeSniffer\Plugin;
use Dealerdirect\Composer\Plugin\Installers\PHPCodeSniffer\Tests\TestCase;
/**
* Test that the plugin does not block other post install/update scripts from running.
*
* This test is about Composer and the plugin, so does not need to be tested against multiple PHPCS versions.
* The behaviour also shouldn't differ between a global vs local Composer install, so only testing one type.
*
* @link https://github.com/PHPCSStandards/composer-installer/pull/105
* @link https://gist.github.com/Potherca/6d615b893c2f93ce391e4c7a5c6fade9
*/
final class PlayNiceWithScriptsTest extends TestCase
{
private $composerConfig = array(
'name' => 'phpcs-composer-installer/dont-block-scripts-test',
'require-dev' => array(
'squizlabs/php_codesniffer' => '*',
'dealerdirect/phpcodesniffer-composer-installer' => '*',
'phpcs-composer-installer/dummy-subdir' => '*',
),
'scripts' => array(
'post-install-cmd' => array(
'echo "post-install-cmd successfully run"',
),
'post-update-cmd' => array(
'echo "post-update-cmd successfully run"',
),
'install-codestandards' => array(
'Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin::run',
'echo "install-codestandards successfully run"',
),
),
);
/**
* Set up test environment before each test.
*/
protected function set_up()
{
$this->createTestEnvironment();
}
/**
* Clean up after each test.
*/
protected function tear_down()
{
$this->removeTestEnvironment();
}
/**
* Test that the plugin does not block other post install/update scripts from running.
*
* @dataProvider dataScriptsAreNotBlockedFromRunning
*
* @param string $secondCommand The command to run after the initial install has been done.
* @param array $expectedOutputs Phrases which are expected to be included in the stdout for the second command.
*
* @return void
*/
public function testScriptsAreNotBlockedFromRunning($command, $expectedOutputs)
{
$this->writeComposerJsonFile($this->composerConfig, static::$tempLocalPath);
/*
* 1. Run an install, all should be fine to begin with.
*/
$installCommand = sprintf(
'composer install -v --no-ansi --working-dir=%s',
escapeshellarg(static::$tempLocalPath)
);
$result = $this->executeCliCommand($installCommand);
$this->assertSame(0, $result['exitcode'], 'Exitcode for initial composer install did not match 0');
if ($this->willPluginOutputShow()) {
$this->assertStringContainsString(
Plugin::MESSAGE_RUNNING_INSTALLER,
$result['stdout'],
'Output from initial composer install missing expected contents.'
);
}
$output = $this->willPluginOutputShow() ? $result['stdout'] : $result['stderr'];
$this->assertStringContainsString(
'post-update-cmd successfully run',
$output,
'Output from initial composer install missing expected contents.'
);
/*
* 2. Run the second command to confirm scripts are not blocked.
*/
$command = sprintf('%s -v --no-ansi --working-dir=%s', $command, escapeshellarg(static::$tempLocalPath));
$result = $this->executeCliCommand($command);
$this->assertSame(0, $result['exitcode'], 'Exitcode for secondary command did not match 0');
foreach ($expectedOutputs as $expected) {
$this->assertStringContainsString(
$expected,
$result['stdout'],
'Output from secondary command missing expected contents.'
);
}
}
/**
* Data provider.
*
* @return array
*/
public function dataScriptsAreNotBlockedFromRunning()
{
$data = array();
$willOutputShow = self::willPluginOutputShow();
$data['install:command'] = array(
'command' => 'composer install',
'expectedOutputs' => array(
'post-install-cmd successfully run',
),
);
if ($willOutputShow) {
$data['install:command']['expectedOutputs'][] = Plugin::MESSAGE_RUNNING_INSTALLER;
}
$data['update:command'] = array(
'command' => 'composer update',
'expectedOutputs' => array(
'post-update-cmd successfully run',
),
);
if ($willOutputShow) {
$data['update:command']['expectedOutputs'][] = Plugin::MESSAGE_RUNNING_INSTALLER;
}
$data['post-install-cmd:script'] = array(
'command' => 'composer run-script post-install-cmd',
'expectedOutputs' => array(
'post-install-cmd successfully run',
),
);
if ($willOutputShow) {
$data['post-install-cmd:command']['expectedOutputs'][] = Plugin::MESSAGE_RUNNING_INSTALLER;
}
$data['post-update-cmd:script'] = array(
'command' => 'composer run-script post-update-cmd',
'expectedOutputs' => array(
'post-update-cmd successfully run',
),
);
if ($willOutputShow) {
$data['post-update-cmd:command']['expectedOutputs'][] = Plugin::MESSAGE_RUNNING_INSTALLER;
}
$data['install-codestandards:script'] = array(
'command' => 'composer run-script install-codestandards',
'expectedOutputs' => array(
'install-codestandards successfully run',
),
);
if ($willOutputShow) {
$data['install-codestandards:command']['expectedOutputs'][] = Plugin::MESSAGE_RUNNING_INSTALLER;
}
return $data;
}
}