diff --git a/tests/Feature/Payments/CreditPaymentTest.php b/tests/Feature/Payments/CreditPaymentTest.php new file mode 100644 index 000000000000..a325d5618d89 --- /dev/null +++ b/tests/Feature/Payments/CreditPaymentTest.php @@ -0,0 +1,220 @@ +faker = \Faker\Factory::create(); + + Model::reguard(); + + $this->makeTestData(); + $this->withoutExceptionHandling(); + + $this->withoutMiddleware( + ThrottleRequests::class + ); + } + + public function testCreditPayments() + { + + $invoice = Invoice::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id, 'client_id' => $this->client->id]); + + $invoice->line_items = $this->buildLineItems(); + $invoice->uses_inclusive_taxes = false; + + // $invoice->save(); + $invoice_calc = new InvoiceSum($invoice); + $invoice_calc->build(); + $invoice = $invoice_calc->getInvoice(); + $invoice->setRelation('client', $this->client); + $invoice->setRelation('company', $this->company); + $invoice->save(); + + $credit = Credit::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id, 'client_id' => $this->client->id]); + + $credit->line_items = $this->buildLineItems(); + $credit->uses_inclusive_taxes = false; + + // $invoice->save(); + $invoice_calc = new InvoiceSum($credit); + $invoice_calc->build(); + $credit = $invoice_calc->getCredit(); + $credit->setRelation('client', $this->client); + $credit->setRelation('company', $this->company); + $credit->save(); + + + $data = [ + 'amount' => 0, + 'client_id' => $this->client->hashed_id, + 'invoices' => [ + [ + 'invoice_id' => $invoice->hashed_id, + 'amount' => 10, + ], + ], + 'credits' => [ + ], + 'date' => '2019/12/12', + ]; + + $response = false; + + try { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/payments/', $data); + } catch (ValidationException $e) { + $message = json_decode($e->validator->getMessageBag(), 1); + \Log::error(print_r($e->validator->getMessageBag(), 1)); + } + + // $response->assertStatus(200); + + // $arr = $response->json(); + + // $payment_id = $arr['data']['id']; + + // $payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first(); + + // $this->assertEquals($payment->amount, 15); + // $this->assertEquals($payment->applied, 10); + + + + + } + + /* + + public function testDoublePaymentTestWithInvalidAmounts() + { + + $data = [ + 'amount' => 15.0, + 'client_id' => $this->encodePrimaryKey($client->id), + 'invoices' => [ + [ + 'invoice_id' => $this->encodePrimaryKey($this->invoice->id), + 'amount' => 10, + ], + ], + 'date' => '2019/12/12', + ]; + + $response = false; + + try { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/payments/', $data); + } catch (ValidationException $e) { + $message = json_decode($e->validator->getMessageBag(), 1); + \Log::error(print_r($e->validator->getMessageBag(), 1)); + } + + $response->assertStatus(200); + + $arr = $response->json(); + + $payment_id = $arr['data']['id']; + + $payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first(); + + $this->assertEquals($payment->amount, 15); + $this->assertEquals($payment->applied, 10); + + $this->invoice = null; + $this->invoice = InvoiceFactory::create($this->company->id, $this->user->id); //stub the company and user_id + $this->invoice->client_id = $client->id; + + $this->invoice->line_items = $this->buildLineItems(); + $this->invoice->uses_inclusive_taxes = false; + + $this->invoice->save(); + + $this->invoice_calc = new InvoiceSum($this->invoice); + $this->invoice_calc->build(); + + $this->invoice = $this->invoice_calc->getInvoice(); + $this->invoice->save(); + $this->invoice->service()->markSent()->save(); + + $data = [ + 'amount' => 15.0, + 'client_id' => $this->encodePrimaryKey($client->id), + 'invoices' => [ + [ + 'invoice_id' => $this->encodePrimaryKey($this->invoice->id), + 'amount' => 10, + ], + ], + 'date' => '2019/12/12', + ]; + + $response = false; + + try { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->put('/api/v1/payments/'.$this->encodePrimaryKey($payment->id), $data); + } catch (ValidationException $e) { + $message = json_decode($e->validator->getMessageBag(), 1); + + $this->assertTrue(array_key_exists('invoices', $message)); + } + } + */ + +} + diff --git a/tests/MockUnitData.php b/tests/MockUnitData.php index c0baea083e25..cb00239f3c78 100644 --- a/tests/MockUnitData.php +++ b/tests/MockUnitData.php @@ -11,10 +11,14 @@ namespace Tests; +use App\DataMapper\CompanySettings; +use App\DataMapper\DefaultSettings; +use App\Factory\InvoiceItemFactory; use App\Models\Account; use App\Models\Client; use App\Models\ClientContact; use App\Models\Company; +use App\Models\CompanyToken; use App\Models\User; /** * Class MockUnitData. @@ -33,6 +37,8 @@ trait MockUnitData public $primary_contact; + public $token; + public function makeTestData() { @@ -49,6 +55,39 @@ trait MockUnitData 'account_id' => $this->account->id ]); + $userPermissions = collect([ + 'view_invoice', + 'view_client', + 'edit_client', + 'edit_invoice', + 'create_invoice', + 'create_client', + ]); + + $userSettings = DefaultSettings::userSettings(); + + $this->user->companies()->attach($this->company->id, [ + 'account_id' => $this->account->id, + 'is_owner' => 1, + 'is_admin' => 1, + 'notifications' => CompanySettings::notificationDefaults(), + 'permissions' => $userPermissions->toJson(), + 'settings' => json_encode($userSettings), + 'is_locked' => 0, + ]); + + + $this->token = \Illuminate\Support\Str::random(64); + + $company_token = new CompanyToken; + $company_token->user_id = $this->user->id; + $company_token->company_id = $this->company->id; + $company_token->account_id = $this->account->id; + $company_token->name = 'test token'; + $company_token->token = $this->token; + $company_token->is_system = true; + $company_token->save(); + $this->client = Client::factory()->create([ 'user_id' => $this->user->id, 'company_id' => $this->company->id @@ -68,4 +107,17 @@ trait MockUnitData ]); } + + + public function buildLineItems() + { + $line_items = []; + + $item = InvoiceItemFactory::create(); + $item->quantity = 1; + $item->cost = 10; + $line_items[] = $item; + + return $line_items; + } } \ No newline at end of file