Skip to content

Commit

Permalink
Introducing Sources of module updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed Aug 8, 2024
1 parent be374de commit b673c6b
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 33 deletions.
58 changes: 25 additions & 33 deletions classes/UpgradeTools/Module/ModuleDownloaderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,63 +30,55 @@

class ModuleDownloaderContext
{
/** @var string */
private $zipFullPath;

/** @var string */
private $moduleName;

/** @var int */
private $moduleId;
/** @var string */
private $referenceVersion;

/** @var bool */
private $moduleIsLocal = false;
/** @var ModuleSource[]|null */
private $updateSources;

Check failure on line 40 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.8)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::$updateSources has unknown class PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource as its type.

Check failure on line 40 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.4.4)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::$updateSources has unknown class PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource as its type.

Check failure on line 40 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.3.4)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::$updateSources has unknown class PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource as its type.

Check failure on line 40 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.2.5)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::$updateSources has unknown class PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource as its type.

Check failure on line 40 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.6)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::$updateSources has unknown class PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource as its type.

Check failure on line 40 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (latest)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::$updateSources has unknown class PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource as its type.

Check failure on line 40 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.7)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::$updateSources has unknown class PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource as its type.

/**
* @param array{id:string, name:string, is_local:true|null} $moduleInfos
*/
public function __construct(string $zipFullPath, array $moduleInfos)
public function __construct(string $moduleName, string $referenceVersion)
{
$this->zipFullPath = $zipFullPath;
$this->moduleName = $moduleInfos['name'];
$this->moduleId = (int) $moduleInfos['id'];
$this->moduleIsLocal = $moduleInfos['is_local'] ?? false;
$this->validate();
$this->moduleName = $moduleName;
$this->referenceVersion = $referenceVersion;
}

/**
* @throws LogicException
*/
private function validate(): void
public function validate(): void
{
if (empty($this->zipFullPath)) {
throw new LogicException('Path to zip file is invalid.');
}
if (empty($this->moduleName)) {
throw new LogicException('Module name is invalid.');
}
if (empty($this->moduleId)) {
throw new LogicException('Module ID is invalid.');
if (empty($this->updateSources)) {
throw new LogicException('List of updates is invalid.');
}
}

public function getZipFullPath(): string
public function getModuleName(): string
{
return $this->zipFullPath;
return $this->moduleName;
}

public function getModuleName(): string
public function getReferenceVersion(): string
{
return $this->moduleName;
return $this->referenceVersion;
}

public function getModuleId(): int
/**
* @return ModuleSource[]
*/
public function getUpdateSources(): array

Check failure on line 71 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.8)

Method PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::getUpdateSources() has invalid return type PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource.

Check failure on line 71 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.4.4)

Method PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::getUpdateSources() has invalid return type PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource.

Check failure on line 71 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.3.4)

Method PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::getUpdateSources() has invalid return type PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource.

Check failure on line 71 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.2.5)

Method PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::getUpdateSources() has invalid return type PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource.

Check failure on line 71 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.6)

Method PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::getUpdateSources() has invalid return type PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource.

Check failure on line 71 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (latest)

Method PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::getUpdateSources() has invalid return type PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource.

Check failure on line 71 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.7)

Method PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::getUpdateSources() has invalid return type PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource.
{
return $this->moduleId;
return $this->updateSources;
}

public function getModuleIsLocal(): bool
/**
* @param ModuleSource[] $moduleSources
*/
public function setUpdateSources(array $moduleSources): self

Check failure on line 79 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.8)

Parameter $moduleSources of method PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::setUpdateSources() has invalid type PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource.

Check failure on line 79 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.4.4)

Parameter $moduleSources of method PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::setUpdateSources() has invalid type PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource.

Check failure on line 79 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.3.4)

Parameter $moduleSources of method PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::setUpdateSources() has invalid type PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource.

Check failure on line 79 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.2.5)

Parameter $moduleSources of method PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::setUpdateSources() has invalid type PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource.

Check failure on line 79 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.6)

Parameter $moduleSources of method PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::setUpdateSources() has invalid type PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource.

Check failure on line 79 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (latest)

Parameter $moduleSources of method PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::setUpdateSources() has invalid type PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource.

