From 651e72da6b2b3b78059d6360d1994a6dc462d6a1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 2 Jul 2021 10:06:26 +1000 Subject: [PATCH] Lean Mock Data Trait --- tests/MockUnitData.php | 71 ++++++++++++++++++++++++++++++++ tests/Unit/CreditBalanceTest.php | 38 ++++++----------- 2 files changed, 84 insertions(+), 25 deletions(-) create mode 100644 tests/MockUnitData.php diff --git a/tests/MockUnitData.php b/tests/MockUnitData.php new file mode 100644 index 0000000000..c0baea083e --- /dev/null +++ b/tests/MockUnitData.php @@ -0,0 +1,71 @@ +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, + ]); + + } +} \ No newline at end of file diff --git a/tests/Unit/CreditBalanceTest.php b/tests/Unit/CreditBalanceTest.php index d8fb81761e..263409b4a8 100644 --- a/tests/Unit/CreditBalanceTest.php +++ b/tests/Unit/CreditBalanceTest.php @@ -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); } }