Skip to content

Commit

Permalink
Add Domain\Service\Filesystem\FilesystemInterface::isLink().
Browse files Browse the repository at this point in the history
  • Loading branch information
TravisCarden committed Jan 25, 2023
1 parent 998fae5 commit 27b94da
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/Domain/Service/Filesystem/FilesystemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ public function copy(PathInterface $source, PathInterface $destination): void;
*/
public function exists(PathInterface $path): bool;

/**
* Determines whether the given path is a symbolic link.
*
* @param \PhpTuf\ComposerStager\Domain\Value\Path\PathInterface $path
* A path to test.
*
* @return bool
* Returns true if the filename exists and is a symbolic link, false otherwise.
*/
public function isLink(PathInterface $path): bool;

/**
* Determines whether the given path is writable.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Infrastructure/Service/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public function exists(PathInterface $path): bool
return $this->symfonyFilesystem->exists($path->resolve());
}

public function isLink(PathInterface $path): bool
{
return is_link($path->resolve());
}

public function isWritable(PathInterface $path): bool
{
return is_writable($path->resolve()); // @codeCoverageIgnore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@
use Symfony\Component\Filesystem\Exception\IOException as SymfonyIOException;
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;

/** @coversNothing */
/**
* @coversDefaultClass \PhpTuf\ComposerStager\Infrastructure\Service\Filesystem\Filesystem
*
* @uses \PhpTuf\ComposerStager\Infrastructure\Factory\Path\PathFactory
* @uses \PhpTuf\ComposerStager\Infrastructure\Service\Filesystem\Filesystem
* @uses \PhpTuf\ComposerStager\Infrastructure\Value\Path\AbstractPath
* @uses \PhpTuf\ComposerStager\Infrastructure\Value\Path\UnixLikePath
* @uses \PhpTuf\ComposerStager\Infrastructure\Value\Path\WindowsPath
* @uses \Symfony\Component\Filesystem\Filesystem
*/
final class FilesystemFunctionalTest extends TestCase
{
private const SOURCE_DIR = self::TEST_ENV . DIRECTORY_SEPARATOR . 'source';
Expand Down Expand Up @@ -38,6 +47,7 @@ protected function createSut(): Filesystem
return $filesystem;
}

/** @covers ::copy */
public function testCopy(): void
{
$filename = 'file.txt';
Expand All @@ -54,12 +64,53 @@ public function testCopy(): void
self::assertDirectoryListing(self::DESTINATION_DIR, [$filename]);
}

/**
* @covers ::isLink
*
* @dataProvider providerIsLink
*/
public function testIsLink(array $files, array $links, bool $expected): void
{
self::createFiles(self::SOURCE_DIR, $files);
self::createSymlinks(self::SOURCE_DIR, $links);

$sut = $this->createSut();

$path = self::SOURCE_DIR . DIRECTORY_SEPARATOR . 'link.txt';
$actual = $sut->isLink(PathFactory::create($path));

self::assertSame($expected, $actual, 'Correctly determined whether path was a link.');
}

public function providerIsLink(): array
{
return [
'File is a link' => [
'files' => ['target.txt'],
'links' => ['link.txt' => 'target.txt'],
'expected' => true,
],
'File is not a link' => [
'files' => ['file.txt'],
'links' => [],
'expected' => false,
],
'No file there' => [
'files' => [],
'links' => [],
'expected' => false,
],
];
}

/**
* Our filesystem service currently depends on the Symfony Filesystem component
* and currently delegates its copy() method directly to it. Therefore, it is
* precisely equivalent to its implementation. Symfony's copy() documentation
* does not specify whether it supports directories as well as files. This
* test is to discover whether it does. (At this time it does not.)
*
* @coversNothing
*/
public function testSymfonyCopyDirectory(): void
{
Expand Down
10 changes: 8 additions & 2 deletions tests/PHPUnit/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,14 @@ protected static function createSymlink(string $baseDir, string $link, string $t
$link = PathFactory::create("{$baseDir}/{$link}")->resolve();
$target = PathFactory::create("{$baseDir}/{$target}")->resolve();
static::ensureParentDirectory($link);
$filesystem = new Filesystem();
$filesystem->symlink($target, $link);

// If the symlink target doesn't exist, the tests will pass on Unix-like
// systems but fail on Windows. Avoid hard-to-debug problems by making
// sure it fails everywhere in that case.
assert(file_exists($target), 'Symlink targets exists.');

symlink($target, $link);

assert(is_link($link), "Created symlink at {$link}.");
}

Expand Down

0 comments on commit 27b94da

Please sign in to comment.