Check failure on line 79 in classes/UpgradeTools/Module/ModuleDownloaderContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.7)

Parameter $moduleSources of method PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext::setUpdateSources() has invalid type PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleSource.
{
return $this->moduleIsLocal;
$this->updateSources = $moduleSources;
return $this;
}
}
56 changes: 56 additions & 0 deletions classes/UpgradeTools/Module/Source/ModuleSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source;

class ModuleSource
{
/** var string */
private $name;

Check failure on line 8 in classes/UpgradeTools/Module/Source/ModuleSource.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.8)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource::$name has no type specified.

Check failure on line 8 in classes/UpgradeTools/Module/Source/ModuleSource.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.4.4)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource::$name has no type specified.

Check failure on line 8 in classes/UpgradeTools/Module/Source/ModuleSource.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.3.4)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource::$name has no type specified.

Check failure on line 8 in classes/UpgradeTools/Module/Source/ModuleSource.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.2.5)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource::$name has no type specified.

Check failure on line 8 in classes/UpgradeTools/Module/Source/ModuleSource.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.6)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource::$name has no type specified.

Check failure on line 8 in classes/UpgradeTools/Module/Source/ModuleSource.php

View workflow job for this annotation

GitHub Actions / PHPStan (latest)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource::$name has no type specified.

Check failure on line 8 in classes/UpgradeTools/Module/Source/ModuleSource.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.7)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource::$name has no type specified.

/** var string */
private $newVersion;

Check failure on line 11 in classes/UpgradeTools/Module/Source/ModuleSource.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.8)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource::$newVersion has no type specified.

Check failure on line 11 in classes/UpgradeTools/Module/Source/ModuleSource.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.4.4)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource::$newVersion has no type specified.

Check failure on line 11 in classes/UpgradeTools/Module/Source/ModuleSource.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.3.4)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource::$newVersion has no type specified.

Check failure on line 11 in classes/UpgradeTools/Module/Source/ModuleSource.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.2.5)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource::$newVersion has no type specified.

Check failure on line 11 in classes/UpgradeTools/Module/Source/ModuleSource.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.6)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource::$newVersion has no type specified.

Check failure on line 11 in classes/UpgradeTools/Module/Source/ModuleSource.php

View workflow job for this annotation

GitHub Actions / PHPStan (latest)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource::$newVersion has no type specified.

Check failure on line 11 in classes/UpgradeTools/Module/Source/ModuleSource.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.7)

Property PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource::$newVersion has no type specified.

/** var string */
private $path;

/** var bool */
private $unzipable;

public function __construct(string $name, string $newVersion, string $path, bool $unzipable)
{
$this->name = $name;
$this->newVersion = $newVersion;
$this->path = $path;
$this->unzipable = $unzipable;
}

public function getName(): string
{
return $this->name;
}

public function getNewVersion(): string
{
return $this->newVersion;
}

public function getPath(): string
{
return $this->path;
}

public function isUnzipable(): bool
{
return $this->unzipable;
}

public function toArray(): array
{
return [
'name' => $this->name,
'newVersion' => $this->newVersion,
'path' => $this->path,
'unzipable' => $this->unzipable,
];
}
}
47 changes: 47 additions & 0 deletions classes/UpgradeTools/Module/Source/ModuleSourceList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source;

use PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\Provider\ModuleSourceProviderInterface;

