123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php
- namespace fphammerle\helpers\tests;
- use \fphammerle\helpers\Path;
- class PathTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * @dataProvider pathProvider
- */
- public function testConstruct($path, $expected_dir_path, $expected_filename, $expected_extension)
- {
- $p = new Path($path);
- if(isset($expected_dir_path)) {
- $this->assertSame($expected_dir_path, $p->dirPath->path);
- } else {
- $this->assertNull($p->dirPath);
- }
- $this->assertSame($expected_filename, $p->filename);
- $this->assertSame($expected_extension, $p->extension);
- }
- /**
- * @dataProvider pathProvider
- */
- public function testToString($expected_path, $dir_path, $filename, $extension)
- {
- $p = new Path;
- $p->dirPath = $dir_path;
- $p->filename = $filename;
- $p->extension = $extension;
- $this->assertSame($expected_path ?: '', (string)$p);
- }
- public function getBasenameProvider()
- {
- return [
- ['.', null, '.'],
- ['..', null, '..'],
- ['file', 'php', 'file.php'],
- ['file', null, 'file'],
- [null, 'php', '.php'],
- [null, null, null],
- ];
- }
- /**
- * @dataProvider getBasenameProvider
- */
- public function testGetBasename($filename, $extension, $expected_basename)
- {
- $p = new Path;
- $p->filename = $filename;
- $p->extension = $extension;
- $this->assertSame($expected_basename, $p->basename);
- }
- public function setBasenameProvider()
- {
- return [
- ['.', '.', null],
- ['..', '..', null],
- ['..txt', '.', 'txt'],
- ['.php', null, 'php'],
- ['file', 'file', null],
- ['file.php', 'file', 'php'],
- [null, null, null],
- ];
- }
- /**
- * @dataProvider setBasenameProvider
- */
- public function testSetBasename($basename, $expected_filename, $expected_extension)
- {
- $p = new Path;
- $p->basename = $basename;
- $this->assertSame($expected_filename, $p->filename);
- $this->assertSame($expected_extension, $p->extension);
- }
- public function dirPathProvider()
- {
- return [
- ['/', '/'],
- ['/dir', '/dir'],
- ['/dir/subdir', '/dir/subdir'],
- ['dir', 'dir'],
- ['dir/subdir', 'dir/subdir'],
- [null, null],
- ];
- }
- /**
- * @dataProvider dirPathProvider
- */
- public function testDirPath($input_dir_path, $expected_dir_path)
- {
- $p = new Path;
- $p->dirPath = $input_dir_path;
- if(isset($input_dir_path)) {
- $this->assertSame($expected_dir_path, $p->dirPath->path);
- } else {
- $this->assertNull($p->dirPath);
- }
- }
- public function extensionProvider()
- {
- return [
- ['.', '.'],
- ['.php', '.php'],
- ['file', 'file'],
- ['file.php', 'file.php'],
- [123, '123'],
- [null, null],
- ];
- }
- /**
- * @dataProvider extensionProvider
- */
- public function testExtension($input_extension, $expected_extension)
- {
- $p = new Path;
- $p->extension = $input_extension;
- $this->assertSame($expected_extension, $p->extension);
- }
- public function filenameProvider()
- {
- return [
- ['.', '.'],
- ['.php', '.php'],
- ['file', 'file'],
- ['file.php', 'file.php'],
- [123, '123'],
- [null, null],
- ];
- }
- /**
- * @dataProvider filenameProvider
- */
- public function testFilename($input_filename, $expected_filename)
- {
- $p = new Path;
- $p->filename = $input_filename;
- $this->assertSame($expected_filename, $p->filename);
- }
- public function pathProvider()
- {
- return [
- ['.', null, '.', null],
- ['..', null, '..', null],
- ['../../file.php', '../..', 'file', 'php'],
- ['../file.php', '..', 'file', 'php'],
- ['./file.php', '.', 'file', 'php'],
- ['dir/../file.php', 'dir/..', 'file', 'php'],
- ['dir/file.php', 'dir', 'file', 'php'],
- ['dir/subdir/file.php', 'dir/subdir', 'file', 'php'],
- ['/file.php', '/', 'file', 'php'],
- ['/dir/file.php', '/dir', 'file', 'php'],
- ['/', '/', null, null],
- ['file.php', null, 'file', 'php'],
- [null, null, null, null],
- ];
- }
- /**
- * @dataProvider pathProvider
- */
- public function testGetPath($expected_path, $dir_path, $filename, $extension)
- {
- $p = new Path;
- $p->dirPath = $dir_path;
- $p->filename = $filename;
- $p->extension = $extension;
- $this->assertSame($expected_path, $p->path);
- }
- /**
- * @dataProvider pathProvider
- */
- public function testSetPath($path, $expected_dir_path, $expected_filename, $expected_extension)
- {
- $p = new Path;
- $p->path = $path;
- if(isset($expected_dir_path)) {
- $this->assertSame($expected_dir_path, $p->dirPath->path);
- } else {
- $this->assertNull($p->dirPath);
- }
- $this->assertSame($expected_filename, $p->filename);
- $this->assertSame($expected_extension, $p->extension);
- }
- /**
- * @dataProvider pathProvider
- */
- public function testSetEqualsGet($path)
- {
- $p = new Path;
- $p->path = $path;
- $this->assertSame($path, $p->path);
- }
- public function isRootProvider()
- {
- return [
- ['.', false],
- ['..', false],
- ['../../file.php', false],
- ['../file.php', false],
- ['./file.php', false],
- ['/', true],
- ['//', false],
- ['/dir/file.php', false],
- ['/file.php', false],
- ['dir/../file.php', false],
- ['dir/file.php', false],
- ['dir/subdir/file.php', false],
- ['file.php', false],
- [null, false],
- ];
- }
- /**
- * @dataProvider isRootProvider
- */
- public function testIsRoot($path, $expected_result)
- {
- $p = new Path;
- $p->path = $path;
- $this->assertSame($expected_result, $p->isRoot());
- }
- public function reverseNameProvider()
- {
- return [
- ['.', '.'],
- ['..', '..'],
- ['../../file.php', '../../elif.php'],
- ['dir/subdir/file.html', 'dir/subdir/elif.lmth'],
- ['file.png', 'elif.gnp'],
- [null, null],
- ];
- }
- /**
- * @dataProvider reverseNameProvider
- */
- public function testReverseName($path, $expected_result)
- {
- $p = new Path($path);
- if(isset($p->filename)) {
- $p->filename = strrev($p->filename);
- }
- if(isset($p->extension)) {
- $p->extension = strrev($p->extension);
- }
- $this->assertSame($expected_result, $p->path);
- }
- }
|