/** * @param File $file * @param $data * * @return File|false */ public function update(File $file, $data) { if (!is_null($file->path)) { $currentFilePath = $file->path; } $file->fill(array_merge($data, [File::PATH => FilesStorage::getFilesDirectory() . $data[File::CLIENT_ORIGINAL_NAME]])); $fileIsStored = Storage::put($file->path, file_get_contents($data[File::TEMPORARY_PATH])); if (!$fileIsStored || !$file->save()) { return false; } if (isset($currentFilePath)) { Storage::delete($currentFilePath); } return $file; }
/** @test */ public function it_updates_a_file() { $dbFileRepository = new DbFileRepository(); $expectedClientOriginalName = 'dummy_logo3.png'; $expectedFilePath = FilesStorage::getFilesDirectory() . $expectedClientOriginalName; $expectedFile = factory(File::class, 'without_storage')->make(['path' => $expectedFilePath, 'client_original_name' => $expectedClientOriginalName]); $expectedTemporaryPath = storage_path('app/testing/dummy_logo.png'); $requestData[File::NAME] = $expectedFile->name; $requestData[File::DESCRIPTION] = $expectedFile->description; $requestData[File::CLIENT_ORIGINAL_NAME] = $expectedClientOriginalName; $tempFilePath = [File::TEMPORARY_PATH => $expectedTemporaryPath]; $currentFile = factory(File::class)->create(); $oldFilePath = $currentFile->path; $this->assertTrue(Storage::exists($currentFile->path)); $this->assertFalse(Storage::exists($expectedFilePath)); $this->assertNotFalse($dbFileRepository->update($currentFile, array_merge($requestData, $tempFilePath))); $this->seeInDatabase('files', [File::NAME => $expectedFile->name, File::DESCRIPTION => $expectedFile->description, File::PATH => $expectedFilePath, File::CLIENT_ORIGINAL_NAME => $expectedClientOriginalName, File::SLUG => 'dummy-logo3-png']); $this->assertFalse(Storage::exists($oldFilePath)); $this->assertTrue(Storage::exists($expectedFilePath)); }
/** @test */ public function it_creates_company() { $dbUserRepository = new DbUserRepository(); $companyRepresentativeUser = factory(User::class)->create(); $dbUserRepository->assignCompanyRepresentativeRole($companyRepresentativeUser); factory(Industry::class, 2)->create(); factory(Country::class, 2)->create(); $expectedCompany = factory(Company::class, 'relationless')->make(['user_id' => $companyRepresentativeUser->id]); $expectedIndustry = factory(Industry::class)->create(); $expectedCountry = factory(Country::class)->create(); $expectedLogoPath = FilesStorage::getFilesDirectory() . 'dummy_logo.png'; $this->actingAs($companyRepresentativeUser)->visit(route('my.companies.create'))->type($expectedCompany->name, 'name')->select($expectedIndustry->id, 'industry_id')->select($expectedCountry->id, 'country_id')->type($expectedCompany->business_leader, 'business_leader')->type($expectedCompany->address, 'address')->type($expectedCompany->email, 'email')->type($expectedCompany->phone_number, 'phone_number')->type($expectedCompany->focus, 'focus')->type($expectedCompany->description, 'description')->attach(storage_path('app/testing/dummy_logo.png'), 'logo_path')->press(trans('ahk.create'))->see(trans('ahk_messages.company_successfully_stored'))->see($expectedCompany->name)->seeIsSelected('industry_id', $expectedIndustry->id)->seeIsSelected('country_id', $expectedCountry->id)->see($expectedCompany->business_leader)->see($expectedCompany->address)->see($expectedCompany->email)->see($expectedCompany->phone_number)->see($expectedCompany->focus)->see($expectedCompany->description)->see(route('files.render', ['path' => $expectedLogoPath])); }
<?php use App\Ahk\File; use App\Ahk\Storage\FilesStorage; use Illuminate\Support\Facades\Storage; $factory->define(File::class, function (Faker\Generator $faker) { $fileWithPrimaryData = factory(File::class, 'without_storage')->make(); $storageLocation = FilesStorage::getFilesDirectory(); $clientOriginalName = $faker->name . $faker->fileExtension; $tempFilePath = file_get_contents(storage_path('app/testing/dummy_logo.png')); $fileLocation = $storageLocation . $clientOriginalName; Storage::put($fileLocation, $tempFilePath); return array_merge($fileWithPrimaryData->toArray(), ['path' => $fileLocation, 'client_original_name' => $clientOriginalName]); }); $factory->defineAs(File::class, 'without_storage', function (Faker\Generator $faker) { $name = $faker->unique()->name; $clientOriginalName = "{$faker->name}.{$faker->fileExtension}"; $storageLocation = FilesStorage::getFilesDirectory(); $fileLocation = $storageLocation . $clientOriginalName; return ['name' => $name, 'path' => $fileLocation, 'description' => $faker->paragraph(), 'client_original_name' => $clientOriginalName]; });