-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helper/str/dirseparator): added dirSeparatorTrait and relevant t…
…ests
- Loading branch information
Showing
3 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Phalcon. | ||
* | ||
* (c) Phalcon Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phalcon\Traits\Helper\Str; | ||
|
||
use function rtrim; | ||
|
||
use const DIRECTORY_SEPARATOR; | ||
|
||
/** | ||
* Accepts a directory name and ensures that it ends with | ||
* DIRECTORY_SEPARATOR | ||
*/ | ||
trait DirSeparatorTrait | ||
{ | ||
/** | ||
* @param string $directory | ||
* | ||
* @return string | ||
*/ | ||
protected function toDirSeparator(string $directory): string | ||
{ | ||
return rtrim($directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Phalcon. | ||
* | ||
* (c) Phalcon Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phalcon\Tests\Fixtures\Helper\Str; | ||
|
||
use Phalcon\Traits\Helper\Str\DirSeparatorTrait; | ||
|
||
/** | ||
* Accepts a directory name and ensures that it ends with | ||
* DIRECTORY_SEPARATOR | ||
*/ | ||
class DirSeparatorFixture | ||
{ | ||
use DirSeparatorTrait; | ||
|
||
/** | ||
* @param string $directory | ||
* | ||
* @return string | ||
*/ | ||
public function dirSeparator(string $directory): string | ||
{ | ||
return $this->toDirSeparator($directory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Phalcon Framework. | ||
* | ||
* (c) Phalcon Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE.txt | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phalcon\Tests\Unit\Helper\Str; | ||
|
||
use Codeception\Example; | ||
use Phalcon\Tests\Fixtures\Helper\Str\DirSeparatorFixture; | ||
use UnitTester; | ||
|
||
/** | ||
* Tests the DirSeparator trait | ||
*/ | ||
class DirSeparatorTraitCest | ||
{ | ||
/** | ||
* Tests Phalcon\Traits\Str\DirFromFileTrait | ||
* | ||
* @dataProvider getExamples | ||
* | ||
* @param UnitTester $I | ||
* @param Example $example | ||
* | ||
* @author Phalcon Team <[email protected]> | ||
* @since 2021-10-26 | ||
*/ | ||
public function helperStrDirFromFileTrait(UnitTester $I, Example $example) | ||
{ | ||
$I->wantToTest('Str\DirFromFileTrait - ' . $example['label']); | ||
|
||
$directory = $example['directory']; | ||
$expected = $example['expected']; | ||
|
||
$object = new DirSeparatorFixture(); | ||
$actual = $object->dirSeparator($directory); | ||
$I->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* @return array[] | ||
*/ | ||
private function getExamples(): array | ||
{ | ||
return [ | ||
[ | ||
'label' => 'without trailing slash', | ||
'directory' => '/home/phalcon', | ||
'expected' => '/home/phalcon/', | ||
], | ||
[ | ||
'label' => 'with trailing slash', | ||
'directory' => '/home/phalcon/', | ||
'expected' => '/home/phalcon/', | ||
], | ||
[ | ||
'label' => 'with double trailing slash', | ||
'directory' => '/home/phalcon//', | ||
'expected' => '/home/phalcon/', | ||
], | ||
[ | ||
'label' => 'empty directory', | ||
'directory' => '', | ||
'expected' => '/', | ||
], | ||
]; | ||
} | ||
} |