Skip to content

Commit

Permalink
Complete Unit Tests for all models and Fixes to prevent less collisio…
Browse files Browse the repository at this point in the history
…ns with unique emails
  • Loading branch information
zanechua committed Nov 12, 2018
1 parent 7c7f31a commit 3e3cc8c
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 13 deletions.
5 changes: 5 additions & 0 deletions app/Models/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ public function invoices()
return $this->hasMany('App\Models\Invoice', 'company_id');
}

public function events()
{
return $this->hasMany('App\Models\InvoiceEvent', 'company_id');
}

public function itemtemplates()
{
return $this->hasMany('App\Models\ItemTemplate', 'company_id');
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected static function boot()

static::saving(function ($invoice) {
$date = clone $invoice->date;
$invoice->duedate = $date->addDays($invoice->netdays)->timezone(config('app.timezone'))->startOfDay();
$invoice->duedate = $date->timezone(config('app.timezone'))->startOfDay()->addDays($invoice->netdays);
});

//Auto Increment of invoice_index per Company;
Expand Down
7 changes: 6 additions & 1 deletion app/Models/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,19 @@ public function getUpdatedAtAttribute($value)
public function getReceiveddateAttribute($value)
{
$date = $this->asDateTime($value);
return $date->timezone(auth()->user()->timezone);
return $date->timezone($this->company->timezone);
}

public function invoice()
{
return $this->belongsTo('App\Models\Invoice', 'invoice_id');
}

public function company()
{
return $this->belongsTo('App\Models\Company', 'company_id');
}

public function client()
{
return $this->belongsTo('App\Models\Client', 'client_id');
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ protected static function boot()
parent::boot();

static::saving(function ($quote) {
$date = $quote->date->addDays($quote->netdays);
$quote->duedate = $date->timezone(config('app.timezone'))->startOfDay();
$date = clone $quote->date;
$quote->duedate = $date->timezone(config('app.timezone'))->startOfDay()->addDays($quote->netdays);
});

//Auto Increment of quote_index per Company;
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ClientTest extends TestCase

public function test_create_client()
{
self::refreshDatabase();
$company = factory(Company::class)->create();

Client::unguard();
Expand Down
71 changes: 69 additions & 2 deletions tests/Unit/CompanyUserRequestTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Tests\Unit;

use App\Models\Company;
use App\Models\CompanyUserRequest;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
Expand All @@ -13,8 +15,73 @@ class CompanyUserRequestTest extends TestCase
*
* @return void
*/
public function testExample()
public function test_create_company_user_request()
{
$this->assertTrue(true);
$company = factory(Company::class)->create();

CompanyUserRequest::unguard();

$companyUserRequest = CompanyUserRequest::create([
'full_name' => 'Poowf Bunny',
'email' => 'bunny@poowf.com',
'phone' => '+6579328669',
'token' => 'asdfounasifdnasfinasasdfasf',
'status' => '1',
'company_id' => $company->id
]);

CompanyUserRequest::reguard();

$this->assertEquals($companyUserRequest->company->name, $company->name);
$this->assertEquals('bunny@poowf.com', $companyUserRequest->email);
}

public function test_update_company_user_request()
{
$company = factory(Company::class)->create();
$companyUserRequest = factory(CompanyUserRequest::class)->create([
'company_id' => $company->id
]);

$this->assertInstanceOf(CompanyUserRequest::class, $companyUserRequest);

$companyUserRequest->full_name = 'tehsecretname';
$companyUserRequest->token = 'bargabarbararba';
$companyUserRequest->save();
$companyUserRequest->refresh();

$this->assertEquals('tehsecretname', $companyUserRequest->full_name);
$this->assertEquals('bargabarbararba', $companyUserRequest->token);

$data = [
'full_name' => 'NyanIndustries',
'email' => 'nowaythiscannotbe@example.com',
'phone' => '+659774123',
'token' => 'asdfnasuifasuifnasidfas',
];

$companyUserRequest->fill($data);
$companyUserRequest->save();
$companyUserRequest->refresh();

$this->assertEquals('NyanIndustries', $companyUserRequest->full_name);
$this->assertEquals('nowaythiscannotbe@example.com', $companyUserRequest->email);
$this->assertEquals('+659774123', $companyUserRequest->phone);
$this->assertNotEquals('asdfnasuifasuifnasidfas', $companyUserRequest->token);
}


public function test_delete_company_user_request()
{
$company = factory(Company::class)->create();
$companyUserRequest = factory(CompanyUserRequest::class)->create([
'company_id' => $company->id
]);

$this->assertInstanceOf(CompanyUserRequest::class, $companyUserRequest);
$companyUserRequest = $companyUserRequest->delete();

$this->assertEquals('true', json_encode($companyUserRequest));
$this->assertEmpty($company->requests);
}
}
71 changes: 69 additions & 2 deletions tests/Unit/InvoiceEventTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Tests\Unit;

use App\Models\Company;
use App\Models\InvoiceEvent;
use Illuminate\Database\Eloquent\MassAssignmentException;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
Expand All @@ -13,8 +16,72 @@ class InvoiceEventTest extends TestCase
*
* @return void
*/
public function testExample()
public function test_create_invoice_event()
{
$this->assertTrue(true);
$company = factory(Company::class)->create();

InvoiceEvent::unguard();

$invoiceEvent = InvoiceEvent::create([
'time_interval' => '1',
'time_period' => 'month',
'until_type' => 'never',
'until_meta' => null,
'rule' => 'FREQ=MONTHLY;INTERVAL=1',
'company_id' => $company->id
]);

InvoiceEvent::reguard();

$this->assertEquals($invoiceEvent->company->name, $company->name);
$this->assertEquals('FREQ=MONTHLY;INTERVAL=1', $invoiceEvent->rule);
}

public function test_update_invoice_event()
{
$invoiceEvent = factory(InvoiceEvent::class)->create();

$this->assertInstanceOf(InvoiceEvent::class, $invoiceEvent);

$invoiceEvent->time_period = 'week';
$invoiceEvent->until_type = 'occurence';
$invoiceEvent->save();
$invoiceEvent->refresh();

$this->assertEquals('week', $invoiceEvent->time_period);
$this->assertEquals('occurence', $invoiceEvent->until_type);

$data = [
'time_interval' => '3',
'time_period' => 'year',
'until_type' => 'date',
'until_meta' => '2020-10-31 00:00:00',
];

$this->expectException(MassAssignmentException::class);

$invoiceEvent->fill($data);
$invoiceEvent->save();
$invoiceEvent->refresh();

$this->assertNotEquals('3', $invoiceEvent->time_interval);
$this->assertNotEquals('year@example.com', $invoiceEvent->time_period);
$this->assertNotEquals('date', $invoiceEvent->until_type);
$this->assertNotEquals('2020-10-31 00:00:00', $invoiceEvent->until_meta);
}


public function test_delete_invoice_event()
{
$company = factory(Company::class)->create();
$invoiceEvent = factory(InvoiceEvent::class)->create([
'company_id' => $company->id
]);

$this->assertInstanceOf(InvoiceEvent::class, $invoiceEvent);
$invoiceEvent = $invoiceEvent->delete();

$this->assertEquals('true', json_encode($invoiceEvent));
$this->assertEmpty($company->events);
}
}
10 changes: 8 additions & 2 deletions tests/Unit/InvoiceTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Unit;

use App\Models\Client;
use App\Models\Company;
use App\Models\InvoiceEvent;
use App\Models\InvoiceTemplate;
use Tests\TestCase;
Expand All @@ -18,8 +19,13 @@ class InvoiceTemplateTest extends TestCase
*/
public function test_create_invoice_template()
{
$client = factory(Client::class)->create();
$invoiceEvent = factory(InvoiceEvent::class)->create();
$company = factory(Company::class)->create();
$client = factory(Client::class)->create([
'company_id' => $company->id
]);
$invoiceEvent = factory(InvoiceEvent::class)->create([
'company_id' => $company->id
]);

InvoiceTemplate::unguard();

Expand Down
89 changes: 87 additions & 2 deletions tests/Unit/PaymentTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Tests\Unit;

use App\Models\Client;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\Payment;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
Expand All @@ -13,8 +17,89 @@ class PaymentTest extends TestCase
*
* @return void
*/
public function testExample()
public function test_create_payment()
{
$this->assertTrue(true);
$company = factory(Company::class)->create();
$invoice = factory(Invoice::class)->create([
'company_id' => $company->id
]);
$client = factory(Client::class)->create([
'company_id' => $company->id
]);
Payment::unguard();

$payment = Payment::create([
'amount' => '516556.52',
'receiveddate' => '2018-11-01 00:00:00',
'invoice_id' => $invoice->id,
'client_id' => $client->id,
'company_id' => $company->id
]);

Payment::reguard();

$this->assertEquals($payment->invoice->id, $invoice->id);
$this->assertEquals('516556.52', $payment->amount);
}

public function test_update_payment()
{
$company = factory(Company::class)->create();
$invoice = factory(Invoice::class)->create([
'company_id' => $company->id
]);
$client = factory(Client::class)->create([
'company_id' => $company->id
]);
$payment = factory(Payment::class)->create([
'invoice_id' => $invoice->id,
'client_id' => $client->id,
'company_id' => $company->id
]);
$this->assertInstanceOf(Payment::class, $payment);

$payment->amount = "1891818.41";
$payment->notes = "asdfasfasfasdfasfsfaffsa";
$payment->save();
$payment->refresh();

$this->assertEquals('1891818.41', $payment->amount);
$this->assertEquals('asdfasfasfasdfasfsfaffsa', $payment->notes);

$data = [
'amount' => '12341451541.00',
'notes' => 'sdfasfasdvcasd asodcnaio9sjecoamfoe[casmo;cnasi;cndik; andio;asno;asdcnio; asdnio;asdcno;asdncio;asdn;',
'receiveddate' => '2018-12-01 00:00:00'
];

$payment->fill($data);
$payment->save();
$payment->refresh();

$this->assertEquals('12341451541.00', $payment->amount);
$this->assertEquals('sdfasfasdvcasd asodcnaio9sjecoamfoe[casmo;cnasi;cndik; andio;asno;asdcnio; asdnio;asdcno;asdncio;asdn;', $payment->notes);
$this->assertNotEquals('2018-12-01 00:00:00', $payment->receiveddate->format('Y-m-d H:i:s'));
}

public function test_delete_payment()
{
$company = factory(Company::class)->create();
$invoice = factory(Invoice::class)->create([
'company_id' => $company->id
]);
$client = factory(Client::class)->create([
'company_id' => $company->id
]);
$payment = factory(Payment::class)->create([
'invoice_id' => $invoice->id,
'client_id' => $client->id,
'company_id' => $company->id
]);

$this->assertInstanceOf(Payment::class, $payment);
$payment = $payment->delete();

$this->assertEquals('true', json_encode($payment));
$this->assertEmpty($invoice->payments);
}
}
1 change: 0 additions & 1 deletion tests/Unit/QuoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class QuoteTest extends TestCase
public function test_create_quote()
{
$client = factory(Client::class)->create();

Quote::unguard();

$quote = Quote::create([
Expand Down

0 comments on commit 3e3cc8c

Please sign in to comment.