Skip to content

Commit

Permalink
media controller test
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jan 29, 2024
1 parent 5e6cec4 commit 871e897
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
74 changes: 74 additions & 0 deletions tests/Http/MediaControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

Check warning on line 1 in tests/Http/MediaControllerTest.php

View workflow job for this annotation

GitHub Actions / 4️⃣ Coding Standards

Found violation(s) of type: no_unused_imports

namespace Cone\Root\Tests\Http\Controllers;

use Cone\Root\Fields\Media;
use Cone\Root\Jobs\MoveFile;
use Cone\Root\Jobs\PerformConversions;
use Cone\Root\Models\Medium;
use Cone\Root\Root;
use Cone\Root\Tests\Post;
use Cone\Root\Tests\TestCase;
use Cone\Root\Tests\User;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Queue;

class MediaControllerTest extends TestCase
{
protected User $admin;

protected Media $field;

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

$this->admin = User::factory()->create();

$this->field = Root::instance()
->resources
->resolve('users')
->resolveFields($this->app['request'])
->first(function ($field) {
return $field->getModelAttribute() === 'media';
});
}

public function test_a_media_controller_handles_index(): void
{
$this->actingAs($this->admin)
->get('/root/users/'.$this->admin->getKey().'/fields/media')
->assertOk()
->assertJson($this->field->paginateRelatable($this->app['request'], $this->admin)->toArray());
}

public function test_a_media_controller_handles_store(): void
{
Queue::fake();

$this->actingAs($this->admin)
->post(
'/root/users/'.$this->admin->getKey().'/fields/media',
['file' => UploadedFile::fake()->image('test.png')],
['X-Chunk-Index' => 1, 'X-Chunk-Total' => 1]
)
->assertCreated()
->assertJson(['processing' => false]);

$this->assertDatabaseHas('root_media', ['name' => 'test']);

Queue::assertPushedWithChain(MoveFile::class, [PerformConversions::class]);
}

public function test_a_media_controller_handles_destroy(): void
{
$medium = Medium::factory()->create();

$this->actingAs($this->admin)
->delete('/root/users/'.$this->admin->getKey().'/fields/media', ['ids' => [$medium->getKey()]])
->assertOk()
->assertJson(['deleted' => 1]);

$this->assertDatabaseMissing('root_media', ['id' => $medium->getKey()]);
}
}
2 changes: 2 additions & 0 deletions tests/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Cone\Root\Fields\Email;
use Cone\Root\Fields\ID;
use Cone\Root\Fields\Media;
use Cone\Root\Fields\Text;
use Cone\Root\Resources\Resource;
use Cone\Root\Tests\Actions\SendPasswordResetNotification;
Expand All @@ -30,6 +31,7 @@ public function fields(Request $request): array
Text::make('Name')->searchable(),
Email::make('Email')->searchable(),
Text::make('Password')->visibleOn(['create', 'update']),
Media::make('Media'),
];
}

Expand Down

0 comments on commit 871e897

Please sign in to comment.