class ModuleSourceList
{
/** @var ModuleSourceProviderInterface[] */
private $providers;

/**
* @param ModuleSourceProviderInterface[] $sourceProviders
*/
public function __construct(array $sourceProviders)
{
$this->providers = $sourceProviders;
}

/**
* @return ModuleSource[]
*/
public function setSourcesIn(ModuleDownloaderContext $moduleContext): void
{
$updateSources = [];
foreach ($this->providers as $provider) {
$updateSources = array_merge(
$updateSources,
$provider->getUpdatesOfModule(
$moduleContext->getModuleName(),
$moduleContext->getReferenceVersion()
));
}
$moduleContext->setUpdateSources($this->orderSources($updateSources));
}

/**
* @param ModuleSource[]
*
* @return ModuleSource[]
*/
private function orderSources(array $sources): array
{
return $sources;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\Provider;

use PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource;

interface ModuleSourceProviderInterface
{
/** @return ModuleSource[] */
public function getUpdatesOfModule(string $moduleName, string $currentVersion): array;
}
92 changes: 92 additions & 0 deletions tests/unit/UpgradeTools/Module/Source/ModuleSourceListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

use PHPUnit\Framework\TestCase;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\ModuleDownloaderContext;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSourceList;

class ModuleSourceListTest extends TestCase
{
public static function setUpBeforeClass()
{
require_once __DIR__ . '/Provider/ModuleSourceProviderMock.php';
}

public function testUpdateSourcesAreSet()
{
$dummyProvider1 = (new ModuleSourceProviderMock())->setSources([
new ModuleSource('Module1', '3.0.0', __DIR__, false),
]);
$dummyProvider2 = (new ModuleSourceProviderMock())->setSources([
new ModuleSource('Module1', '2.0.0', __DIR__.'.zip', true),
]);
$moduleSourceList = new ModuleSourceList([$dummyProvider1, $dummyProvider2]);

$moduleContext = new ModuleDownloaderContext('Module1', '1.0.0');

$moduleSourceList->setSourcesIn($moduleContext);

$results = $moduleContext->getUpdateSources();

$this->assertSame(2, count($results));
$this->assertEquals(['name' => 'Module1', 'newVersion' => '3.0.0', 'path' => __DIR__, 'unzipable' => false], $results[0]->toArray());
$this->assertEquals(['name' => 'Module1', 'newVersion' => '2.0.0', 'path' => __DIR__.'.zip', 'unzipable' => true], $results[1]->toArray());
}

public function testUpdateSourcesAreOrderedByVersion()
{
$dummyProvider1 = (new ModuleSourceProviderMock())->setSources([
new ModuleSource('Module1', '2.0.0', __DIR__ . '/1', false),
new ModuleSource('Module1', '4.0.0', __DIR__ . '/2', false),
]);
$dummyProvider2 = (new ModuleSourceProviderMock())->setSources([
new ModuleSource('Module1', '3.0.0', __DIR__.'.zip', true),
]);
$moduleSourceList = new ModuleSourceList([$dummyProvider1, $dummyProvider2]);

$moduleContext = new ModuleDownloaderContext('Module1', '1.0.0');

$moduleSourceList->setSourcesIn($moduleContext);

$results = $moduleContext->getUpdateSources();

$this->assertSame(3, count($results));
$this->assertEquals(['name' => 'Module1', 'newVersion' => '4.0.0', 'path' => __DIR__ .'/2', 'unzipable' => false], $results[0]->toArray());
$this->assertEquals(['name' => 'Module1', 'newVersion' => '3.0.0', 'path' => __DIR__.'.zip', 'unzipable' => true], $results[1]->toArray());
$this->assertEquals(['name' => 'Module1', 'newVersion' => '2.0.0', 'path' => __DIR__.'/1', 'unzipable' => false], $results[2]->toArray());
}

public function testUpdateSourcesAreOrderedByProviderPriority()
{
// TODO
}

public function testUpdateSourcesAreFilteredIfNewVersionIsLower()
{
// TODO
}
}
46 changes: 46 additions & 0 deletions tests/unit/UpgradeTools/Module/Source/ModuleSourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

use PHPUnit\Framework\TestCase;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource;

class ModuleSourceTest extends TestCase
{
public function testClassIsProperlyCreated()
{
$moduleName = 'TheModule';
$newVersion = '9.8.7';
$path = '/somewhere/only/we/know.zip';
$unzipable = true;

$source = new ModuleSource($moduleName, $newVersion, $path, $unzipable);

$this->assertSame('TheModule', $source->getName());
$this->assertSame('9.8.7', $source->getNewVersion());
$this->assertSame('/somewhere/only/we/know.zip', $source->getPath());
$this->assertSame(true, $source->isUnzipable());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\Provider\ModuleSourceProviderInterface;

class ModuleSourceProviderMock implements ModuleSourceProviderInterface
{
private $sources;

/** @inheritdoc */
public function getUpdatesOfModule(string $moduleName, string $currentVersion): array
{
return $this->sources;
}

/** @return ModuleSources[] */
public function setSources($sources): self
{
$this->sources = $sources;
return $this;
}
}

0 comments on commit b673c6b

Please sign in to comment.