Skip to content

Commit

Permalink
tests: added more tests and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
luislard committed Dec 13, 2023
1 parent 5d8db1a commit 8e8276b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
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
1 change: 0 additions & 1 deletion .phpunit.result.cache

This file was deleted.

36 changes: 28 additions & 8 deletions src/WpDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use PHPUnit\Framework\TestCase;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;

use Brain\Monkey;
abstract class AbstractTestCase extends TestCase
{
use MockeryPHPUnitIntegration;
Expand All @@ -40,5 +40,6 @@ protected function tearDown(): void
{
// Monkey\tearDown();
parent::tearDown();

}
}
20 changes: 18 additions & 2 deletions tests/Unit/WpDownloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

0 comments on commit 8e8276b

Please sign in to comment.