Skip to content

Commit

Permalink
feat(helper/str/dirseparator): added dirSeparatorTrait and relevant t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
niden committed Oct 27, 2021
1 parent 96a987e commit 91307a2
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Helper/Str/DirSeparatorTrait.php
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;
}
}
35 changes: 35 additions & 0 deletions tests/_data/fixtures/Helper/Str/DirSeparatorFixture.php
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);
}
}
76 changes: 76 additions & 0 deletions tests/unit/Helper/Str/DirSeparatorTraitCest.php
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' => '/',
],
];
}
}

0 comments on commit 91307a2

Please sign in to comment.