Add feature tests for Document, FastRequest, FileStorage, Shift, and SMS services

This commit is contained in:
2026-09-21 05:17:45 +03:30
parent fd0d445b6e
commit f77ff527e7
10 changed files with 1368 additions and 9 deletions
@@ -0,0 +1,74 @@
<?php
namespace Tests\Feature\Services\Sms;
use App\Services\Sms\IranPayamakService;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use RuntimeException;
use Tests\TestCase;
class IranPayamakServiceTest extends TestCase
{
protected IranPayamakService $service;
protected function setUp(): void
{
parent::setUp();
config([
'services.iranpayamak.api_url' => 'https://sms.test/pattern',
'services.iranpayamak.api_key' => 'api-key',
'services.iranpayamak.line_number' => '5000',
'services.iranpayamak.patterns.verify_code' => 'pattern-1',
]);
$this->service = app(IranPayamakService::class);
}
public function test_send_pattern_posts_expected_payload_and_headers(): void
{
Http::fake(['sms.test/pattern' => Http::response(['status' => 'ok'])]);
$result = $this->service->sendPattern('09120000000', 'verify_code', ['otp' => '1234']);
$this->assertSame(['status' => 'ok'], $result);
Http::assertSent(function (Request $request) {
return $request->url() === 'https://sms.test/pattern'
&& $request->hasHeader('Api-Key', 'api-key')
&& $request['code'] === 'pattern-1'
&& $request['attributes'] === ['otp' => '1234']
&& $request['recipient'] === '09120000000'
&& $request['line_number'] === '5000'
&& $request['number_format'] === 'english';
});
}
public function test_send_pattern_converts_plus98_prefix_to_local_format(): void
{
Http::fake(['sms.test/pattern' => Http::response([])]);
$this->service->sendPattern('+989120000000', 'verify_code', []);
Http::assertSent(fn (Request $request) => $request['recipient'] === '09120000000');
}
public function test_send_pattern_returns_empty_array_for_empty_body(): void
{
Http::fake(['sms.test/pattern' => Http::response('', 200)]);
$this->assertSame([], $this->service->sendPattern('09120000000', 'verify_code', []));
}
public function test_send_pattern_throws_on_unsuccessful_response(): void
{
Http::fake(['sms.test/pattern' => Http::response('unauthorized', 401)]);
try {
$this->service->sendPattern('09120000000', 'verify_code', []);
$this->fail('RuntimeException was not thrown');
} catch (RuntimeException $e) {
$this->assertStringContainsString('401', $e->getMessage());
$this->assertStringContainsString('unauthorized', $e->getMessage());
}
}
}
@@ -0,0 +1,82 @@
<?php
namespace Tests\Feature\Services\Sms;
use App\Jobs\SendPatternSms;
use App\Models\Document;
use App\Models\Invoice;
use App\Services\Sms\SmsNotificationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
class SmsNotificationServiceTest extends TestCase
{
use RefreshDatabase;
protected SmsNotificationService $service;
protected function setUp(): void
{
parent::setUp();
Queue::fake();
config(['services.iranpayamak.patterns' => [
'request_registered' => 'p-registered',
'payment_confirmed' => 'p-payment',
'report_ready' => 'p-report',
]]);
$this->service = app(SmsNotificationService::class);
}
public function test_request_registered_dispatches_sms_to_requester(): void
{
$this->service->requestRegistered(Document::factory()->create());
Queue::assertPushed(SendPatternSms::class, 1);
}
public function test_payment_confirmed_dispatches_sms_to_invoice_user(): void
{
$this->service->paymentConfirmed(Invoice::factory()->create());
Queue::assertPushed(SendPatternSms::class, 1);
}
public function test_report_ready_dispatches_sms_to_requester(): void
{
$this->service->reportReady(Document::factory()->create());
Queue::assertPushed(SendPatternSms::class, 1);
}
public function test_nothing_is_dispatched_when_pattern_is_missing(): void
{
config(['services.iranpayamak.patterns.request_registered' => null]);
$this->service->requestRegistered(Document::factory()->create());
Queue::assertNothingPushed();
}
public function test_nothing_is_dispatched_when_document_has_no_requester(): void
{
$document = Document::factory()->create();
$document->requester()->dissociate();
$document->setRelation('requester', null);
$this->service->reportReady($document);
Queue::assertNothingPushed();
}
public function test_nothing_is_dispatched_when_user_phone_is_empty(): void
{
$document = Document::factory()->create();
$document->requester->phone = '';
$this->service->requestRegistered($document);
Queue::assertNothingPushed();
}
}