Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #29 from tienvx/fix-composer-v1-not-installed
Browse files Browse the repository at this point in the history
Fix composer v1 is not installed in lowest dependencies mode
  • Loading branch information
tienvx authored Dec 4, 2022
2 parents cf0473b + c8586c2 commit 849489d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 40 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ jobs:
matrix:
php: [8.0, 8.1]
os: [ubuntu-latest, macos-latest, windows-latest]
composer: [v1, v2]
dependencies: [ 'lowest', 'highest' ]
name: PHP ${{ matrix.php }} - OS ${{ matrix.os }} - Composer ${{ matrix.composer }} - Dependencies ${{ matrix.dependencies }}
name: PHP ${{ matrix.php }} - OS ${{ matrix.os }} - Dependencies ${{ matrix.dependencies }}
steps:
- uses: actions/checkout@v3
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, zip, rar, bz2
coverage: pcov
tools: composer:${{ matrix.composer }}
tools: composer:v2, php-cs-fixer
- name: Install unrar on Ubuntu with PHP 8.1
run: sudo apt install unrar
if: ${{ matrix.os == 'ubuntu-latest' && matrix.php == '8.1' }}
Expand All @@ -36,7 +35,7 @@ jobs:
dependency-versions: ${{ matrix.dependencies }}
composer-options: "--no-progress --no-suggest"
- name: Run PHP CS Fixer
run: ./vendor/bin/php-cs-fixer fix --diff --dry-run
run: php-cs-fixer fix --diff --dry-run
- name: Run tests
run: ./vendor/bin/phpunit
- name: Upload coverage results to Coveralls
Expand All @@ -45,4 +44,4 @@ jobs:
run: |
composer global require php-coveralls/php-coveralls
php-coveralls --coverage_clover=clover.xml -v
if: ${{ github.event_name == 'push' && matrix.os == 'ubuntu-latest' && matrix.composer == 'v2' && matrix.php == '8.1' && matrix.dependencies == 'highest' }}
if: ${{ github.event_name == 'push' && matrix.os == 'ubuntu-latest' && matrix.php == '8.1' && matrix.dependencies == 'highest' }}
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
"symfony/finder": "^4.4.23 || ^5.4 || ^6.0"
},
"require-dev": {
"composer/composer": "^1.6 || ^2.2",
"composer/composer": "^1.10.26 || ^2.2",
"phpunit/phpunit": "^9.5.10",
"friendsofphp/php-cs-fixer": "^3.4",
"symfony/process": "^4.4 || ^5.4 || ^6.0",
"php-vfs/php-vfs": "^1.4"
},
Expand Down
21 changes: 3 additions & 18 deletions doc/TESTS.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
# Automated Tests

To run the tests, you will need `composer` and `phpunit` in the `PATH`.
To run the tests:

```
[~/composer-downloads-plugin] which composer
/usr/local/bin/composer
[~/composer-downloads-plugin] which phpunit
/usr/local/bin/phpunit
[~/src/composer-downloads-plugin] phpunit
PHPUnit 9.5.26 by Sebastian Bergmann and contributors.
.................................... 36 / 36 (100%)
Time: 00:06.560, Memory: 30.38 MB
OK (36 tests, 172 assertions)
Generating code coverage report in Clover XML format ... done [00:00.035]
./vendor/bin/phpunit
```

To debug, set project dir so it will not be removed after running:

