2019-05-27 12:48:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit;
|
|
|
|
|
|
|
|
use App\DataMapper\DefaultSettings;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Credit;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\RecurringInvoice;
|
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2019-05-27 13:54:27 +02:00
|
|
|
use App\Utils\Traits\GeneratesNumberCounter;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-05-27 12:48:52 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2019-05-27 13:54:27 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-05-27 12:48:52 +02:00
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Utils\Traits\GeneratesCounter
|
|
|
|
*/
|
|
|
|
class GeneratesCounterTest extends TestCase
|
|
|
|
{
|
2019-05-27 13:54:27 +02:00
|
|
|
use GeneratesCounter;
|
2019-05-27 12:48:52 +02:00
|
|
|
use DatabaseTransactions;
|
2019-05-27 13:54:27 +02:00
|
|
|
use MakesHash;
|
|
|
|
//use MockAccountData;
|
2019-05-27 12:48:52 +02:00
|
|
|
|
|
|
|
public function setUp() :void
|
|
|
|
{
|
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
|
2019-05-27 13:54:27 +02:00
|
|
|
Session::start();
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
Model::reguard();
|
|
|
|
$account = factory(\App\Models\Account::class)->create();
|
|
|
|
$company = factory(\App\Models\Company::class)->create([
|
|
|
|
'account_id' => $account->id,
|
|
|
|
]);
|
|
|
|
$account->default_company_id = $company->id;
|
|
|
|
$account->save();
|
|
|
|
$user = factory(\App\Models\User::class)->create([
|
|
|
|
// 'account_id' => $account->id,
|
|
|
|
'confirmation_code' => $this->createDbHash(config('database.default'))
|
|
|
|
]);
|
|
|
|
$userPermissions = collect([
|
|
|
|
'view_invoice',
|
|
|
|
'view_client',
|
|
|
|
'edit_client',
|
|
|
|
'edit_invoice',
|
|
|
|
'create_invoice',
|
|
|
|
'create_client'
|
|
|
|
]);
|
|
|
|
$userSettings = DefaultSettings::userSettings();
|
|
|
|
$user->companies()->attach($company->id, [
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'is_owner' => 1,
|
|
|
|
'is_admin' => 1,
|
|
|
|
'permissions' => $userPermissions->toJson(),
|
|
|
|
'settings' => json_encode($userSettings),
|
|
|
|
'is_locked' => 0,
|
|
|
|
]);
|
|
|
|
factory(\App\Models\Client::class)->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,2)->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'client_id' => $c->id,
|
|
|
|
'company_id' => $company->id
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
$this->client = Client::whereUserId($user->id)->whereCompanyId($company->id)->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testHasSharedCounter()
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->hasSharedCounter($this->client));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvoiceNumberValue()
|
|
|
|
{
|
|
|
|
|
|
|
|
$invoice_number = $this->getNextInvoiceNumber($this->client);
|
|
|
|
|
|
|
|
$this->assertEquals($invoice_number, 1);
|
|
|
|
|
|
|
|
$invoice_number = $this->getNextInvoiceNumber($this->client);
|
|
|
|
|
|
|
|
$this->assertEquals($invoice_number, 2);
|
|
|
|
|
|
|
|
|
|
|
|
// Log::error(print_r($client->company->settings,1));
|
|
|
|
// Log::error(print_r($client->settings,1));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvoiceNumberPattern()
|
|
|
|
{
|
|
|
|
$settings = $this->client->company->settings;
|
|
|
|
|
|
|
|
$settings->invoice_number_pattern = '{$year}-{$counter}';
|
|
|
|
$this->client->company->settings = $settings;
|
|
|
|
$this->client->company->save();
|
|
|
|
|
|
|
|
//Log::error(print_r($this->client->company->settings,1));
|
|
|
|
|
|
|
|
$invoice_number = $this->getNextInvoiceNumber($this->client);
|
|
|
|
$invoice_number2 = $this->getNextInvoiceNumber($this->client);
|
|
|
|
|
|
|
|
$this->assertEquals($invoice_number, '2019-1');
|
|
|
|
$this->assertEquals($invoice_number2, '2019-2');
|
|
|
|
$this->assertEquals($this->client->company->settings->invoice_number_counter,3);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvoiceClientNumberPattern()
|
|
|
|
{
|
|
|
|
$settings = $this->client->company->settings;
|
|
|
|
|
|
|
|
$settings->invoice_number_pattern = '{$year}-{$client_counter}';
|
|
|
|
$this->client->company->settings = $settings;
|
|
|
|
$this->client->company->save();
|
|
|
|
|
|
|
|
$settings = $this->client->settings;
|
|
|
|
$settings->invoice_number_counter = 10;
|
|
|
|
$this->client->settings = $settings;
|
|
|
|
$this->client->save();
|
|
|
|
|
|
|
|
$this->assertEquals($this->client->settings->invoice_number_counter,10);
|
2019-05-27 12:48:52 +02:00
|
|
|
|
2019-05-27 13:54:27 +02:00
|
|
|
$invoice_number = $this->getNextInvoiceNumber($this->client);
|
|
|
|
|
|
|
|
$this->assertEquals($invoice_number, '2019-10');
|
|
|
|
|
|
|
|
$invoice_number = $this->getNextInvoiceNumber($this->client);
|
|
|
|
$this->assertEquals($invoice_number, '2019-11');
|
|
|
|
|
|
|
|
|
2019-05-27 12:48:52 +02:00
|
|
|
}
|
|
|
|
|
2019-05-28 07:55:50 +02:00
|
|
|
public function testInvoicePadding()
|
|
|
|
{
|
|
|
|
$settings = $this->client->company->settings;
|
|
|
|
$settings->counter_padding = 5;
|
|
|
|
$this->client->company->settings = $settings;
|
|
|
|
$this->client->company->save();
|
|
|
|
|
|
|
|
$invoice_number = $this->getNextInvoiceNumber($this->client);
|
|
|
|
|
|
|
|
$this->assertEquals(strlen($invoice_number), 5);
|
|
|
|
|
|
|
|
$settings = $this->client->company->settings;
|
|
|
|
$settings->counter_padding = 10;
|
|
|
|
$this->client->company->settings = $settings;
|
|
|
|
$this->client->company->save();
|
|
|
|
|
|
|
|
$invoice_number = $this->getNextInvoiceNumber($this->client);
|
|
|
|
|
|
|
|
$this->assertEquals(strlen($invoice_number), 10);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvoicePrefix()
|
|
|
|
{
|
|
|
|
$settings = $this->client->company->settings;
|
|
|
|
$settings->invoice_number_prefix = 'R';
|
|
|
|
$this->client->company->settings = $settings;
|
|
|
|
$this->client->company->save();
|
|
|
|
|
|
|
|
$invoice_number = $this->getNextInvoiceNumber($this->client);
|
|
|
|
|
|
|
|
$this->assertEquals($invoice_number, 'R1');
|
|
|
|
|
|
|
|
$invoice_number = $this->getNextInvoiceNumber($this->client);
|
|
|
|
|
|
|
|
$this->assertEquals($invoice_number, 'R2');
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClientNumber()
|
|
|
|
{
|
|
|
|
$client_number = $this->getNextClientNumber($this->client);
|
|
|
|
|
|
|
|
$this->assertEquals($client_number, '1');
|
|
|
|
|
|
|
|
$client_number = $this->getNextClientNumber($this->client);
|
|
|
|
|
|
|
|
$this->assertEquals($client_number, '2');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testClientNumberPrefix()
|
|
|
|
{
|
|
|
|
$settings = $this->client->company->settings;
|
|
|
|
$settings->client_number_prefix = 'C';
|
|
|
|
$this->client->company->settings = $settings;
|
|
|
|
$this->client->company->save();
|
|
|
|
|
|
|
|
$client_number = $this->getNextClientNumber($this->client);
|
|
|
|
|
|
|
|
$this->assertEquals($client_number, 'C1');
|
|
|
|
|
|
|
|
$client_number = $this->getNextClientNumber($this->client);
|
|
|
|
|
|
|
|
$this->assertEquals($client_number, 'C2');
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClientNumberPattern()
|
|
|
|
{
|
|
|
|
$settings = $this->client->company->settings;
|
|
|
|
$settings->client_number_prefix = 'C-';
|
|
|
|
$settings->client_number_pattern = '{$year}-{$user_id}-{$counter}';
|
|
|
|
$this->client->company->settings = $settings;
|
|
|
|
$this->client->company->save();
|
|
|
|
|
|
|
|
$client_number = $this->getNextClientNumber($this->client);
|
|
|
|
|
|
|
|
$this->assertEquals($client_number, 'C-' . date('Y') . '-' . $this->client->user_id . '-1');
|
|
|
|
|
|
|
|
$client_number = $this->getNextClientNumber($this->client);
|
|
|
|
|
|
|
|
$this->assertEquals($client_number, 'C-' . date('Y') . '-' . $this->client->user_id . '-1');
|
|
|
|
|
|
|
|
}
|
2019-05-27 13:54:27 +02:00
|
|
|
/*
|
|
|
|
public function testPrefixOnlyInvoiceNumber()
|
2019-05-27 12:48:52 +02:00
|
|
|
{
|
|
|
|
$this->assertEquals(true, true);
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:54:27 +02:00
|
|
|
public function testClientCounterValue()
|
|
|
|
{
|
|
|
|
$this->assertEquals($this->getCounter($this->client), 1);
|
|
|
|
}
|
|
|
|
public function testClientNextNumber()
|
|
|
|
{
|
|
|
|
$this->assertEquals($this->getNextNumber($this->client),1);
|
|
|
|
}
|
|
|
|
public function testRecurringInvoiceNumberPrefix()
|
|
|
|
{
|
|
|
|
//$this->assertEquals($this->getNextNumber(RecurringInvoice::class), 'R1');
|
|
|
|
$this->assertEquals($this->getCounter($this->client), 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
public function testClientIncrementer()
|
|
|
|
{
|
|
|
|
$this->incrementCounter($this->client);
|
|
|
|
$this->assertEquals($this->getCounter($this->client), 2);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
public function testCounterValues()
|
|
|
|
{
|
|
|
|
$this->assertEquals($this->getCounter(Invoice::class), 1);
|
|
|
|
$this->assertEquals($this->getCounter(RecurringInvoice::class), 1);
|
|
|
|
$this->assertEquals($this->getCounter(Credit::class), 1);
|
|
|
|
}
|
|
|
|
public function testClassIncrementers()
|
|
|
|
{
|
|
|
|
$this->client->incrementCounter(Invoice::class);
|
|
|
|
$this->client->incrementCounter(RecurringInvoice::class);
|
|
|
|
$this->client->incrementCounter(Credit::class);
|
|
|
|
$this->assertEquals($this->getCounter(Invoice::class), 3);
|
|
|
|
$this->assertEquals($this->getCounter(RecurringInvoice::class), 3);
|
|
|
|
$this->assertEquals($this->getCounter(Credit::class), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClientNumberPattern()
|
|
|
|
{
|
|
|
|
$settings = $this->client->getSettingsByKey('client_number_pattern');
|
|
|
|
$settings->client_number_pattern = '{$year}-{$counter}';
|
|
|
|
$this->client->setSettingsByEntity(Client::class, $settings);
|
|
|
|
$company = Company::find($this->client->company_id);
|
|
|
|
$this->assertEquals($company->settings->client_number_counter,1);
|
|
|
|
$this->assertEquals($this->getNextNumber($this->client), '2019-1');
|
|
|
|
$this->assertEquals($this->getNextNumber($this->client), '2019-2');
|
|
|
|
|
|
|
|
$company = Company::find($this->client->company_id);
|
|
|
|
$this->assertEquals($company->settings->client_number_counter,2);
|
|
|
|
$this->assertEquals($this->client->settings->client_number_counter,1);
|
|
|
|
}
|
|
|
|
public function testClientNumberPatternWithDate()
|
|
|
|
{
|
|
|
|
date_default_timezone_set('US/Eastern');
|
|
|
|
$settings = $this->client->getSettingsByKey('client_number_pattern');
|
|
|
|
$settings->client_number_pattern = '{$date:j}-{$counter}';
|
|
|
|
$this->client->setSettingsByEntity(Client::class, $settings);
|
|
|
|
|
|
|
|
$this->assertEquals($this->getNextNumber($this->client), date('j') . '-1');
|
|
|
|
}
|
|
|
|
public function testClientNumberPatternWithDate2()
|
|
|
|
{
|
|
|
|
date_default_timezone_set('US/Eastern');
|
|
|
|
$settings = $this->client->getSettingsByKey('client_number_pattern');
|
|
|
|
$settings->client_number_pattern = '{$date:d M Y}-{$counter}';
|
|
|
|
$this->client->setSettingsByEntity(Client::class, $settings);
|
|
|
|
|
|
|
|
$this->assertEquals($this->getNextNumber($this->client), date('d M Y') . '-1');
|
|
|
|
}
|
|
|
|
*/
|
2019-05-27 12:48:52 +02:00
|
|
|
|
|
|
|
}
|