Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php namespace Tests\Feature; use App\Events\LibraryChanged; use App\Exceptions\MediaPa..

Decoded Output download

<?php

namespace Tests\Feature;

use App\Events\LibraryChanged;
use App\Exceptions\MediaPathNotSetException;
use App\Exceptions\SongUploadFailedException;
use App\Models\Setting;
use App\Models\Song;
use App\Models\User;
use App\Services\UploadService;
use Illuminate\Http\Response;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Event;
use Mockery\MockInterface;

class UploadTest extends TestCase
{
    private UploadService|MockInterface $uploadService;
    private UploadedFile $file;

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

        $this->uploadService = self::mock(UploadService::class);
        $this->file = UploadedFile::fake()
            ->createWithContent('song.mp3', file_get_contents(__DIR__ . '/../songs/full.mp3'));
    }

    public function testUnauthorizedPost(): void
    {
        Setting::set('media_path', '/media/koel');

        $this->uploadService
            ->shouldReceive('handleUploadedFile')
            ->never();

        $this->postAs('/api/upload', ['file' => $this->file])->assertForbidden();
    }

    /** @return array<mixed> */
    public function provideUploadExceptions(): array
    {
        return [
            [MediaPathNotSetException::class, Response::HTTP_FORBIDDEN],
            [SongUploadFailedException::class, Response::HTTP_BAD_REQUEST],
        ];
    }

    /** @dataProvider provideUploadExceptions */
    public function testPostShouldFail(string $exceptionClass, int $statusCode): void
    {
        /** @var User $admin */
        $admin = User::factory()->admin()->create();

        $this->uploadService
            ->shouldReceive('handleUploadedFile')
            ->once()
            ->with($this->file)
            ->andThrow($exceptionClass);

        $this->postAs('/api/upload', ['file' => $this->file], $admin)->assertStatus($statusCode);
    }

    public function testPost(): void
    {
        Event::fake(LibraryChanged::class);
        Setting::set('media_path', '/media/koel');

        /** @var Song $song */
        $song = Song::factory()->create();

        /** @var User $admin */
        $admin = User::factory()->admin()->create();

        $this->uploadService
            ->shouldReceive('handleUploadedFile')
            ->once()
            ->with($this->file)
            ->andReturn($song);

        $this->postAs('/api/upload', ['file' => $this->file], $admin)->assertJsonStructure(['song', 'album']);
        Event::assertDispatched(LibraryChanged::class);
    }
}
 ?>

Did this file decode correctly?

Original Code

<?php

namespace Tests\Feature;

use App\Events\LibraryChanged;
use App\Exceptions\MediaPathNotSetException;
use App\Exceptions\SongUploadFailedException;
use App\Models\Setting;
use App\Models\Song;
use App\Models\User;
use App\Services\UploadService;
use Illuminate\Http\Response;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Event;
use Mockery\MockInterface;

class UploadTest extends TestCase
{
    private UploadService|MockInterface $uploadService;
    private UploadedFile $file;

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

        $this->uploadService = self::mock(UploadService::class);
        $this->file = UploadedFile::fake()
            ->createWithContent('song.mp3', file_get_contents(__DIR__ . '/../songs/full.mp3'));
    }

    public function testUnauthorizedPost(): void
    {
        Setting::set('media_path', '/media/koel');

        $this->uploadService
            ->shouldReceive('handleUploadedFile')
            ->never();

        $this->postAs('/api/upload', ['file' => $this->file])->assertForbidden();
    }

    /** @return array<mixed> */
    public function provideUploadExceptions(): array
    {
        return [
            [MediaPathNotSetException::class, Response::HTTP_FORBIDDEN],
            [SongUploadFailedException::class, Response::HTTP_BAD_REQUEST],
        ];
    }

    /** @dataProvider provideUploadExceptions */
    public function testPostShouldFail(string $exceptionClass, int $statusCode): void
    {
        /** @var User $admin */
        $admin = User::factory()->admin()->create();

        $this->uploadService
            ->shouldReceive('handleUploadedFile')
            ->once()
            ->with($this->file)
            ->andThrow($exceptionClass);

        $this->postAs('/api/upload', ['file' => $this->file], $admin)->assertStatus($statusCode);
    }

    public function testPost(): void
    {
        Event::fake(LibraryChanged::class);
        Setting::set('media_path', '/media/koel');

        /** @var Song $song */
        $song = Song::factory()->create();

        /** @var User $admin */
        $admin = User::factory()->admin()->create();

        $this->uploadService
            ->shouldReceive('handleUploadedFile')
            ->once()
            ->with($this->file)
            ->andReturn($song);

        $this->postAs('/api/upload', ['file' => $this->file], $admin)->assertJsonStructure(['song', 'album']);
        Event::assertDispatched(LibraryChanged::class);
    }
}

Function Calls

None

Variables

None

Stats

MD5 e4e7177777c9b877045b6fea660ad5b0
Eval Count 0
Decode Time 98 ms