ToeServiceTest.php
1 <?php 2 3 namespace Tests\Services; 4 5 use App\Http\Services\ToeService; 6 use App\Jobs\GrypeSpdx; 7 use App\Models\Country; 8 use App\Models\Entity; 9 use App\Models\TOE; 10 use App\Models\TOEType; 11 use Illuminate\Foundation\Testing\WithFaker; 12 use Illuminate\Pagination\LengthAwarePaginator; 13 use Illuminate\Support\Collection; 14 use Illuminate\Support\Facades\Bus; 15 use Illuminate\Support\Facades\Queue; 16 use Illuminate\Support\Facades\Storage; 17 use Tests\TestCase; 18 19 class ToeServiceTest extends TestCase 20 { 21 use WithFaker; 22 23 public TOE $toe; 24 25 public function setUp(): void 26 { 27 parent::setUp(); 28 $this->toe = TOE::factory()->create(); 29 } 30 31 public function testSearchToes() 32 { 33 $query = ToeService::searchToes('', 'id', 'asc', []); 34 $this->assertInstanceOf(\Illuminate\Database\Eloquent\Builder::class, $query); 35 } 36 37 public function testFind() 38 { 39 $this->assertInstanceOf(TOE::class, ToeService::find($this->toe->id)); 40 } 41 42 public function testIndex() 43 { 44 $this->assertInstanceOf(LengthAwarePaginator::class, ToeService::index()); 45 } 46 47 /** 48 * @throws \Throwable 49 */ 50 public function testCreate() 51 { 52 Queue::fake(); 53 54 $data = $this->toe->toArray(); 55 $created = ToeService::create($data); 56 $this->assertTrue($created); 57 Queue::assertPushed(GrypeSpdx::class); 58 } 59 60 /** 61 * @throws \Throwable 62 */ 63 public function testUpdate() 64 { 65 Queue::fake(); 66 67 $data = $this->toe->toArray(); 68 $word = $this->faker->word; 69 $data['name'] = $word; 70 $oldName = $this->toe->name; 71 72 $updated = ToeService::update($data, $this->toe->id); 73 74 $this->assertTrue($updated); 75 $this->assertDatabaseMissing('toes', [ 76 'id' => $this->toe->id, 77 'name' => $oldName, 78 ]); 79 $this->assertDatabaseHas('toes', [ 80 'id' => $this->toe->id, 81 'name' => $word, 82 ]); 83 84 Queue::assertNotPushed(GrypeSpdx::class); 85 } 86 87 public function testUpdateSpdx() 88 { 89 $path = 'tmp/test.spdx'; 90 Storage::put($path, 'test'); 91 Queue::fake(); 92 93 $data = [ 94 'spdx_path' => $path, 95 ]; 96 97 $updated = ToeService::update($data, $this->toe->id); 98 $this->assertTrue($updated); 99 Queue::assertPushed(GrypeSpdx::class); 100 101 Storage::delete($path); 102 } 103 104 /** 105 * @throws \Throwable 106 */ 107 public function testRemove() 108 { 109 $removed = ToeService::remove($this->toe->id); 110 $this->assertTrue($removed); 111 $this->assertSoftDeleted($this->toe->getTable(), ['id' => $this->toe->id]); 112 } 113 114 public function testRestore() 115 { 116 $this->toe->delete(); 117 $restore = ToeService::restore($this->toe->id); 118 $this->assertTrue($restore); 119 } 120 121 public function testGetToes() 122 { 123 $query = ToeService::getToes(); 124 $this->assertInstanceOf(Collection::class, $query); 125 } 126 127 128 }