/ tests / Services / NormScopeServiceTest.php
NormScopeServiceTest.php
  1  <?php
  2  
  3  namespace Tests\Services;
  4  
  5  use App\Http\Resources\NormScopeResource;
  6  use App\Http\Services\NormScopeService;
  7  use App\Models\Norm;
  8  use App\Models\NormGroup;
  9  use App\Models\NormScope;
 10  use App\Models\Workbook;
 11  use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
 12  use Tests\TestCase;
 13  
 14  class NormScopeServiceTest extends TestCase
 15  {
 16      public NormGroup $group;
 17  
 18      public function test_index()
 19      {
 20          $norm = Norm::factory()
 21              ->has(NormScope::factory()->count(15))
 22              ->create();
 23  
 24          $got = NormScopeService::index($norm->id);
 25          $this->assertInstanceOf(AnonymousResourceCollection::class, $got);
 26          $this->assertCount(10, $got);
 27      }
 28  
 29      public function test_create()
 30      {
 31          $name = 'NormScope';
 32          $desc = 'Description';
 33          $workbookId = Workbook::firstOrFail()->id;
 34          $normId = Norm::firstOrFail()->id;
 35  
 36          $created = NormScopeService::create([
 37              'name' => $name,
 38              'description' => $desc,
 39              'norm_id' => $normId,
 40              'workbooksIds' => [$workbookId],
 41          ]);
 42  
 43          $this->assertTrue($created);
 44          $this->assertDatabaseHas('norm_scopes', [
 45              'name' => $name,
 46              'description' => $desc,
 47              'norm_id' => $normId,
 48          ]);
 49          $this->assertDatabaseHas('norm_scope_workbook', [
 50              'norm_scope_id' => NormScope::latest()->first()->id,
 51              'workbook_id' => $workbookId,
 52          ]);
 53      }
 54  
 55      public function test_remove()
 56      {
 57          $scope = NormScope::latest()->first();
 58          $removed = NormScopeService::remove($scope->id);
 59  
 60          $this->assertTrue($removed);
 61          $this->assertSoftDeleted($scope);
 62      }
 63  
 64      public function test_update()
 65      {
 66          $scope = NormScope::latest()->first();
 67          $old = $scope->toArray();
 68  
 69          $name = 'Updated Test Norm Scope';
 70          $desc = 'This is an updated test Norm Scope';
 71          $workbookId = Workbook::firstOrFail()->id;
 72  
 73          $updated = NormScopeService::update([
 74              'name' => $name,
 75              'description' => $desc,
 76              'workbooksIds' => [$workbookId],
 77          ], $scope->id);
 78  
 79          $this->assertTrue($updated);
 80          $this->assertDatabaseHas('norm_scopes', [
 81              'name' => $name,
 82              'description' => $desc,
 83          ]);
 84          $this->assertDatabaseHas('norm_scope_workbook', [
 85              'norm_scope_id' => $scope->id,
 86              'workbook_id' => $workbookId,
 87          ]);
 88          $this->assertDatabaseMissing('norm_scopes', $old);
 89      }
 90  
 91      public function test_find()
 92      {
 93          $scope = NormScope::latest()->first();
 94  
 95          $found = NormScopeService::find($scope->id);
 96          $this->assertInstanceOf(NormScopeResource::class, $found);
 97      }
 98  
 99      public function test_get_norm_groups()
100      {
101          $norm = Norm::first();
102          $scopesInNorm = NormScope::where('norm_id', '=', $norm->id)->get();
103  
104          $got = NormScopeService::getNormScopes($norm->id);
105          $this->assertNotNull($got);
106          $this->assertCount($scopesInNorm->count(), $got);
107      }
108  }