From f72645e2c2ce19cefd94a5aadbe7d3c4baa029bb Mon Sep 17 00:00:00 2001 From: Aqeeb Imtiaz Harun Date: Wed, 16 May 2018 09:57:03 +0800 Subject: [PATCH] Company Profile has been completed and tested --- app/Http/Controllers/CompanyController.php | 51 ++++++------- tests/Unit/CompanyTest.php | 86 ++++++++++++---------- 2 files changed, 70 insertions(+), 67 deletions(-) diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php index 0d90711..e5c45b1 100644 --- a/app/Http/Controllers/CompanyController.php +++ b/app/Http/Controllers/CompanyController.php @@ -4,7 +4,7 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Company; -use Image; +//use Image; //only used in companyProfile(), uncomment if using that portion of the code use Illuminate\Support\Facades\Storage; use Validator; @@ -24,13 +24,6 @@ class CompanyController extends Controller return redirect('/'); } - /** - * Update the specified resource in storage. - * - * @param \Illuminate\Http\Request $request - * @param int $id - * @return \Illuminate\Http\Response - */ public function update(Request $request, $id) { $company = Company::find($id); @@ -57,16 +50,15 @@ class CompanyController extends Controller public function companyProfile(Request $request, $id) { - $company = Company::find($id); + $company = Company::find($id);//find the company first if (!$company) { return response()->json(['message'=>'Document not found'],200); } - + //validaating file types $validator = Validator::make($request->all(), [ 'company_profile' => 'image|mimes:jpg,jpeg,bmp,png' ]); - if($validator->fails()) { return "failed"; @@ -75,10 +67,8 @@ class CompanyController extends Controller if($file = $request->file('company_profile')) //company profile picture { $company_profile = $request->file('company_profile'); - $filename = $company_profile->getClientOriginalName(); - //$filename = $company_profile->uniqid('img_'); - $unique_name = 'comp_prof_' . md5($filename); - //$destinationPath = config('app.fileDestinationPath').'/'.$filename; + $filename = $company_profile->getClientOriginalName(); //get the original file name + $unique_name = 'comp_prof_' . md5($filename. time()); //generating a random file name $file = $request->file('company_profile'); $ext = $file->getClientOriginalExtension(); @@ -86,14 +76,17 @@ class CompanyController extends Controller Storage::putFileAs( 'company_profile',/*folder name*/ $file, $unique_name. '.' .$ext ); - $company->company_profile = $unique_name. '.' .$ext; - // $name = $file->getClientOriginalName(); - // $img_file = $request->file('company_profile'); - // Image::make($img_file)->resize(300,300)->save(public_path('\company_profile')); - // //return $img_file; - // // $file->move('uploads/company_profile/', $img_file); + $company->company_profile = $unique_name. '.' .$ext; //update database + // only use the following section of the code if you're omiting the upper portion, also uncomment the Image Library on line 7 & install Intervention Image package + /* + $name = $file->getClientOriginalName(); + $img_file = $request->file('company_profile'); + Image::make($img_file)->resize(300,300)->save(public_path('\company_profile')); + //return $img_file; + // $file->move('uploads/company_profile/', $img_file); - // $input['company_profile'] = $name; + $input['company_profile'] = $name; + */ } //end if $company->save(); return response()->json(['company'=>$company],200); @@ -111,17 +104,15 @@ class CompanyController extends Controller $validator = Validator::make($request->all(), [ 'reg_cert' => 'mimes:jpg,jpeg,bmp,png,gif,svg,pdf' ]); - - if($validator->fails()){ - return "failed"; - } + if($validator->fails()){ + return "failed"; + } if($request->hasFile('reg_cert')) //Registration Certificate upload { $reg_cert = $request->file('reg_cert'); - $filename = $reg_cert->getClientOriginalName(); - $unique_name = 'reg_cert_' . md5($filename); - //$destinationPath = config('app.fileDestinationPath').'/'.$filename; + $filename = $reg_cert->getClientOriginalName(); //original filename + $unique_name = 'reg_cert_' . md5($filename. time()); //generating a random file name $file = $request->file('reg_cert'); $ext = $file->getClientOriginalExtension(); @@ -129,7 +120,7 @@ class CompanyController extends Controller Storage::putFileAs( 'reg_cert',/*folder name*/ $file, $unique_name. '.' .$ext ); - $company->reg_cert = $unique_name. '.' .$ext; + $company->reg_cert = $unique_name. '.' .$ext; //this updates database }//end if $company->save(); return response()->json(['company'=>$company],200); diff --git a/tests/Unit/CompanyTest.php b/tests/Unit/CompanyTest.php index c6b980f..0e8fb7e 100644 --- a/tests/Unit/CompanyTest.php +++ b/tests/Unit/CompanyTest.php @@ -20,50 +20,62 @@ class CompanyTest extends TestCase $this->assertTrue(true); } - // public function testPOST() - // { - // $response = $this->json('POST', '/api/company', [ - // 'company_name'=>'testing', - // 'registration_no'=>'testing', - // 'tax_no'=>'testing', - // 'tel_no'=>'12345', - // 'fax'=>'12345', - // 'address'=>'testing', - // 'city'=>'testing', - // 'postcode'=>'testing', - // 'state'=>'testing', - // 'country'=>'testing', - // 'contact_person'=>'testing', - // ]); - // var_dump($response); - // $response->assertStatus(302); - // } + public function testPOST() + { + $response = $this->json('POST', '/api/company', [ + 'company_name'=>'testing', + 'registration_no'=>'testing', + 'tax_no'=>'testing', + 'tel_no'=>'12345', + 'fax'=>'12345', + 'address'=>'testing', + 'city'=>'testing', + 'postcode'=>'testing', + 'state'=>'testing', + 'country'=>'testing', + 'contact_person'=>'testing', + ]); + var_dump($response); + $response->assertStatus(302); //here 302 as the page redirects + } + + public function testPUT() + { + $response = $this->json('PUT', '/api/company/9', [ + 'company_name'=>'changed testing', + 'registration_no'=>'testing', + 'tax_no'=>'testing', + 'tel_no'=>'12345', + 'fax'=>'12345', + 'address'=>'testing', + 'city'=>'testing', + 'postcode'=>'testing', + 'state'=>'testing', + 'country'=>'testing', + 'contact_person'=>'testing', + ]); + $response->assertStatus(200); + } - // public function testPUT() - // { - // $response = $this->json('PUT', '/api/company/4', [ - // 'company_name'=>'changed testing', - // 'registration_no'=>'testing', - // 'tax_no'=>'testing', - // 'tel_no'=>'12345', - // 'fax'=>'12345', - // 'address'=>'testing', - // 'city'=>'testing', - // 'postcode'=>'testing', - // 'state'=>'testing', - // 'country'=>'testing', - // 'contact_person'=>'testing', - // ]); - // $response->assertStatus(200); - // } public function testcompanyProfile() { Storage::fake('company_profile'); - $response = $this->json('POST', '/company/company_profile/4', [ + $response = $this->json('POST', '/api/company/company_profile/9', [ 'company_profile' => UploadedFile::fake()->image('comp.jpg') ]); - var_dump($response); + dd($response->getContent()); + $response->assertStatus(200); + } + + public function testregCert() + { + Storage::fake('reg_cert'); + + $response = $this->json('POST', '/api/company/reg_cert/9', [ + 'reg_cert' => UploadedFile::fake()->image('reg.pdf') + ]); + dd($response->getContent()); $response->assertStatus(200); } }