-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing Sources of module updates
- Loading branch information
1 parent
52399ce
commit 8d401aa
Showing
7 changed files
with
311 additions
and
33 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
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,56 @@ | ||
<?php | ||
|
||
namespace PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source; | ||
|
||
class ModuleSource | ||
{ | ||
/** var string */ | ||
private $name; | ||
|
||
/** var string */ | ||
private $newVersion; | ||
|
||
/** 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, | ||
]; | ||
} | ||
} |
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,53 @@ | ||
<?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 | ||
{ | ||
usort($sources, function (ModuleSource $source1, ModuleSource $source2) { | ||
return version_compare($source2->getNewVersion(), $source1->getNewVersion()); | ||
|
||
// TODO: Add provider priority check when versions are the same | ||
}); | ||
|
||
return $sources; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
classes/UpgradeTools/Module/Source/Provider/ModuleSourceProviderInterface.php
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,16 @@ | ||
<?php | ||
|
||
namespace PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\Provider; | ||
|
||
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource; | ||
|
||
interface ModuleSourceProviderInterface | ||
{ | ||
/** | ||
* The provider is able to return a list of new versions for a given module in a current version. | ||
* If the module is unknown to the provider or if there is no new version available, the array is empty. | ||
* | ||
* @return ModuleSource[] | ||
*/ | ||
public function getUpdatesOfModule(string $moduleName, string $currentVersion): array; | ||
} |
92 changes: 92 additions & 0 deletions
92
tests/unit/UpgradeTools/Module/Source/ModuleSourceListTest.php
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,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: Will be done in unit tests of providers | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
tests/unit/UpgradeTools/Module/Source/ModuleSourceTest.php
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,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()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/unit/UpgradeTools/Module/Source/Provider/ModuleSourceProviderMock.php
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,22 @@ | ||
<?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; | ||
} | ||
} |