forked from tienvx/composer-downloads-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from pact-foundation/extra-download-factories
refactor(2.0): Add ExtraDownload factories
- Loading branch information
Showing
5 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace LastCall\DownloadsPlugin\Factory; | ||
|
||
use Composer\Package\PackageInterface; | ||
use LastCall\DownloadsPlugin\Composer\Package\ExtraArchive; | ||
use LastCall\DownloadsPlugin\Enum\Attribute; | ||
|
||
class ExtraArchiveFactory extends ExtraDownloadFactory | ||
{ | ||
public function create(string $id, PackageInterface $parent): PackageInterface | ||
{ | ||
return new ExtraArchive( | ||
$parent, | ||
$id, | ||
$this->attributeManager->get(Attribute::VERSION), | ||
$this->attributeManager->get(Attribute::HASH), | ||
$this->attributeManager->get(Attribute::TYPE), | ||
$this->attributeManager->get(Attribute::EXECUTABLE), | ||
$this->attributeManager->get(Attribute::URL), | ||
$this->attributeManager->get(Attribute::PATH), | ||
$this->attributeManager->get(Attribute::IGNORE), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace LastCall\DownloadsPlugin\Factory; | ||
|
||
use Composer\Package\PackageInterface; | ||
use LastCall\DownloadsPlugin\Attribute\AttributeManagerInterface; | ||
use LastCall\DownloadsPlugin\Composer\Package\ExtraDownload; | ||
use LastCall\DownloadsPlugin\Enum\Attribute; | ||
|
||
class ExtraDownloadFactory implements ExtraDownloadFactoryInterface | ||
{ | ||
public function __construct( | ||
protected AttributeManagerInterface $attributeManager, | ||
) { | ||
} | ||
|
||
public function create(string $id, PackageInterface $parent): PackageInterface | ||
{ | ||
return new ExtraDownload( | ||
$parent, | ||
$id, | ||
$this->attributeManager->get(Attribute::VERSION), | ||
$this->attributeManager->get(Attribute::HASH), | ||
$this->attributeManager->get(Attribute::TYPE), | ||
$this->attributeManager->get(Attribute::EXECUTABLE), | ||
$this->attributeManager->get(Attribute::URL), | ||
$this->attributeManager->get(Attribute::PATH), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace LastCall\DownloadsPlugin\Factory; | ||
|
||
use Composer\Package\PackageInterface; | ||
|
||
interface ExtraDownloadFactoryInterface | ||
{ | ||
public function create(string $id, PackageInterface $parent): PackageInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace LastCall\DownloadsPlugin\Tests\Unit\Factory; | ||
|
||
use LastCall\DownloadsPlugin\Composer\Package\ExtraArchive; | ||
use LastCall\DownloadsPlugin\Enum\Attribute; | ||
use LastCall\DownloadsPlugin\Factory\ExtraArchiveFactory; | ||
use LastCall\DownloadsPlugin\Factory\ExtraDownloadFactoryInterface; | ||
|
||
class ExtraArchiveFactoryTest extends ExtraDownloadFactoryTest | ||
{ | ||
protected function createFactory(): ExtraDownloadFactoryInterface | ||
{ | ||
return new ExtraArchiveFactory($this->attributeManager); | ||
} | ||
|
||
protected function getExtraDownloadClass(): string | ||
{ | ||
return ExtraArchive::class; | ||
} | ||
|
||
protected function getAttributesMap(): array | ||
{ | ||
return [ | ||
...parent::getAttributesMap(), | ||
[Attribute::IGNORE, [ | ||
'dir/*', | ||
'!dir/file1', | ||
]], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace LastCall\DownloadsPlugin\Tests\Unit\Factory; | ||
|
||
use Composer\Package\Package; | ||
use Composer\Package\PackageInterface; | ||
use LastCall\DownloadsPlugin\Attribute\AttributeManagerInterface; | ||
use LastCall\DownloadsPlugin\Composer\Package\ExtraDownload; | ||
use LastCall\DownloadsPlugin\Enum\Attribute; | ||
use LastCall\DownloadsPlugin\Enum\Type; | ||
use LastCall\DownloadsPlugin\Factory\ExtraDownloadFactory; | ||
use LastCall\DownloadsPlugin\Factory\ExtraDownloadFactoryInterface; | ||
use LastCall\DownloadsPlugin\Model\Hash; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ExtraDownloadFactoryTest extends TestCase | ||
{ | ||
private PackageInterface $parent; | ||
protected AttributeManagerInterface|MockObject $attributeManager; | ||
private ExtraDownloadFactoryInterface $factory; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->parent = new Package('vendor/package-name', '1.0.0', 'v1.0.0'); | ||
$this->attributeManager = $this->createMock(AttributeManagerInterface::class); | ||
$this->factory = $this->createFactory(); | ||
} | ||
|
||
public function testCreate(): void | ||
{ | ||
$attributesMap = $this->getAttributesMap(); | ||
$this->attributeManager | ||
->expects($this->exactly(\count($attributesMap))) | ||
->method('get') | ||
->willReturnMap($attributesMap); | ||
$extraDownload = $this->factory->create( | ||
$id = 'file-name', | ||
$this->parent | ||
); | ||
$this->assertInstanceOf($this->getExtraDownloadClass(), $extraDownload); | ||
$this->assertSame(sprintf('vendor/package-name:%s', $id), $extraDownload->getName()); | ||
$this->assertSame(ExtraDownload::FAKE_VERSION, $extraDownload->getVersion()); | ||
$this->assertSame('v1.2.3', $extraDownload->getPrettyVersion()); | ||
$this->assertSame('http://example.com/file.zip', $extraDownload->getDistUrl()); | ||
$this->assertSame('zip', $extraDownload->getDistType()); | ||
$this->assertSame('path/to/dir', $extraDownload->getTargetDir()); | ||
$this->assertSame('dist', $extraDownload->getInstallationSource()); | ||
$this->assertSame('extra-download:archive', $extraDownload->getType()); | ||
} | ||
|
||
protected function createFactory(): ExtraDownloadFactoryInterface | ||
{ | ||
return new ExtraDownloadFactory($this->attributeManager); | ||
} | ||
|
||
protected function getExtraDownloadClass(): string | ||
{ | ||
return ExtraDownload::class; | ||
} | ||
|
||
protected function getAttributesMap(): array | ||
{ | ||
return [ | ||
[Attribute::VERSION, 'v1.2.3'], | ||
[Attribute::HASH, new Hash('md5', 'text')], | ||
[Attribute::TYPE, Type::ZIP], | ||
[Attribute::EXECUTABLE, [ | ||
'file1', | ||
'path/to/file2', | ||
]], | ||
[Attribute::URL, 'http://example.com/file.zip'], | ||
[Attribute::PATH, 'path/to/dir'], | ||
]; | ||
} | ||
} |