From 167643c62de9fe8994d61471f8170b7f80efb996 Mon Sep 17 00:00:00 2001 From: Luis Rosales Date: Tue, 21 Nov 2023 01:59:33 +0100 Subject: [PATCH 1/5] Drop support PHP 7 and composer version 1 --- composer.json | 7 +++++-- src/Unzipper.php | 10 +++++++--- src/WpDownloader.php | 9 +++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index c19547e..a17fdb3 100644 --- a/composer.json +++ b/composer.json @@ -11,8 +11,8 @@ ], "minimum-stability": "stable", "require": { - "php": ">=5.4", - "composer-plugin-api": "^1.1" + "php": ">=8.0", + "composer-plugin-api": "^2.0" }, "autoload": { "classmap": [ @@ -21,5 +21,8 @@ }, "extra": { "class": "WCM\\WpDownloader\\WpDownloader" + }, + "require-dev": { + "composer/composer": "^2.0" } } diff --git a/src/Unzipper.php b/src/Unzipper.php index ea28009..c553785 100644 --- a/src/Unzipper.php +++ b/src/Unzipper.php @@ -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; /** @@ -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); } /** diff --git a/src/WpDownloader.php b/src/WpDownloader.php index c8925b5..2c161c6 100644 --- a/src/WpDownloader.php +++ b/src/WpDownloader.php @@ -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 */ From 5d8db1ae6e5243869ee96e8ce1d7a89a93eb12da Mon Sep 17 00:00:00 2001 From: Luis Rosales Date: Wed, 13 Dec 2023 00:03:48 +0100 Subject: [PATCH 2/5] tests: added initial tests and refactor autoload to meet psr-4 --- .phpunit.result.cache | 1 + composer.json | 37 +++++++++++++++++---------- phpunit.xml.dist | 25 +++++++++++++++++++ tests/Unit/AbstractTestCase.php | 44 +++++++++++++++++++++++++++++++++ tests/Unit/WpDownloaderTest.php | 22 +++++++++++++++++ tests/bootstrap.php | 26 +++++++++++++++++++ 6 files changed, 142 insertions(+), 13 deletions(-) create mode 100644 .phpunit.result.cache create mode 100644 phpunit.xml.dist create mode 100644 tests/Unit/AbstractTestCase.php create mode 100644 tests/Unit/WpDownloaderTest.php create mode 100644 tests/bootstrap.php diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 0000000..f8628cf --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +{"version":1,"defects":{"Warning":6,"WCM\\WpDownloader\\Tests\\Unit\\WpDownloaderTest::testCanInstantiateClass":4},"times":{"Warning":0.016,"WCM\\WpDownloader\\Tests\\Unit\\WpDownloaderTest::testCanInstantiateClass":0.045}} \ No newline at end of file diff --git a/composer.json b/composer.json index a17fdb3..fcc2bd8 100644 --- a/composer.json +++ b/composer.json @@ -1,28 +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": ">=8.0", + "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" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..b6562cf --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,25 @@ + + + + + src + + + + + tests/Unit + + + diff --git a/tests/Unit/AbstractTestCase.php b/tests/Unit/AbstractTestCase.php new file mode 100644 index 0000000..f6c48ef --- /dev/null +++ b/tests/Unit/AbstractTestCase.php @@ -0,0 +1,44 @@ +assertInstanceOf(WpDownloader::class, $instance); + } + + public function testActivate() + { + $instance = new WpDownloader(); + $this->assertInstanceOf(WpDownloader::class, $instance); + } +} \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..94fb028 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,26 @@ + Date: Wed, 13 Dec 2023 01:16:57 +0100 Subject: [PATCH 3/5] tests: added more tests and refactor --- .gitignore | 1 + .phpunit.result.cache | 1 - src/WpDownloader.php | 36 +++++++++++++++++++++++++-------- tests/Unit/AbstractTestCase.php | 3 ++- tests/Unit/WpDownloaderTest.php | 20 ++++++++++++++++-- 5 files changed, 49 insertions(+), 12 deletions(-) delete mode 100644 .phpunit.result.cache diff --git a/.gitignore b/.gitignore index e26f45e..6db987f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ composer.phar /vendor/ composer.lock +.phpunit.result.cache diff --git a/.phpunit.result.cache b/.phpunit.result.cache deleted file mode 100644 index f8628cf..0000000 --- a/.phpunit.result.cache +++ /dev/null @@ -1 +0,0 @@ -{"version":1,"defects":{"Warning":6,"WCM\\WpDownloader\\Tests\\Unit\\WpDownloaderTest::testCanInstantiateClass":4},"times":{"Warning":0.016,"WCM\\WpDownloader\\Tests\\Unit\\WpDownloaderTest::testCanInstantiateClass":0.045}} \ No newline at end of file diff --git a/src/WpDownloader.php b/src/WpDownloader.php index 2c161c6..ec024cf 100644 --- a/src/WpDownloader.php +++ b/src/WpDownloader.php @@ -112,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; } /** diff --git a/tests/Unit/AbstractTestCase.php b/tests/Unit/AbstractTestCase.php index f6c48ef..483491d 100644 --- a/tests/Unit/AbstractTestCase.php +++ b/tests/Unit/AbstractTestCase.php @@ -15,7 +15,7 @@ use PHPUnit\Framework\TestCase; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; - +use Brain\Monkey; abstract class AbstractTestCase extends TestCase { use MockeryPHPUnitIntegration; @@ -40,5 +40,6 @@ protected function tearDown(): void { // Monkey\tearDown(); parent::tearDown(); + } } diff --git a/tests/Unit/WpDownloaderTest.php b/tests/Unit/WpDownloaderTest.php index 67fce7d..47968c8 100644 --- a/tests/Unit/WpDownloaderTest.php +++ b/tests/Unit/WpDownloaderTest.php @@ -4,6 +4,10 @@ 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 @@ -16,7 +20,19 @@ public function testCanInstantiateClass() public function testActivate() { - $instance = new WpDownloader(); - $this->assertInstanceOf(WpDownloader::class, $instance); + $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); } } \ No newline at end of file From 1f180910cf6bbef40162acb2008e298443e9a346 Mon Sep 17 00:00:00 2001 From: Luis Rosales Date: Wed, 13 Dec 2023 01:22:27 +0100 Subject: [PATCH 4/5] ci: added automatic tests --- .github/workflows/php-unit-tests.yml | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 .github/workflows/php-unit-tests.yml diff --git a/.github/workflows/php-unit-tests.yml b/.github/workflows/php-unit-tests.yml new file mode 100644 index 0000000..c7cc2f5 --- /dev/null +++ b/.github/workflows/php-unit-tests.yml @@ -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@v + + - 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 \ No newline at end of file From 5bb3ffeca9e4b92e904dc89d39469d6a720f416e Mon Sep 17 00:00:00 2001 From: Luis Rosales Date: Wed, 13 Dec 2023 01:24:04 +0100 Subject: [PATCH 5/5] chore: fix version --- .github/workflows/php-unit-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php-unit-tests.yml b/.github/workflows/php-unit-tests.yml index c7cc2f5..38cbc55 100644 --- a/.github/workflows/php-unit-tests.yml +++ b/.github/workflows/php-unit-tests.yml @@ -29,7 +29,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v + uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2