DossierServiceTest.php
1 <?php 2 3 namespace Tests\Services; 4 5 use App\Enums\FolderEnum; 6 use App\Http\Services\DossierService; 7 use App\Models\Dossier; 8 use App\Models\Folder; 9 use App\Models\Taxonomy; 10 use Illuminate\Support\Facades\DB; 11 use Illuminate\Http\UploadedFile; 12 use Illuminate\Support\Collection; 13 use Illuminate\Support\Facades\Storage; 14 use Tests\TestCase; 15 16 class DossierServiceTest extends TestCase 17 { 18 public function setUp(): void 19 { 20 parent::setUp(); 21 $this->actingAs($this->getAdminUser()); 22 } 23 24 25 public function testSearch() 26 { 27 $query = DossierService::search('', 'id', 'asc', []); 28 $this->assertInstanceOf( 29 \Illuminate\Database\Eloquent\Builder::class, 30 $query 31 ); 32 } 33 34 /** 35 * @throws \Throwable 36 */ 37 public function testCreate() 38 { 39 $data = $this->exampleDataArray(); 40 41 $created = DossierService::create($data); 42 $this->assertInstanceOf(Dossier::class, $created); 43 } 44 45 /** 46 * @throws \Throwable 47 */ 48 public function testUpdate() 49 { 50 $data = $this->exampleDataArray(); 51 DossierService::create($data); 52 $dossier = Dossier::latest()->first(); 53 $data = $this->exampleDataArray(true); 54 $update = DossierService::update($data, $dossier->id); 55 $this->assertTrue($update); 56 } 57 58 /** 59 * @throws \Throwable 60 */ 61 public function testRemove() 62 { 63 $data = $this->exampleDataArray(); 64 DossierService::create($data); 65 $dossier = Dossier::latest()->first(); 66 $delete = DossierService::remove($dossier->id); 67 $this->assertTrue($delete); 68 } 69 70 /** 71 * @throws \Throwable 72 */ 73 public function testRestore() 74 { 75 $data = $this->exampleDataArray(); 76 DossierService::create($data); 77 $dossier = Dossier::latest()->first(); 78 $restore = DossierService::restore($dossier->id); 79 $this->assertTrue($restore); 80 } 81 82 public function testGetNextCodeByYear() 83 { 84 $this->assertIsString(DossierService::getNextCodeByYear()); 85 } 86 87 public function testBuildFromTaxonomy() 88 { 89 $created = DossierService::buildFromTaxonomy($this->exampleTaxonomyData()); 90 $this->assertInstanceOf(Dossier::class, $created); 91 } 92 93 public function testGetDossierTypes() 94 { 95 $this->assertInstanceOf( 96 Collection::class, 97 DossierService::getDossierTypes() 98 ); 99 } 100 101 public function testGetDossierSubTypes() 102 { 103 $this->assertInstanceOf( 104 Collection::class, 105 DossierService::getDossierSubTypes(1) 106 ); 107 } 108 109 public function testGetDossierModes() 110 { 111 $this->assertInstanceOf( 112 Collection::class, 113 DossierService::getDossierModes(1) 114 ); 115 } 116 117 public function testCreateFolders() 118 { 119 $data = $this->exampleDataArray(); 120 DossierService::create($data); 121 $dossier = Dossier::latest()->first(); 122 $rootDossierFolder = Folder::find(FolderEnum::DOSSIERS); 123 $this->assertDatabaseHas('folders', [ 124 'name' => $dossier->code_year . '-' . $dossier->code_seq, 125 'created_by' => $this->getAdminUser()->id, 126 'parent_id' => $rootDossierFolder->id, 127 ]); 128 } 129 130 public function testFindByCode() { 131 $dossier = DossierService::findByCode(Dossier::first()->code); 132 $this->assertNotNull($dossier); 133 $this->assertInstanceOf(Dossier::class, $dossier); 134 } 135 136 private function exampleDataArray(bool $update = false): array 137 { 138 return [ 139 'code_year' => date('Y'), 140 'code_seq' => DossierService::getNextCodeByYear(), 141 'toe_id' => 1, 142 'dossier_type_id' => 1, 143 'mode' => 1, 144 'applicant_id' => 1, 145 'sponsor_id' => 1, 146 'manufacturer_id' => 1, 147 'laboratory_id' => 1, 148 'principal_certifier_id' => 1, 149 'norm_id' => 1, 150 'norm_scope_id' => 1, 151 'related_workbooks' => $update ? [1] : [], 152 'evaluators' => $update ? [1] : [], 153 'secondary_certifiers' => $update ? [1] : [], 154 'external_certifiers' => $update ? [1] : [], 155 'experts' => $update ? [1] : [], 156 'informed_staff' => $update ? [1] : [], 157 ]; 158 } 159 160 private function exampleTaxonomyData() 161 { 162 return Taxonomy::factory()->create(); 163 } 164 165 private function exampleFileData(): UploadedFile 166 { 167 $dir = 'test_files'; 168 $filename = '2023-03-24_2023-19_solicitud_certificacion_v1.0.0.zip'; 169 170 return UploadedFile::fake() 171 ->createWithContent( 172 $filename, 173 Storage::get("$dir/$filename") 174 ); 175 } 176 }