Skip to content

Commit

Permalink
Merge pull request #35 from pact-foundation/extra-download-factories
Browse files Browse the repository at this point in the history
refactor(2.0): Add ExtraDownload factories
  • Loading branch information
tienvx authored Apr 17, 2024
2 parents a69ede7 + a764ca7 commit e785c2e
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Factory/ExtraArchiveFactory.php
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),
);
}
}
30 changes: 30 additions & 0 deletions src/Factory/ExtraDownloadFactory.php
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),
);
}
}
10 changes: 10 additions & 0 deletions src/Factory/ExtraDownloadFactoryInterface.php
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;
}
32 changes: 32 additions & 0 deletions tests/Unit/Factory/ExtraArchiveFactoryTest.php
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',
]],
];
}
}
76 changes: 76 additions & 0 deletions tests/Unit/Factory/ExtraDownloadFactoryTest.php
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'],
];
}
}

0 comments on commit e785c2e

Please sign in to comment.