Skip to content

Commit e9ef4c2

Browse files
authored
chore: recommend 'pre-autoload-dump' for cleanup command so it works with 'optimize-autoloader' (#2063)
1 parent 33b28ac commit e9ef4c2

File tree

2 files changed

+72
-40
lines changed

2 files changed

+72
-40
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ you want to keep in `composer.json`:
6464
"google/apiclient": "^2.9"
6565
},
6666
"scripts": {
67-
"post-update-cmd": "Google\\Task\\Composer::cleanup"
67+
"pre-autoload-dump": "Google\\Task\\Composer::cleanup"
6868
},
6969
"extra": {
7070
"google/apiclient-services": [

tests/Google/Task/ComposerTest.php

Lines changed: 71 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@
1717

1818
class Google_Task_ComposerTest extends BaseTest
1919
{
20+
private static $composerBaseConfig = [
21+
'repositories' => [
22+
[
23+
'type' => 'path',
24+
'url' => __DIR__ . '/../../..',
25+
'options' => [
26+
'symlink' => false
27+
]
28+
]
29+
],
30+
'require' => [
31+
'google/apiclient' => '*'
32+
],
33+
'scripts' => [
34+
'pre-autoload-dump' => 'Google\Task\Composer::cleanup'
35+
],
36+
'minimum-stability' => 'dev',
37+
];
38+
2039
/**
2140
* @expectedException InvalidArgumentException
2241
* @expectedExceptionMessage Google service "Foo" does not exist
@@ -147,26 +166,7 @@ private function createMockEvent(
147166

148167
public function testE2E()
149168
{
150-
$composer = [
151-
'repositories' => [
152-
[
153-
'type' => 'path',
154-
'url' => __DIR__ . '/../../..',
155-
'options' => [
156-
'symlink' => false
157-
]
158-
]
159-
],
160-
'require' => [
161-
'google/apiclient' => '*'
162-
],
163-
'scripts' => [
164-
'post-update-cmd' => 'Google\Task\Composer::cleanup'
165-
],
166-
'minimum-stability' => 'dev',
167-
];
168-
169-
$composerJson = json_encode($composer + [
169+
$dir = $this->runComposerInstall(self::$composerBaseConfig + [
170170
'extra' => [
171171
'google/apiclient-services' => [
172172
'Drive',
@@ -175,53 +175,50 @@ public function testE2E()
175175
]
176176
]);
177177

178-
$tmpDir = sys_get_temp_dir() . '/test-' . rand();
179-
mkdir($tmpDir);
180-
file_put_contents($tmpDir . '/composer.json', $composerJson);
181-
passthru('composer install -d ' . $tmpDir);
182-
183-
$serviceDir = $tmpDir . '/vendor/google/apiclient-services/src/Google/Service';
178+
$serviceDir = $dir . '/vendor/google/apiclient-services/src/Google/Service';
184179
$this->assertFileExists($serviceDir . '/Drive.php');
185180
$this->assertFileExists($serviceDir . '/Drive');
186181
$this->assertFileExists($serviceDir . '/YouTube.php');
187182
$this->assertFileExists($serviceDir . '/YouTube');
188183
$this->assertFileNotExists($serviceDir . '/YouTubeReporting.php');
189184
$this->assertFileNotExists($serviceDir . '/YouTubeReporting');
190185

191-
$composerJson = json_encode($composer + [
186+
// Remove the "apiclient-services" directory, which is required to
187+
// update the cleanup command.
188+
passthru('rm -r ' . $dir . '/vendor/google/apiclient-services');
189+
190+
$this->runComposerInstall(self::$composerBaseConfig + [
192191
'extra' => [
193192
'google/apiclient-services' => [
194193
'Drive',
195194
'YouTube',
196195
'YouTubeReporting',
197196
]
198197
]
199-
]);
200-
201-
file_put_contents($tmpDir . '/composer.json', $composerJson);
202-
passthru('rm -r ' . $tmpDir . '/vendor/google/apiclient-services');
203-
passthru('composer update -d ' . $tmpDir);
198+
], $dir);
204199

205200
$this->assertFileExists($serviceDir . '/Drive.php');
206201
$this->assertFileExists($serviceDir . '/Drive');
207202
$this->assertFileExists($serviceDir . '/YouTube.php');
208203
$this->assertFileExists($serviceDir . '/YouTube');
209204
$this->assertFileExists($serviceDir . '/YouTubeReporting.php');
210205
$this->assertFileExists($serviceDir . '/YouTubeReporting');
206+
}
211207

212-
// Test BC Task name
213-
$composer['scripts']['post-update-cmd'] = 'Google_Task_Composer::cleanup';
214-
$composerJson = json_encode($composer + [
208+
public function testE2EBCTaskName()
209+
{
210+
$composerConfig = self::$composerBaseConfig + [
215211
'extra' => [
216212
'google/apiclient-services' => [
217213
'Drive',
218214
]
219215
]
220-
]);
216+
];
217+
// Test BC Task name
218+
$composerConfig['scripts']['pre-autoload-dump'] = 'Google_Task_Composer::cleanup';
221219

222-
file_put_contents($tmpDir . '/composer.json', $composerJson);
223-
passthru('rm -r ' . $tmpDir . '/vendor/google/apiclient-services');
224-
passthru('composer update -d ' . $tmpDir);
220+
$dir = $this->runComposerInstall($composerConfig);
221+
$serviceDir = $dir . '/vendor/google/apiclient-services/src/Google/Service';
225222

226223
$this->assertFileExists($serviceDir . '/Drive.php');
227224
$this->assertFileExists($serviceDir . '/Drive');
@@ -230,4 +227,39 @@ public function testE2E()
230227
$this->assertFileNotExists($serviceDir . '/YouTubeReporting.php');
231228
$this->assertFileNotExists($serviceDir . '/YouTubeReporting');
232229
}
230+
231+
public function testE2EOptimized()
232+
{
233+
$dir = $this->runComposerInstall(self::$composerBaseConfig + [
234+
'config' => [
235+
'optimize-autoloader' => true,
236+
],
237+
'extra' => [
238+
'google/apiclient-services' => [
239+
'Drive'
240+
]
241+
]
242+
]);
243+
244+
$classmap = require_once $dir . '/vendor/composer/autoload_classmap.php';
245+
246+
// Verify removed services do not show up in the classmap
247+
$this->assertArrayHasKey('Google_Service_Drive', $classmap);
248+
$this->assertArrayNotHasKey('Google_Service_YouTube', $classmap);
249+
}
250+
251+
private function runComposerInstall(array $composerConfig, $dir = null)
252+
{
253+
$composerJson = json_encode($composerConfig);
254+
255+
if (is_null($dir)) {
256+
$dir = sys_get_temp_dir() . '/test-' . rand();
257+
mkdir($dir);
258+
}
259+
260+
file_put_contents($dir . '/composer.json', $composerJson);
261+
passthru('composer install -d ' . $dir);
262+
263+
return $dir;
264+
}
233265
}

0 commit comments

Comments
 (0)