Files

59 lines
1.5 KiB
PHP

<?php
namespace Tests\Feature\Services\Document;
use App\Models\Document;
use App\Services\Document\DocumentCodeService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use RuntimeException;
use Tests\TestCase;
class DocumentCodeServiceTest extends TestCase
{
use RefreshDatabase;
protected DocumentCodeService $service;
protected function setUp(): void
{
parent::setUp();
$this->service = app(DocumentCodeService::class);
}
public function test_generate_returns_six_digit_zero_padded_code(): void
{
$this->assertMatchesRegularExpression('/^\d{6}$/', $this->service->generate());
}
public function test_generate_returns_code_not_used_by_existing_document(): void
{
Document::factory()->count(5)->create();
$code = $this->service->generate();
$this->assertDatabaseMissing('documents', ['code' => $code]);
}
public function test_generate_throws_when_no_unique_code_found_within_tries(): void
{
$this->expectException(RuntimeException::class);
$this->service->generate(0);
}
public function test_document_observer_assigns_code_on_create(): void
{
$document = Document::factory()->create(['code' => null]);
$this->assertMatchesRegularExpression('/^\d{6}$/', $document->code);
}
public function test_document_observer_keeps_explicit_code(): void
{
$document = Document::factory()->create(['code' => '123456']);
$this->assertSame('123456', $document->code);
}
}