From 8e8276b418c57959500b798116e77bf3e8f72d23 Mon Sep 17 00:00:00 2001 From: Luis Rosales Date: Wed, 13 Dec 2023 01:16:57 +0100 Subject: [PATCH] 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