Skip to content

Commit

Permalink
tests!
Browse files Browse the repository at this point in the history
  • Loading branch information
SethSharp committed Feb 10, 2024
1 parent d1cead6 commit 6adbab5
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 13 deletions.
8 changes: 3 additions & 5 deletions app/Domain/File/Actions/StoreFileAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ class StoreFileAction
{
public function __invoke(UploadedFile $file): string
{
$structure = app()->environment('local') ? 'testing/' : 'production/';
$structure = app()->environment('testing') || app()->environment('local')
? 'testing/' : 'production/';

$dir = $structure . 'blogs';

return Storage::disk(app()->environment('local')
? 'public'
: config('filesystems.default'))
->put($dir, $file);
return Storage::disk('s3')->put($dir, $file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __invoke(StoreBlogRequest $storeBlogRequest, StoreBlogAction $st
$drafted = (bool)$storeBlogRequest->input('is_draft');

return redirect()
->back()
->route('dashboard.blogs.index')
->with('success', $blog->title . ' successfully ' . ($drafted ? 'drafted' : 'published'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
use App\Domain\File\Models\File;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use App\Domain\File\Actions\StoreFileAction;
use App\Domain\File\Actions\DestroyFileAction;
use App\Http\Requests\Dashboard\Blogs\StoreBlogImageRequest;
use Illuminate\Support\Facades\Storage;

class StoreBlogImageController extends Controller
{
public function __invoke(StoreBlogImageRequest $request, StoreFileAction $action): JsonResponse
{
$path = $action($request->file('file'));
$url = app()->environment('local')
? $path
: Storage::url($path);
$url = Storage::disk('s3')->url($path);

$fileId = intval($request->input('fileId'));
$blogId = intval($request->input('blogId')) ?: null;
Expand Down
1 change: 0 additions & 1 deletion resources/js/Components/Editor/Nodes/Image/View.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ let { fileId, blogId, src, alt, height } = breakdownNodeViewProps(props)
<div class="flex w-full justify-center">
<div class="flex-col w-full">
<div v-if="src" class="flex mb-4 w-full">
{{ src }}
<img :src="src" />
</div>

Expand Down
31 changes: 31 additions & 0 deletions tests/Domain/File/Actions/DestroyFileActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Tests\Domain\File\Actions;

use Tests\TestCase;
use App\Domain\File\Models\File;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use App\Domain\File\Actions\DestroyFileAction;

class DestroyFileActionTest extends TestCase
{
/** @test */
public function can_destroy_file()
{
Storage::fake('s3');

$uploadedFile = UploadedFile::fake()->image('file.jpg');

$path = Storage::disk('s3')->put('testing', $uploadedFile);

$file = File::create([
'path' => $path,
'url' => 'some-url'
]);

app(DestroyFileAction::class)($file);

Storage::assertMissing($uploadedFile->hashName('testing'));
}
}
4 changes: 2 additions & 2 deletions tests/Domain/File/Actions/StoreFileActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class StoreFileActionTest extends TestCase
/** @test */
public function can_store_file()
{
Storage::fake();
Storage::fake('s3');

$file = UploadedFile::fake()->image('file.jpg');

app(StoreFileAction::class)($file);

Storage::assertExists($file->hashName('testing/blogs'));
Storage::disk('s3')->assertExists($file->hashName('testing/blogs'));
}
}
158 changes: 158 additions & 0 deletions tests/Http/Dashboard/Blogs/StoreBlogImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

namespace Dashboard\Blogs;

use Tests\TestCase;
use Mockery\MockInterface;
use App\Domain\Iam\Models\User;
use App\Domain\File\Models\File;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

use App\Domain\File\Actions\StoreFileAction;
use App\Domain\File\Actions\DestroyFileAction;

class StoreBlogImageTest extends TestCase
{
protected MockInterface $destroyFileAction;
protected MockInterface $storeFileAction;

/** @test */
public function must_be_authenticated()
{
$this->postJson(route('dashboard.blogs.store'))
->assertStatus(401);
}

/** @test */
public function file_is_required()
{
$this->actingAs(User::factory()->author()->create())
->postJson(route('dashboard.blogs.image.store'), [
'file' => null,
])
->assertStatus(422)
->assertJsonValidationErrors([
'file' => 'The file field is required'
]);
}

/** @test */
public function file_must_be_an_image()
{
$this->actingAs(User::factory()->author()->create())
->postJson(route('dashboard.blogs.image.store'), [
'file' => '1234',
])
->assertStatus(422)
->assertJsonValidationErrors([
'file' => 'The file must be an image'
]);
}

/** @test */
public function fields_must_be_a_string()
{
$this->actingAs(User::factory()->author()->create())
->postJson(route('dashboard.blogs.image.store'), [
'fileId' => 1234,
'blogId' => 1234,
])
->assertStatus(422)
->assertJsonValidationErrors([
'fileId' => 'The file id must be a string',
'blogId' => 'The blog id must be a string'
]);
}

/** @test */
public function fields_are_not_required()
{
Storage::fake('s3');

$uploadedFile = UploadedFile::fake()->image('file.jpg');

$this->actingAs(User::factory()->author()->create())
->postJson(route('dashboard.blogs.image.store'), [
'file' => $uploadedFile,
])
->assertOk();
}

/** @test */
public function file_info_is_replaced_with_new_info_if_id_is_provided()
{
Storage::fake('s3');

$this->storeFileAction = $this->mock(StoreFileAction::class);
app()->instance(StoreFileAction::class, $this->storeFileAction);

$this->destroyFileAction = $this->mock(DestroyFileAction::class);
app()->instance(DestroyFileAction::class, $this->destroyFileAction);

$uploadedFile = UploadedFile::fake()->image('file.jpg');

$file = File::create([
'path' => 'some-old-path',
'url' => '/storage/some-old-path'
]);

$this->storeFileAction->shouldReceive('__invoke')
->once()
->withArgs(function ($uploadedFileInstance) {
return $uploadedFileInstance instanceof UploadedFile;
})
->andReturn('new-file-path');

$this->destroyFileAction->shouldReceive('__invoke')
->once()
->withArgs(function ($storedFileInstance) use ($file) {
return $storedFileInstance instanceof File
&& $file->is($storedFileInstance);
})
->andReturn('new-file-path');

$this->actingAs(User::factory()->author()->create())
->postJson(route('dashboard.blogs.image.store'), [
'file' => $uploadedFile,
'fileId' => (string)$file->id
])
->assertOk();

$this->assertDatabaseHas('files', [
'blog_id' => null,
'path' => 'new-file-path',
'url' => '/storage/new-file-path'
]);
}

/** @test */
public function new_file_is_created_if_no_file_is_is_stored()
{
Storage::fake('s3');

$this->storeFileAction = $this->mock(StoreFileAction::class);
app()->instance(StoreFileAction::class, $this->storeFileAction);

$file = UploadedFile::fake()->image('file.jpg');

$this->storeFileAction->shouldReceive('__invoke')
->once()
->withArgs(function ($uploadedFile) use ($file) {
return $uploadedFile instanceof UploadedFile;
})
->andReturn('some-random-path');

$this->actingAs(User::factory()->author()->create())
->postJson(route('dashboard.blogs.image.store'), [
'file' => $file,
])
->assertOk();

$this->assertDatabaseHas('files', [
'blog_id' => null,
'path' => 'some-random-path',
'url' => '/storage/some-random-path'
]);
}
}
75 changes: 75 additions & 0 deletions tests/Http/Dashboard/Blogs/StoreBlogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Domain\Blog\Models\Tag;
use App\Domain\Iam\Models\User;
use App\Domain\Blog\Models\Blog;
use App\Domain\File\Models\File;

class StoreBlogTest extends TestCase
{
Expand Down Expand Up @@ -253,4 +254,78 @@ public function tags_is_attached_to_blog()
'tag_id' => $tag->id,
]);
}

/** @test */
public function will_replace_blog_id_in_content()
{
$tag = Tag::factory()->create();

$this->actingAs($user = User::factory()->author()->create())
->postJson(route('dashboard.blogs.store'), [
'title' => 'Some Title',
'slug' => 'some-slug',
'tags' => [
[
'id' => $tag->id,
'name' => $tag->name
]
],
'meta_title' => 'some title',
'meta_tags' => 'some tag',
'meta_description' => 'some description',
'content' => '<tt-image blogid="null"> </tt-image>',
'is_draft' => true
])
->assertRedirect(route('dashboard.blogs.index'));

$blog = Blog::first();

$this->assertDatabaseHas('blogs', [
'author_id' => $user->id,
'is_draft' => 1,
'title' => 'Some Title',
'slug' => 'some-slug',
'meta_title' => 'some title',
'meta_tags' => 'some tag',
'meta_description' => 'some description',
'content' => "<tt-image blogid=\"$blog->id\"> </tt-image>"
]);
}

/** @test */
public function any_files_without_blog_id_will_be_assigned_recently_created_one()
{
File::create([
'path' => 'some-path',
'url' => 'some-url'
]);

$tag = Tag::factory()->create();

$this->actingAs($user = User::factory()->author()->create())
->postJson(route('dashboard.blogs.store'), [
'title' => 'Some Title',
'slug' => 'some-slug',
'tags' => [
[
'id' => $tag->id,
'name' => $tag->name
]
],
'meta_title' => 'some title',
'meta_tags' => 'some tag',
'meta_description' => 'some description',
'content' => '<tt-image blogid="null"> </tt-image>',
'is_draft' => true
])
->assertRedirect(route('dashboard.blogs.index'));

$blog = Blog::first();

$this->assertDatabaseHas('files', [
'blog_id' => $blog->id,
'path' => 'some-path',
'url' => 'some-url'
]);
}
}
Loading

0 comments on commit 6adbab5

Please sign in to comment.