silverstripe-framework/tests/php/Core/Manifest/VersionProviderTest.php

107 lines
3.5 KiB
PHP
Raw Normal View History

2017-07-15 14:20:37 +02:00
<?php
namespace SilverStripe\Core\Tests\Manifest;
2024-02-26 09:48:15 +01:00
use SebastianBergmann\Version;
2024-02-26 10:13:34 +01:00
use Composer\Semver\Comparator;
2024-02-26 09:48:15 +01:00
use SilverStripe\Dev\SapphireTest;
2024-02-26 10:13:34 +01:00
use Psr\SimpleCache\CacheInterface;
2017-07-15 14:20:37 +02:00
use SilverStripe\Core\Config\Config;
2022-09-07 06:08:54 +02:00
use SilverStripe\Core\Injector\Injector;
2017-07-15 14:20:37 +02:00
use SilverStripe\Core\Manifest\VersionProvider;
class VersionProviderTest extends SapphireTest
{
/**
* @var VersionProvider
*/
protected $provider;
2022-09-07 06:08:54 +02:00
protected function setup(): void
{
parent::setup();
$this->clearCache();
}
2024-02-26 09:48:15 +01:00
public function getProvider()
2017-07-15 14:20:37 +02:00
{
2024-02-26 09:48:15 +01:00
$provider = Injector::inst()->get(VersionProvider::class);
return $provider;
2017-07-15 14:20:37 +02:00
}
public function testGetModules()
{
Config::modify()->set(VersionProvider::class, 'modules', [
'silverstripe/mypackage' => 'My Package',
2017-07-15 14:20:37 +02:00
'silverstripe/somepackage' => 'Some Package',
'silverstripe/another' => 'Another',
'cwp/cwp-something' => 'CWP something',
]);
2024-02-26 09:48:15 +01:00
$result = $this->getProvider()->getModules();
$this->assertArrayHasKey('silverstripe/mypackage', $result);
2017-07-15 14:20:37 +02:00
$this->assertArrayHasKey('silverstripe/somepackage', $result);
$this->assertArrayHasKey('silverstripe/another', $result);
$this->assertArrayHasKey('cwp/cwp-something', $result);
}
public function testGetModulesEmpty()
{
Config::modify()->set(VersionProvider::class, 'modules', []);
$this->assertEquals(
['silverstripe/framework' => 'Framework'],
2024-02-26 09:48:15 +01:00
$this->getProvider()->getModules()
);
}
public function testGetModulesNone()
{
Config::modify()->remove(VersionProvider::class, 'modules');
$this->assertEquals(
['silverstripe/framework' => 'Framework'],
2024-02-26 09:48:15 +01:00
$this->getProvider()->getModules()
);
2017-07-15 14:20:37 +02:00
}
public function testGetModuleVersionFromComposer()
{
Config::modify()->set(VersionProvider::class, 'modules', [
'silverstripe/siteconfig' => 'SiteConfig',
2017-07-15 14:20:37 +02:00
'silverstripe/framework' => 'Framework',
]);
2017-07-15 14:20:37 +02:00
2024-02-26 09:48:15 +01:00
$result = $this->getProvider()->getModules(['silverstripe/framework']);
2017-07-15 14:20:37 +02:00
$this->assertArrayHasKey('silverstripe/framework', $result);
$this->assertNotEmpty($result['silverstripe/framework']);
}
public function testGetVersion()
{
Config::modify()->set(VersionProvider::class, 'modules', [
'silverstripe/siteconfig' => 'SiteConfig',
'silverstripe/framework' => 'Framework'
]);
2024-02-26 09:48:15 +01:00
$result = $this->getProvider()->getVersion();
$this->assertStringNotContainsString('SiteConfig: ', $result);
$this->assertStringContainsString('Framework: ', $result);
$this->assertStringNotContainsString(', ', $result);
}
public function testGetModuleVersion()
{
2024-02-26 09:48:15 +01:00
$provider = $this->getProvider();
Config::modify()->set(VersionProvider::class, 'modules', [
'silverstripe/framework' => 'Framework',
]);
2024-02-26 10:21:23 +01:00
$moduleVersion = $provider->getModuleVersion('silverstripe/framework');
$this->assertTrue(Comparator::greaterThanOrEqualTo($moduleVersion, '5.0.0'), "Expected > 5.0.0 but got $moduleVersion");
$result = $provider->getVersion();
$this->assertStringNotContainsString('Framework: 1.2.3', $result);
}
2022-09-07 06:08:54 +02:00
private function clearCache()
{
$cache = Injector::inst()->get(CacheInterface::class . '.VersionProvider');
$cache->clear();
}
2017-07-15 14:20:37 +02:00
}