From a764ca7d5ebd553304abe496d8876db23db86610 Mon Sep 17 00:00:00 2001 From: tienvx Date: Tue, 16 Apr 2024 23:28:56 +0700 Subject: [PATCH] refactor(2.0): Add ExtraDownload factories --- src/Factory/ExtraArchiveFactory.php | 25 ++++++ src/Factory/ExtraDownloadFactory.php | 30 ++++++++ src/Factory/ExtraDownloadFactoryInterface.php | 10 +++ .../Unit/Factory/ExtraArchiveFactoryTest.php | 32 ++++++++ .../Unit/Factory/ExtraDownloadFactoryTest.php | 76 +++++++++++++++++++ 5 files changed, 173 insertions(+) create mode 100644 src/Factory/ExtraArchiveFactory.php create mode 100644 src/Factory/ExtraDownloadFactory.php create mode 100644 src/Factory/ExtraDownloadFactoryInterface.php create mode 100644 tests/Unit/Factory/ExtraArchiveFactoryTest.php create mode 100644 tests/Unit/Factory/ExtraDownloadFactoryTest.php diff --git a/src/Factory/ExtraArchiveFactory.php b/src/Factory/ExtraArchiveFactory.php new file mode 100644 index 0000000..0584156 --- /dev/null +++ b/src/Factory/ExtraArchiveFactory.php @@ -0,0 +1,25 @@ +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), + ); + } +} diff --git a/src/Factory/ExtraDownloadFactory.php b/src/Factory/ExtraDownloadFactory.php new file mode 100644 index 0000000..ca41e1b --- /dev/null +++ b/src/Factory/ExtraDownloadFactory.php @@ -0,0 +1,30 @@ +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), + ); + } +} diff --git a/src/Factory/ExtraDownloadFactoryInterface.php b/src/Factory/ExtraDownloadFactoryInterface.php new file mode 100644 index 0000000..181c47d --- /dev/null +++ b/src/Factory/ExtraDownloadFactoryInterface.php @@ -0,0 +1,10 @@ +attributeManager); + } + + protected function getExtraDownloadClass(): string + { + return ExtraArchive::class; + } + + protected function getAttributesMap(): array + { + return [ + ...parent::getAttributesMap(), + [Attribute::IGNORE, [ + 'dir/*', + '!dir/file1', + ]], + ]; + } +} diff --git a/tests/Unit/Factory/ExtraDownloadFactoryTest.php b/tests/Unit/Factory/ExtraDownloadFactoryTest.php new file mode 100644 index 0000000..935c2f5 --- /dev/null +++ b/tests/Unit/Factory/ExtraDownloadFactoryTest.php @@ -0,0 +1,76 @@ +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'], + ]; + } +}