Skip to content

Commit 5fe9804

Browse files
committed
Get rid of PHPDOX_HOME constant
added tests and testdata for GlobalConfig
1 parent 55ea2aa commit 5fe9804

14 files changed

+116
-36
lines changed

build/phar/autoload.php.in

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ spl_autoload_register(
2121

2222
Phar::mapPhar('phpdox.phar');
2323
define('PHPDOX_PHAR', 'phpdox.phar');
24-
define('PHPDOX_HOME', 'phar://' . PHPDOX_PHAR);
2524

2625
TheSeer\phpDox\Version::setVersion('___VERSION___');
2726

28-
(new TheSeer\phpDox\Factory())->getCLI()->run(
27+
$factory = new TheSeer\phpDox\Factory(new \TheSeer\phpDox\FileInfo('phar://' . PHPDOX_PHAR));
28+
$factory->getCLI()->run(
2929
new TheSeer\phpDox\CLIOptions($_SERVER['argv'])
3030
);
3131

phpdox

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ if (!$found) {
5959
die(1);
6060
}
6161

62-
$factory = new TheSeer\phpDox\Factory();
62+
$factory = new TheSeer\phpDox\Factory(new \TheSeer\phpDox\FileInfo(__DIR__));
6363
$factory->getCLI()->run(
6464
new TheSeer\phpDox\CLIOptions($_SERVER['argv'])
6565
);

src/config/ConfigLoader.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@
4343

4444
class ConfigLoader {
4545

46+
/**
47+
* @var FileInfo
48+
*/
49+
private $homeDir;
50+
51+
/**
52+
* @param FileInfo $homeDir
53+
*/
54+
public function __construct(FileInfo $homeDir) {
55+
$this->homeDir = $homeDir;
56+
}
57+
4658
/**
4759
* @param $fname
4860
*
@@ -108,7 +120,7 @@ private function createInstanceFor($fname) {
108120
ConfigLoaderException::WrongType);
109121
}
110122

111-
return new GlobalConfig($dom, new FileInfo($fname));
123+
return new GlobalConfig($this->homeDir, $dom, new FileInfo($fname));
112124
} catch (fDOMException $e) {
113125
throw new ConfigLoaderException(
114126
"Parsing config file '$fname' failed.",

src/config/GlobalConfig.php

+14-17
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242

4343
class GlobalConfig {
4444

45+
/**
46+
* Directory of phpDox home
47+
* @var Fileinfo
48+
*/
49+
private $homeDir;
50+
4551
/**
4652
* @var fDOMDocument
4753
*/
@@ -59,11 +65,12 @@ class GlobalConfig {
5965
* @param fDOMDocument $cfg A configuration dom
6066
* @param FileInfo $file FileInfo of the cfg file
6167
*/
62-
public function __construct(fDOMDocument $cfg, FileInfo $file) {
68+
public function __construct(FileInfo $home, fDOMDocument $cfg, FileInfo $file) {
6369
if ($cfg->documentElement->nodeName != 'phpdox' ||
6470
$cfg->documentElement->namespaceURI != 'http://xml.phpdox.net/config') {
6571
throw new ConfigException("Not a valid phpDox configuration", ConfigException::InvalidDataStructure);
6672
}
73+
$this->homeDir = $home;
6774
$this->cfg = $cfg;
6875
$this->file = $file;
6976
}
@@ -112,16 +119,10 @@ public function getProjects() {
112119
* @throws ConfigException
113120
*/
114121
private function runResolver($ctx) {
115-
if (defined('PHPDOX_HOME')) {
116-
$home = PHPDOX_HOME;
117-
} else {
118-
$home = realpath(__DIR__.'/../../');
119-
}
120-
121122
$vars = array(
122123
'basedir' => $ctx->getAttribute('basedir', dirname($this->file->getRealPath())),
123124

124-
'phpDox.home' => $home,
125+
'phpDox.home' => $this->homeDir->getPathname(),
125126
'phpDox.file' => $this->file->getPathname(),
126127
'phpDox.version' => Version::getVersion(),
127128

@@ -146,7 +147,6 @@ private function runResolver($ctx) {
146147
throw new ConfigException("Cannot overwrite existing property '$name' in line $line", ConfigException::OverrideNotAllowed);
147148
}
148149
$vars[$name] = $this->resolveValue($property->getAttribute('value'), $vars, $line);
149-
150150
}
151151

152152
foreach($ctx->query('.//*[not(name()="property")]/@*|@*') as $attr) {
@@ -157,23 +157,20 @@ private function runResolver($ctx) {
157157
}
158158

159159
/**
160-
* @param $value
161-
* @param array $vars
162-
* @param $line
160+
* @param string $value
161+
* @param string[] $vars
162+
* @param int $line
163163
*
164-
* @return mixed
164+
* @return string
165165
*/
166166
private function resolveValue($value, Array $vars, $line) {
167167
$result = preg_replace_callback('/\${(.*?)}/',
168168
function($matches) use ($vars, $line) {
169169
if (!isset($vars[$matches[1]])) {
170-
throw new ConfigException("No value for property '{$matches[1]} found in line $line", ConfigException::PropertyNotFound);
170+
throw new ConfigException("No value for property '{$matches[1]}' found in line $line", ConfigException::PropertyNotFound);
171171
}
172172
return $vars[$matches[1]];
173173
}, $value);
174-
if (preg_match('/\${(.*?)}/', $result)) {
175-
$result = $this->resolveValue($result, $vars, $line);
176-
}
177174
return $result;
178175
}
179176

src/shared/Factory.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
class Factory {
5151

5252
/**
53-
* @var array
53+
* @var FileInfo
5454
*/
55-
private $map = array();
55+
private $homeDir;
5656

5757
/**
5858
* @var array
@@ -67,10 +67,8 @@ class Factory {
6767
/**
6868
* @param array $map
6969
*/
70-
public function __construct(array $map = NULL) {
71-
if ($map !== NULL) {
72-
$this->map = $map;
73-
}
70+
public function __construct(FileInfo $home) {
71+
$this->homeDir = $home;
7472
}
7573

7674
public function activateSilentMode() {
@@ -95,7 +93,7 @@ public function getCLI() {
9593
* @return ConfigLoader
9694
*/
9795
public function getConfigLoader() {
98-
return new ConfigLoader();
96+
return new ConfigLoader($this->homeDir);
9997
}
10098

10199

tests/Integration/factoryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class FactoryTest extends \PHPUnit_Framework_TestCase {
5454
private $factory;
5555

5656
protected function setUp() {
57-
$this->factory = new Factory();
57+
$this->factory = new Factory(new FileInfo(__DIR__));
5858
}
5959

6060
/**

tests/Unit/config/GlobalConfigTest.php

+28-7
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private function init($cfgName) {
7474
$this->cfgDom = new fDOMDocument();
7575
$this->cfgDom->load($this->fileInfo->getPathname());
7676
$this->cfgDom->registerNamespace('cfg', 'http://xml.phpdox.net/config');
77-
$this->config = new GlobalConfig($this->cfgDom, $this->fileInfo);
77+
$this->config = new GlobalConfig(new FileInfo('/tmp'), $this->cfgDom, $this->fileInfo);
7878
}
7979

8080
/**
@@ -200,26 +200,47 @@ public function testVariablesGetResolvedCorrectly($expected, $file) {
200200
}
201201

202202
public function resolverProvider() {
203-
204203
return array(
205204
'basedir' => array( $this->baseDir . 'resolver', 'basedir'),
206205

207-
'phpDox.home' => array( realpath(__DIR__ . '/../../..'), 'home'),
206+
'phpDox.home' => array( '/tmp', 'home'),
208207
'phpDox.file' => array( $this->baseDir . 'resolver/file.xml', 'file'),
209208
'phpDox.version' => array(Version::getVersion(), 'phpdox-version'),
210209

210+
'multi' => array( '/tmp ' . Version::getVersion(), 'multi'),
211+
211212
'phpDox.project.name' => array('projectname', 'named'),
212213
'phpDox.project.name[undefined]' => array('unnamed', 'named-undefined'),
213214

214-
// 'phpDox.project.source' => array('source','src'),
215-
// 'phpDox.project.source[undefined]' => array('src','src-undefined'),
216215
'phpDox.project.workdir' => array('output','workdir'),
217216
'phpDox.project.workdir[undefined]' => array('xml','workdir-undefined'),
218217

219-
'phpDox.php.version' => array(PHP_VERSION, 'php-version')
218+
'phpDox.php.version' => array(PHP_VERSION, 'php-version'),
219+
220+
'property-global' => array('propvalue', 'property-global'),
221+
'property-project' => array('propvalue', 'property-project'),
222+
223+
'property-recursive' => array(Version::getVersion(), 'property-recursive')
224+
);
225+
}
226+
227+
/**
228+
* @dataProvider exceptionProvider
229+
* @uses TheSeer\phpDox\Version
230+
*/
231+
public function testInvalidPropertyRequestThrowsException($file, $code) {
232+
$this->init('resolver/' . $file);
233+
$this->setExpectedException('TheSeer\\phpDox\\ConfigException', $code);
234+
$this->config->getProjects();
235+
}
220236

221-
// @todo Add properties, recursive resolving
237+
public function exceptionProvider() {
238+
return array(
239+
'property-internal' => array('property-internal', ConfigException::OverrideNotAllowed),
240+
'property-overwrite' => array('property-overwrite', ConfigException::OverrideNotAllowed),
241+
'property-undefined' => array('property-undefined', ConfigException::PropertyNotFound)
222242
);
223243
}
244+
224245
}
225246
}

tests/data/config/resolver/multi.xml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<phpdox xmlns="http://xml.phpdox.net/config">
3+
4+
<project name="foo" source="${phpDox.home} ${phpDox.version}" />
5+
6+
</phpdox>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<phpdox xmlns="http://xml.phpdox.net/config">
3+
4+
<property name="test" value="propvalue" />
5+
<project source="${test}" />
6+
7+
</phpdox>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<phpdox xmlns="http://xml.phpdox.net/config">
3+
4+
<property name="phpDox.home" value="propvalue" />
5+
<project source="${test}" />
6+
7+
</phpdox>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<phpdox xmlns="http://xml.phpdox.net/config">
3+
4+
<property name="test" value="propvalue" />
5+
<property name="test" value="propvalue2" />
6+
7+
<project name="foo" />
8+
9+
</phpdox>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<phpdox xmlns="http://xml.phpdox.net/config">
3+
4+
5+
<project source="${test}">
6+
<property name="test" value="propvalue" />
7+
</project>
8+
9+
</phpdox>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<phpdox xmlns="http://xml.phpdox.net/config">
3+
4+
<property name="test" value="${phpDox.version}" />
5+
6+
<project name="foo" source="${test}" />
7+
8+
</phpdox>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<phpdox xmlns="http://xml.phpdox.net/config">
3+
4+
<project source="${test}" />
5+
6+
</phpdox>

0 commit comments

Comments
 (0)