From d9324697c05056ed292edd7059ebbf18bf94d1c5 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 2 May 2019 21:24:00 +1000 Subject: [PATCH] Quote Tests --- app/Models/Client.php | 4 +- app/Models/Quote.php | 12 +- app/Providers/RouteServiceProvider.php | 4 + tests/Feature/QuoteTest.php | 202 +++++++++++++++++++++++++ 4 files changed, 217 insertions(+), 5 deletions(-) create mode 100644 tests/Feature/QuoteTest.php diff --git a/app/Models/Client.php b/app/Models/Client.php index 08fc961493..dcfbd1754e 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -133,13 +133,13 @@ class Client extends BaseModel { switch ($entity) { case Client::class: - Log::error('saving client settings'); + // Log::error('saving client settings'); $this->settings = $settings; $this->save(); $this->fresh(); break; case Company::class: - Log::error('saving company settings'); + // Log::error('saving company settings'); $this->company->settings = $settings; $this->company->save(); $this->company->fresh(); diff --git a/app/Models/Quote.php b/app/Models/Quote.php index 337f6fb551..c6ebabd97d 100644 --- a/app/Models/Quote.php +++ b/app/Models/Quote.php @@ -2,21 +2,27 @@ namespace App\Models; +use App\Models\Filterable; use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\Model; class Quote extends BaseModel { use MakesHash; - + use Filterable; + protected $guarded = [ 'id', ]; + protected $casts = [ + 'settings' => 'object' + ]; + const STATUS_DRAFT = 1; const STATUS_SENT = 2; - const STATUS_APPROVED = 4; - const STATUS_OVERDUE = -1; + const STATUS_APPROVED = 3; + const STATUS_EXPIRED = -1; public function company() { diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index f5bb53a2f0..7547c292e6 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -42,6 +42,10 @@ class RouteServiceProvider extends ServiceProvider return \App\Models\Invoice::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); }); + Route::bind('quote', function ($value) { + return \App\Models\Quote::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); + }); + Route::bind('payment', function ($value) { return \App\Models\Payment::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); }); diff --git a/tests/Feature/QuoteTest.php b/tests/Feature/QuoteTest.php new file mode 100644 index 0000000000..834dd56e01 --- /dev/null +++ b/tests/Feature/QuoteTest.php @@ -0,0 +1,202 @@ +faker = \Faker\Factory::create(); + + Model::reguard(); + + + } + + public function testQuoteList() + { + $data = [ + 'first_name' => $this->faker->firstName, + 'last_name' => $this->faker->lastName, + 'email' => $this->faker->unique()->safeEmail, + 'password' => 'ALongAndBrilliantPassword123', + '_token' => csrf_token(), + 'privacy_policy' => 1, + 'terms_of_service' => 1 + ]; + + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + ])->post('/api/v1/signup', $data); + + $acc = $response->json(); + + $account = Account::find($this->decodePrimaryKey($acc['data']['id'])); + + $company_token = $account->default_company->tokens()->first(); + $token = $company_token->token; + $company = $company_token->company; + + $user = $company_token->user; + + $this->assertNotNull($company_token); + $this->assertNotNull($token); + $this->assertNotNull($user); + $this->assertNotNull($company); + $this->assertNotNull($user->tokens->first()->company); + + factory(\App\Models\Client::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company){ + + factory(\App\Models\ClientContact::class,1)->create([ + 'user_id' => $user->id, + 'client_id' => $c->id, + 'company_id' => $company->id, + 'is_primary' => 1 + ]); + + factory(\App\Models\ClientContact::class,1)->create([ + 'user_id' => $user->id, + 'client_id' => $c->id, + 'company_id' => $company->id + ]); + + }); + $client = Client::all()->first(); + + factory(\App\Models\Quote::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); + + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $token, + ])->get('/api/v1/quotes'); + + $response->assertStatus(200); + + } + + public function testQuoteRESTEndPoints() + { + $data = [ + 'first_name' => $this->faker->firstName, + 'last_name' => $this->faker->lastName, + 'email' => $this->faker->unique()->safeEmail, + 'password' => 'ALongAndBrilliantPassword123', + '_token' => csrf_token(), + 'privacy_policy' => 1, + 'terms_of_service' => 1 + ]; + + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + ])->post('/api/v1/signup', $data); + + $acc = $response->json(); + + $account = Account::find($this->decodePrimaryKey($acc['data']['id'])); + + $company_token = $account->default_company->tokens()->first(); + $token = $company_token->token; + $company = $company_token->company; + + $user = $company_token->user; + + $this->assertNotNull($company_token); + $this->assertNotNull($token); + $this->assertNotNull($user); + $this->assertNotNull($company); + $this->assertNotNull($user->tokens->first()->company); + + factory(\App\Models\Client::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company){ + + factory(\App\Models\ClientContact::class,1)->create([ + 'user_id' => $user->id, + 'client_id' => $c->id, + 'company_id' => $company->id, + 'is_primary' => 1 + ]); + + factory(\App\Models\ClientContact::class,1)->create([ + 'user_id' => $user->id, + 'client_id' => $c->id, + 'company_id' => $company->id + ]); + + }); + $client = Client::all()->first(); + + factory(\App\Models\Quote::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); + + $quote = Quote::where('user_id',$user->id)->first(); + $quote->settings = $client->getMergedSettings(); + $quote->save(); + Log::error(print_r($quote,1)); + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $token, + ])->get('/api/v1/quotes/'.$this->encodePrimaryKey($quote->id)); + + $response->assertStatus(200); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $token, + ])->get('/api/v1/quotes/'.$this->encodePrimaryKey($quote->id).'/edit'); + + $response->assertStatus(200); + + $quote_update = [ + 'status_id' => Quote::STATUS_APPROVED + ]; + + $this->assertNotNull($quote); + $this->assertNotNull($quote->settings); + + $this->assertTrue(property_exists($quote->settings, 'custom_taxes1')); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $token, + ])->put('/api/v1/quotes/'.$this->encodePrimaryKey($quote->id), $quote_update) + ->assertStatus(200); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $token, + ])->delete('/api/v1/quotes/'.$this->encodePrimaryKey($quote->id)); + + $response->assertStatus(200); + + } + +}