Skip to content

Commit 76bd17c

Browse files
committed
fix bug
1 parent 67159e4 commit 76bd17c

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

routes/web.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
use Illuminate\Support\Facades\Route;
66
use Projektgopher\Scrawl\Blog;
7+
use Symfony\Component\HttpKernel\Exception\HttpException;
78

89
Route::get('blog/{slug}', function ($slug) {
10+
if (! Blog::isPublished($slug)) {
11+
// abort(404, 'We couldn\'t find this post.');
12+
// throw new HttpException(404, 'We couldn\'t find this post.');
13+
return response(status: 404, content: 'We couldn\'t find this post.');
14+
}
915

10-
return Blog::isPublished($slug)
11-
? response(status: 200, content: Blog::asHtml($slug))
12-
: response(status: 404);
16+
return response(status: 200, content: Blog::asHtml($slug));
1317
});

src/Blog.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Blog
1212

1313
public static function isPublished($slug): bool
1414
{
15-
return File::exists(self::$publishedDirectory . "/{$slug}.md");
15+
return File::exists(base_path(self::$publishedDirectory . "/{$slug}.md"));
1616
}
1717

1818
public static function asHtml($slug): string
@@ -25,7 +25,7 @@ public static function asHtml($slug): string
2525
);
2626

2727
return (string) $converter->convertToHtml(
28-
File::get(self::$publishedDirectory . "/{$slug}.md")
28+
File::get(base_path(self::$publishedDirectory . "/{$slug}.md"))
2929
);
3030
}
3131
}

tests/routes/RoutesTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ public function it_registers_a_blog_post_route()
1818
/** @test */
1919
public function it_returns_a_200_if_the_blog_exists()
2020
{
21+
$this->withoutExceptionHandling();
2122
File::shouldReceive('exists')
2223
->once()
23-
->with('resources/blogs/published/test.md')
24+
->with(base_path('resources/blogs/published/test.md'))
2425
->andReturn(true);
2526
File::shouldReceive('get')
2627
->once()
27-
->with('resources/blogs/published/test.md')
28+
->with(base_path('resources/blogs/published/test.md'))
2829
->andReturn('test');
2930

3031
$this->get('/blog/test')->assertOk();
@@ -33,9 +34,10 @@ public function it_returns_a_200_if_the_blog_exists()
3334
/** @test */
3435
public function it_returns_a_404_if_the_blog_does_not_exist()
3536
{
37+
$this->withoutExceptionHandling();
3638
File::shouldReceive('exists')
3739
->once()
38-
->with('resources/blogs/published/does-not-exist.md')
40+
->with(base_path('resources/blogs/published/does-not-exist.md'))
3941
->andReturn(false);
4042

4143
$this->get('/blog/does-not-exist')->assertStatus(404);
@@ -44,13 +46,14 @@ public function it_returns_a_404_if_the_blog_does_not_exist()
4446
/** @test */
4547
public function it_converts_md_to_html()
4648
{
49+
$this->withoutExceptionHandling();
4750
File::shouldReceive('exists')
4851
->once()
49-
->with('resources/blogs/published/published-post.md')
52+
->with(base_path('resources/blogs/published/published-post.md'))
5053
->andReturn(true);
5154
File::shouldReceive('get')
5255
->once()
53-
->with('resources/blogs/published/published-post.md')
56+
->with(base_path('resources/blogs/published/published-post.md'))
5457
->andReturn('# Hello World!');
5558

5659
$this->get('/blog/published-post')

0 commit comments

Comments
 (0)