Skip to content

Commit 7c3cc88

Browse files
Copilotiamgergo
andcommitted
Add missing tests for Support, Casts, Enums, Exceptions, Jobs, Policies, Notifications, Listeners, Traits, and View Components
Co-authored-by: iamgergo <[email protected]>
1 parent 48981e8 commit 7c3cc88

File tree

16 files changed

+982
-0
lines changed

16 files changed

+982
-0
lines changed

tests/Casts/MetaValueTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cone\Root\Tests\Casts;
6+
7+
use Cone\Root\Casts\MetaValue;
8+
use Cone\Root\Tests\TestCase;
9+
use Illuminate\Database\Eloquent\Model;
10+
11+
final class MetaValueTest extends TestCase
12+
{
13+
protected MetaValue $cast;
14+
15+
protected Model $model;
16+
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$this->cast = new MetaValue();
22+
23+
$this->model = new class extends Model
24+
{
25+
protected $guarded = [];
26+
};
27+
}
28+
29+
public function test_it_returns_null_when_value_is_null(): void
30+
{
31+
$result = $this->cast->get($this->model, 'test_key', null, []);
32+
33+
$this->assertNull($result);
34+
}
35+
36+
public function test_it_decodes_json_value(): void
37+
{
38+
$jsonValue = json_encode(['foo' => 'bar', 'baz' => 'qux']);
39+
40+
$result = $this->cast->get($this->model, 'test_key', $jsonValue, []);
41+
42+
$this->assertSame(['foo' => 'bar', 'baz' => 'qux'], $result);
43+
}
44+
45+
public function test_it_returns_string_value_when_json_decode_fails(): void
46+
{
47+
$result = $this->cast->get($this->model, 'test_key', 'plain string', []);
48+
49+
$this->assertSame('plain string', $result);
50+
}
51+
52+
public function test_it_returns_null_for_storage_when_value_is_null(): void
53+
{
54+
$result = $this->cast->set($this->model, 'test_key', null, []);
55+
56+
$this->assertNull($result);
57+
}
58+
59+
public function test_it_returns_string_value_for_storage(): void
60+
{
61+
$result = $this->cast->set($this->model, 'test_key', 'test value', []);
62+
63+
$this->assertSame('test value', $result);
64+
}
65+
66+
public function test_it_returns_numeric_value_as_string_for_storage(): void
67+
{
68+
$result = $this->cast->set($this->model, 'test_key', 123, []);
69+
70+
$this->assertSame('123', $result);
71+
}
72+
73+
public function test_it_encodes_array_to_json_for_storage(): void
74+
{
75+
$result = $this->cast->set($this->model, 'test_key', ['foo' => 'bar'], []);
76+
77+
$this->assertSame('{"foo":"bar"}', $result);
78+
}
79+
80+
public function test_it_converts_stringable_object_for_storage(): void
81+
{
82+
$stringable = new class
83+
{
84+
public function __toString(): string
85+
{
86+
return 'stringable value';
87+
}
88+
};
89+
90+
$result = $this->cast->set($this->model, 'test_key', $stringable, []);
91+
92+
$this->assertSame('stringable value', $result);
93+
}
94+
}

