diff --git a/Dockerfile b/Dockerfile index 32df4f4..1db0872 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,8 @@ FROM php:7 -RUN apt-get update -y && apt-get install -y openssl zip unzip git netcat +RUN apt-get update -y && apt-get install -y openssl zip unzip git netcat libpng-dev RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN docker-php-ext-install pdo pdo_mysql +RUN docker-php-ext-install gd WORKDIR /app COPY . /app RUN composer update diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 2707ce1..25c9a28 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -145,6 +145,12 @@ class AuthController extends Controller // attach role to user $user->attachRole($role); $user->update(['name' => $name, 'password' => Hash::make($password)]); + + // TODO : create company for user + $company = new Company; + $company->user = $user; + $company->save(); + return response()->json(['success'=> true, 'message'=> 'Profile updated.']); } diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php index aba4fdc..e95914b 100644 --- a/app/Http/Controllers/CompanyController.php +++ b/app/Http/Controllers/CompanyController.php @@ -6,23 +6,16 @@ use Illuminate\Http\Request; use App\Company; use Illuminate\Support\Facades\Storage; use Validator; +use Auth; class CompanyController extends Controller { - public function store(Request $request) + public function update(Request $request) { - $input = $request->all(); - Company::create($input); - return response()->json(['input'=>$input],201); - //return redirect('/'); - } - - public function update(Request $request, $id) - { - $company = Company::find($id); + $company = Company::where("user_id", Auth::user()->id)->first(); if (!$company) { - return response()->json(['message'=>'company not found'],200); + return response()->json(['message'=>'company not found'], 404); } $company->company_name=$request->input('company_name'); @@ -41,9 +34,9 @@ class CompanyController extends Controller return response()->json(['company'=>$company],200); } - public function companyProfile(Request $request, $id) + public function companyProfile(Request $request) { - $company = Company::find($id);//find the company first + $company = Company::where("user_id", Auth::user()->id)->first(); if (!$company) { return response()->json(['message'=>'company for company_prof not found'],200); @@ -75,12 +68,12 @@ class CompanyController extends Controller return response()->json(['company'=>$company],200); }//end of companyProfile() - public function regCert(Request $request, $id) + public function regCert(Request $request) { - $company = Company::find($id); //find the company first + $company = Company::where("user_id", Auth::user()->id)->first(); if (!$company) { - return response()->json(['message'=>'company for reg_cert not found'],200); + return response()->json(['message'=>'Company not found'], 404); } //validating file types diff --git a/database/factories/CompanyFactory.php b/database/factories/CompanyFactory.php new file mode 100644 index 0000000..ccbfa28 --- /dev/null +++ b/database/factories/CompanyFactory.php @@ -0,0 +1,30 @@ +define(App\Company::class, function (Faker $faker) { + return [ + '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' + ]; +}); diff --git a/database/migrations/2018_05_26_014004_add_user_to_company_table.php b/database/migrations/2018_05_26_014004_add_user_to_company_table.php new file mode 100644 index 0000000..39ecc01 --- /dev/null +++ b/database/migrations/2018_05_26_014004_add_user_to_company_table.php @@ -0,0 +1,31 @@ +unsignedInteger('user_id'); + $table->foreign('user_id')->references('id')->on('users'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/routes/api.php b/routes/api.php index bffebdb..1beaaec 100644 --- a/routes/api.php +++ b/routes/api.php @@ -26,10 +26,10 @@ Route::group(['middleware' => ['jwt.auth']], function() { Route::get('test', function(Request $request){ return response()->json(['user'=> $request->user()]); }); + + /* Company */ + Route::patch('/company', 'CompanyController@update'); //update company information + Route::post('/company/company_profile/', 'CompanyController@companyProfile');//upload company profile picture + Route::post('/company/reg_cert/', 'CompanyController@regCert'); //upload registration certificaate }); -/* Company */ -Route::post('/company', 'CompanyController@store'); //store company information -Route::put('/company/{company}', 'CompanyController@update'); //update company information -Route::post('/company/company_profile/{companyID}', 'CompanyController@companyProfile');//upload company profile picture -Route::post('/company/reg_cert/{companyID}', 'CompanyController@regCert'); //upload registration certificaate \ No newline at end of file diff --git a/tests/Unit/CompanyTest.php b/tests/Feature/CompanyTest.php similarity index 50% rename from tests/Unit/CompanyTest.php rename to tests/Feature/CompanyTest.php index 89f4249..1a4a7fc 100644 --- a/tests/Unit/CompanyTest.php +++ b/tests/Feature/CompanyTest.php @@ -7,40 +7,31 @@ use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; +use Artisan; +use App\User; +use App\Company; class CompanyTest extends TestCase { - /** - * A basic test example. - * - * @return void - */ - public function testExample() + protected $company; + protected $user; + + public function setUp() { - $this->assertTrue(true); + parent::setUp(); + Artisan::call('db:seed'); + $this->user = factory(User::class)->create(); + $this->company = $this->user->each(function ($u) { + factory(Company::class)->create([ + 'user_id' => $u->id, + ]); + }); } - public function testPOST() + public function testUpdateCompany() { - $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', - ]); - $response->assertStatus(201); - } - - public function testPUT() - { - $response = $this->json('PUT', '/api/company/2', [ + $response = $this->actingAs($this->user) + ->patchJson('/api/company', [ 'company_name'=>'changed testing', 'registration_no'=>'testing', 'tax_no'=>'testing', @@ -52,29 +43,32 @@ class CompanyTest extends TestCase 'state'=>'testing', 'country'=>'testing', 'contact_person'=>'testing', + ]); $response->assertStatus(200); + } - public function testcompanyProfile() - { + public function testUploadCompanyProfile() + { Storage::fake('company_profile'); - - $response = $this->json('POST', '/api/company/company_profile/2', [ + + $response = $this->actingAs($this->user) + ->json('POST', '/api/company/company_profile/', [ 'company_profile' => UploadedFile::fake()->image('comp.jpg') ]); - dd($response->getContent()); + // dd($response->getContent()); $response->assertStatus(200); } - public function testregCert() + public function testUploadRegCert() { Storage::fake('reg_cert'); - $response = $this->json('POST', '/api/company/reg_cert/2', [ + $response = $this->actingAs($this->user) + ->json('POST', '/api/company/reg_cert/', [ 'reg_cert' => UploadedFile::fake()->create('document.pdf')//, $sizeInKilobytes) ]); - dd($response->getContent()); $response->assertStatus(200); } } diff --git a/tests/Feature/LoginTest.php b/tests/Feature/LoginTest.php index 34e9cce..7d0cce8 100644 --- a/tests/Feature/LoginTest.php +++ b/tests/Feature/LoginTest.php @@ -13,7 +13,7 @@ class LoginTest extends TestCase * * @return void */ - public function testExample() + public function testLogin() { $response = $this->json('POST', '/api/login', ['phone' => '0123456789','password' => 'secret']); $response diff --git a/tests/Feature/RegisterTest.php b/tests/Feature/RegisterTest.php index f0e600b..8d47119 100644 --- a/tests/Feature/RegisterTest.php +++ b/tests/Feature/RegisterTest.php @@ -26,7 +26,7 @@ class RegisterTest extends TestCase 'success' => true, ]); - $response = $this->json('POST', '/api/verify', ['phone' => '0123456789', 'token' => '1234']); + $response = $this->json('POST', '/api/verify-phone', ['phone' => '0123456789', 'token' => '1234']); $response ->assertStatus(200) ->assertJson([ diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/ExampleTest.php deleted file mode 100644 index e9fe19c..0000000 --- a/tests/Unit/ExampleTest.php +++ /dev/null @@ -1,19 +0,0 @@ -assertTrue(true); - } -}