Skip to content

Commit

Permalink
Fix init with existing file.
Browse files Browse the repository at this point in the history
Fixes #977
Closes #982
  • Loading branch information
Denis Brumann committed Sep 22, 2022
1 parent 7cd45c9 commit 81785c6
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Supportive/File/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Qossmic\Deptrac\Supportive\File\Exception\FileNotWritableException;
use SplFileInfo;
use Symfony\Component\Filesystem\Filesystem;
use function is_writable;

class Dumper
{
Expand All @@ -30,7 +31,7 @@ public function dump(string $file): void
if ($filesystem->exists($target->getPathname())) {
throw FileAlreadyExistsException::alreadyExists($target);
}
if (!$target->isWritable()) {
if (!is_writable($target->getPath())) {
throw FileNotWritableException::notWritable($target);
}

Expand Down
89 changes: 89 additions & 0 deletions tests/Supportive/File/DumperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace Tests\Qossmic\Deptrac\Supportive\File;

use PHPUnit\Framework\TestCase;
use Qossmic\Deptrac\Supportive\File\Dumper;
use Qossmic\Deptrac\Supportive\File\Exception\FileAlreadyExistsException;
use Qossmic\Deptrac\Supportive\File\Exception\FileNotWritableException;
use SplFileInfo;
use function file_exists;
use function file_get_contents;
use function is_writable;
use function rtrim;
use function sprintf;
use function sys_get_temp_dir;
use function tempnam;
use function uniqid;
use function unlink;
use const DIRECTORY_SEPARATOR;

final class DumperTest extends TestCase
{
private string $sourceFile;
private Dumper $dumper;

protected function setUp(): void
{
parent::setUp();

$this->sourceFile = __DIR__.'/Fixtures/deptrac.yaml';
$this->dumper = new Dumper($this->sourceFile);
}

protected function tearDown(): void
{
parent::tearDown();

unset($this->dumper);
}

public function testFailsWhenFileAlreadyExists(): void
{
$targetFile = $this->sourceFile;

$this->expectException(FileAlreadyExistsException::class);

$this->dumper->dump($targetFile);
}

public function testFailsWhenFileIsNotWritable(): void
{
$tempDir = sprintf('%s/%s', rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR), uniqid());
if (!mkdir($tempDir) || !chmod($tempDir, 0444)) {
$this->markTestSkipped(sprintf('Skipping test. Could not create readonly temporary directory "%s". Please check permissions', $tempDir));
}
$tempFile = new SplFileInfo($tempDir.DIRECTORY_SEPARATOR.'deptrac.yaml');

$this->expectException(FileNotWritableException::class);

$this->dumper->dump($tempFile->getPathname());
}

public function testCopiesTemplateToNewFile(): void
{
$tempFilename = tempnam(sys_get_temp_dir(), 'deptrac');
if (false === $tempFilename) {
$this->markTestSkipped('Skipping test. Could not create temporary file. Please check permissions');
}
$tempFile = new SplFileInfo($tempFilename);
unlink($tempFile->getPathname());
$tempDir = $tempFile->getPath();
if (!is_writable($tempDir)) {
$this->markTestSkipped(sprintf('Skipping test. Can not write to temporary directory "%s". Please check your permissions.', $tempDir));
}
if ($tempFile->isFile()) {
$this->fail(sprintf('Temporary file "%s" already exists.', $tempFile->getPathname()));
}

$this->dumper->dump($tempFile->getPathname());

self::assertTrue(file_exists($tempFile->getPathname()));
self::assertSame(file_get_contents($this->sourceFile), file_get_contents($tempFile->getPathname()));

@unlink($tempFile->getPathname());
@rmdir($tempDir);
}
}
24 changes: 24 additions & 0 deletions tests/Supportive/File/Fixtures/deptrac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
deptrac:
paths:
- ./src
exclude_files:
- '#.*test.*#'
layers:
- name: Controller
collectors:
- type: className
value: .*Controller.*
- name: Repository
collectors:
- type: className
value: .*Repository.*
- name: Service
collectors:
- type: className
value: .*Service.*
ruleset:
Controller:
- Service
Service:
- Repository
Repository:

0 comments on commit 81785c6

Please sign in to comment.