tests/Enums/ArrayableTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cone\Root\Tests\Enums;
6+
7+
use Cone\Root\Enums\Arrayable;
8+
use Cone\Root\Tests\TestCase;
9+
10+
final class ArrayableTest extends TestCase
11+
{
12+
public function test_enum_can_be_converted_to_array(): void
13+
{
14+
$array = TestEnum::toArray();
15+
16+
$this->assertSame([
17+
'active' => 'Active',
18+
'inactive' => 'Inactive',
19+
'pending' => 'Pending',
20+
], $array);
21+
}
22+
}
23+
24+
enum TestEnum: string
25+
{
26+
use Arrayable;
27+
28+
case ACTIVE = 'active';
29+
case INACTIVE = 'inactive';
30+
case PENDING = 'pending';
31+
32+
public function label(): string
33+
{
34+
return match ($this) {
35+
self::ACTIVE => 'Active',
36+
self::INACTIVE => 'Inactive',
37+
self::PENDING => 'Pending',
38+
};
39+
}
40+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cone\Root\Tests\Exceptions;
6+
7+
use Cone\Root\Exceptions\QueryResolutionException;
8+
use Cone\Root\Exceptions\ResourceResolutionException;
9+
use Cone\Root\Exceptions\SaveFormDataException;
10+
use Cone\Root\Tests\TestCase;
11+
use Exception;
12+
13+
final class ExceptionsTest extends TestCase
14+
{
15+
public function test_query_resolution_exception_can_be_thrown(): void
16+
{
17+
$this->expectException(QueryResolutionException::class);
18+
$this->expectExceptionMessage('Test message');
19+
20+
throw new QueryResolutionException('Test message');
21+
}
22+
23+
public function test_query_resolution_exception_extends_exception(): void
24+
{
25+
$exception = new QueryResolutionException('Test');
26+
27+
$this->assertInstanceOf(Exception::class, $exception);
28+
}
29+
30+
public function test_resource_resolution_exception_can_be_thrown(): void
31+
{
32+
$this->expectException(ResourceResolutionException::class);
33+
$this->expectExceptionMessage('Test message');
34+
35+
throw new ResourceResolutionException('Test message');
36+
}
37+
38+
public function test_resource_resolution_exception_extends_exception(): void
39+
{
40+
$exception = new ResourceResolutionException('Test');
41+
42+
$this->assertInstanceOf(Exception::class, $exception);
43+
}
44+
45+
public function test_save_form_data_exception_can_be_thrown(): void
46+
{
47+
$this->expectException(SaveFormDataException::class);
48+
$this->expectExceptionMessage('Test message');
49+
50+
throw new SaveFormDataException('Test message');
51+
}
52+
53+
public function test_save_form_data_exception_extends_exception(): void
54+
{
55+
$exception = new SaveFormDataException('Test');
56+
57+
$this->assertInstanceOf(Exception::class, $exception);
58+
}
59+
}

tests/Jobs/MoveFileTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cone\Root\Tests\Jobs;
6+
7+
use Cone\Root\Jobs\MoveFile;
8+
use Cone\Root\Models\Medium;
9+
use Cone\Root\Tests\TestCase;
10+
use Illuminate\Http\UploadedFile;
11+
use Illuminate\Support\Facades\File;
12+
use Illuminate\Support\Facades\Queue;
13+
use Illuminate\Support\Facades\Storage;
14+
15+
final class MoveFileTest extends TestCase
16+
{
17+
public function test_move_file_job_can_be_dispatched(): void
18+
{
19+
Queue::fake();
20+
21+
$medium = Medium::factory()->make();
22+
$path = '/tmp/test-file.jpg';
23+
24+
MoveFile::dispatch($medium, $path, false);
25+
26+
Queue::assertPushed(MoveFile::class);
27+
}
28+
29+
public function test_move_file_job_moves_file_to_storage(): void
30+
{
31+
Storage::fake('public');
32+
33+
$file = UploadedFile::fake()->image('test.jpg');
34+
$tempPath = $file->getRealPath();
35+
36+
$medium = Medium::factory()->make([
37+
'disk' => 'public',
38+
'path' => 'media/test.jpg',
39+
]);
40+
41+
$job = new MoveFile($medium, $tempPath, true);
42+
$job->handle();
43+
44+
Storage::disk('public')->assertExists('media/test.jpg');
45+
}
46+
47+
public function test_move_file_job_deletes_original_when_not_preserved(): void
48+
{
49+
Storage::fake('public');
50+
51+
$tempDir = Storage::disk('local')->path('root-tmp');
52+
$tempPath = $tempDir.'/test-file.jpg';
53+
File::ensureDirectoryExists($tempDir);
54+
File::put($tempPath, 'test content');
55+
56+
$medium = Medium::factory()->make([
57+
'disk' => 'public',
58+
'path' => 'media/test.jpg',
59+
]);
60+
61+
$job = new MoveFile($medium, $tempPath, false);
62+
$job->handle();
63+
64+
Storage::disk('public')->assertExists('media/test.jpg');
65+
$this->assertFalse(File::exists($tempPath));
66+
}
67+
68+
public function test_move_file_job_preserves_original_when_preserve_is_true(): void
69+
{
70+
Storage::fake('public');
71+
72+
$tempDir = Storage::disk('local')->path('root-tmp');
73+
$tempPath = $tempDir.'/test-file.jpg';
74+
File::ensureDirectoryExists($tempDir);
75+
File::put($tempPath, 'test content');
76+
77+
$medium = Medium::factory()->make([
78+
'disk' => 'public',
79+
'path' => 'media/test.jpg',
80+
]);
81+
82+
$job = new MoveFile($medium, $tempPath, true);
83+
$job->handle();
84+
85+
Storage::disk('public')->assertExists('media/test.jpg');
86+
$this->assertTrue(File::exists($tempPath));
87+
}
88+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cone\Root\Tests\Jobs;
6+
7+
use Cone\Root\Jobs\PerformConversions;
8+
use Cone\Root\Models\Medium;
9+
use Cone\Root\Tests\TestCase;
10+
use Illuminate\Support\Facades\Queue;
11+
12+
final class PerformConversionsTest extends TestCase
13+
{
14+
public function test_perform_conversions_job_can_be_dispatched(): void
15+
{
16+
Queue::fake();
17+
18+
$medium = Medium::factory()->make();
19+
20+
PerformConversions::dispatch($medium);
21+
22+
Queue::assertPushed(PerformConversions::class);
23+
}
24+
25+
public function test_perform_conversions_job_calls_convert_on_medium(): void
26+
{
27+
$medium = $this->createMock(Medium::class);
28+
$medium->expects($this->once())->method('convert');
29+
30+
$job = new PerformConversions($medium);
31+
$job->handle();
32+
}
33+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cone\Root\Tests\Listeners;
6+
7+
use Cone\Root\Listeners\FormatRootStubs;
8+
use Cone\Root\Tests\TestCase;
9+
use Illuminate\Foundation\Events\VendorTagPublished;
10+
use Illuminate\Support\Facades\App;
11+
use Illuminate\Support\Facades\File;
12+
13+
final class FormatRootStubsTest extends TestCase
14+
{
15+
public function test_listener_formats_root_stubs(): void
16+
{
17+
$tempFile = tempnam(sys_get_temp_dir(), 'stub_');
18+
file_put_contents($tempFile, '<?php namespace {{ namespace }}Tests;');
19+
20+
$event = new VendorTagPublished('root-stubs', [$tempFile]);
21+
22+
$listener = new FormatRootStubs();
23+
$listener->handle($event);
24+
25+
$contents = file_get_contents($tempFile);
26+
27+
$this->assertStringContainsString(App::getNamespace(), $contents);
28+
$this->assertStringNotContainsString('{{ namespace }}', $contents);
29+
30+
unlink($tempFile);
31+
}
32+
33+
public function test_listener_ignores_non_root_stubs_tag(): void
34+
{
35+
$tempFile = tempnam(sys_get_temp_dir(), 'stub_');
36+
file_put_contents($tempFile, '<?php namespace {{ namespace }}Tests;');
37+
38+
$event = new VendorTagPublished('other-tag', [$tempFile]);
39+
40+
$listener = new FormatRootStubs();
41+
$listener->handle($event);
42+
43+
$contents = file_get_contents($tempFile);
44+
45+
$this->assertStringContainsString('{{ namespace }}', $contents);
46+
47+
unlink($tempFile);
48+
}
49+
50+
public function test_listener_handles_multiple_files(): void
51+
{
52+
$tempFile1 = tempnam(sys_get_temp_dir(), 'stub_');
53+
$tempFile2 = tempnam(sys_get_temp_dir(), 'stub_');
54+
55+
file_put_contents($tempFile1, '<?php namespace {{ namespace }}Tests;');
56+
file_put_contents($tempFile2, '<?php namespace {{ namespace }}Models;');
57+
58+
$event = new VendorTagPublished('root-stubs', [$tempFile1, $tempFile2]);
59+
60+
$listener = new FormatRootStubs();
61+
$listener->handle($event);
62+
63+
$contents1 = file_get_contents($tempFile1);
64+
$contents2 = file_get_contents($tempFile2);
65+
66+
$this->assertStringNotContainsString('{{ namespace }}', $contents1);
67+
$this->assertStringNotContainsString('{{ namespace }}', $contents2);
68+
69+
unlink($tempFile1);
70+
unlink($tempFile2);
71+
}
72+
}

0 commit comments

Comments
 (0)