Skip to content

Commit

Permalink
fix #137 by splitting up PhiveXmlConfig into LocalPhiveXmlConfig and …
Browse files Browse the repository at this point in the history
…GlobalPhiveXmlConfig
  • Loading branch information
sebastianheuer committed Jun 26, 2018
1 parent e14c2de commit 4bd427f
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 15 deletions.
12 changes: 11 additions & 1 deletion src/Factory.php
Expand Up @@ -483,7 +483,17 @@ private function getPharInstaller() {
* @return PhiveXmlConfig
*/
private function getPhiveXmlConfig($global) {
return new PhiveXmlConfig(
if ($global) {
return new GlobalPhiveXmlConfig(
new XmlFile(
$this->getPhiveXmlConfigFileLocator()->getFile($global),
'https://phar.io/phive',
'phive'
),
new VersionConstraintParser()
);
}
return new LocalPhiveXmlConfig(
new XmlFile(
$this->getPhiveXmlConfigFileLocator()->getFile($global),
'https://phar.io/phive',
Expand Down
2 changes: 2 additions & 0 deletions src/autoload.php
Expand Up @@ -76,6 +76,7 @@ function($class) {
'phario\\phive\\githubaliasresolverexception' => '/GithubAliasResolverException.php',
'phario\\phive\\githubrepository' => '/shared/repository/GithubRepository.php',
'phario\\phive\\githubversion' => '/shared/version/GitHubVersion.php',
'phario\\phive\\globalphivexmlconfig' => '/shared/config/GlobalPhiveXmlConfig.php',
'phario\\phive\\gnupgkeydownloader' => '/services/key/gpg/GnupgKeyDownloader.php',
'phario\\phive\\gnupgkeydownloaderexception' => '/shared/exceptions/GnupgKeyDownloaderException.php',
'phario\\phive\\gnupgkeyimporter' => '/services/key/gpg/GnupgKeyImporter.php',
Expand Down Expand Up @@ -109,6 +110,7 @@ function($class) {
'phario\\phive\\listcommand' => '/commands/list/ListCommand.php',
'phario\\phive\\localaliasresolver' => '/services/resolver/LocalAliasResolver.php',
'phario\\phive\\localfirstresolvingstrategy' => '/services/resolver/strategy/LocalFirstResolvingStrategy.php',
'phario\\phive\\localphivexmlconfig' => '/shared/config/LocalPhiveXmlConfig.php',
'phario\\phive\\localrepository' => '/shared/repository/LocalRepository.php',
'phario\\phive\\localsourceslistfileloader' => '/shared/sources/LocalSourcesListFileLoader.php',
'phario\\phive\\localsslcertificate' => '/shared/http/LocalSslCertificate.php',
Expand Down
17 changes: 17 additions & 0 deletions src/shared/config/GlobalPhiveXmlConfig.php
@@ -0,0 +1,17 @@
<?php
namespace PharIo\Phive;

use PharIo\FileSystem\Filename;

class GlobalPhiveXmlConfig extends PhiveXmlConfig {

/**
* @param InstalledPhar $installedPhar
*
* @return Filename
*/
protected function getLocation(InstalledPhar $installedPhar) {
return $installedPhar->getLocation()->withAbsolutePath();
}

}
17 changes: 17 additions & 0 deletions src/shared/config/LocalPhiveXmlConfig.php
@@ -0,0 +1,17 @@
<?php
namespace PharIo\Phive;

use PharIo\FileSystem\Filename;

class LocalPhiveXmlConfig extends PhiveXmlConfig {

/**
* @param InstalledPhar $installedPhar
*
* @return Filename
*/
protected function getLocation(InstalledPhar $installedPhar) {
return $installedPhar->getLocation()->getRelativePathTo($this->getOwnDirectory());
}

}
20 changes: 16 additions & 4 deletions src/shared/config/PhiveXmlConfig.php
Expand Up @@ -6,7 +6,7 @@
use PharIo\Version\Version;
use PharIo\Version\VersionConstraintParser;

class PhiveXmlConfig {
abstract class PhiveXmlConfig {

/**
* @var XmlFile
Expand Down Expand Up @@ -43,15 +43,13 @@ public function addPhar(InstalledPhar $installedPhar, RequestedPhar $requestedPh
$this->configFile->addElement($pharNode);
}

$xmlFileDirectory = $this->configFile->getDirectory();

if ($requestedPhar->hasUrl()) {
$pharNode->setAttribute('url', $requestedPhar->getUrl()->asString());
}

$pharNode->setAttribute('version', $installedPhar->getVersionConstraint()->asString());
$pharNode->setAttribute('installed', $installedPhar->getInstalledVersion()->getVersionString());
$pharNode->setAttribute('location', $installedPhar->getLocation()->getRelativePathTo($xmlFileDirectory));
$pharNode->setAttribute('location', $this->getLocation($installedPhar));

if ($installedPhar->isCopy()) {
$pharNode->setAttribute('copy', 'true');
Expand Down Expand Up @@ -281,6 +279,20 @@ public function setTargetDirectory(Directory $directory) {
$node->nodeValue = $directory->getRelativePathTo($xmlFileDirectory);
}

/**
* @param InstalledPhar $installedPhar
*
* @return Filename
*/
abstract protected function getLocation(InstalledPhar $installedPhar);

/**
* @return Directory
*/
protected function getOwnDirectory() {
return $this->configFile->getDirectory();
}

/**
* @return \DOMNode
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/autoload.php
Expand Up @@ -46,7 +46,7 @@ function($class) {
'phario\\phive\\pharregistrytest' => '/unit/shared/PharRegistryTest.php',
'phario\\phive\\pharservicetest' => '/unit/services/phar/PharServiceTest.php',
'phario\\phive\\phartest' => '/unit/shared/phar/PharTest.php',
'phario\\phive\\phivexmlconfigtest' => '/unit/shared/config/PhiveXmlConfigTest.php',
'phario\\phive\\LocalPhiveXmlConfigTest' => '/unit/shared/config/PhiveXmlConfigTest.php',
'phario\\phive\\purgecommandtest' => '/unit/commands/purge/PurgeCommandTest.php',
'phario\\phive\\regressiontestbootstrap' => '/regression/RegressionTestBootstrap.php',
'phario\\phive\\regressiontests\\installcommandtest' => '/regression/InstallCommandTest.php',
Expand Down
6 changes: 3 additions & 3 deletions tests/regression/RegressionTestCase.php
Expand Up @@ -4,9 +4,9 @@
use PharIo\FileSystem\Directory;
use PharIo\FileSystem\File;
use PharIo\FileSystem\Filename;
use PharIo\Phive\LocalPhiveXmlConfig;
use PharIo\Phive\Phar;
use PharIo\Phive\PharRegistry;
use PharIo\Phive\PhiveXmlConfig;
use PharIo\Phive\XmlFile;
use PharIo\Version\Version;
use PharIo\Version\VersionConstraintParser;
Expand Down Expand Up @@ -130,10 +130,10 @@ protected function usePhiveXmlConfig($filename) {
}

/**
* @return PhiveXmlConfig
* @return LocalPhiveXmlConfig
*/
protected function getPhiveXmlConfig() {
return new PhiveXmlConfig(
return new LocalPhiveXmlConfig(
new XmlFile(
$this->getWorkingDirectory()->file('phive.xml'),
'https://phar.io/phive',
Expand Down
25 changes: 24 additions & 1 deletion tests/regression/fixtures/phive-home/repositories.xml
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<repositories xmlns="https://phar.io/repository-list">

<phar alias="phpab" composer="theseer/autoload">
<repository type="github" url="https://api.github.com/repos/theseer/autoload/releases" />
</phar>
Expand Down Expand Up @@ -49,4 +48,28 @@
<phar alias="pipelines" composer="ktomk/pipelines">
<repository type="github" url="https://api.github.com/repos/ktomk/pipelines/releases" />
</phar>
<phar alias="phing" composer="phing/phing">
<repository type="github" url="https://api.github.com/repos/phingofficial/phing/releases" />
</phar>
<phar alias="psalm" composer="vimeo/psalm">
<repository type="github" url="https://api.github.com/repos/vimeo/psalm/releases" />
</phar>
<phar alias="clapi" composer="ngabor84/clapi">
<repository type="github" url="https://api.github.com/repos/ngabor84/clapi/releases" />
</phar>
<phar alias="sshman" composer="mwender/sshman">
<repository type="github" url="https://api.github.com/repos/mwender/sshman/releases" />
</phar>
<phar alias="php-coveralls" composer="php-coveralls/php-coveralls">
<repository type="github" url="https://api.github.com/repos/php-coveralls/php-coveralls/releases" />
</phar>
<phar alias="php-cs-fixer" composer="friendsofphp/php-cs-fixer">
<repository type="github" url="https://api.github.com/repos/FriendsOfPHP/PHP-CS-Fixer/releases" />
</phar>
<phar alias="phan" composer="phan/phan">
<repository type="github" url="https://api.github.com/repos/phan/phan/releases" />
</phar>
<phar alias="ecc" composer="ngabor84/ecc">
<repository type="github" url="https://api.github.com/repos/ngabor84/ecc/releases" />
</phar>
</repositories>
Expand Up @@ -11,8 +11,9 @@

/**
* @covers \PharIo\Phive\PhiveXmlConfig
* @covers \PharIo\Phive\LocalPhiveXmlConfig
*/
class PhiveXmlConfigTest extends TestCase {
class LocalPhiveXmlConfigTest extends TestCase {

public function testAddPharUpdatesExistingNode() {
$node = $this->getDomElementMock();
Expand Down Expand Up @@ -46,7 +47,7 @@ public function testAddPharUpdatesExistingNode() {
$targetDirectory = $this->getDirectoryMock();
$targetDirectory->method('getRelativePathTo')->willReturn($this->getDirectoryMock());

$config = new PhiveXmlConfig($configFile, $this->getVersionConstraintParserMock());
$config = new LocalPhiveXmlConfig($configFile, $this->getVersionConstraintParserMock());

$configFile->expects($this->once())->method('save');
$configFile->method('getDirectory')->willReturn($this->getDirectoryMock());
Expand All @@ -57,7 +58,7 @@ public function testAddPharUpdatesExistingNode() {
public function testFindsPharNodesWithoutMatchingCase()
{
$xmlFile = new XmlFile(new Filename(__DIR__ . '/fixtures/phive.xml'), 'https://phar.io/phive', 'phive');
$config = new PhiveXmlConfig($xmlFile, new VersionConstraintParser());
$config = new LocalPhiveXmlConfig($xmlFile, new VersionConstraintParser());
$this->assertTrue($config->hasPhar('theseer/AUTOLOAD'));
}

Expand Down Expand Up @@ -93,7 +94,7 @@ public function testAddPharCreatesNewNode() {
$phar = $this->getRequestedPharMock();
$phar->method('getAlias')->willReturn($alias);

$config = new PhiveXmlConfig($configFile, $this->getVersionConstraintParserMock());
$config = new LocalPhiveXmlConfig($configFile, $this->getVersionConstraintParserMock());

$configFile->expects($this->once())->method('save');
$configFile->method('getDirectory')->willReturn($this->getDirectoryMock());
Expand Down Expand Up @@ -150,7 +151,7 @@ public function testGetPharsReturnsExpectedPhars() {
$configFile->method('query')->with('//phive:phar')
->willReturn([$node1, $node2, $node3]);

$config = new PhiveXmlConfig($configFile, $parserMock);
$config = new LocalPhiveXmlConfig($configFile, $parserMock);
$expected = [
new ConfiguredPhar('https://example.com/phpunit-5.3.0.phar', new AnyVersionConstraint(), null, null, new PharUrl('https://example.com/phpunit-5.3.0.phar')),
new ConfiguredPhar('phpunit', new AnyVersionConstraint(), new Version('5.2.12'), new Filename(__DIR__ . '/fixtures/tools/phpunit')),
Expand Down

0 comments on commit 4bd427f

Please sign in to comment.