/
itp_practice
/
itp_backend
Обзор
Документация
Войти
/
itp_practice
/
itp_backend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
tests/Unit/Services/TapAppServiceTest.php
207 строк
6 KB
vivanenko
tariff buy
27 май 2025, 19:46
27 май 2025, 19:46
d32c67d
Код
Авторство
О чём код?
<?php namespace Tests\Unit\Services; use Tests\TestCase; use App\Services\TapAppService; use App\Models\Order; use App\Models\User; use App\Models\TapApp; use App\Models\Tap; use Illuminate\Foundation\Testing\RefreshDatabase; class TapAppServiceTest extends TestCase { use RefreshDatabase; private TapAppService $tapAppService; protected function setUp(): void { parent::setUp(); $this->tapAppService = new TapAppService(); } public function test_initializes_tap_apps_for_order() { // Arrange $user = User::factory()->create(); $order = Order::factory()->create(['user_id' => $user->id]); $tapApp = TapApp::factory()->create(); $tap = Tap::factory()->create([ 'user_id' => $user->id, 'tap_app_id' => $tapApp->id, 'process_state' => 'new', 'is_active' => false, ]); // Act $result = $this->tapAppService->initializeTapAppsForOrder($order); // Assert $this->assertCount(1, $result['initialized_taps']); $this->assertEquals(1, $result['subscriptions_processed']); $this->assertEquals($tap->id, $result['initialized_taps'][0]->id); } public function test_returns_empty_when_no_new_taps() { // Arrange $user = User::factory()->create(); $order = Order::factory()->create(['user_id' => $user->id]); // Act $result = $this->tapAppService->initializeTapAppsForOrder($order); // Assert $this->assertCount(0, $result['initialized_taps']); $this->assertEquals(0, $result['subscriptions_processed']); } public function test_starts_tap() { // Arrange $user = User::factory()->create(); $tapApp = TapApp::factory()->create(); $tap = Tap::factory()->create([ 'user_id' => $user->id, 'tap_app_id' => $tapApp->id, 'is_active' => false, 'process_state' => 'new', ]); // Act $result = $this->tapAppService->startTap($tap->id, $user->id); // Assert $this->assertTrue($result); $tap->refresh(); $this->assertTrue($tap->is_active); $this->assertEquals('running', $tap->process_state); } public function test_throws_exception_when_starting_non_existent_tap() { // Arrange $user = User::factory()->create(); // Act & Assert $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Tap not found'); $this->tapAppService->startTap(999, $user->id); } public function test_throws_exception_when_starting_already_active_tap() { // Arrange $user = User::factory()->create(); $tapApp = TapApp::factory()->create(); $tap = Tap::factory()->create([ 'user_id' => $user->id, 'tap_app_id' => $tapApp->id, 'is_active' => true, 'process_state' => 'running', ]); // Act & Assert $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Tap is already active'); $this->tapAppService->startTap($tap->id, $user->id); } public function test_stops_tap() { // Arrange $user = User::factory()->create(); $tapApp = TapApp::factory()->create(); $tap = Tap::factory()->create([ 'user_id' => $user->id, 'tap_app_id' => $tapApp->id, 'is_active' => true, 'process_state' => 'running', ]); // Act $result = $this->tapAppService->stopTap($tap->id, $user->id); // Assert $this->assertTrue($result); $tap->refresh(); $this->assertFalse($tap->is_active); $this->assertEquals('stopped', $tap->process_state); } public function test_gets_user_taps_status() { // Arrange $user = User::factory()->create(); $tapApp1 = TapApp::factory()->create(); $tapApp2 = TapApp::factory()->create(); $activeTap = Tap::factory()->create([ 'user_id' => $user->id, 'tap_app_id' => $tapApp1->id, 'is_active' => true, 'process_state' => 'running', ]); $newTap = Tap::factory()->create([ 'user_id' => $user->id, 'tap_app_id' => $tapApp2->id, 'is_active' => false, 'process_state' => 'new', ]); // Act $result = $this->tapAppService->getUserTapsStatus($user->id); // Assert $this->assertEquals(2, $result['total_taps']); $this->assertEquals(1, $result['active_taps']); $this->assertEquals(1, $result['new_taps']); $this->assertEquals(1, $result['running_taps']); $this->assertEquals(0, $result['stopped_taps']); $this->assertCount(2, $result['taps']); } public function test_restarts_user_taps() { // Arrange $user = User::factory()->create(); $tapApp1 = TapApp::factory()->create(); $tapApp2 = TapApp::factory()->create(); $stoppedTap1 = Tap::factory()->create([ 'user_id' => $user->id, 'tap_app_id' => $tapApp1->id, 'is_active' => false, 'process_state' => 'stopped', ]); $stoppedTap2 = Tap::factory()->create([ 'user_id' => $user->id, 'tap_app_id' => $tapApp2->id, 'is_active' => false, 'process_state' => 'stopped', ]); // Act $result = $this->tapAppService->restartUserTaps($user->id); // Assert $this->assertEquals(2, $result); $stoppedTap1->refresh(); $stoppedTap2->refresh(); $this->assertTrue($stoppedTap1->is_active); $this->assertEquals('running', $stoppedTap1->process_state); $this->assertTrue($stoppedTap2->is_active); $this->assertEquals('running', $stoppedTap2->process_state); } }