/ tests / Services / NormServiceTest.php
NormServiceTest.php
 1  <?php
 2  
 3  namespace Tests\Services;
 4  
 5  use App\Http\Resources\NormResource;
 6  use App\Http\Services\NormService;
 7  use App\Models\Norm;
 8  use App\Models\NormGroup;
 9  use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
10  use Tests\TestCase;
11  
12  class NormServiceTest extends TestCase
13  {
14      public Norm $norm;
15  
16      public function setUp(): void
17      {
18          parent::setUp();
19          Norm::factory()->count(10)->create();
20          $this->norm = Norm::factory()->create();
21      }
22  
23      public function test_index()
24      {
25          $got = NormService::index();
26  
27          $this->assertInstanceOf(AnonymousResourceCollection::class, $got);
28          $this->assertCount(5, $got);
29      }
30  
31      public function test_create()
32      {
33          $data = Norm::factory()->make()->toArray();
34          $created = NormService::create($data);
35          $this->assertTrue($created);
36          $this->assertDatabaseHas('norms', $data);
37      }
38  
39      public function test_remove()
40      {
41          $removed = NormService::remove($this->norm->id);
42          $this->assertTrue($removed);
43          $this->assertSoftDeleted($this->norm);
44      }
45  
46      public function test_update()
47      {
48          $data = Norm::factory()->make()->toArray();
49          $updated = NormService::update($data, $this->norm->id);
50  
51          $this->assertTrue($updated);
52          $this->assertDatabaseHas('norms', $data);
53          $this->assertDatabaseMissing('norms', $this->norm->toArray());
54      }
55  
56      public function test_find()
57      {
58          $found = NormService::find($this->norm->id);
59          $this->assertNotNull($found);
60          $this->assertInstanceOf(NormResource::class, $found);
61      }
62  
63      public function test_get_norm_groups()
64      {
65          $group = NormGroup::first();
66          $count = $group->norms->count();
67          Norm::factory()->count(10)->create([
68              'norm_group_id' => $group->id,
69          ]);
70  
71          $got = NormService::getNorms($group->id);
72          $this->assertNotNull($got);
73          $this->assertCount($count + 10, $got);
74      }
75  }