mirror of
https://github.com/crater-invoice/crater.git
synced 2025-10-27 19:51:09 -04:00
build version 400
This commit is contained in:
33
tests/Unit/AddressTest.php
Normal file
33
tests/Unit/AddressTest.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\Address;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::find(1);
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('an address belongs to user', function () {
|
||||
$address = Address::factory()->forUser()->create();
|
||||
|
||||
$this->assertTrue($address->user->exists());
|
||||
});
|
||||
|
||||
test('an address belongs to country', function () {
|
||||
$address = Address::factory()->create();
|
||||
|
||||
$this->assertTrue($address->country->exists());
|
||||
});
|
||||
|
||||
56
tests/Unit/CompanySettingTest.php
Normal file
56
tests/Unit/CompanySettingTest.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\CompanySetting;
|
||||
use Crater\Models\Company;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use function Pest\Faker\faker;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::find(1);
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('company setting belongs to company', function () {
|
||||
$setting = CompanySetting::factory()->create();
|
||||
|
||||
$this->assertTrue($setting->company()->exists());
|
||||
});
|
||||
|
||||
test('set settings', function () {
|
||||
$key = faker()->name;
|
||||
|
||||
$value = faker()->word;
|
||||
|
||||
$company = Company::factory()->create();
|
||||
|
||||
CompanySetting::setSettings([$key => $value], $company->id);
|
||||
|
||||
$response = CompanySetting::getSetting($key, $company->id);
|
||||
|
||||
$this->assertEquals($value, $response);
|
||||
});
|
||||
|
||||
test('get settings', function () {
|
||||
$key = faker()->name;
|
||||
|
||||
$value = faker()->word;
|
||||
|
||||
$company = Company::factory()->create();
|
||||
|
||||
CompanySetting::setSettings([$key => $value], $company->id);
|
||||
|
||||
$response = CompanySetting::getSettings([$key], $company->id);
|
||||
|
||||
$this->assertEquals([$key => $value], $response);
|
||||
});
|
||||
34
tests/Unit/CompanyTest.php
Normal file
34
tests/Unit/CompanyTest.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\Company;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::find(1);
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('company has one user', function () {
|
||||
$company = Company::factory()->hasUser()->create();
|
||||
|
||||
$this->assertTrue($company->user()->exists());
|
||||
});
|
||||
|
||||
test('company has many company setings', function () {
|
||||
$company = Company::factory()->hasSettings(5)->create();
|
||||
|
||||
$this->assertCount(5, $company->settings);
|
||||
|
||||
$this->assertTrue($company->settings()->exists());
|
||||
});
|
||||
32
tests/Unit/CountryTest.php
Normal file
32
tests/Unit/CountryTest.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\Country;
|
||||
use Crater\Models\Address;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('country has many addresses', function () {
|
||||
$country = Country::find(1);
|
||||
|
||||
$address = Address::factory()->count(5)->create([
|
||||
'country_id' => $country->id
|
||||
]);
|
||||
|
||||
$this->assertTrue($country->address()->exists());
|
||||
});
|
||||
|
||||
34
tests/Unit/CustomFieldTest.php
Normal file
34
tests/Unit/CustomFieldTest.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\CustomField;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::find(1);
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('custom field belongs to company', function () {
|
||||
$customField = CustomField::factory()->create();
|
||||
|
||||
$this->assertTrue($customField->company()->exists());
|
||||
});
|
||||
|
||||
test('custom field has many custom field value', function () {
|
||||
$customField = CustomField::factory()->hascustomFieldValue(5)->create();
|
||||
|
||||
$this->assertCount(5, $customField->customFieldValue);
|
||||
|
||||
$this->assertTrue($customField->customFieldValue()->exists());
|
||||
});
|
||||
32
tests/Unit/CustomFieldValueTest.php
Normal file
32
tests/Unit/CustomFieldValueTest.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\CustomFieldValue;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::find(1);
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('custom field value belongs to company', function () {
|
||||
$fieldValue = CustomFieldValue::factory()->create();
|
||||
|
||||
$this->assertTrue($fieldValue->company()->exists());
|
||||
});
|
||||
|
||||
test('custom field value belongs to custom field', function () {
|
||||
$fieldValue = CustomFieldValue::factory()->forCustomField()->create();
|
||||
|
||||
$this->assertTrue($fieldValue->customField()->exists());
|
||||
});
|
||||
48
tests/Unit/EstimateItemTest.php
Normal file
48
tests/Unit/EstimateItemTest.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\Estimate;
|
||||
use Crater\Models\Item;
|
||||
use Crater\Models\EstimateItem;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('estimate item belongs to estimate', function () {
|
||||
$estimateItem = EstimateItem::factory()->forEstimate()->create();
|
||||
|
||||
$this->assertTrue($estimateItem->estimate()->exists());
|
||||
});
|
||||
|
||||
test('estimate item belongs to item', function () {
|
||||
$estimateItem = EstimateItem::factory()->create([
|
||||
'item_id' => Item::factory(),
|
||||
'estimate_id' => Estimate::factory()
|
||||
]);
|
||||
|
||||
$this->assertTrue($estimateItem->item()->exists());
|
||||
});
|
||||
|
||||
|
||||
test('estimate item has many taxes', function () {
|
||||
$estimateItem = EstimateItem::factory()->hasTaxes(5)->create([
|
||||
'estimate_id' => Estimate::factory()
|
||||
]);
|
||||
|
||||
$this->assertCount(5, $estimateItem->taxes);
|
||||
|
||||
$this->assertTrue($estimateItem->taxes()->exists());
|
||||
});
|
||||
28
tests/Unit/EstimateTemplateTest.php
Normal file
28
tests/Unit/EstimateTemplateTest.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\EstimateTemplate;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('estimate template has many estimates', function () {
|
||||
$estimateTemplate = EstimateTemplate::factory()->hasEstimates(5)->create();
|
||||
|
||||
$this->assertCount(5, $estimateTemplate->estimates);
|
||||
|
||||
$this->assertTrue($estimateTemplate->estimates()->exists());
|
||||
});
|
||||
233
tests/Unit/EstimateTest.php
Normal file
233
tests/Unit/EstimateTest.php
Normal file
@ -0,0 +1,233 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\Tax;
|
||||
use Crater\Models\Estimate;
|
||||
use Crater\Models\EstimateItem;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('estimate has many estimate items', function () {
|
||||
$estimate = Estimate::factory()->create();
|
||||
|
||||
$estimate = Estimate::factory()->hasItems(5)->create();
|
||||
|
||||
$this->assertCount(5, $estimate->items);
|
||||
|
||||
$this->assertTrue($estimate->items()->exists());
|
||||
});
|
||||
|
||||
test('estimate belongs to user', function () {
|
||||
$estimate = Estimate::factory()->forUser()->create();
|
||||
|
||||
$this->assertTrue($estimate->user()->exists());
|
||||
});
|
||||
|
||||
test('estimate has many taxes', function () {
|
||||
$estimate = Estimate::factory()->hasTaxes(5)->create();
|
||||
|
||||
$this->assertCount(5, $estimate->taxes);
|
||||
|
||||
$this->assertTrue($estimate->taxes()->exists());
|
||||
});
|
||||
|
||||
test('estimate belongs to estimate template', function () {
|
||||
$estimate = Estimate::factory()->forEstimateTemplate()->create();
|
||||
|
||||
$this->assertTrue($estimate->estimateTemplate()->exists());
|
||||
});
|
||||
|
||||
|
||||
test('get next estimate number', function () {
|
||||
$estimate = Estimate::factory()->create();
|
||||
|
||||
$prefix = $estimate->getEstimatePrefixAttribute();
|
||||
|
||||
$nextNumber = $estimate->getNextEstimateNumber($prefix);
|
||||
|
||||
$estimate1 = Estimate::factory()->create();
|
||||
|
||||
$this->assertEquals($prefix.'-'.$nextNumber, $estimate1['estimate_number']);
|
||||
});
|
||||
|
||||
test('get estimate prefix attribute', function () {
|
||||
$estimate = Estimate::factory()->create();
|
||||
|
||||
$num = $estimate->getEstimateNumAttribute();
|
||||
|
||||
$prefix = $estimate->getEstimatePrefixAttribute();
|
||||
|
||||
$this->assertEquals($prefix.'-'.$num, $estimate['estimate_number']);
|
||||
});
|
||||
|
||||
test('get estimate num attribute', function () {
|
||||
$estimate = Estimate::factory()->create();
|
||||
|
||||
$prefix = $estimate->getEstimatePrefixAttribute();
|
||||
|
||||
$num = $estimate->getEstimateNumAttribute();
|
||||
|
||||
$this->assertEquals($prefix.'-'.$num, $estimate['estimate_number']);
|
||||
});
|
||||
|
||||
test('create estimate', function () {
|
||||
$estimate = Estimate::factory()->raw();
|
||||
|
||||
$item = EstimateItem::factory()->raw();
|
||||
|
||||
$estimate['items'] = [];
|
||||
array_push($estimate['items'], $item);
|
||||
|
||||
$estimate['taxes'] = [];
|
||||
array_push($estimate['taxes'], Tax::factory()->raw());
|
||||
|
||||
$request = new Request;
|
||||
|
||||
$request->replace($estimate);
|
||||
|
||||
$response = Estimate::createEstimate($request);
|
||||
|
||||
$this->assertDatabaseHas('estimate_items', [
|
||||
'estimate_id' => $response->id,
|
||||
'name' => $item['name'],
|
||||
'description' => $item['description'],
|
||||
'price' => $item['price'],
|
||||
'quantity' => $item['quantity'],
|
||||
'total' => $item['total'],
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('estimates', [
|
||||
'estimate_number' => $estimate['estimate_number'],
|
||||
'user_id' => $estimate['user_id'],
|
||||
'estimate_template_id' => $estimate['estimate_template_id'],
|
||||
'sub_total' => $estimate['sub_total'],
|
||||
'total' => $estimate['total'],
|
||||
'discount' => $estimate['discount'],
|
||||
'discount_type' => $estimate['discount_type'],
|
||||
'discount_val' => $estimate['discount_val'],
|
||||
'tax' => $estimate['tax'],
|
||||
'notes' => $estimate['notes']
|
||||
]);
|
||||
});
|
||||
|
||||
test('update estimate', function () {
|
||||
$estimate = Estimate::factory()->hasItems()->hasTaxes()->create();
|
||||
|
||||
$newEstimate = Estimate::factory()->raw();
|
||||
|
||||
$item = EstimateItem::factory()->raw([
|
||||
'estimate_id' => $estimate->id
|
||||
]);
|
||||
|
||||
$newEstimate['items'] = [];
|
||||
$newEstimate['taxes'] = [];
|
||||
|
||||
array_push($newEstimate['items'], $item);
|
||||
array_push($newEstimate['taxes'], Tax::factory()->raw());
|
||||
|
||||
$request = new Request;
|
||||
|
||||
$request->replace($newEstimate);
|
||||
|
||||
$estimate_number = explode("-", $newEstimate['estimate_number']);
|
||||
|
||||
$number_attributes['estimate_number'] = $estimate_number[0].'-'.sprintf('%06d', intval($estimate_number[1]));
|
||||
|
||||
$estimate->updateEstimate($request);
|
||||
|
||||
$this->assertDatabaseHas('estimate_items', [
|
||||
'estimate_id' => $estimate->id,
|
||||
'name' => $item['name'],
|
||||
'description' => $item['description'],
|
||||
'price' => $item['price'],
|
||||
'total' => $item['total'],
|
||||
'quantity' => $item['quantity']
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('estimates', [
|
||||
'estimate_number' => $newEstimate['estimate_number'],
|
||||
'user_id' => $newEstimate['user_id'],
|
||||
'estimate_template_id' => $newEstimate['estimate_template_id'],
|
||||
'sub_total' => $newEstimate['sub_total'],
|
||||
'total' => $newEstimate['total'],
|
||||
'discount' => $newEstimate['discount'],
|
||||
'discount_type' => $newEstimate['discount_type'],
|
||||
'discount_val' => $newEstimate['discount_val'],
|
||||
'tax' => $newEstimate['tax'],
|
||||
'notes' => $newEstimate['notes']
|
||||
]);
|
||||
});
|
||||
|
||||
test('create items', function () {
|
||||
$estimate = Estimate::factory()->create();
|
||||
|
||||
$items = [];
|
||||
|
||||
$item = EstimateItem::factory()->raw([
|
||||
'invoice_id' => $estimate->id
|
||||
]);
|
||||
|
||||
array_push($items, $item);
|
||||
|
||||
$request = new Request;
|
||||
|
||||
$request->replace(['items' => $items ]);
|
||||
|
||||
Estimate::createItems($estimate, $request);
|
||||
|
||||
$this->assertDatabaseHas('estimate_items', [
|
||||
'estimate_id' => $estimate->id,
|
||||
'description' => $item['description'],
|
||||
'price' => $item['price'],
|
||||
'tax' => $item['tax'],
|
||||
'quantity' => $item['quantity'],
|
||||
'total' => $item['total']
|
||||
]);
|
||||
|
||||
$this->assertCount(1, $estimate->items);
|
||||
});
|
||||
|
||||
test('create taxes', function () {
|
||||
$estimate = Estimate::factory()->create();
|
||||
$taxes = [];
|
||||
|
||||
$tax1 = Tax::factory()->raw([
|
||||
'estimate_id' => $estimate->id
|
||||
]);
|
||||
|
||||
$tax2 = Tax::factory()->raw([
|
||||
'estimate_id' => $estimate->id
|
||||
]);
|
||||
|
||||
array_push($taxes, $tax1);
|
||||
array_push($taxes, $tax2);
|
||||
|
||||
$request = new Request;
|
||||
|
||||
$request->replace(['taxes' => $taxes ]);
|
||||
|
||||
Estimate::createTaxes($estimate, $request);
|
||||
|
||||
$this->assertCount(2, $estimate->taxes);
|
||||
|
||||
$this->assertDatabaseHas('taxes', [
|
||||
'estimate_id' => $estimate->id,
|
||||
'name' => $tax1['name'],
|
||||
'amount' => $tax1['amount'],
|
||||
]);
|
||||
});
|
||||
@ -1,18 +0,0 @@
|
||||
<?php
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class ExampleTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* A basic test example.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBasicTest()
|
||||
{
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
27
tests/Unit/ExpenseCategoryTest.php
Normal file
27
tests/Unit/ExpenseCategoryTest.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\ExpenseCategory;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('expense category has many expenses', function () {
|
||||
$category = ExpenseCategory::factory()->hasExpenses(5)->create();
|
||||
|
||||
$this->assertCount(5, $category->expenses);
|
||||
$this->assertTrue($category->expenses()->exists());
|
||||
});
|
||||
32
tests/Unit/ExpenseTest.php
Normal file
32
tests/Unit/ExpenseTest.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\Expense;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('expense belongs to category', function () {
|
||||
$expense = Expense::factory()->forCategory()->create();
|
||||
|
||||
$this->assertTrue($expense->category()->exists());
|
||||
});
|
||||
|
||||
test('expense belongs to user', function () {
|
||||
$expense = Expense::factory()->forUser()->create();
|
||||
|
||||
$this->assertTrue($expense->user()->exists());
|
||||
});
|
||||
48
tests/Unit/InvoiceItemTest.php
Normal file
48
tests/Unit/InvoiceItemTest.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\Invoice;
|
||||
use Crater\Models\Item;
|
||||
use Crater\Models\InvoiceItem;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('invoice item belongs to invoice', function () {
|
||||
$invoiceItem = InvoiceItem::factory()->forInvoice()->create();
|
||||
|
||||
$this->assertTrue($invoiceItem->invoice()->exists());
|
||||
});
|
||||
|
||||
test('invoice item belongs to item', function () {
|
||||
$invoiceItem = InvoiceItem::factory()->create([
|
||||
'item_id' => Item::factory(),
|
||||
'invoice_id' => Invoice::factory()
|
||||
]);
|
||||
|
||||
$this->assertTrue($invoiceItem->item()->exists());
|
||||
});
|
||||
|
||||
|
||||
test('invoice item has many taxes', function () {
|
||||
$invoiceItem = InvoiceItem::factory()->hasTaxes(5)->create([
|
||||
'invoice_id' => Invoice::factory()
|
||||
]);
|
||||
|
||||
$this->assertCount(5, $invoiceItem->taxes);
|
||||
|
||||
$this->assertTrue($invoiceItem->taxes()->exists());
|
||||
});
|
||||
28
tests/Unit/InvoiceTemplateTest.php
Normal file
28
tests/Unit/InvoiceTemplateTest.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\InvoiceTemplate;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('invoice template has many invoices', function () {
|
||||
$invoiceTemplate = InvoiceTemplate::factory()->hasInvoices(5)->create();
|
||||
|
||||
$this->assertCount(5, $invoiceTemplate->invoices);
|
||||
|
||||
$this->assertTrue($invoiceTemplate->invoices()->exists());
|
||||
});
|
||||
247
tests/Unit/InvoiceTest.php
Normal file
247
tests/Unit/InvoiceTest.php
Normal file
@ -0,0 +1,247 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\Tax;
|
||||
use Crater\Models\Invoice;
|
||||
use Crater\Models\InvoiceItem;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'UnitSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'PaymentMethodSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('invoice has many invoice items', function () {
|
||||
$invoice = Invoice::factory()->hasItems(5)->create();
|
||||
|
||||
$this->assertCount(5, $invoice->items);
|
||||
|
||||
$this->assertTrue($invoice->items()->exists());
|
||||
});
|
||||
|
||||
test('invoice has many taxes', function () {
|
||||
$invoice = Invoice::factory()->hasTaxes(5)->create();
|
||||
|
||||
$this->assertCount(5, $invoice->taxes);
|
||||
|
||||
$this->assertTrue($invoice->taxes()->exists());
|
||||
});
|
||||
|
||||
test('invoice has many payments', function () {
|
||||
$invoice = Invoice::factory()->hasPayments(5)->create();
|
||||
|
||||
$this->assertCount(5, $invoice->payments);
|
||||
|
||||
$this->assertTrue($invoice->payments()->exists());
|
||||
});
|
||||
|
||||
test('invoice belongs to user', function () {
|
||||
$invoice = Invoice::factory()->forUser()->create();
|
||||
|
||||
$this->assertTrue($invoice->user()->exists());
|
||||
});
|
||||
|
||||
test('invoice belongs to invoice template', function () {
|
||||
$invoice = Invoice::factory()->forInvoiceTemplate()->create();
|
||||
|
||||
$this->assertTrue($invoice->invoiceTemplate()->exists());
|
||||
});
|
||||
|
||||
test('get next invoice number', function () {
|
||||
$invoice = Invoice::factory()->create();
|
||||
|
||||
$prefix = $invoice->getInvoicePrefixAttribute();
|
||||
|
||||
$nextNumber = $invoice->getNextInvoiceNumber($prefix);
|
||||
|
||||
$invoice1 = Invoice::factory()->create();
|
||||
|
||||
$this->assertEquals($prefix.'-'.$nextNumber, $invoice1['invoice_number']);
|
||||
});
|
||||
|
||||
test('get invoice prefix attribute', function () {
|
||||
$invoice = Invoice::factory()->create();
|
||||
|
||||
$num = $invoice->getInvoiceNumAttribute();
|
||||
|
||||
$prefix = $invoice->getInvoicePrefixAttribute();
|
||||
|
||||
$this->assertEquals($prefix.'-'.$num, $invoice['invoice_number']);
|
||||
});
|
||||
|
||||
test('get invoice num attribute', function () {
|
||||
$invoice = Invoice::factory()->create();
|
||||
|
||||
$num = $invoice->getInvoiceNumAttribute();
|
||||
|
||||
$prefix = $invoice->getInvoicePrefixAttribute();
|
||||
|
||||
$this->assertEquals($prefix.'-'.$num, $invoice['invoice_number']);
|
||||
});
|
||||
|
||||
|
||||
test('get previous status', function () {
|
||||
$invoice = Invoice::factory()->create();
|
||||
|
||||
$status = $invoice->getPreviousStatus();
|
||||
|
||||
$this->assertEquals('OVERDUE', $status);
|
||||
});
|
||||
|
||||
|
||||
test('create invoice', function () {
|
||||
$invoice = Invoice::factory()->raw();
|
||||
|
||||
$item = InvoiceItem::factory()->raw();
|
||||
|
||||
$invoice['items'] = [];
|
||||
array_push($invoice['items'], $item);
|
||||
|
||||
$invoice['taxes'] = [];
|
||||
array_push($invoice['taxes'], Tax::factory()->raw());
|
||||
|
||||
$request = new Request;
|
||||
|
||||
$request->replace($invoice);
|
||||
|
||||
$invoice_number = explode("-",$invoice['invoice_number']);
|
||||
$number_attributes['invoice_number'] = $invoice_number[0].'-'.sprintf('%06d', intval($invoice_number[1]));
|
||||
|
||||
$response = Invoice::createInvoice($request);
|
||||
|
||||
$this->assertDatabaseHas('invoice_items', [
|
||||
'invoice_id' => $response->id,
|
||||
'name' => $item['name'],
|
||||
'description' => $item['description'],
|
||||
'total' => $item['total'],
|
||||
'quantity' => $item['quantity'],
|
||||
'discount' => $item['discount'],
|
||||
'price' => $item['price'],
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('invoices', [
|
||||
'invoice_number' => $invoice['invoice_number'],
|
||||
'sub_total' => $invoice['sub_total'],
|
||||
'total' => $invoice['total'],
|
||||
'tax' => $invoice['tax'],
|
||||
'discount' => $invoice['discount'],
|
||||
'notes' => $invoice['notes'],
|
||||
'user_id' => $invoice['user_id'],
|
||||
'invoice_template_id' => $invoice['invoice_template_id'],
|
||||
]);
|
||||
});
|
||||
|
||||
test('update invoice', function () {
|
||||
$invoice = Invoice::factory()->create();
|
||||
|
||||
$newInvoice = Invoice::factory()->raw();
|
||||
|
||||
$item = InvoiceItem::factory()->raw([
|
||||
'invoice_id' => $invoice->id
|
||||
]);
|
||||
|
||||
$tax = Tax::factory()->raw([
|
||||
'invoice_id' => $invoice->id
|
||||
]);
|
||||
|
||||
$newInvoice['items'] = [];
|
||||
$newInvoice['taxes'] = [];
|
||||
|
||||
array_push($newInvoice['items'], $item);
|
||||
array_push($newInvoice['taxes'], $tax);
|
||||
|
||||
$request = new Request;
|
||||
|
||||
$request->replace($newInvoice);
|
||||
|
||||
$invoice_number = explode("-", $newInvoice['invoice_number']);
|
||||
|
||||
$number_attributes['invoice_number'] = $invoice_number[0].'-'.sprintf('%06d', intval($invoice_number[1]));
|
||||
|
||||
$response = $invoice->updateInvoice($request);
|
||||
|
||||
$this->assertDatabaseHas('invoice_items', [
|
||||
'invoice_id' => $response->id,
|
||||
'name' => $item['name'],
|
||||
'description' => $item['description'],
|
||||
'total' => $item['total'],
|
||||
'quantity' => $item['quantity'],
|
||||
'discount' => $item['discount'],
|
||||
'price' => $item['price'],
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('invoices', [
|
||||
'invoice_number' => $newInvoice['invoice_number'],
|
||||
'sub_total' => $newInvoice['sub_total'],
|
||||
'total' => $newInvoice['total'],
|
||||
'tax' => $newInvoice['tax'],
|
||||
'discount' => $newInvoice['discount'],
|
||||
'notes' => $newInvoice['notes'],
|
||||
'user_id' => $newInvoice['user_id'],
|
||||
'invoice_template_id' => $newInvoice['invoice_template_id'],
|
||||
]);
|
||||
});
|
||||
|
||||
test('create items', function () {
|
||||
$invoice = Invoice::factory()->create();
|
||||
|
||||
$items = [];
|
||||
|
||||
$item = InvoiceItem::factory()->raw([
|
||||
'invoice_id' => $invoice->id
|
||||
]);
|
||||
|
||||
array_push($items, $item);
|
||||
|
||||
$request = new Request;
|
||||
|
||||
$request->replace(['items' => $items ]);
|
||||
|
||||
Invoice::createItems($invoice, $request);
|
||||
|
||||
$this->assertDatabaseHas('invoice_items', [
|
||||
'invoice_id' => $invoice->id,
|
||||
'description' => $item['description'],
|
||||
'price' => $item['price'],
|
||||
'tax' => $item['tax'],
|
||||
'quantity' => $item['quantity'],
|
||||
'total' => $item['total']
|
||||
]);
|
||||
});
|
||||
|
||||
test('create taxes', function () {
|
||||
$invoice = Invoice::factory()->create();
|
||||
|
||||
$taxes = [];
|
||||
|
||||
$tax = Tax::factory()->raw([
|
||||
'invoice_id' => $invoice->id
|
||||
]);
|
||||
|
||||
array_push($taxes, $tax);
|
||||
|
||||
$request = new Request;
|
||||
|
||||
$request->replace(['taxes' => $taxes ]);
|
||||
|
||||
Invoice::createTaxes($invoice, $request);
|
||||
|
||||
$this->assertDatabaseHas('taxes', [
|
||||
'invoice_id' => $invoice->id,
|
||||
'name' => $tax['name'],
|
||||
'amount' => $tax['amount'],
|
||||
]);
|
||||
});
|
||||
62
tests/Unit/ItemTest.php
Normal file
62
tests/Unit/ItemTest.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\Item;
|
||||
use Crater\Models\InvoiceItem;
|
||||
use Crater\Models\Invoice;
|
||||
use Crater\Models\EstimateItem;
|
||||
use Crater\Models\Estimate;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'UnitSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('an item belongs to unit', function () {
|
||||
$item = Item::factory()->forUnit()->create();
|
||||
|
||||
$this->assertTrue($item->unit()->exists());
|
||||
});
|
||||
|
||||
test('an item has many taxes', function () {
|
||||
$item = Item::factory()->hasTaxes(5)->create();
|
||||
|
||||
$this->assertCount(5, $item->taxes);
|
||||
$this->assertTrue($item->taxes()->exists());
|
||||
});
|
||||
|
||||
test('an item has many invoice items', function () {
|
||||
$item = Item::factory()->has(InvoiceItem::factory()->count(5)->state([
|
||||
'invoice_id' => Invoice::factory()
|
||||
]))->create();
|
||||
|
||||
$this->assertCount(5, $item->invoiceItems);
|
||||
|
||||
$this->assertTrue($item->invoiceItems()->exists());
|
||||
});
|
||||
|
||||
test('an item has many estimate items', function () {
|
||||
$item = Item::factory()->has(EstimateItem::factory()
|
||||
->count(5)
|
||||
->state([
|
||||
'estimate_id' => Estimate::factory()
|
||||
]))
|
||||
->create();
|
||||
|
||||
$this->assertCount(5, $item->estimateItems);
|
||||
|
||||
$this->assertTrue($item->estimateItems()->exists());
|
||||
});
|
||||
36
tests/Unit/PaymentMethodTest.php
Normal file
36
tests/Unit/PaymentMethodTest.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\PaymentMethod;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'UnitSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'PaymentMethodSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('payment method has many payment', function () {
|
||||
$method = PaymentMethod::factory()->hasPayments(5)->create();
|
||||
|
||||
$this->assertCount(5, $method->payments);
|
||||
|
||||
$this->assertTrue($method->payments()->exists());
|
||||
});
|
||||
|
||||
test('payment method belongs to company', function () {
|
||||
$method = PaymentMethod::factory()->create();
|
||||
|
||||
$this->assertTrue($method->company()->exists());
|
||||
});
|
||||
74
tests/Unit/PaymentTest.php
Normal file
74
tests/Unit/PaymentTest.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\Payment;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'UnitSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'PaymentMethodSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('payment belongs to invoice', function () {
|
||||
$payment = Payment::factory()->forInvoice()->create();
|
||||
|
||||
$this->assertTrue($payment->invoice()->exists());
|
||||
});
|
||||
|
||||
|
||||
test('payment belongs to user', function () {
|
||||
$payment = Payment::factory()->create();
|
||||
|
||||
$this->assertTrue($payment->user()->exists());
|
||||
});
|
||||
|
||||
test('payment belongs to payment method', function () {
|
||||
$payment = Payment::factory()->forPaymentMethod()->create();
|
||||
|
||||
$this->assertTrue($payment->paymentMethod()->exists());
|
||||
});
|
||||
|
||||
test('get payment num attribute', function () {
|
||||
$payment = Payment::factory()->create();
|
||||
|
||||
$num_attribute = $payment->getPaymentNumAttribute();
|
||||
|
||||
$prefix_attribute = $payment->getPaymentPrefixAttribute();
|
||||
|
||||
$this->assertEquals($prefix_attribute.'-'.$num_attribute , $payment['payment_number']);
|
||||
});
|
||||
|
||||
test('get payment prefix attribute', function () {
|
||||
$payment = Payment::factory()->create();
|
||||
|
||||
$num_attribute = $payment->getPaymentNumAttribute();
|
||||
|
||||
$prefix_attribute = $payment->getPaymentPrefixAttribute();
|
||||
|
||||
$this->assertEquals($prefix_attribute.'-'.$num_attribute , $payment['payment_number']);
|
||||
});
|
||||
|
||||
test('get next payment number', function () {
|
||||
$payment = Payment::factory()->create();
|
||||
|
||||
$prefix_attribute = $payment->getPaymentPrefixAttribute();
|
||||
|
||||
$next_payment_number = $payment->getNextPaymentNumber($prefix_attribute);
|
||||
|
||||
$payment2 = Payment::factory()->create();
|
||||
|
||||
$this->assertEquals($prefix_attribute.'-'.$next_payment_number , $payment2['payment_number']);
|
||||
});
|
||||
|
||||
26
tests/Unit/Request/CustomFieldTest.php
Normal file
26
tests/Unit/Request/CustomFieldTest.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
use Crater\Http\Requests\CustomFieldRequest;
|
||||
|
||||
test('custom field request validation rules', function () {
|
||||
$request = new CustomFieldRequest;
|
||||
|
||||
$this->assertEquals([
|
||||
'name' => 'required',
|
||||
'label' => 'required',
|
||||
'model_type' => 'required',
|
||||
'type' => 'required',
|
||||
'is_required' => 'required|boolean',
|
||||
'options' => 'array',
|
||||
'placeholder' => 'string|nullable',
|
||||
'order' => 'required',
|
||||
],
|
||||
$request->rules()
|
||||
);
|
||||
});
|
||||
|
||||
test('custom field request authorize', function () {
|
||||
$request = new CustomFieldRequest;
|
||||
|
||||
$this->assertTrue($request->authorize());
|
||||
});
|
||||
59
tests/Unit/Request/CustomerTest.php
Normal file
59
tests/Unit/Request/CustomerTest.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
use Crater\Http\Requests\CustomerRequest;
|
||||
|
||||
test('customer request post validation rules', function () {
|
||||
$request = new CustomerRequest;
|
||||
|
||||
$request->setMethod('POST');
|
||||
|
||||
$this->assertEquals([
|
||||
'name' => [
|
||||
'required'
|
||||
],
|
||||
'addresses.*.address_street_1' => [
|
||||
'max:255'
|
||||
],
|
||||
'addresses.*.address_street_2' => [
|
||||
'max:255'
|
||||
],
|
||||
'email' => [
|
||||
'email',
|
||||
'nullable',
|
||||
'unique:users,email',
|
||||
]
|
||||
],
|
||||
$request->rules()
|
||||
);
|
||||
});
|
||||
|
||||
test('customer request put validation rules', function () {
|
||||
$request = new CustomerRequest;
|
||||
|
||||
$request->setMethod('PUT');
|
||||
|
||||
$this->assertEquals([
|
||||
'name' => [
|
||||
'required'
|
||||
],
|
||||
'addresses.*.address_street_1' => [
|
||||
'max:255'
|
||||
],
|
||||
'addresses.*.address_street_2' => [
|
||||
'max:255'
|
||||
],
|
||||
'email' => [
|
||||
'email',
|
||||
'nullable',
|
||||
'unique:users,email',
|
||||
]
|
||||
],
|
||||
$request->rules()
|
||||
);
|
||||
});
|
||||
|
||||
test('customer request authorize', function () {
|
||||
$request = new CustomerRequest;
|
||||
|
||||
$this->assertTrue($request->authorize());
|
||||
});
|
||||
71
tests/Unit/Request/EstimateTest.php
Normal file
71
tests/Unit/Request/EstimateTest.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\Estimate;
|
||||
use Crater\Http\Requests\EstimatesRequest;
|
||||
use Crater\Rules\UniqueNumber;
|
||||
|
||||
test('estimate request validation rules', function () {
|
||||
$request = new EstimatesRequest;
|
||||
|
||||
$this->assertEquals([
|
||||
'estimate_date' => [
|
||||
'required'
|
||||
],
|
||||
'expiry_date' => [
|
||||
'required'
|
||||
],
|
||||
'user_id' => [
|
||||
'required'
|
||||
],
|
||||
'discount' => [
|
||||
'required'
|
||||
],
|
||||
'discount_val' => [
|
||||
'required'
|
||||
],
|
||||
'sub_total' => [
|
||||
'required'
|
||||
],
|
||||
'total' => [
|
||||
'required'
|
||||
],
|
||||
'tax' => [
|
||||
'required'
|
||||
],
|
||||
'estimate_template_id' => [
|
||||
'required'
|
||||
],
|
||||
'items' => [
|
||||
'required',
|
||||
'array'
|
||||
],
|
||||
'items.*.description' => [
|
||||
'max:255'
|
||||
],
|
||||
'items.*' => [
|
||||
'required',
|
||||
'max:255'
|
||||
],
|
||||
'items.*.name' => [
|
||||
'required'
|
||||
],
|
||||
'items.*.quantity' => [
|
||||
'required'
|
||||
],
|
||||
'items.*.price' => [
|
||||
'required'
|
||||
],
|
||||
'estimate_number' => [
|
||||
'required',
|
||||
new UniqueNumber(Estimate::class)
|
||||
],
|
||||
],
|
||||
$request->rules()
|
||||
);
|
||||
});
|
||||
|
||||
test('estimate request authorize', function () {
|
||||
$request = new EstimatesRequest;
|
||||
|
||||
$this->assertTrue($request->authorize());
|
||||
});
|
||||
24
tests/Unit/Request/ExpenseCategoryTest.php
Normal file
24
tests/Unit/Request/ExpenseCategoryTest.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
use Crater\Http\Requests\ExpenseCategoryRequest;
|
||||
|
||||
test('expense category request validation rules', function () {
|
||||
$request = new ExpenseCategoryRequest;
|
||||
|
||||
$this->assertEquals([
|
||||
'name' => [
|
||||
'required'
|
||||
],
|
||||
'description' => [
|
||||
'nullable'
|
||||
]
|
||||
],
|
||||
$request->rules()
|
||||
);
|
||||
});
|
||||
|
||||
test('expense category request authorize', function () {
|
||||
$request = new ExpenseCategoryRequest;
|
||||
|
||||
$this->assertTrue($request->authorize());
|
||||
});
|
||||
33
tests/Unit/Request/ExpenseTest.php
Normal file
33
tests/Unit/Request/ExpenseTest.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Crater\Http\Requests\ExpenseRequest;
|
||||
|
||||
test('expense request validation rules', function () {
|
||||
$request = new ExpenseRequest;
|
||||
|
||||
$this->assertEquals([
|
||||
'expense_date' => [
|
||||
'required'
|
||||
],
|
||||
'expense_category_id' => [
|
||||
'required'
|
||||
],
|
||||
'amount' => [
|
||||
'required'
|
||||
],
|
||||
'user_id' => [
|
||||
'nullable'
|
||||
],
|
||||
'notes' => [
|
||||
'nullable'
|
||||
]
|
||||
],
|
||||
$request->rules()
|
||||
);
|
||||
});
|
||||
|
||||
test('expense request authorize', function () {
|
||||
$request = new ExpenseRequest;
|
||||
|
||||
$this->assertTrue($request->authorize());
|
||||
});
|
||||
26
tests/Unit/Request/GetSettingsRequestTest.php
Normal file
26
tests/Unit/Request/GetSettingsRequestTest.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
use Crater\Http\Requests\GetSettingsRequest;
|
||||
|
||||
test('get settings request rules', function () {
|
||||
$request = new GetSettingsRequest;
|
||||
|
||||
$this->assertEquals([
|
||||
'settings' => [
|
||||
'required'
|
||||
],
|
||||
'settings.*' => [
|
||||
'required',
|
||||
'string'
|
||||
]
|
||||
],
|
||||
$request->rules()
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
test('get settings request authorize', function () {
|
||||
$request = new GetSettingsRequest;
|
||||
|
||||
$this->assertTrue($request->authorize());
|
||||
});
|
||||
71
tests/Unit/Request/InvoiceTest.php
Normal file
71
tests/Unit/Request/InvoiceTest.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
use Crater\Http\Requests\InvoicesRequest;
|
||||
use Crater\Models\Invoice;
|
||||
use Crater\Rules\UniqueNumber;
|
||||
|
||||
test('invoice request validation rules', function () {
|
||||
$request = new InvoicesRequest;
|
||||
|
||||
$this->assertEquals([
|
||||
'invoice_date' => [
|
||||
'required'
|
||||
],
|
||||
'due_date' => [
|
||||
'required'
|
||||
],
|
||||
'user_id' => [
|
||||
'required'
|
||||
],
|
||||
'discount' => [
|
||||
'required'
|
||||
],
|
||||
'discount_val' => [
|
||||
'required'
|
||||
],
|
||||
'sub_total' => [
|
||||
'required'
|
||||
],
|
||||
'total' => [
|
||||
'required'
|
||||
],
|
||||
'tax' => [
|
||||
'required'
|
||||
],
|
||||
'invoice_template_id' => [
|
||||
'required'
|
||||
],
|
||||
'items' => [
|
||||
'required',
|
||||
'array'
|
||||
],
|
||||
'items.*' => [
|
||||
'required',
|
||||
'max:255'
|
||||
],
|
||||
'items.*.description' => [
|
||||
'max:255'
|
||||
],
|
||||
'items.*.name' => [
|
||||
'required'
|
||||
],
|
||||
'items.*.quantity' => [
|
||||
'required'
|
||||
],
|
||||
'items.*.price' => [
|
||||
'required'
|
||||
],
|
||||
'invoice_number' => [
|
||||
'required',
|
||||
new UniqueNumber(Invoice::class)
|
||||
],
|
||||
],
|
||||
$request->rules()
|
||||
);
|
||||
});
|
||||
|
||||
test('invoices request authorize', function () {
|
||||
$request = new InvoicesRequest;
|
||||
|
||||
$this->assertTrue($request->authorize());
|
||||
});
|
||||
30
tests/Unit/Request/ItemTest.php
Normal file
30
tests/Unit/Request/ItemTest.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Crater\Http\Requests\ItemsRequest;
|
||||
|
||||
test('items request validation rules', function () {
|
||||
$request = new ItemsRequest;
|
||||
|
||||
$this->assertEquals([
|
||||
'name' => [
|
||||
'required'
|
||||
],
|
||||
'price' => [
|
||||
'required'
|
||||
],
|
||||
'unit_id' => [
|
||||
'nullable'
|
||||
],
|
||||
'description' => [
|
||||
'nullable'
|
||||
]
|
||||
],
|
||||
$request->rules()
|
||||
);
|
||||
});
|
||||
|
||||
test('item request authorize', function () {
|
||||
$request = new ItemsRequest;
|
||||
|
||||
$this->assertTrue($request->authorize());
|
||||
});
|
||||
22
tests/Unit/Request/PaymentMethodTest.php
Normal file
22
tests/Unit/Request/PaymentMethodTest.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
use Crater\Http\Requests\PaymentMethodRequest;
|
||||
|
||||
test('payment method request validation rules', function () {
|
||||
$request = new PaymentMethodRequest;
|
||||
|
||||
$this->assertEquals([
|
||||
'name' => [
|
||||
'required',
|
||||
'unique:payment_methods,name'
|
||||
]
|
||||
],
|
||||
$request->rules()
|
||||
);
|
||||
});
|
||||
|
||||
test('payment method request authorize', function () {
|
||||
$request = new PaymentMethodRequest;
|
||||
|
||||
$this->assertTrue($request->authorize());
|
||||
});
|
||||
42
tests/Unit/Request/PaymentTest.php
Normal file
42
tests/Unit/Request/PaymentTest.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
use Crater\Http\Requests\PaymentRequest;
|
||||
use Crater\Models\Payment;
|
||||
use Crater\Rules\UniqueNumber;
|
||||
|
||||
test('payment request validation rules', function () {
|
||||
$request = new PaymentRequest;
|
||||
|
||||
$this->assertEquals([
|
||||
'payment_date' => [
|
||||
'required'
|
||||
],
|
||||
'user_id' => [
|
||||
'required'
|
||||
],
|
||||
'amount' => [
|
||||
'required'
|
||||
],
|
||||
'payment_number' => [
|
||||
'required',
|
||||
new UniqueNumber(Payment::class)
|
||||
],
|
||||
'invoice_id' => [
|
||||
'nullable',
|
||||
],
|
||||
'payment_method_id' => [
|
||||
'nullable',
|
||||
],
|
||||
'notes' => [
|
||||
'nullable',
|
||||
],
|
||||
],
|
||||
$request->rules()
|
||||
);
|
||||
});
|
||||
|
||||
test('payment request authorize', function () {
|
||||
$request = new PaymentRequest;
|
||||
|
||||
$this->assertTrue($request->authorize());
|
||||
});
|
||||
34
tests/Unit/Request/TaxTypeTest.php
Normal file
34
tests/Unit/Request/TaxTypeTest.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Crater\Http\Requests\TaxTypeRequest;
|
||||
|
||||
test('tax type request validation rules', function () {
|
||||
$request = new TaxTypeRequest;
|
||||
|
||||
$this->assertEquals([
|
||||
'name' => [
|
||||
'required'
|
||||
],
|
||||
'percent' => [
|
||||
'required'
|
||||
],
|
||||
'description' => [
|
||||
'nullable'
|
||||
],
|
||||
'compound_tax' => [
|
||||
'nullable'
|
||||
],
|
||||
'collective_tax' => [
|
||||
'nullable'
|
||||
]
|
||||
],
|
||||
$request->rules()
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
test('tax type request authorize', function () {
|
||||
$request = new TaxTypeRequest;
|
||||
|
||||
$this->assertTrue($request->authorize());
|
||||
});
|
||||
22
tests/Unit/Request/UnitTest.php
Normal file
22
tests/Unit/Request/UnitTest.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
use Crater\Http\Requests\UnitRequest;
|
||||
|
||||
test('unit request validation rules', function () {
|
||||
$request = new UnitRequest;
|
||||
|
||||
$this->assertEquals([
|
||||
'name' => [
|
||||
'required',
|
||||
'unique:units,name'
|
||||
]
|
||||
],
|
||||
$request->rules()
|
||||
);
|
||||
});
|
||||
|
||||
test('unit request authorize', function () {
|
||||
$request = new UnitRequest;
|
||||
|
||||
$this->assertTrue($request->authorize());
|
||||
});
|
||||
25
tests/Unit/Request/UpdateSettingsRequestTest.php
Normal file
25
tests/Unit/Request/UpdateSettingsRequestTest.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
use Crater\Http\Requests\UpdateSettingsRequest;
|
||||
|
||||
test('update settings request rules', function () {
|
||||
$request = new UpdateSettingsRequest;
|
||||
|
||||
$this->assertEquals([
|
||||
'settings' => [
|
||||
'required'
|
||||
],
|
||||
'settings.*' => [
|
||||
'required'
|
||||
]
|
||||
],
|
||||
$request->rules()
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
test('update settings request authorize', function () {
|
||||
$request = new UpdateSettingsRequest;
|
||||
|
||||
$this->assertTrue($request->authorize());
|
||||
});
|
||||
45
tests/Unit/SettingTest.php
Normal file
45
tests/Unit/SettingTest.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\Setting;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use function Pest\Faker\faker;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('set setting', function () {
|
||||
$key = faker()->name;
|
||||
|
||||
$value = faker()->word;
|
||||
|
||||
Setting::setSetting($key, $value);
|
||||
|
||||
$response = Setting::getSetting($key);
|
||||
|
||||
$this->assertEquals($value, $response);
|
||||
});
|
||||
|
||||
test('get setting', function () {
|
||||
$key = faker()->name;
|
||||
|
||||
$value = faker()->word;
|
||||
|
||||
Setting::setSetting($key, $value);
|
||||
|
||||
$response = Setting::getSetting($key);
|
||||
|
||||
$this->assertEquals($value, $response);
|
||||
});
|
||||
65
tests/Unit/TaxTest.php
Normal file
65
tests/Unit/TaxTest.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\Tax;
|
||||
use Crater\Models\Estimate;
|
||||
use Crater\Models\EstimateItem;
|
||||
use Crater\Models\Invoice;
|
||||
use Crater\Models\InvoiceItem;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('tax belongs to tax type', function () {
|
||||
$tax = Tax::factory()->create();
|
||||
|
||||
$this->assertTrue($tax->taxType->exists());
|
||||
});
|
||||
|
||||
test('tax belongs to invoice', function () {
|
||||
$tax = Tax::factory()->forInvoice()->create();
|
||||
|
||||
$this->assertTrue($tax->invoice()->exists());
|
||||
});
|
||||
|
||||
test('tax belongs to estimate', function () {
|
||||
$tax = Tax::factory()->forEstimate()->create();
|
||||
|
||||
$this->assertTrue($tax->estimate()->exists());
|
||||
});
|
||||
|
||||
test('tax belongs to invoice item', function () {
|
||||
$tax = Tax::factory()->for(InvoiceItem::factory()->state([
|
||||
'invoice_id' => Invoice::factory()
|
||||
]))->create();
|
||||
|
||||
$this->assertTrue($tax->invoiceItem()->exists());
|
||||
});
|
||||
|
||||
test('tax belongs to estimate item', function () {
|
||||
$tax = Tax::factory()->for(EstimateItem::factory()->state([
|
||||
'estimate_id' => Estimate::factory()
|
||||
]))->create();
|
||||
|
||||
$this->assertTrue($tax->estimateItem()->exists());
|
||||
});
|
||||
|
||||
test('tax belongs to item', function () {
|
||||
$tax = Tax::factory()->forItem()->create();
|
||||
|
||||
$this->assertTrue($tax->item()->exists());
|
||||
});
|
||||
28
tests/Unit/TaxTypeTest.php
Normal file
28
tests/Unit/TaxTypeTest.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\TaxType;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('tax type has many taxes', function () {
|
||||
$taxtype = TaxType::factory()->hasTaxes(4)->create();
|
||||
|
||||
$this->assertCount(4, $taxtype->taxes);
|
||||
$this->assertTrue($taxtype->taxes()->exists());
|
||||
});
|
||||
35
tests/Unit/UnitTest.php
Normal file
35
tests/Unit/UnitTest.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\Unit;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'UnitSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('unit has many items', function () {
|
||||
|
||||
$unit = Unit::factory()->hasItems(5)->create();
|
||||
|
||||
$this->assertCount(5, $unit->items);
|
||||
$this->assertTrue($unit->items()->exists());
|
||||
});
|
||||
|
||||
test('unit belongs to company', function () {
|
||||
$unit = Unit::factory()->create();
|
||||
|
||||
$this->assertTrue($unit->company()->exists());
|
||||
});
|
||||
126
tests/Unit/UserTest.php
Normal file
126
tests/Unit/UserTest.php
Normal file
@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
use Crater\Models\User;
|
||||
use Crater\Models\Address;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
|
||||
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
|
||||
|
||||
$user = User::where('role', 'super admin')->first();
|
||||
$this->withHeaders([
|
||||
'company' => $user->company_id,
|
||||
]);
|
||||
Sanctum::actingAs(
|
||||
$user,
|
||||
['*']
|
||||
);
|
||||
});
|
||||
|
||||
test('user has many estimates', function () {
|
||||
$user = User::factory()->hasEstimates(5)->create();
|
||||
|
||||
$this->assertCount(5, $user->estimates);
|
||||
$this->assertTrue($user->estimates()->exists());
|
||||
});
|
||||
|
||||
test('user belongs to currency', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->assertTrue($user->currency()->exists());
|
||||
});
|
||||
|
||||
test('user belongs to company', function () {
|
||||
$user = User::factory()->forCompany()->create();
|
||||
|
||||
$this->assertTrue($user->company()->exists());
|
||||
});
|
||||
|
||||
test('user has many addresses', function () {
|
||||
$user = User::factory()->hasAddresses(2)->create();
|
||||
|
||||
$this->assertTrue($user->addresses()->exists());
|
||||
});
|
||||
|
||||
it('user has many expenses', function () {
|
||||
$user = User::factory()->hasExpenses(5)->create();
|
||||
|
||||
$this->assertCount(5, $user->expenses);
|
||||
$this->assertTrue($user->expenses()->exists());
|
||||
});
|
||||
|
||||
it('user has one billing address', function () {
|
||||
$user = User::factory()->has(Address::factory()->state([
|
||||
'type' => Address::BILLING_TYPE,
|
||||
]))->create();
|
||||
|
||||
$this->assertTrue($user->billingAddress()->exists());
|
||||
});
|
||||
|
||||
it('user has one shipping address', function () {
|
||||
$user = User::factory()->has(Address::factory()->state([
|
||||
'type' => Address::SHIPPING_TYPE,
|
||||
]))->create();
|
||||
|
||||
$this->assertTrue($user->shippingAddress()->exists());
|
||||
});
|
||||
|
||||
test('user has many payments', function () {
|
||||
$user = User::factory()->hasPayments(5)->create();
|
||||
|
||||
$this->assertCount(5, $user->payments);
|
||||
$this->assertTrue($user->payments()->exists());
|
||||
});
|
||||
|
||||
test('user has many invoices', function () {
|
||||
$user = User::factory()->hasInvoices(5)->create();
|
||||
|
||||
$this->assertCount(5, $user->invoices);
|
||||
$this->assertTrue($user->invoices()->exists());
|
||||
});
|
||||
|
||||
test('create customer', function () {
|
||||
$customer = User::factory()->raw([
|
||||
'role' => 'customer'
|
||||
]);
|
||||
|
||||
$request = new Request;
|
||||
|
||||
$request->replace($customer);
|
||||
|
||||
$response = User::createCustomer($request);
|
||||
|
||||
$this->assertDatabaseHas('users', [
|
||||
'name' => $customer['name'],
|
||||
'email' => $customer['email'],
|
||||
'role' => $customer['role']
|
||||
]);
|
||||
});
|
||||
|
||||
test('update customer', function () {
|
||||
$customer = User::factory()->create([
|
||||
'role' => 'customer'
|
||||
]);
|
||||
|
||||
$customer2 = User::factory()->raw([
|
||||
'role' => 'customer'
|
||||
]);
|
||||
|
||||
$request = new Request;
|
||||
|
||||
$request->replace($customer2);
|
||||
|
||||
$updateCustomer = User::updateCustomer($request, $customer);
|
||||
|
||||
$newCustomer = User::find($customer->id);
|
||||
|
||||
$this->assertDatabaseHas('users', [
|
||||
'id' => $customer->id,
|
||||
'name' => $customer2['name'],
|
||||
'email' => $customer2['email'],
|
||||
'role' => $customer2['role']
|
||||
]);
|
||||
});
|
||||
Reference in New Issue
Block a user