Skip to content

Commit 988c131

Browse files
committed
Merge pull request #95 from dantleech/fix-nodename-helper
Allow node names to be passed to getNodeName
2 parents 843d22f + a9a1d83 commit 988c131

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/PHPCR/Util/PathHelper.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,15 @@ public static function getParentPath($path)
229229
*/
230230
public static function getNodeName($path)
231231
{
232-
return substr($path, strrpos($path, '/') + 1);
232+
$strrpos = strrpos($path, '/');
233+
234+
if (false === $strrpos) {
235+
self::error(sprintf(
236+
'Path "%s" must be an absolute path', $path
237+
), true);
238+
}
239+
240+
return substr($path, $strrpos + 1);
233241
}
234242

235243
/**

tests/PHPCR/Tests/Util/PathHelperTest.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,21 +249,30 @@ public function testGetParentPathRoot()
249249
$this->assertEquals('/', PathHelper::getParentPath('/'));
250250
}
251251

252-
// getNodeName tests
253-
254-
public function testGetNodeName()
252+
public function provideGetNodeName()
255253
{
256-
$this->assertEquals('child', PathHelper::getNodeName('/parent/child'));
254+
return array(
255+
array('/parent/child', 'child'),
256+
array('/parent/ns:child', 'ns:child'),
257+
array('/', ''),
258+
);
257259
}
258260

259-
public function testGetNodeNameNamespaced()
261+
/**
262+
* @dataProvider provideGetNodeName
263+
*/
264+
public function testGetNodeName($path, $expected = null)
260265
{
261-
$this->assertEquals('ns:child', PathHelper::getNodeName('/parent/ns:child'));
266+
$this->assertEquals($expected, PathHelper::getNodeName($path));
262267
}
263268

264-
public function testGetNodeNameRoot()
269+
/**
270+
* @expectedException PHPCR\RepositoryException
271+
* @expectedExceptionMessage must be an absolute path
272+
*/
273+
public function testGetNodeNameMustBeAbsolute()
265274
{
266-
$this->assertEquals('', PathHelper::getNodeName('/'));
275+
PathHelper::getNodeName('foobar');
267276
}
268277

269278
public function testGetPathDepth()

0 commit comments

Comments
 (0)