Skip to content

Commit d5bb9f4

Browse files
authored
Merge pull request #1081 from hydephp/update-FileConflictException-class
Update FileConflictException message to turn absolute paths to relative
2 parents 2c7eb45 + cad1b86 commit d5bb9f4

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

packages/framework/src/Framework/Exceptions/FileConflictException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Hyde\Framework\Exceptions;
66

77
use Exception;
8+
use Hyde\Hyde;
89

910
class FileConflictException extends Exception
1011
{
@@ -16,7 +17,7 @@ class FileConflictException extends Exception
1617

1718
public function __construct(?string $path = null, ?string $message = null)
1819
{
19-
$this->message = $message ?? ($path ? "File already exists: $path" : $this->message);
20+
$this->message = $message ?? ($path ? sprintf('File already exists: %s', Hyde::pathToRelative($path)) : $this->message);
2021

2122
parent::__construct($this->message, $this->code);
2223
}

packages/framework/tests/Feature/Actions/CreatesNewPageSourceFileTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function test_that_an_exception_is_thrown_if_file_already_exists_and_over
3939
$this->file('_pages/foo.md', 'foo');
4040

4141
$this->expectException(FileConflictException::class);
42-
$this->expectExceptionMessage('File already exists: '.Hyde::path('_pages/foo.md'));
42+
$this->expectExceptionMessage('File already exists: _pages/foo.md');
4343
$this->expectExceptionCode(409);
4444

4545
new CreatesNewPageSourceFile('foo');
@@ -61,7 +61,7 @@ public function test_exception_is_thrown_for_conflicting_blade_pages()
6161
$this->file('_pages/foo.blade.php', 'foo');
6262

6363
$this->expectException(FileConflictException::class);
64-
$this->expectExceptionMessage('File already exists: '.Hyde::path('_pages/foo.blade.php'));
64+
$this->expectExceptionMessage('File already exists: _pages/foo.blade.php');
6565
$this->expectExceptionCode(409);
6666

6767
new CreatesNewPageSourceFile('foo', BladePage::class);
@@ -74,7 +74,7 @@ public function test_exception_is_thrown_for_conflicting_documentation_pages()
7474
$this->file('_docs/foo.md', 'foo');
7575

7676
$this->expectException(FileConflictException::class);
77-
$this->expectExceptionMessage('File already exists: '.Hyde::path('_docs/foo.md'));
77+
$this->expectExceptionMessage('File already exists: _docs/foo.md');
7878
$this->expectExceptionCode(409);
7979

8080
new CreatesNewPageSourceFile('foo', DocumentationPage::class);

packages/framework/tests/Feature/Commands/MakePageCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function test_command_fails_if_file_already_exists()
9898
file_put_contents($this->markdownPath, 'This should not be overwritten');
9999

100100
$this->expectException(FileConflictException::class);
101-
$this->expectExceptionMessage("File already exists: $this->markdownPath");
101+
$this->expectExceptionMessage('File already exists: _pages/foo-test-page.md');
102102
$this->expectExceptionCode(409);
103103
$this->artisan('make:page "foo test page"')->assertExitCode(409);
104104

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyde\Framework\Testing\Unit;
6+
7+
use Hyde\Foundation\HydeKernel;
8+
use Hyde\Framework\Exceptions\FileConflictException;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/**
12+
* @covers \Hyde\Framework\Exceptions\FileConflictException
13+
*/
14+
class FileConflictExceptionTest extends TestCase
15+
{
16+
public function test_it_can_be_instantiated()
17+
{
18+
$this->assertInstanceOf(FileConflictException::class, new FileConflictException());
19+
}
20+
21+
public function test_it_can_be_thrown()
22+
{
23+
$this->expectException(FileConflictException::class);
24+
25+
throw new FileConflictException();
26+
}
27+
28+
public function test_exception_code()
29+
{
30+
$this->assertSame(409, (new FileConflictException())->getCode());
31+
}
32+
33+
public function test_exception_message()
34+
{
35+
$this->assertSame('A file already exists at this path.', (new FileConflictException())->getMessage());
36+
}
37+
38+
public function test_exception_message_with_path()
39+
{
40+
HydeKernel::setInstance(new HydeKernel('my-base-path'));
41+
$this->assertSame('File already exists: path/to/file', (new FileConflictException('path/to/file'))->getMessage());
42+
}
43+
44+
public function test_exception_message_with_absolute_path()
45+
{
46+
HydeKernel::setInstance(new HydeKernel('my-base-path'));
47+
$this->assertSame('File already exists: path/to/file', (new FileConflictException('my-base-path/path/to/file'))->getMessage());
48+
}
49+
50+
public function test_exception_message_with_custom_message()
51+
{
52+
$this->assertSame('Custom message', (new FileConflictException(null, 'Custom message'))->getMessage());
53+
}
54+
55+
public function test_exception_message_with_custom_message_and_path()
56+
{
57+
$this->assertSame('Custom message', (new FileConflictException('foo', 'Custom message'))->getMessage());
58+
}
59+
}

0 commit comments

Comments
 (0)