Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions tests/Casts/MetaValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

Check warning on line 1 in tests/Casts/MetaValueTest.php

View workflow job for this annotation

GitHub Actions / 4️⃣ Coding Standards

Found violation(s) of type: braces_position

Check warning on line 1 in tests/Casts/MetaValueTest.php

View workflow job for this annotation

GitHub Actions / 4️⃣ Coding Standards

Found violation(s) of type: class_definition

Check warning on line 1 in tests/Casts/MetaValueTest.php

View workflow job for this annotation

GitHub Actions / 4️⃣ Coding Standards

Found violation(s) of type: new_with_parentheses

declare(strict_types=1);

namespace Cone\Root\Tests\Casts;

use Cone\Root\Casts\MetaValue;
use Cone\Root\Tests\TestCase;
use Illuminate\Database\Eloquent\Model;

final class MetaValueTest extends TestCase
{
protected MetaValue $cast;

protected Model $model;

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

$this->cast = new MetaValue();

$this->model = new class extends Model
{
protected $guarded = [];
};
}

public function test_it_returns_null_when_value_is_null(): void
{
$result = $this->cast->get($this->model, 'test_key', null, []);

$this->assertNull($result);
}

public function test_it_decodes_json_value(): void
{
$jsonValue = json_encode(['foo' => 'bar', 'baz' => 'qux']);

$result = $this->cast->get($this->model, 'test_key', $jsonValue, []);

$this->assertSame(['foo' => 'bar', 'baz' => 'qux'], $result);
}

public function test_it_returns_string_value_when_json_decode_fails(): void
{
$result = $this->cast->get($this->model, 'test_key', 'plain string', []);

$this->assertSame('plain string', $result);
}

public function test_it_returns_null_for_storage_when_value_is_null(): void
{
$result = $this->cast->set($this->model, 'test_key', null, []);

$this->assertNull($result);
}

public function test_it_returns_string_value_for_storage(): void
{
$result = $this->cast->set($this->model, 'test_key', 'test value', []);

$this->assertSame('test value', $result);
}

public function test_it_returns_numeric_value_as_string_for_storage(): void
{
$result = $this->cast->set($this->model, 'test_key', 123, []);

$this->assertSame('123', $result);
}

public function test_it_encodes_array_to_json_for_storage(): void
{
$result = $this->cast->set($this->model, 'test_key', ['foo' => 'bar'], []);

$this->assertSame('{"foo":"bar"}', $result);
}

public function test_it_converts_stringable_object_for_storage(): void
{
$stringable = new class
{
public function __toString(): string
{
return 'stringable value';
}
};

$result = $this->cast->set($this->model, 'test_key', $stringable, []);

$this->assertSame('stringable value', $result);
}
}
40 changes: 40 additions & 0 deletions tests/Enums/ArrayableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Cone\Root\Tests\Enums;

use Cone\Root\Enums\Arrayable;
use Cone\Root\Tests\TestCase;

final class ArrayableTest extends TestCase
{
public function test_enum_can_be_converted_to_array(): void
{
$array = TestEnum::toArray();

$this->assertSame([
'active' => 'Active',
'inactive' => 'Inactive',
'pending' => 'Pending',
], $array);
}
}

enum TestEnum: string
{
use Arrayable;

case ACTIVE = 'active';
case INACTIVE = 'inactive';
case PENDING = 'pending';

public function label(): string
{
return match ($this) {
self::ACTIVE => 'Active',
self::INACTIVE => 'Inactive',
self::PENDING => 'Pending',
};
}
}
59 changes: 59 additions & 0 deletions tests/Exceptions/ExceptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Cone\Root\Tests\Exceptions;

use Cone\Root\Exceptions\QueryResolutionException;
use Cone\Root\Exceptions\ResourceResolutionException;
use Cone\Root\Exceptions\SaveFormDataException;
use Cone\Root\Tests\TestCase;
use Exception;

final class ExceptionsTest extends TestCase
{
public function test_query_resolution_exception_can_be_thrown(): void
{
$this->expectException(QueryResolutionException::class);
$this->expectExceptionMessage('Test message');

throw new QueryResolutionException('Test message');
}

public function test_query_resolution_exception_extends_exception(): void
{
$exception = new QueryResolutionException('Test');

$this->assertInstanceOf(Exception::class, $exception);
}

public function test_resource_resolution_exception_can_be_thrown(): void
{
$this->expectException(ResourceResolutionException::class);
$this->expectExceptionMessage('Test message');

throw new ResourceResolutionException('Test message');
}

public function test_resource_resolution_exception_extends_exception(): void
{
$exception = new ResourceResolutionException('Test');

$this->assertInstanceOf(Exception::class, $exception);
}

public function test_save_form_data_exception_can_be_thrown(): void
{
$this->expectException(SaveFormDataException::class);
$this->expectExceptionMessage('Test message');

throw new SaveFormDataException('Test message');
}

public function test_save_form_data_exception_extends_exception(): void
{
$exception = new SaveFormDataException('Test');

$this->assertInstanceOf(Exception::class, $exception);
}
}
88 changes: 88 additions & 0 deletions tests/Jobs/MoveFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace Cone\Root\Tests\Jobs;

use Cone\Root\Jobs\MoveFile;
use Cone\Root\Models\Medium;
use Cone\Root\Tests\TestCase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Storage;

