Skip to content

Commit

Permalink
chore: recommend 'pre-autoload-dump' for cleanup command so it works …
Browse files Browse the repository at this point in the history
…with 'optimize-autoloader' (#2063)
  • Loading branch information
bshaffer authored Jun 9, 2021
1 parent 33b28ac commit e9ef4c2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ you want to keep in `composer.json`:
"google/apiclient": "^2.9"
},
"scripts": {
"post-update-cmd": "Google\\Task\\Composer::cleanup"
"pre-autoload-dump": "Google\\Task\\Composer::cleanup"
},
"extra": {
"google/apiclient-services": [
Expand Down
110 changes: 71 additions & 39 deletions tests/Google/Task/ComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@

class Google_Task_ComposerTest extends BaseTest
{
private static $composerBaseConfig = [
'repositories' => [
[
'type' => 'path',
'url' => __DIR__ . '/../../..',
'options' => [
'symlink' => false
]
]
],
'require' => [
'google/apiclient' => '*'
],
'scripts' => [
'pre-autoload-dump' => 'Google\Task\Composer::cleanup'
],
'minimum-stability' => 'dev',
];

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Google service "Foo" does not exist
Expand Down Expand Up @@ -147,26 +166,7 @@ private function createMockEvent(

public function testE2E()
{
$composer = [
'repositories' => [
[
'type' => 'path',
'url' => __DIR__ . '/../../..',
'options' => [
'symlink' => false
]
]
],
'require' => [
'google/apiclient' => '*'
],
'scripts' => [
'post-update-cmd' => 'Google\Task\Composer::cleanup'
],
'minimum-stability' => 'dev',
];

$composerJson = json_encode($composer + [
$dir = $this->runComposerInstall(self::$composerBaseConfig + [
'extra' => [
'google/apiclient-services' => [
'Drive',
Expand All @@ -175,53 +175,50 @@ public function testE2E()
]
]);

$tmpDir = sys_get_temp_dir() . '/test-' . rand();
mkdir($tmpDir);
file_put_contents($tmpDir . '/composer.json', $composerJson);
passthru('composer install -d ' . $tmpDir);

$serviceDir = $tmpDir . '/vendor/google/apiclient-services/src/Google/Service';
$serviceDir = $dir . '/vendor/google/apiclient-services/src/Google/Service';
$this->assertFileExists($serviceDir . '/Drive.php');
$this->assertFileExists($serviceDir . '/Drive');
$this->assertFileExists($serviceDir . '/YouTube.php');
$this->assertFileExists($serviceDir . '/YouTube');
$this->assertFileNotExists($serviceDir . '/YouTubeReporting.php');
$this->assertFileNotExists($serviceDir . '/YouTubeReporting');

$composerJson = json_encode($composer + [
// Remove the "apiclient-services" directory, which is required to
// update the cleanup command.
passthru('rm -r ' . $dir . '/vendor/google/apiclient-services');

$this->runComposerInstall(self::$composerBaseConfig + [
'extra' => [
'google/apiclient-services' => [
'Drive',
'YouTube',
'YouTubeReporting',
]
]
]);

file_put_contents($tmpDir . '/composer.json', $composerJson);
passthru('rm -r ' . $tmpDir . '/vendor/google/apiclient-services');
passthru('composer update -d ' . $tmpDir);
], $dir);

$this->assertFileExists($serviceDir . '/Drive.php');
$this->assertFileExists($serviceDir . '/Drive');
$this->assertFileExists($serviceDir . '/YouTube.php');
$this->assertFileExists($serviceDir . '/YouTube');
$this->assertFileExists($serviceDir . '/YouTubeReporting.php');
$this->assertFileExists($serviceDir . '/YouTubeReporting');
}

// Test BC Task name
$composer['scripts']['post-update-cmd'] = 'Google_Task_Composer::cleanup';
$composerJson = json_encode($composer + [
public function testE2EBCTaskName()
{
$composerConfig = self::$composerBaseConfig + [
'extra' => [
'google/apiclient-services' => [
'Drive',
]
]
]);
];
// Test BC Task name
$composerConfig['scripts']['pre-autoload-dump'] = 'Google_Task_Composer::cleanup';

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

$this->assertFileExists($serviceDir . '/Drive.php');
$this->assertFileExists($serviceDir . '/Drive');
Expand All @@ -230,4 +227,39 @@ public function testE2E()
$this->assertFileNotExists($serviceDir . '/YouTubeReporting.php');
$this->assertFileNotExists($serviceDir . '/YouTubeReporting');
}

public function testE2EOptimized()
{
$dir = $this->runComposerInstall(self::$composerBaseConfig + [
'config' => [
'optimize-autoloader' => true,
],
'extra' => [
'google/apiclient-services' => [
'Drive'
]
]
]);

$classmap = require_once $dir . '/vendor/composer/autoload_classmap.php';

// Verify removed services do not show up in the classmap
$this->assertArrayHasKey('Google_Service_Drive', $classmap);
$this->assertArrayNotHasKey('Google_Service_YouTube', $classmap);
}

private function runComposerInstall(array $composerConfig, $dir = null)
{
$composerJson = json_encode($composerConfig);

if (is_null($dir)) {
$dir = sys_get_temp_dir() . '/test-' . rand();
mkdir($dir);
}

file_put_contents($dir . '/composer.json', $composerJson);
passthru('composer install -d ' . $dir);

return $dir;
}
}

0 comments on commit e9ef4c2

Please sign in to comment.