```
env USE_TEST_PROJECT=$HOME/my-project DEBUG_COMPOSER=1 phpunit tests/Integration/InstallTest.php
env USE_TEST_PROJECT=$HOME/my-project DEBUG_COMPOSER=1 ./vendor/bin/phpunit tests/Integration/InstallTest.php
```
21 changes: 10 additions & 11 deletions tests/Unit/Handler/FileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,31 +91,30 @@ protected function assertDownload(): void
$this->composer->expects($this->once())->method('getLoop')->willReturn($loop);
$this->filesystem->expects($this->once())->method('rename')->with($tmpFile, $this->getTargetFilePath());
} else {
$tmpDir = null;
$tmpFile = $tmpDir.\DIRECTORY_SEPARATOR.'/file';
$downloader = $this->createMock(FileDownloader::class);
$this->filesystem
->expects($this->once())
->method('ensureDirectoryExists')
->with($this->callback(function (string $dir) use (&$tmpDir): bool {
->with($this->callback(function (string $dir) use ($downloader): bool {
$this->assertStringContainsString(\dirname($this->getTargetFilePath()), $dir);
$this->assertStringContainsString(FileHandler::TMP_PREFIX, $dir);
$tmpDir = $dir;
$tmpFile = $tmpDir.\DIRECTORY_SEPARATOR.'/file';
$downloader
->expects($this->once())
->method('download')
->with($this->isInstanceOf(Subpackage::class), $tmpDir)
->willReturn($tmpFile);
$this->filesystem->expects($this->once())->method('rename')->with($tmpFile, $this->getTargetFilePath());
$this->filesystem->expects($this->once())->method('remove')->with($tmpDir);

return true;
}));
$downloader = $this->createMock(FileDownloader::class);
$downloader
->expects($this->once())
->method('download')
->with($this->isInstanceOf(Subpackage::class), $tmpDir)
->willReturn($tmpFile);
$this->downloadManager
->expects($this->once())
->method('getDownloader')
->with('file')
->willReturn($downloader);
$this->filesystem->expects($this->once())->method('rename')->with($tmpFile, $this->getTargetFilePath());
$this->filesystem->expects($this->once())->method('remove')->with($tmpDir);
}
}

Expand Down
56 changes: 52 additions & 4 deletions tests/Unit/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
use Composer\Composer;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\DependencyResolver\PolicyInterface;
use Composer\DependencyResolver\Pool;
use Composer\DependencyResolver\Request;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Package\RootPackageInterface;
use Composer\Repository\CompositeRepository;
use Composer\Repository\InstalledRepositoryInterface;
use Composer\Repository\RepositoryInterface;
use Composer\Repository\RepositoryManager;
Expand All @@ -26,15 +30,13 @@ class PluginTest extends TestCase
private Plugin $plugin;
private Composer|MockObject $composer;
private IOInterface|MockObject $io;
private RepositoryInterface|MockObject $repository;

protected function setUp(): void
{
$this->installer = $this->createMock(PackageInstaller::class);
$this->plugin = new Plugin($this->installer);
$this->composer = $this->createMock(Composer::class);
$this->io = $this->createMock(IOInterface::class);
$this->repository = $this->createMock(RepositoryInterface::class);
}

public function testGetSubscribedEvents(): void
Expand Down Expand Up @@ -97,7 +99,30 @@ public function testInstallDownloads(): void
->expects($this->once())
->method('install')
->with($package, $this->composer, $this->io);
$event = new PackageEvent('name', $this->composer, $this->io, false, $this->repository, [], new InstallOperation($package));
if (version_compare(Composer::RUNTIME_API_VERSION, '2.0.0') >= 0) {
$event = new PackageEvent(
'name',
$this->composer,
$this->io,
false,
$this->createMock(RepositoryInterface::class),
[],
new InstallOperation($package)
);
} else {
$event = new PackageEvent(
'name',
$this->composer,
$this->io,
false,
$this->createMock(PolicyInterface::class),
$this->createMock(Pool::class),
$this->createMock(CompositeRepository::class),
$this->createMock(Request::class),
[],
new InstallOperation($package)
);
}
$this->plugin->installDownloads($event);
}

Expand All @@ -109,7 +134,30 @@ public function testUpdateDownloads(): void
->expects($this->once())
->method('install')
->with($target, $this->composer, $this->io);
$event = new PackageEvent('name', $this->composer, $this->io, false, $this->repository, [], new UpdateOperation($initial, $target));
if (version_compare(Composer::RUNTIME_API_VERSION, '2.0.0') >= 0) {
$event = new PackageEvent(
'name',
$this->composer,
$this->io,
false,
$this->createMock(RepositoryInterface::class),
[],
new UpdateOperation($initial, $target)
);
} else {
$event = new PackageEvent(
'name',
$this->composer,
$this->io,
false,
$this->createMock(PolicyInterface::class),
$this->createMock(Pool::class),
$this->createMock(CompositeRepository::class),
$this->createMock(Request::class),
[],
new UpdateOperation($initial, $target)
);
}
$this->plugin->updateDownloads($event);
}
}

0 comments on commit 849489d

Please sign in to comment.