final class MoveFileTest extends TestCase
{
public function test_move_file_job_can_be_dispatched(): void
{
Queue::fake();

$medium = Medium::factory()->make();
$path = '/tmp/test-file.jpg';

MoveFile::dispatch($medium, $path, false);

Queue::assertPushed(MoveFile::class);
}

public function test_move_file_job_moves_file_to_storage(): void
{
Storage::fake('public');

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

$medium = Medium::factory()->make([
'disk' => 'public',
'path' => 'media/test.jpg',
]);

$job = new MoveFile($medium, $tempPath, true);
$job->handle();

Storage::disk('public')->assertExists('media/test.jpg');
}

public function test_move_file_job_deletes_original_when_not_preserved(): void
{
Storage::fake('public');

$tempDir = Storage::disk('local')->path('root-tmp');
$tempPath = $tempDir.'/test-file.jpg';
File::ensureDirectoryExists($tempDir);
File::put($tempPath, 'test content');

$medium = Medium::factory()->make([
'disk' => 'public',
'path' => 'media/test.jpg',
]);

$job = new MoveFile($medium, $tempPath, false);
$job->handle();

Storage::disk('public')->assertExists('media/test.jpg');
$this->assertFalse(File::exists($tempPath));
}

public function test_move_file_job_preserves_original_when_preserve_is_true(): void
{
Storage::fake('public');

$tempDir = Storage::disk('local')->path('root-tmp');
$tempPath = $tempDir.'/test-file.jpg';
File::ensureDirectoryExists($tempDir);
File::put($tempPath, 'test content');

$medium = Medium::factory()->make([
'disk' => 'public',
'path' => 'media/test.jpg',
]);

$job = new MoveFile($medium, $tempPath, true);
$job->handle();

Storage::disk('public')->assertExists('media/test.jpg');
$this->assertTrue(File::exists($tempPath));
}
}
33 changes: 33 additions & 0 deletions tests/Jobs/PerformConversionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Cone\Root\Tests\Jobs;

use Cone\Root\Jobs\PerformConversions;
use Cone\Root\Models\Medium;
use Cone\Root\Tests\TestCase;
use Illuminate\Support\Facades\Queue;

final class PerformConversionsTest extends TestCase
{
public function test_perform_conversions_job_can_be_dispatched(): void
{
Queue::fake();

$medium = Medium::factory()->make();

PerformConversions::dispatch($medium);

Queue::assertPushed(PerformConversions::class);
}

public function test_perform_conversions_job_calls_convert_on_medium(): void
{
$medium = $this->createMock(Medium::class);
$medium->expects($this->once())->method('convert');

$job = new PerformConversions($medium);
$job->handle();
}
}
72 changes: 72 additions & 0 deletions tests/Listeners/FormatRootStubsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

Check warning on line 1 in tests/Listeners/FormatRootStubsTest.php

View workflow job for this annotation

GitHub Actions / 4️⃣ Coding Standards

Found violation(s) of type: no_unused_imports

Check warning on line 1 in tests/Listeners/FormatRootStubsTest.php

View workflow job for this annotation

GitHub Actions / 4️⃣ Coding Standards

Found violation(s) of type: new_with_parentheses

declare(strict_types=1);

namespace Cone\Root\Tests\Listeners;

use Cone\Root\Listeners\FormatRootStubs;
use Cone\Root\Tests\TestCase;
use Illuminate\Foundation\Events\VendorTagPublished;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\File;

final class FormatRootStubsTest extends TestCase
{
public function test_listener_formats_root_stubs(): void
{
$tempFile = tempnam(sys_get_temp_dir(), 'stub_');
file_put_contents($tempFile, '<?php namespace {{ namespace }}Tests;');

$event = new VendorTagPublished('root-stubs', [$tempFile]);

$listener = new FormatRootStubs();
$listener->handle($event);

$contents = file_get_contents($tempFile);

$this->assertStringContainsString(App::getNamespace(), $contents);
$this->assertStringNotContainsString('{{ namespace }}', $contents);

unlink($tempFile);
}

public function test_listener_ignores_non_root_stubs_tag(): void
{
$tempFile = tempnam(sys_get_temp_dir(), 'stub_');
file_put_contents($tempFile, '<?php namespace {{ namespace }}Tests;');

$event = new VendorTagPublished('other-tag', [$tempFile]);

$listener = new FormatRootStubs();
$listener->handle($event);

$contents = file_get_contents($tempFile);

$this->assertStringContainsString('{{ namespace }}', $contents);

unlink($tempFile);
}

public function test_listener_handles_multiple_files(): void
{
$tempFile1 = tempnam(sys_get_temp_dir(), 'stub_');
$tempFile2 = tempnam(sys_get_temp_dir(), 'stub_');

file_put_contents($tempFile1, '<?php namespace {{ namespace }}Tests;');
file_put_contents($tempFile2, '<?php namespace {{ namespace }}Models;');

$event = new VendorTagPublished('root-stubs', [$tempFile1, $tempFile2]);

$listener = new FormatRootStubs();
$listener->handle($event);

$contents1 = file_get_contents($tempFile1);
$contents2 = file_get_contents($tempFile2);

$this->assertStringNotContainsString('{{ namespace }}', $contents1);
$this->assertStringNotContainsString('{{ namespace }}', $contents2);

unlink($tempFile1);
unlink($tempFile2);
}
}
Loading
Loading