mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge branch 3 into 4
This commit is contained in:
commit
7fd316d405
@ -18,3 +18,7 @@ SilverStripe\Core\Injector\Injector:
|
||||
factory: SilverStripe\Core\Cache\CacheFactory
|
||||
constructor:
|
||||
namespace: "LeftAndMain_CMSVersion"
|
||||
Psr\SimpleCache\CacheInterface.VersionProvider_composerlock:
|
||||
factory: SilverStripe\Core\Cache\CacheFactory
|
||||
constructor:
|
||||
namespace: "VersionProvider_composerlock"
|
||||
|
@ -12,3 +12,9 @@ SilverStripe\Control\HTTP:
|
||||
must-revalidate: "true"
|
||||
no-transform: "true"
|
||||
vary: "Cookie, X-Forwarded-Protocol, User-Agent, Accept"
|
||||
LeftAndMain:
|
||||
dependencies:
|
||||
versionProvider: %$VersionProvider
|
||||
SilverStripe\Core\Manifest\VersionProvider:
|
||||
modules:
|
||||
silverstripe/framework: Framework
|
||||
|
@ -9,6 +9,39 @@ SilverStripe starts with a fresh database containing no records. `Fixtures` prov
|
||||
to load into the database. The [SapphireTest](api:SilverStripe\Dev\SapphireTest) class takes care of populating a test database with data from
|
||||
fixtures - all we have to do is define them.
|
||||
|
||||
To include your fixture file in your tests, you should define it as your `$fixture_file`:
|
||||
|
||||
|
||||
**mysite/tests/MyNewTest.php**
|
||||
|
||||
:::php
|
||||
<?php
|
||||
|
||||
class MyNewTest extends SapphireTest {
|
||||
|
||||
protected static $fixture_file = 'fixtures.yml';
|
||||
|
||||
}
|
||||
|
||||
You can also use an array of fixture files, if you want to use parts of multiple other tests:
|
||||
|
||||
|
||||
**mysite/tests/MyNewTest.php**
|
||||
|
||||
:::php
|
||||
<?php
|
||||
|
||||
class MyNewTest extends SapphireTest {
|
||||
|
||||
protected static $fixture_file = array(
|
||||
'fixtures.yml',
|
||||
'otherfixtures.yml'
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
Typically, you'd have a separate fixture file for each class you are testing - although overlap between tests is common.
|
||||
|
||||
Fixtures are defined in `YAML`. `YAML` is a markup language which is deliberately simple and easy to read, so it is
|
||||
ideal for fixture generation. Say we have the following two DataObjects:
|
||||
|
||||
|
112
src/Core/Manifest/VersionProvider.php
Normal file
112
src/Core/Manifest/VersionProvider.php
Normal file
@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace SilverStripe\Core\Manifest;
|
||||
|
||||
use SilverStripe\Core\Config\Config;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use SilverStripe\Core\Convert;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
|
||||
/**
|
||||
* The version provider will look up configured modules and examine the composer.lock file
|
||||
* to find the current version installed for each. This is used for the logo title in the CMS
|
||||
* via {@link LeftAndMain::CMSVersion()}
|
||||
*
|
||||
* Example configuration:
|
||||
*
|
||||
* <code>
|
||||
* SilverStripe\Core\Manifest\VersionProvider:
|
||||
* modules:
|
||||
* # package/name: Package Title
|
||||
* silverstripe/framework: Framework
|
||||
* silverstripe/cms: CMS
|
||||
* </code>
|
||||
*/
|
||||
class VersionProvider
|
||||
{
|
||||
/**
|
||||
* Gets a comma delimited string of package titles and versions
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
$modules = $this->getModules();
|
||||
$lockModules = $this->getModuleVersionFromComposer(array_keys($modules));
|
||||
$output = array();
|
||||
foreach ($modules as $module => $title) {
|
||||
$version = isset($lockModules[$module])
|
||||
? $lockModules[$module]
|
||||
: _t('SilverStripe\Core\Manifest\VersionProvider.VERSIONUNKNOWN', 'Unknown');
|
||||
$output[] = $title . ': ' . $version;
|
||||
}
|
||||
return implode(', ', $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the configured core modules to use for the SilverStripe application version. Filtering
|
||||
* is used to ensure that modules can turn the result off for other modules, e.g. CMS can disable Framework.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getModules()
|
||||
{
|
||||
$modules = Config::inst()->get(static::class, 'modules');
|
||||
return $modules ? array_filter($modules) : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to obtain version number from composer.lock if it exists
|
||||
*
|
||||
* @param array $modules
|
||||
* @return array
|
||||
*/
|
||||
public function getModuleVersionFromComposer($modules = array())
|
||||
{
|
||||
$versions = array();
|
||||
$lockData = $this->getComposerLock();
|
||||
if ($lockData && !empty($lockData['packages'])) {
|
||||
foreach ($lockData['packages'] as $package) {
|
||||
if (in_array($package['name'], $modules) && isset($package['version'])) {
|
||||
$versions[$package['name']] = $package['version'];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $versions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load composer.lock's contents and return it
|
||||
*
|
||||
* @param bool $cache
|
||||
* @return array
|
||||
*/
|
||||
protected function getComposerLock($cache = true)
|
||||
{
|
||||
$composerLockPath = BASE_PATH . '/composer.lock';
|
||||
if (!file_exists($composerLockPath)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$lockData = array();
|
||||
$jsonData = file_get_contents($composerLockPath);
|
||||
|
||||
if ($cache) {
|
||||
$cache = Injector::inst()->get(CacheInterface::class . '.VersionProvider_composerlock');
|
||||
$cacheKey = md5($jsonData);
|
||||
if ($versions = $cache->get($cacheKey)) {
|
||||
$lockData = Convert::json2array($versions);
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($lockData) && $jsonData) {
|
||||
$lockData = Convert::json2array($jsonData);
|
||||
|
||||
if ($cache) {
|
||||
$cache->set($cacheKey, $jsonData);
|
||||
}
|
||||
}
|
||||
|
||||
return $lockData;
|
||||
}
|
||||
}
|
92
tests/php/Core/Manifest/VersionProviderTest.php
Normal file
92
tests/php/Core/Manifest/VersionProviderTest.php
Normal file
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace SilverStripe\Core\Tests\Manifest;
|
||||
|
||||
use SilverStripe\Core\Config\Config;
|
||||
use SilverStripe\Core\Manifest\VersionProvider;
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
|
||||
class VersionProviderTest extends SapphireTest
|
||||
{
|
||||
/**
|
||||
* @var VersionProvider
|
||||
*/
|
||||
protected $provider;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->provider = new VersionProvider;
|
||||
}
|
||||
|
||||
public function testGetModules()
|
||||
{
|
||||
Config::modify()->set(VersionProvider::class, 'modules', array(
|
||||
'silverstripe/somepackage' => 'Some Package',
|
||||
'silverstripe/hidden' => '',
|
||||
'silverstripe/another' => 'Another'
|
||||
));
|
||||
|
||||
$result = $this->provider->getModules();
|
||||
$this->assertArrayHasKey('silverstripe/somepackage', $result);
|
||||
$this->assertSame('Some Package', $result['silverstripe/somepackage']);
|
||||
$this->assertArrayHasKey('silverstripe/another', $result);
|
||||
$this->assertArrayNotHasKey('silverstripe/hidden', $result);
|
||||
}
|
||||
|
||||
public function testGetModuleVersionFromComposer()
|
||||
{
|
||||
Config::modify()->set(VersionProvider::class, 'modules', array(
|
||||
'silverstripe/framework' => 'Framework',
|
||||
'silverstripe/siteconfig' => 'SiteConfig'
|
||||
));
|
||||
|
||||
$result = $this->provider->getModules(array('silverstripe/framework'));
|
||||
$this->assertArrayHasKey('silverstripe/framework', $result);
|
||||
$this->assertNotEmpty($result['silverstripe/framework']);
|
||||
}
|
||||
|
||||
public function testGetVersion()
|
||||
{
|
||||
Config::modify()->set(VersionProvider::class, 'modules', array(
|
||||
'silverstripe/framework' => 'Framework',
|
||||
'silverstripe/siteconfig' => 'SiteConfig'
|
||||
));
|
||||
|
||||
$result = $this->provider->getVersion();
|
||||
$this->assertContains('SiteConfig: ', $result);
|
||||
$this->assertContains('Framework: ', $result);
|
||||
$this->assertContains(', ', $result);
|
||||
}
|
||||
|
||||
public function testGetModulesFromComposerLock()
|
||||
{
|
||||
$this->markTestSkipped('Unable to get this passing');
|
||||
|
||||
$mock = $this->getMockBuilder(VersionProvider::class)
|
||||
->setMethods(array('getComposerLock'))
|
||||
->getMock();
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('getComposerLock')
|
||||
->will($this->returnValue(array(
|
||||
'packages' => array(
|
||||
array(
|
||||
'name' => 'silverstripe/somepackage',
|
||||
'version' => '1.2.3'
|
||||
),
|
||||
array(
|
||||
'name' => 'silverstripe/another',
|
||||
'version' => '2.3.4'
|
||||
)
|
||||
)
|
||||
)));
|
||||
|
||||
Config::modify()->set(VersionProvider::class, 'modules', array(
|
||||
'silverstripe/somepackage' => 'Some Package'
|
||||
));
|
||||
|
||||
$result = $mock->getVersion();
|
||||
$this->assertContains('Some Package: 1.2.3', $result);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user