-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Denis Brumann
committed
Sep 22, 2022
1 parent
7cd45c9
commit 81785c6
Showing
3 changed files
with
115 additions
and
1 deletion.
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
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,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); | ||
} | ||
} |
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,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: |