Skip to content
Permalink
Browse files

add some test for _basename()

  • Loading branch information...
saeideng committed Feb 26, 2018
1 parent 902e05e commit 56271d933344f2a6489ab5ef7f946e0c067d97a9
Showing with 51 additions and 3 deletions.
  1. +3 −3 src/Filesystem/File.php
  2. +48 −0 tests/TestCase/Filesystem/FileTest.php
@@ -345,7 +345,7 @@ public function name()
$this->info();
}
if (isset($this->info['extension'])) {
return $this->_basename($this->name, '.' . $this->info['extension']);
return static::_basename($this->name, '.' . $this->info['extension']);
}
if ($this->name) {
return $this->name;
@@ -361,7 +361,7 @@ public function name()
* @param string|null $ext The name of the extension
* @return string the file basename.
*/
protected function _basename($path, $ext = null)
protected static function _basename($path, $ext = null)
{
$splInfo = new SplFileInfo($path);
$name = ltrim($splInfo->getFilename(), DS);
@@ -388,7 +388,7 @@ public function safe($name = null, $ext = null)
$ext = $this->ext();
}
return preg_replace("/(?:[^\w\.-]+)/", '_', $this->_basename($name, $ext));
return preg_replace("/(?:[^\w\.-]+)/", '_', static::_basename($name, $ext));
}
/**
@@ -137,6 +137,54 @@ public function testUtf8Filenames()
$this->assertTrue($File->readable());
}
/**
* Test _basename method
* @dataProvider baseNameValueProvider
* @return void
*/
public function testBasename($path, $suffix)
{
$class = new \ReflectionClass('Cake\Filesystem\File');
$method = $class->getMethod('_basename');
$method->setAccessible(true);
if ($suffix === null) {
$this->assertEquals(basename($path), $method->invokeArgs(null, [$path]));
} else {
$this->assertEquals(basename($path, $suffix), $method->invokeArgs(null, [$path, $suffix]));
}
}
/**
* Data provider for testBasename().
*
* @return array
*/
public function baseNameValueProvider()
{
return [
['folder/نام.txt', null],
['folder/نام فارسی.txt', null],
['نام.txt', null],
['نام فارسی.txt', null],
['/نام.txt', null],
['/نام فارسی.txt', null],
//
['folder/نام.txt', '.txt'],
['folder/نام فارسی.txt', '.txt'],
['نام.txt', '.txt'],
['نام فارسی.txt', '.txt'],
['/نام.txt', '.txt'],
['/نام فارسی.txt', '.txt'],
//
['/etc/sudoers.d', null],
['/etc/sudoers.d', '.d'],
['/etc/passwd', null],
['/etc/', null],
['.', null],
['/', null],
];
}
/**
* testPermission method
*

0 comments on commit 56271d9

Please sign in to comment.
You can’t perform that action at this time.