Skip to content

Commit

Permalink
Merge pull request #2 from luislard/composer_plugin_api_v2_compat
Browse files Browse the repository at this point in the history
Add support for composer plugin api 2 , Drop support PHP 7 and composer version 1
  • Loading branch information
gmazzap authored Dec 14, 2023
2 parents dba0e81 + 5bb3ffe commit 5c9bbc5
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 25 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/php-unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: PHP Unit Tests

on:
push:
paths:
- '**workflows/php-unit-tests.yml'
- '**.php'
- '**phpunit.xml.dist'
- '**composer.json'
pull_request:
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
unit-tests:
runs-on: ubuntu-latest
if: ${{ !contains(github.event.head_commit.message, 'skip unit') }}

env:
USE_COVERAGE: 'no'

strategy:
fail-fast: false
matrix:
php-versions: [ '8.0', '8.1', '8.2' ]

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
ini-values: zend.assertions=1, error_reporting=E_ALL, display_errors=On
coverage: ${{ env.USE_COVERAGE == 'yes' && 'xdebug' || 'none' }}

- name: Install Composer dependencies
uses: ramsey/composer-install@v2

- name: Run unit tests
run: composer tests
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.phar
/vendor/
composer.lock
.phpunit.result.cache
42 changes: 28 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
{
"name": "wecodemore/wp-downloader",
"description": "A Composer plugin that downloads WordPress releases zip from official repo.",
"type": "composer-plugin",
"license": "MIT",
"authors": [
"name": "wecodemore/wp-downloader",
"description": "A Composer plugin that downloads WordPress releases zip from official repo.",
"type": "composer-plugin",
"license": "MIT",
"authors": [
{
"name": "Giuseppe Mazzapica",
"name": "Giuseppe Mazzapica",
"email": "giuseppe.mazzapica@gmail.com"
}
],
"minimum-stability": "stable",
"require": {
"php": ">=5.4",
"composer-plugin-api": "^1.1"
"require": {
"php": ">=8.0",
"composer-plugin-api": "^2.0"
},
"autoload": {
"classmap": [
"src/"
]
"autoload": {
"psr-4": {
"WCM\\WpDownloader\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"WCM\\WpDownloader\\Tests\\Unit\\": "tests/Unit/"
}
},
"extra": {
"extra": {
"class": "WCM\\WpDownloader\\WpDownloader"
},
"require-dev": {
"brain/monkey": "^2.6",
"phpunit/phpunit": "^9.6",
"composer/composer": "^2.0"
},
"scripts": {
"tests": "@php ./vendor/phpunit/phpunit/phpunit --coverage-text",
"tests:no-cov": "@php ./vendor/phpunit/phpunit/phpunit --no-coverage"
}
}
25 changes: 25 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="tests/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
stopOnError="false"
verbose="true">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="unit">
<directory suffix="Test.php">tests/Unit</directory>
</testsuite>
</testsuites>
</phpunit>
10 changes: 7 additions & 3 deletions src/Unzipper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Composer\Downloader\ZipDownloader;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use React\Promise\Promise;
use React\Promise\PromiseInterface;
use Symfony\Component\Process\ExecutableFinder;

/**
Expand Down Expand Up @@ -51,15 +53,17 @@ public function unzip($zipPath, $target)
}

/**
* This this just un unzipper, we don't download anything.
* This is just an unzipper, we don't download anything.
*
* @param PackageInterface $package
* @param string $path
* @param PackageInterface|null $prevPackage
* @param bool $output
* @return string|void
* @return PromiseInterface
*/
public function download(PackageInterface $package, $path, $output = true)
public function download(PackageInterface $package, string $path, ?PackageInterface $prevPackage = null, bool $output = true): PromiseInterface
{
return new Promise(fn() => null);
}

/**
Expand Down
45 changes: 37 additions & 8 deletions src/WpDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ class WpDownloader implements PluginInterface, EventSubscriberInterface
const RELEASES_URL = 'https://api.wordpress.org/core/version-check/1.7/';
const DOWNLOADS_BASE_URL = 'https://downloads.wordpress.org/release/wordpress-';

public function deactivate(Composer $composer, IOInterface $io)
{
}

public function uninstall(Composer $composer, IOInterface $io)
{
}


/**
* @var array
*/
Expand Down Expand Up @@ -103,23 +112,43 @@ public static function getSubscribedEvents()
*/
public function activate(Composer $composer, IOInterface $io)
{
$this->filesystem = $this->prepareFilesystem();
$this->remoteFilesystem = $this->prepareRemoteSystem($composer, $io);
$this->composer = $composer;
$this->filesystem = new Filesystem();
$this->remoteFilesystem = new RemoteFilesystem($io, $composer->getConfig());
$this->io = $io;
$this->config = $this->prepareConfig($composer);
}

protected function prepareFilesystem(): Filesystem
{
return new Filesystem();
}

protected function prepareRemoteSystem(Composer $composer, IOInterface $io): RemoteFilesystem
{
return new RemoteFilesystem($io, $composer->getConfig());
}

protected function prepareConfig(Composer $composer): array
{
$extra = (array)$composer->getPackage()->getExtra();
$dirs = empty($extra['wordpress-install-dir']) ? [] : $extra['wordpress-install-dir'];
is_string($dirs) and $dirs = [$dirs];
is_array($dirs) and $dirs = array_filter(array_filter($dirs, 'is_string'));
$dirs = $this->wpInstallationDirs($extra);

$default = [
'version' => '',
'no-content' => true,
'target-dir' => (is_array($dirs) && $dirs) ? reset($dirs) : 'wordpress',
'target-dir' => $dirs ? reset($dirs) : 'wordpress',
];
$config = array_key_exists('wp-downloader', $extra) ? $extra['wp-downloader'] : [];
$config = array_merge($default, $config);
$this->config = $config;
return array_merge($default, $config);
}

protected function wpInstallationDirs(array $extra): array
{
$dirs = empty($extra['wordpress-install-dir']) ? [] : $extra['wordpress-install-dir'];
is_string($dirs) and $dirs = [$dirs];
is_array($dirs) and $dirs = array_filter(array_filter($dirs, 'is_string'));
return $dirs;
}

/**
Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/AbstractTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* This file is part of @package Mercury\Blocks.
*
* (c) Inpsyde GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace WCM\WpDownloader\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Brain\Monkey;
abstract class AbstractTestCase extends TestCase
{
use MockeryPHPUnitIntegration;

/**
* Sets up the environment.
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
// Monkey\setUp();
}

/**
* Tears down the environment.
*
* @return void
*/
protected function tearDown(): void
{
// Monkey\tearDown();
parent::tearDown();

}
}
38 changes: 38 additions & 0 deletions tests/Unit/WpDownloaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace WCM\WpDownloader\Tests\Unit;

use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Util\Filesystem;
use Composer\Util\RemoteFilesystem;
use WCM\WpDownloader\WpDownloader;

class WpDownloaderTest extends AbstractTestCase
{
public function testCanInstantiateClass()
{
$instance = new WpDownloader();
$this->assertInstanceOf(WpDownloader::class, $instance);
}

public function testActivate()
{
$mock = \Mockery::mock(WpDownloader::class)->shouldAllowMockingProtectedMethods()->makePartial();
$composerMock = \Mockery::mock(Composer::class);
$ioMock = \Mockery::mock(IOInterface::class);
$filesystemMock = \Mockery::mock(Filesystem::class);
$remoteFsMock = \Mockery::mock(RemoteFilesystem::class);
$mock->shouldReceive('prepareFilesystem')->once()->andReturn($filesystemMock);
$mock->shouldReceive('prepareRemoteSystem')->withArgs([
$composerMock,
$ioMock
])->once()->andReturn($remoteFsMock);
$mock->shouldReceive('prepareConfig')->withArgs([
$composerMock,
])->once();
$mock->activate($composerMock, $ioMock);
}
}
26 changes: 26 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php


declare(strict_types=1);

//phpcs:disable PSR1.Files.SideEffects

$libraryPath = dirname(__DIR__, 1);
$vendorPath = "{$libraryPath}/vendor";
if (!realpath($vendorPath)) {
die('Please install via Composer before running tests.');
}

putenv('LIBRARY_PATH=' . $libraryPath);

if (!defined('PHPUNIT_COMPOSER_INSTALL')) {
define('PHPUNIT_COMPOSER_INSTALL', "{$vendorPath}/autoload.php");
}


require_once "{$vendorPath}/antecedent/patchwork/Patchwork.php";
require_once "{$vendorPath}/autoload.php";

unset($libraryPath, $vendorPath);

//phpcs:enable

0 comments on commit 5c9bbc5

Please sign in to comment.