1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Lean Mock Data Trait

This commit is contained in:
David Bomba 2021-07-02 10:06:26 +10:00
parent 84dd24c070
commit 651e72da6b
2 changed files with 84 additions and 25 deletions

71
tests/MockUnitData.php Normal file
View File

@ -0,0 +1,71 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace Tests;
use App\Models\Account;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Company;
use App\Models\User;
/**
* Class MockUnitData.
*/
trait MockUnitData
{
public $account;
public $company;
public $user;
public $client;
public $faker;
public $primary_contact;
public function makeTestData()
{
$this->faker = \Faker\Factory::create();
$this->account = Account::factory()->create();
$this->user = User::factory()->create([
'account_id' => $this->account->id,
'email' => $this->faker->safeEmail
]);
$this->company = Company::factory()->create([
'account_id' => $this->account->id
]);
$this->client = Client::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id
]);
$this->primary_contact = ClientContact::factory()->create([
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'company_id' => $this->company->id,
'is_primary' => 1,
]);
ClientContact::factory()->count(2)->create([
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'company_id' => $this->company->id,
]);
}
}

View File

@ -15,6 +15,7 @@ use App\Models\Client;
use App\Models\Company;
use App\Models\Credit;
use App\Models\User;
use Tests\MockUnitData;
use Tests\TestCase;
/**
@ -22,58 +23,45 @@ use Tests\TestCase;
*/
class CreditBalanceTest extends TestCase
{
use MockUnitData;
public function setUp() :void
{
parent::setUp();
$this->faker = \Faker\Factory::create();
$this->makeTestData();
}
public function testCreditBalance()
{
$account = Account::factory()->create();
$user = User::factory()->create(
['account_id' => $account->id, 'email' => $this->faker->safeEmail]
);
$company = Company::factory()->create(['account_id' => $account->id]);
$client = Client::factory()->create(['company_id' => $company->id, 'user_id' => $user->id]);
$credit = Credit::factory()->create([
'user_id' => $user->id,
'company_id' => $company->id,
'client_id' => $client->id,
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'client_id' => $this->client->id,
'balance' => 10,
'number' => 'testing-number-01',
'status_id' => Credit::STATUS_SENT,
]);
$this->assertEquals($client->service()->getCreditBalance(), 10);
$this->assertEquals($this->client->service()->getCreditBalance(), 10);
}
public function testExpiredCreditBalance()
{
$account = Account::factory()->create();
$user = User::factory()->create(
['account_id' => $account->id, 'email' => $this->faker->safeEmail]
);
$company = Company::factory()->create(['account_id' => $account->id]);
$client = Client::factory()->create(['company_id' => $company->id, 'user_id' => $user->id]);
$credit = Credit::factory()->create([
'user_id' => $user->id,
'company_id' => $company->id,
'client_id' => $client->id,
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'client_id' => $this->client->id,
'balance' => 10,
'due_date' => now()->addDays(5),
'number' => 'testing-number-02',
'status_id' => Credit::STATUS_SENT,
]);
$this->assertEquals($client->service()->getCreditBalance(), 0);
$this->assertEquals($this->client->service()->getCreditBalance(), 0);
}
}