build version 400

This commit is contained in:
Mohit Panjwani
2020-12-02 17:54:08 +05:30
parent 326508e567
commit 89ee58590c
963 changed files with 62887 additions and 48868 deletions

View File

@ -0,0 +1,56 @@
<?php
use Crater\Models\User;
use Crater\Models\FileDisk;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use Illuminate\Support\Facades\Queue;
use Crater\Jobs\CreateBackupJob;
use function Pest\Laravel\{postJson, getJson};
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('get backups', function () {
$disk = FileDisk::factory()->create([
'set_as_default' => true
]);
$response = getJson("/api/v1/backups?disk={$disk->driver}&&file_disk_id={$disk->id}");
$response->assertOk();
});
test('create backup', function () {
Queue::fake();
$disk = FileDisk::factory()->create();
$data = [
'option' => 'full',
'file_disk_id' => $disk->id,
];
$response = postJson("/api/v1/backups", $data);
Queue::assertPushed(CreateBackupJob::class);
$response = getJson("/api/v1/backups?disk={$disk->driver}&&file_disk_id={$disk->id}");
$response->assertStatus(200)->assertJson([
"disks" => [
"local"
]
]);
});

View File

@ -1,220 +1,144 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Crater\User;
use Laravel\Passport\Passport;
use SettingsSeeder;
use Crater\Http\Controllers\V1\Settings\CompanyController;
use Crater\Http\Requests\CompanyRequest;
use Crater\Http\Requests\ProfileRequest;
use Crater\Models\User;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use function Pest\Laravel\{postJson, getJson, putJson};
class CompanySettingTest extends TestCase
{
use RefreshDatabase;
beforeEach(function () {
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
protected $user;
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Sanctum::actingAs(
$user,
['*']
);
});
public function setUp(): void
{
parent::setUp();
$this->seed();
$this->seed(SettingsSeeder::class);
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
test('get profile', function () {
$response = getJson('api/v1/me');
$response->assertOk();
});
test('update profile using a form request', function () {
$this->assertActionUsesFormRequest(
CompanyController::class,
'updateProfile',
ProfileRequest::class
);
});
test('update profile', function () {
$user = [
'name' => 'John Doe',
'password' => 'admin@123',
'email' => 'admin@crater.in'
];
$response = putJson('api/v1/me', $user);
$response->assertOk();
$this->assertDatabaseHas('users', [
'name' => $user['name'],
'email' => $user['email']
]);
});
test('update company using a form request', function () {
$this->assertActionUsesFormRequest(
CompanyController::class,
'updateCompany',
CompanyRequest::class
);
});
test('update company', function () {
$company = [
'name' => 'XYZ',
'country_id' => 2,
'state' => 'city',
'city' => 'state',
'address_street_1' => 'test1',
'address_street_2' => 'test2',
'phone' => '1234567890',
'zip' => '112233'
];
$response = putJson('api/v1/company', $company);
$response->assertOk();
$this->assertDatabaseHas('companies', [
'name' => $company['name']
]);
$this->assertDatabaseHas('addresses', [
'country_id' => $company['country_id'],
'state' => $company['state'],
'city' => $company['city'],
'address_street_1' => $company['address_street_1'],
'address_street_2' => $company['address_street_2'],
'phone' => $company['phone'],
'zip' => $company['zip']
]);
});
test('update settings', function () {
$settings = [
'currency'=> 1,
'time_zone' => 'Asia/Kolkata',
'language' => 'en',
'fiscal_year' => '1-12',
'carbon_date_format' => 'Y/m/d',
'moment_date_format' => 'YYYY/MM/DD',
'notification_email' => 'noreply@crater.in',
'notify_invoice_viewed' => 'YES',
'notify_estimate_viewed' => 'YES',
'tax_per_item' => 'YES',
'discount_per_item' => 'YES'
];
$response = postJson('/api/v1/company/settings', ['settings' => $settings]);
$response->assertOk()
->assertJson([
'success' => true
]);
foreach ($settings as $key => $value) {
$this->assertDatabaseHas('company_settings', [
'option' => $key,
'value' => $value
]);
Passport::actingAs(
$user,
['*']
);
}
});
/** @test */
public function testGetProfile()
{
$response = $this->json('GET', 'api/settings/profile');
test('get notification email settings', function () {
$data['settings'] = [
'currency',
'time_zone',
'language',
'fiscal_year',
'carbon_date_format',
'moment_date_format',
'notification_email',
'notify_invoice_viewed',
'notify_estimate_viewed',
'tax_per_item',
'discount_per_item'
];
$response->assertOk();
}
$response = getJson('/api/v1/company/settings?'.http_build_query($data));
/** @test */
public function testUpdateProfile()
{
$user = [
'name' => 'John Doe',
'password' => 'admin@123',
'email' => 'admin@crater.in'
];
$response = $this->json('PUT', 'api/settings/profile', $user);
$response->assertOk();
}
/** @test */
public function testGetUpdateCompanyDetails()
{
$response = $this->json('GET', 'api/settings/company');
$response->assertOk();
}
/** @test */
public function testUpdateCompany()
{
$company = [
'name' => 'XYZ',
'country_id' => 2,
'state' => 'city',
'city' => 'state',
'address_street_1' => 'test1',
'address_street_2' => 'test2',
'phone' => '1234567890',
'zip' => '112233'
];
$response = $this->json('POST', 'api/settings/company', $company);
$response->assertOk();
$company2 = $response->decodeResponseJson()['user']['company'];
$address2 = $response->decodeResponseJson()['user']['addresses'][0];
$this->assertEquals($company['name'], $company2['name']);
$this->assertEquals($company['country_id'], $address2['country_id']);
$this->assertEquals($company['state'], $address2['state']);
$this->assertEquals($company['city'], $address2['city']);
$this->assertEquals($company['address_street_1'], $address2['address_street_1']);
$this->assertEquals($company['address_street_2'], $address2['address_street_2']);
$this->assertEquals($company['phone'], $address2['phone']);
$this->assertEquals($company['zip'], $address2['zip']);
}
/** @test */
public function testGetSettings()
{
$response = $this->json('GET', 'api/settings/general');
$response->assertOk();
}
/** @test */
public function testUpdateSettings()
{
$settings = [
'currency' => 1,
'time_zone' => 'Asia/Kolkata',
'language' => 'en',
'fiscal_year' => '1-12',
'carbon_date_format' => 'Y/m/d',
'moment_date_format' => 'YYYY/MM/DD'
];
$response = $this->json('PUT', 'api/settings/general', $settings);
$response->assertOk();
}
/** @test */
public function testUpdateNotificationEmailSettings()
{
$settings = [
'key' => 'notification_email',
'value' => 'noreply@crater.in'
];
$response = $this->json('PUT', 'api/settings/update-setting', $settings);
$response->assertOk();
}
/** @test */
public function testGetNotificationEmailSettings()
{
$response = $this->json('GET', 'api/settings/get-setting?key=notification_email');
$response->assertOk();
}
/** @test */
public function testUpdateNotificationInvoiceViewedSettings()
{
$settings = [
'key' => 'notify_invoice_viewed',
'value' => 'YES'
];
$response = $this->json('PUT', 'api/settings/update-setting', $settings);
$response->assertOk();
}
/** @test */
public function testGetNotificationInvoiceViewedSettings()
{
$response = $this->json('GET', 'api/settings/get-setting?key=notify_invoice_viewed');
$response->assertOk();
}
/** @test */
public function testUpdateNotificationEstimateViewedSettings()
{
$settings = [
'key' => 'notify_estimate_viewed',
'value' => 'YES'
];
$response = $this->json('PUT', 'api/settings/update-setting', $settings);
$response->assertOk();
}
/** @test */
public function testGetNotificationEstimateViewedSettings()
{
$response = $this->json('GET', 'api/settings/get-setting?key=notify_estimate_viewed');
$response->assertOk();
}
/** @test */
public function testUpdateTaxPerItemSetting()
{
$settings = [
'key' => 'tax_per_item',
'value' => 'YES'
];
$response = $this->json('PUT', 'api/settings/update-setting', $settings);
$response->assertOk();
}
/** @test */
public function testGetTaxPerItemSetting()
{
$response = $this->json('GET', 'api/settings/get-setting?key=tax_per_item');
$response->assertOk();
}
/** @test */
public function testUpdateDiscountPerItemSetting()
{
$settings = [
'key' => 'discount_per_item',
'value' => 'YES'
];
$response = $this->json('PUT', 'api/settings/update-setting', $settings);
$response->assertOk();
}
/** @test */
public function testGetDiscountPerItemSetting()
{
$response = $this->json('GET', 'api/settings/get-setting?key=discount_per_item');
$response->assertOk();
}
}
$response->assertOk();
});

View File

@ -0,0 +1,99 @@
<?php
use Crater\Models\User;
use Crater\Models\CustomField;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use Crater\Http\Requests\CustomFieldRequest;
use Crater\Http\Controllers\V1\CustomField\CustomFieldsController;
use function Pest\Laravel\{postJson, putJson, getJson, deleteJson};
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('get custom fields', function () {
$response = getJson('api/v1/custom-fields?page=1');
$response->assertOk();
});
test('create custom field', function () {
$data = CustomField::factory()->raw();
postJson('api/v1/custom-fields', $data)
->assertStatus(200)
->assertJson([
'success' => true
]);
$this->assertDatabaseHas('custom_fields', [
'name' => $data['name'],
'label' => $data['label'],
'type' => $data['type'],
'model_type' => $data['model_type'],
'is_required' => $data['is_required']
]);
});
test('store validates using a form request', function () {
$this->assertActionUsesFormRequest(
CustomFieldsController::class,
'store',
CustomFieldRequest::class
);
});
test('update custom field', function () {
$customField = CustomField::factory()->create();
$newCustomField = CustomField::factory()->raw([
'is_required' => false
]);
putJson('api/v1/custom-fields/' . $customField->id, $newCustomField)
->assertStatus(200)
->assertJson([
'success' => true
]);
$this->assertDatabaseHas('custom_fields', [
'id' => $customField->id,
'name' => $newCustomField['name'],
'label' => $newCustomField['label'],
'type' => $newCustomField['type'],
'model_type' => $newCustomField['model_type'],
]);
});
test('update validates using a form request', function () {
$this->assertActionUsesFormRequest(
CustomFieldsController::class,
'update',
CustomFieldRequest::class
);
});
test('delete custom field', function () {
$customField = CustomField::factory()->create();
$response = deleteJson('api/v1/custom-fields/' . $customField->id);
$response
->assertOk()
->assertJson([
'success' => true
]);
$this->assertDeleted($customField);
});

View File

@ -1,181 +1,161 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Crater\User;
use Laravel\Passport\Passport;
use SettingsSeeder;
use Crater\Models\User;
use Crater\Models\Invoice;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use Crater\Http\Requests\CustomerRequest;
use Crater\Http\Controllers\V1\Customer\CustomersController;
use function Pest\Laravel\{postJson, putJson, getJson};
class CustomerTest extends TestCase
{
use RefreshDatabase;
beforeEach(function () {
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
protected $user;
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Sanctum::actingAs(
$user,
['*']
);
});
public function setUp(): void
{
parent::setUp();
$this->seed();
$this->seed(SettingsSeeder::class);
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
test('get customers', function () {
$response = getJson('api/v1/customers?page=1');
$response->assertOk();
});
test('customer stats', function () {
$customer = User::factory()->create([
'role' => 'customer'
]);
$invoice = Invoice::factory()->create([
'user_id' => $customer->id
]);
$response = getJson("api/v1/customers/{$customer->id}/stats");
$response->assertStatus(200);
});
test('create customer', function () {
$customer = User::factory()->raw([
'password' => 'secret',
'role' => 'customer'
]);
$response = postJson('api/v1/customers', $customer);
$this->assertDatabaseHas('users', [
'name' => $customer['name'],
'email' => $customer['email'],
'role' => $customer['role'],
'company_id' => $customer['company_id']
]);
$response
->assertOk()
->assertJson([
'success' => true
]);
Passport::actingAs(
$user,
['*']
);
}
});
/** @test */
public function testGetCustomers()
{
$response = $this->json('GET', 'api/customers?page=1');
test('store validates using a form request', function () {
$this->assertActionUsesFormRequest(
CustomersController::class,
'store',
CustomerRequest::class
);
});
$response->assertOk();
}
test('get customer', function () {
$customer = User::factory()->create([
'role' => 'customer'
]);
/** @test */
public function testCreateCustomer()
{
$customer = factory(User::class)->raw([
'password' => 'secret',
'role' => 'customer'
$response = getJson("api/v1/customers/{$customer->id}");
$this->assertDatabaseHas('users', [
'id' => $customer->id,
'name' => $customer['name'],
'email' => $customer['email'],
'role' => $customer['role'],
'company_id' => $customer['company_id']
]);
$response->assertOk();
});
test('update customer', function () {
$customer = User::factory()->create([
'role' => 'customer'
]);
$customer1 = User::factory()->raw([
'role' => 'customer',
'name' => 'new name'
]);
$response = putJson('api/v1/customers/' . $customer->id, $customer1);
$this->assertDatabaseHas('users', [
'id' => $customer->id,
'name' => $customer1['name'],
'email' => $customer1['email'],
'role' => $customer1['role'],
'company_id' => $customer1['company_id']
]);
$response
->assertOk()
->assertJson([
'success' => true
]);
});
$response = $this->json('POST', 'api/customers', $customer);
test('update validates using a form request', function () {
$this->assertActionUsesFormRequest(
CustomersController::class,
'update',
CustomerRequest::class
);
});
$newCustomer = $response->decodeResponseJson()['customer'];
test('search customers', function () {
$filters = [
'page' => 1,
'limit' => 15,
'search' => 'doe',
'email' => '.com'
];
$this->assertEquals($customer['name'], $newCustomer['name']);
$this->assertEquals($customer['email'], $newCustomer['email']);
$this->assertEquals($customer['role'], $newCustomer['role']);
$this->assertEquals($customer['phone'], $newCustomer['phone']);
$this->assertEquals($customer['company_name'], $newCustomer['company_name']);
$this->assertEquals($customer['contact_name'], $newCustomer['contact_name']);
$this->assertEquals($customer['website'], $newCustomer['website']);
$this->assertEquals($customer['enable_portal'], $newCustomer['enable_portal']);
$queryString = http_build_query($filters, '', '&');
$response
->assertOk()
->assertJson([
'success' => true
]);
}
$response = getJson('api/v1/customers?' . $queryString);
/** @test */
public function testCustomerNameRequired()
{
$customer = factory(User::class)->raw([
'name' => '',
'password' => 'secret',
'role' => 'customer'
$response->assertOk();
});
test('delete multiple customer', function () {
$customers = User::factory()->count(4)->create([
'role' => 'customer'
]);
$ids = $customers->pluck('id');
$data = [
'ids' => $ids
];
$response = postJson('api/v1/customers/delete', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
$response = $this->json('POST', 'api/customers', $customer);
$response->assertStatus(422)->assertJsonValidationErrors(['name']);
}
/** @test */
public function testGetUpdateCustomerData()
{
$customer = factory(User::class)->create(['role' => 'customer']);
$response = $this->json('GET', 'api/customers/'.$customer->id.'/edit');
$newCustomer = $response->decodeResponseJson()['customer'];
$this->assertEquals($customer->name, $newCustomer['name']);
$this->assertEquals($customer->email, $newCustomer['email']);
$this->assertEquals($customer->role, $newCustomer['role']);
$this->assertEquals($customer->phone, $newCustomer['phone']);
$this->assertEquals($customer->company_name, $newCustomer['company_name']);
$this->assertEquals($customer->contact_name, $newCustomer['contact_name']);
$this->assertEquals($customer->website, $newCustomer['website']);
$this->assertEquals($customer->enable_portal, $newCustomer['enable_portal']);
$response->assertOk();
}
/** @test */
public function testUpdateCustomer()
{
$customer = factory(User::class)->create(['role' => 'customer']);
$customer2 = factory(User::class)->raw(['role' => 'customer']);
$response = $this->json('PUT', 'api/customers/'.$customer->id, $customer2);
$newCustomer = $response->decodeResponseJson()['customer'];
$this->assertEquals($customer2['name'], $newCustomer['name']);
$this->assertEquals($customer2['email'], $newCustomer['email']);
$this->assertEquals($customer2['role'], $newCustomer['role']);
$this->assertEquals($customer2['phone'], $newCustomer['phone']);
$this->assertEquals($customer2['company_name'], $newCustomer['company_name']);
$this->assertEquals($customer2['contact_name'], $newCustomer['contact_name']);
$this->assertEquals($customer2['website'], $newCustomer['website']);
$this->assertEquals($customer2['enable_portal'], $newCustomer['enable_portal']);
$response
->assertOk()
->assertJson([
'success' => true
]);
}
/** @test */
public function testDeleteCustomer()
{
$customer = factory(User::class)->create(['role' => 'customer']);
$response = $this->json('DELETE', 'api/customers/'.$customer->id);
$response
->assertOk()
->assertJson([
'success' => true
]);
$deletedCustomer = User::find($customer->id);
$this->assertNull($deletedCustomer);
}
/** @test */
public function testSearchCustomers()
{
$filters = [
'page' => 1,
'limit' => 15,
'search' => 'doe',
'email' => '.com'
];
$queryString = http_build_query($filters, '', '&');
$response = $this->json('GET', 'api/customers?'.$queryString);
$response->assertOk();
}
/** @test */
public function testDeleteMultipleCustomer()
{
$customers = factory(User::class, 3)->create(['role' => 'customer']);
$ids = $customers->pluck('id');
$data = [
'id' => $ids
];
$response = $this->json('POST', 'api/customers/delete', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
}
}
});

View File

@ -1,47 +1,23 @@
<?php
namespace Tests\Feature;
use Crater\Models\User;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use function Pest\Laravel\getJson;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Crater\User;
use Laravel\Passport\Passport;
use SettingsSeeder;
beforeEach(function () {
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
class DashboardTest extends TestCase
{
use RefreshDatabase;
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Sanctum::actingAs(
$user,
['*']
);
});
protected $user;
getJson('api/v1/dashboard')->assertOk();
public function setUp(): void
{
parent::setUp();
$this->seed();
$this->seed(SettingsSeeder::class);
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Passport::actingAs(
$user,
['*']
);
}
/** @test */
public function testDashboard()
{
$response = $this->json('GET', 'api/dashboard');
$response->assertOk();
}
/** @test */
public function testPieChartData()
{
$response = $this->json('GET', 'api/dashboard/expense/chart');
$response->assertOk();
}
}
getJson('api/v1/search?name=ab')->assertOk();

View File

@ -1,626 +1,263 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Crater\User;
use Crater\Estimate;
use Crater\Invoice;
use Crater\EstimateItem;
use Crater\Tax;
use Laravel\Passport\Passport;
use SettingsSeeder;
use Crater\Models\User;
use Crater\Models\Estimate;
use Crater\Models\EstimateItem;
use Crater\Models\Tax;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use Crater\Http\Requests\EstimatesRequest;
use Crater\Http\Controllers\V1\Estimate\EstimatesController;
use Crater\Http\Controllers\V1\Estimate\SendEstimateController;
use Crater\Http\Requests\DeleteEstimatesRequest;
use Crater\Http\Requests\SendEstimatesRequest;
use function Pest\Laravel\{postJson, putJson, getJson};
class EstimateTest extends TestCase
{
use RefreshDatabase;
beforeEach(function () {
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
protected $user;
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Sanctum::actingAs(
$user,
['*']
);
});
public function setUp(): void
{
parent::setUp();
$this->seed();
$this->seed(SettingsSeeder::class);
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Passport::actingAs(
$user,
['*']
);
}
test('get estimates', function () {
$response = getJson('api/v1/estimates?page=1');
/** @test */
public function testGetEstimates()
{
$response = $this->json('GET', 'api/estimates?page=1');
$response->assertOk();
});
$response->assertOk();
}
test('create estimate', function () {
$estimate = Estimate::factory()->raw([
'items' => [
EstimateItem::factory()->raw()
],
'taxes' => [
Tax::factory()->raw()
],
]);
/** @test */
public function testGetCreateEstimateData()
{
$response = $this->json('GET', 'api/estimates/create');
postJson('api/v1/estimates', $estimate)
->assertStatus(200);
$response->assertOk();
}
$this->assertDatabaseHas('estimates', [
'estimate_template_id' => $estimate['estimate_template_id'],
'estimate_number' => $estimate['estimate_number'],
'discount_type' => $estimate['discount_type'],
'discount_val' => $estimate['discount_val'],
'sub_total' => $estimate['sub_total'],
'discount' => $estimate['discount'],
'user_id' => $estimate['user_id'],
'total' => $estimate['total'],
'notes' => $estimate['notes'],
'tax' => $estimate['tax'],
]);
});
/** @test */
public function testCreateEstimate()
{
$estimate = factory(Estimate::class)->raw([
'items' => [
factory(EstimateItem::class)->raw()
],
'taxes' => [
factory(Tax::class)->raw()
]
]);
test('store validates using a form request', function () {
$this->assertActionUsesFormRequest(
EstimatesController::class,
'store',
EstimatesRequest::class
);
});
$response = $this->json('POST', 'api/estimates', $estimate);
$newEstimate = $response->decodeResponseJson()['estimate'];
$response->assertOk();
$this->assertEquals($estimate['estimate_number'], $newEstimate['estimate_number']);
$this->assertEquals($estimate['user_id'], $newEstimate['user_id']);
$this->assertEquals($estimate['estimate_template_id'], $newEstimate['estimate_template_id']);
$this->assertEquals($estimate['sub_total'], $newEstimate['sub_total']);
$this->assertEquals($estimate['total'], $newEstimate['total']);
$this->assertEquals($estimate['discount'], $newEstimate['discount']);
$this->assertEquals($estimate['discount_type'], $newEstimate['discount_type']);
$this->assertEquals($estimate['discount_val'], $newEstimate['discount_val']);
$this->assertEquals($estimate['tax'], $newEstimate['tax']);
$this->assertEquals($estimate['notes'], $newEstimate['notes']);
$this->assertEquals($estimate['items'][0]['name'], $newEstimate['items'][0]['name']);
$this->assertEquals($estimate['items'][0]['description'], $newEstimate['items'][0]['description']);
$this->assertEquals($estimate['items'][0]['price'], $newEstimate['items'][0]['price']);
$this->assertEquals($estimate['items'][0]['quantity'], $newEstimate['items'][0]['quantity']);
$this->assertEquals($estimate['items'][0]['discount_type'], $newEstimate['items'][0]['discount_type']);
$this->assertEquals($estimate['items'][0]['discount'], $newEstimate['items'][0]['discount']);
$this->assertEquals($estimate['items'][0]['total'], $newEstimate['items'][0]['total']);
$this->assertEquals($estimate['taxes'][0]['tax_type_id'], $newEstimate['taxes'][0]['tax_type_id']);
$this->assertEquals($estimate['taxes'][0]['percent'], $newEstimate['taxes'][0]['percent']);
$this->assertEquals($estimate['taxes'][0]['amount'], $newEstimate['taxes'][0]['amount']);
$this->assertEquals($estimate['taxes'][0]['name'], $newEstimate['taxes'][0]['name']);
$this->assertEquals($newEstimate['id'], $newEstimate['taxes'][0]['estimate_id']);
}
/** @test */
public function testCreateEstimateEstimateDateRequired()
{
$estimate = factory(Estimate::class)->raw([
'estimate_date' => '',
'items' => [
factory(EstimateItem::class)->raw()
]
]);
$response = $this->json('POST', 'api/estimates', $estimate);
$response->assertStatus(422)->assertJsonValidationErrors(['estimate_date']);
}
/** @test */
public function testCreateEstimateExpiryDateRequired()
{
$estimate = factory(Estimate::class)->raw([
'expiry_date' => '',
'items' => [
factory(EstimateItem::class)->raw()
]
]);
$response = $this->json('POST', 'api/estimates', $estimate);
$response->assertStatus(422)->assertJsonValidationErrors(['expiry_date']);
}
/** @test */
public function testCreateEstimateRequiresEstimateNumber()
{
$estimate = factory(Estimate::class)->raw([
'estimate_number' => '',
'items' => [
factory(EstimateItem::class)->raw()
]
]);
$response = $this->json('POST', 'api/estimates', $estimate);
$response->assertStatus(422)->assertJsonValidationErrors(['estimate_number']);
}
/** @test */
public function testCreateEstimateRequiresUser()
{
$estimate = factory(Estimate::class)->raw([
'user_id' => '',
'items' => [
factory(EstimateItem::class)->raw()
]
]);
$response = $this->json('POST', 'api/estimates', $estimate);
$response->assertStatus(422)->assertJsonValidationErrors(['user_id']);
}
/** @test */
public function testCreateEstimateRequiresDiscount()
{
$estimate = factory(Estimate::class)->raw([
'discount' => '',
'items' => [
factory(EstimateItem::class)->raw()
]
]);
$response = $this->json('POST', 'api/estimates', $estimate);
$response->assertStatus(422)->assertJsonValidationErrors(['discount']);
}
/** @test */
public function testCreateEstimateRequiresTemplate()
{
$estimate = factory(Estimate::class)->raw([
'estimate_template_id' => '',
'items' => [
factory(EstimateItem::class)->raw()
]
]);
$response = $this->json('POST', 'api/estimates', $estimate);
$response->assertStatus(422)->assertJsonValidationErrors(['estimate_template_id']);
}
/** @test */
public function testCreateEstimateRequiresItems()
{
$estimate = factory(Estimate::class)->raw(['items' => []]);
$response = $this->json('POST', 'api/estimates', $estimate);
$response->assertStatus(422)->assertJsonValidationErrors(['items']);
}
/** @test */
public function testCreateEstimateRequiresItemsName()
{
$estimate = factory(Estimate::class)->raw([
'items' => [factory(EstimateItem::class)->raw(['name' => ''])]
]);
$response = $this->json('POST', 'api/estimates', $estimate);
$response->assertStatus(422)->assertJsonValidationErrors(['items.0.name']);
}
/** @test */
public function testCreateEstimateRequiresItemsQuantity()
{
$estimate = factory(Estimate::class)->raw([
'items' => [factory(EstimateItem::class)->raw(['quantity' => null])]
]);
$response = $this->json('POST', 'api/estimates', $estimate);
$response->assertStatus(422)->assertJsonValidationErrors(['items.0.quantity']);
}
/** @test */
public function testCreateEstimateRequiresItemsPrice()
{
$estimate = factory(Estimate::class)->raw([
'items' => [factory(EstimateItem::class)->raw(['price' => null])]
]);
$response = $this->json('POST', 'api/estimates', $estimate);
$response->assertStatus(422)->assertJsonValidationErrors(['items.0.price']);
}
/** @test */
public function testGetEditEstimateData()
{
$estimate = factory(Estimate::class)->create([
test('update estimate', function () {
$estimate = Estimate::factory()
->hasItems(1)
->hasTaxes(1)
->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$response = $this->json('GET', 'api/estimates/'.$estimate->id.'/edit');
$estimate2 = Estimate::factory()->raw([
'items' => [
EstimateItem::factory()->raw()
],
'taxes' => [
Tax::factory()->raw([
'tax_type_id' => $estimate->taxes[0]->tax_type_id
])
]
]);
$response->assertOk();
}
$response = putJson('api/v1/estimates/' . $estimate->id, $estimate2);
/** @test */
public function testUpdateEstimate()
{
$estimate = factory(Estimate::class)->create([
$newEstimate = $response->decodeResponseJson()['estimate'];
$this->assertDatabaseHas('estimates', [
'estimate_template_id' => $estimate2['estimate_template_id'],
'estimate_number' => $estimate2['estimate_number'],
'discount_type' => $estimate2['discount_type'],
'discount_val' => $estimate2['discount_val'],
'sub_total' => $estimate2['sub_total'],
'discount' => $estimate2['discount'],
'user_id' => $estimate2['user_id'],
'total' => $estimate2['total'],
'notes' => $estimate2['notes'],
'tax' => $estimate2['tax'],
]);
$this->assertDatabaseHas('taxes', [
'estimate_id' => $newEstimate['id']
]);
$this->assertDatabaseHas('estimate_items', [
'estimate_id' => $newEstimate['id']
]);
$response->assertStatus(200);
});
test('update validates using a form request', function () {
$this->assertActionUsesFormRequest(
EstimatesController::class,
'update',
EstimatesRequest::class
);
});
test('search estimates', function () {
$filters = [
'page' => 1,
'limit' => 15,
'search' => 'doe',
'from_date' => '2020-07-18',
'to_date' => '2020-07-20',
'estimate_number' => '000003'
];
$queryString = http_build_query($filters, '', '&');
$response = getJson('api/v1/estimates?' . $queryString);
$response->assertStatus(200);
});
test('send estimate using a form request', function () {
$this->assertActionUsesFormRequest(
SendEstimateController::class,
'__invoke',
SendEstimatesRequest::class
);
});
test('send estimate to customer', function () {
$estimate = Estimate::factory()->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$data = [
'subject' => 'test',
'body' => 'test',
'from' => 'john@example.com',
'to' => 'doe@example.com'
];
postJson("api/v1/estimates/{$estimate->id}/send", $data)
->assertStatus(200)
->assertJson([
'success' => true
]);
});
test('estimate mark as accepted', function () {
$estimate = Estimate::factory()->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$data = [
'status' => Estimate::STATUS_ACCEPTED
];
$response = postJson("api/v1/estimates/{$estimate->id}/status", $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
$estimate2 = Estimate::find($estimate->id);
$this->assertEquals($estimate2->status, Estimate::STATUS_ACCEPTED);
});
test('estimate mark as rejected', function () {
$estimate = Estimate::factory()->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$data = [
'status' => Estimate::STATUS_REJECTED
];
$response = postJson("api/v1/estimates/{$estimate->id}/status", $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
$estimate2 = Estimate::find($estimate->id);
$this->assertEquals($estimate2->status, Estimate::STATUS_REJECTED);
});
test('create invoice from estimate', function () {
$estimate = Estimate::factory()->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$response = postJson("api/v1/estimates/{$estimate->id}/convert-to-invoice")
->assertStatus(200);
});
test('delete multiple estimates using a form request', function () {
$this->assertActionUsesFormRequest(
EstimatesController::class,
'delete',
DeleteEstimatesRequest::class
);
});
test('delete multiple estimates', function () {
$estimates = Estimate::factory()
->count(3)
->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$estimate2 = factory(Estimate::class)->raw([
'items' => [
factory(EstimateItem::class)->raw()
],
'taxes' => [
factory(Tax::class)->raw([
'tax_type_id' => $estimate->taxes[0]->tax_type_id
])
]
$ids = $estimates->pluck('id');
$data = [
'ids' => $ids
];
$response = postJson('api/v1/estimates/delete', $data);
$response
->assertStatus(200)
->assertJson([
'success' => true
]);
$response = $this->json('PUT', 'api/estimates/'.$estimate->id, $estimate2);
$newEstimate = $response->decodeResponseJson()['estimate'];
$response->assertOk();
$this->assertEquals($estimate2['estimate_number'], $newEstimate['estimate_number']);
$this->assertEquals($estimate2['user_id'], $newEstimate['user_id']);
$this->assertEquals($estimate2['estimate_template_id'], $newEstimate['estimate_template_id']);
$this->assertEquals($estimate2['sub_total'], $newEstimate['sub_total']);
$this->assertEquals($estimate2['total'], $newEstimate['total']);
$this->assertEquals($estimate2['discount'], $newEstimate['discount']);
$this->assertEquals($estimate2['discount_type'], $newEstimate['discount_type']);
$this->assertEquals($estimate2['discount_val'], $newEstimate['discount_val']);
$this->assertEquals($estimate2['tax'], $newEstimate['tax']);
$this->assertEquals($estimate2['notes'], $newEstimate['notes']);
$this->assertEquals($estimate2['items'][0]['name'], $newEstimate['items'][0]['name']);
$this->assertEquals($estimate2['items'][0]['description'], $newEstimate['items'][0]['description']);
$this->assertEquals($estimate2['items'][0]['price'], $newEstimate['items'][0]['price']);
$this->assertEquals($estimate2['items'][0]['quantity'], $newEstimate['items'][0]['quantity']);
$this->assertEquals($estimate2['items'][0]['discount_type'], $newEstimate['items'][0]['discount_type']);
$this->assertEquals($estimate2['items'][0]['discount'], $newEstimate['items'][0]['discount']);
$this->assertEquals($estimate2['items'][0]['total'], $newEstimate['items'][0]['total']);
$this->assertEquals($estimate2['taxes'][0]['tax_type_id'], $newEstimate['taxes'][0]['tax_type_id']);
$this->assertEquals($estimate2['taxes'][0]['percent'], $newEstimate['taxes'][0]['percent']);
$this->assertEquals($estimate2['taxes'][0]['amount'], $newEstimate['taxes'][0]['amount']);
$this->assertEquals($estimate2['taxes'][0]['name'], $newEstimate['taxes'][0]['name']);
$this->assertEquals($newEstimate['id'], $newEstimate['taxes'][0]['estimate_id']);
foreach ($estimates as $estimate) {
$this->assertDeleted($estimate);
}
});
/** @test */
public function testUpdateEstimateEstimateDateRequired()
{
$estimate = factory(Estimate::class)->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$estimate2 = factory(Estimate::class)->raw([
'estimate_date' => '',
'items' => [
factory(EstimateItem::class)->raw()
]
]);
$response = $this->json('PUT', 'api/estimates/'.$estimate->id, $estimate2);
$response->assertStatus(422)->assertJsonValidationErrors(['estimate_date']);
}
/** @test */
public function testUpdateEstimateExpiryDateRequired()
{
$estimate = factory(Estimate::class)->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$estimate2 = factory(Estimate::class)->raw([
'expiry_date' => '',
'items' => [
factory(EstimateItem::class)->raw()
]
]);
$response = $this->json('PUT', 'api/estimates/'.$estimate->id, $estimate2);
$response->assertStatus(422)->assertJsonValidationErrors(['expiry_date']);
}
/** @test */
public function testUpdateEstimateRequiresEstimateNumber()
{
$estimate = factory(Estimate::class)->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$estimate2 = factory(Estimate::class)->raw([
'estimate_number' => '',
'items' => [
factory(EstimateItem::class)->raw()
]
]);
$response = $this->json('PUT', 'api/estimates/'.$estimate->id, $estimate2);
$response->assertStatus(422)->assertJsonValidationErrors(['estimate_number']);
}
/** @test */
public function testUpdateEstimateRequiresUser()
{
$estimate = factory(Estimate::class)->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$estimate2 = factory(Estimate::class)->raw([
'user_id' => '',
'items' => [
factory(EstimateItem::class)->raw()
]
]);
$response = $this->json('PUT', 'api/estimates/'.$estimate->id, $estimate2);
$response->assertStatus(422)->assertJsonValidationErrors(['user_id']);
}
/** @test */
public function testUpdateEstimateRequiresDiscount()
{
$estimate = factory(Estimate::class)->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$estimate2 = factory(Estimate::class)->raw([
'discount' => '',
'items' => [
factory(EstimateItem::class)->raw()
]
]);
$response = $this->json('PUT', 'api/estimates/'.$estimate->id, $estimate2);
$response->assertStatus(422)->assertJsonValidationErrors(['discount']);
}
/** @test */
public function testUpdateEstimateRequiresTemplate()
{
$estimate = factory(Estimate::class)->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$estimate2 = factory(Estimate::class)->raw([
'estimate_template_id' => '',
'items' => [
factory(EstimateItem::class)->raw()
]
]);
$response = $this->json('PUT', 'api/estimates/'.$estimate->id, $estimate2);
$response->assertStatus(422)->assertJsonValidationErrors(['estimate_template_id']);
}
/** @test */
public function testUpdateEstimateRequiresItems()
{
$estimate = factory(Estimate::class)->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$estimate2 = factory(Estimate::class)->raw(['items' => []]);
$response = $this->json('PUT', 'api/estimates/'.$estimate->id, $estimate2);
$response->assertStatus(422)->assertJsonValidationErrors(['items']);
}
/** @test */
public function testUpdateEstimateRequiresItemsName()
{
$estimate = factory(Estimate::class)->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$estimate2 = factory(Estimate::class)->raw([
'items' => [factory(EstimateItem::class)->raw(['name' => ''])]
]);
$response = $this->json('PUT', 'api/estimates/'.$estimate->id, $estimate2);
$response->assertStatus(422)->assertJsonValidationErrors(['items.0.name']);
}
/** @test */
public function testUpdateEstimateRequiresItemsQuantity()
{
$estimate = factory(Estimate::class)->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$estimate2 = factory(Estimate::class)->raw([
'items' => [factory(EstimateItem::class)->raw(['quantity' => null])]
]);
$response = $this->json('PUT', 'api/estimates/'.$estimate->id, $estimate2);
$response->assertStatus(422)->assertJsonValidationErrors(['items.0.quantity']);
}
/** @test */
public function testUpdateEstimateRequiresItemsPrice()
{
$estimate = factory(Estimate::class)->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$estimate2 = factory(Estimate::class)->raw([
'items' => [factory(EstimateItem::class)->raw(['price' => null])]
]);
$response = $this->json('PUT', 'api/estimates/'.$estimate->id, $estimate2);
$response->assertStatus(422)->assertJsonValidationErrors(['items.0.price']);
}
/** @test */
public function testDeleteEstimate()
{
$estimate = factory(Estimate::class)->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$response = $this->json('DELETE', 'api/estimates/'.$estimate->id);
$response
->assertOk()
->assertJson([
'success' => 'Estimate deleted successfully'
]);
$deleted = Estimate::find($estimate->id);
$this->assertNull($deleted);
}
/** @test */
public function testSearchEstimates()
{
$filters = [
'page' => 1,
'limit' => 15,
'search' => 'doe',
'from_date' => '01/09/2019',
'to_date' => '11/09/2019',
'estimate_number' => '000003'
];
$queryString = http_build_query($filters, '', '&');
$response = $this->json('GET', 'api/estimates?'.$queryString);
$response->assertOk();
}
/** @test */
public function testSendEstimateToCustomer()
{
$estimate = factory(Estimate::class)->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$data = [
'id' => $estimate->id
];
$response = $this->json('POST', 'api/estimates/send', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
}
/** @test */
public function testEstimateMarkAsAccepted()
{
$estimate = factory(Estimate::class)->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$data = [
'id' => $estimate->id
];
$response = $this->json('POST', 'api/estimates/accept', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
$estimate2 = Estimate::find($estimate->id);
$this->assertEquals($estimate2->status, Estimate::STATUS_ACCEPTED);
}
/** @test */
public function testEstimateMarkAsRejected()
{
$estimate = factory(Estimate::class)->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$data = [
'id' => $estimate->id
];
$response = $this->json('POST', 'api/estimates/reject', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
$estimate2 = Estimate::find($estimate->id);
$this->assertEquals($estimate2->status, Estimate::STATUS_REJECTED);
}
/** @test */
public function testCreateInvoiceFromEstimate()
{
$estimate = factory(Estimate::class)->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$invoice = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$response = $this->json('POST', 'api/estimates/'.$estimate->id.'/convert-to-invoice');
$response->assertOk();
}
/** @test */
public function testDeleteMultipleEstimates()
{
$estimates = factory(Estimate::class, 3)->create([
'estimate_date' => '1988-07-18',
'expiry_date' => '1988-08-18',
]);
$ids = $estimates->pluck('id');
$data = [
'id' => $ids
];
$response = $this->json('POST', 'api/estimates/delete', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
}
}
test('get estimate templates', function () {
getJson('api/v1/estimates/templates')->assertStatus(200);
});

View File

@ -1,123 +1,89 @@
<?php
namespace Tests\Feature;
use Crater\Models\User;
use Crater\Models\ExpenseCategory;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use Crater\Http\Requests\ExpenseCategoryRequest;
use Crater\Http\Controllers\V1\Expense\ExpenseCategoriesController;
use function Pest\Laravel\{postJson, putJson, getJson, deleteJson};
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Crater\User;
use Crater\ExpenseCategory;
use Laravel\Passport\Passport;
use SettingsSeeder;
beforeEach(function () {
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
class ExpenseCategoryTest extends TestCase
{
use RefreshDatabase;
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Sanctum::actingAs(
$user,
['*']
);
});
protected $user;
test('get categories', function () {
$response = getJson('api/v1/categories');
public function setUp(): void
{
parent::setUp();
$this->seed();
$this->seed(SettingsSeeder::class);
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Passport::actingAs(
$user,
['*']
);
}
$response->assertOk();
});
/** @test */
public function testGetCategories()
{
$response = $this->json('GET', 'api/categories');
test('create category', function () {
$category = ExpenseCategory::factory()->raw();
$response->assertOk();
}
$response = postJson('api/v1/categories', $category);
/** @test */
public function testCreateCategory()
{
$category = factory(ExpenseCategory::class)->raw();
$response->assertStatus(200);
$response = $this->json('POST', 'api/categories', $category);
$this->assertDatabaseHas('expense_categories', [
'name' => $category['name'],
'description' => $category['description'],
]);
});
$category2 = $response->decodeResponseJson()['category'];
test('store validates using a form request', function () {
$this->assertActionUsesFormRequest(
ExpenseCategoriesController::class,
'store',
ExpenseCategoryRequest::class
);
});
$response->assertOk();
$this->assertEquals($category['name'], $category2['name']);
$this->assertEquals($category['description'], $category2['description']);
}
test('get category', function () {
$category = ExpenseCategory::factory()->create();
/** @test */
public function testCreateCategoryRequiresName()
{
$category = factory(ExpenseCategory::class)->raw([
'name' => ''
getJson("api/v1/categories/{$category->id}")->assertOk();
});
test('update category', function () {
$category = ExpenseCategory::factory()->create();
$category2 = ExpenseCategory::factory()->raw();
putJson('api/v1/categories/'.$category->id, $category2)->assertOk();
$this->assertDatabaseHas('expense_categories', [
'id' => $category->id,
'name' => $category2['name'],
'description' => $category2['description'],
]);
});
test('update validates using a form request', function () {
$this->assertActionUsesFormRequest(
ExpenseCategoriesController::class,
'update',
ExpenseCategoryRequest::class
);
});
test('delete category', function () {
$category = ExpenseCategory::factory()->create();
deleteJson('api/v1/categories/'.$category->id)
->assertOk()
->assertJson([
'success' => true
]);
$response = $this->json('POST', 'api/categories', $category);
$response->assertStatus(422)->assertJsonValidationErrors(['name']);
}
/** @test */
public function testGetEditCategoryData()
{
$category = factory(ExpenseCategory::class)->create();
$response = $this->json('GET', 'api/categories/'.$category->id.'/edit');
$category2 = $response->decodeResponseJson()['category'];
$response->assertOk();
$this->assertEquals($category->toArray(), $category2);
}
/** @test */
public function testUpdateCategory()
{
$category = factory(ExpenseCategory::class)->create();
$category2 = factory(ExpenseCategory::class)->raw();
$response = $this->json('PUT', 'api/categories/'.$category->id, $category2);
$category3 = $response->decodeResponseJson()['category'];
$response->assertOk();
$this->assertEquals($category3['name'], $category2['name']);
$this->assertEquals($category3['description'], $category2['description']);
}
/** @test */
public function testUpdateCategoryRequiresName()
{
$category = factory(ExpenseCategory::class)->create();
$category2 = factory(ExpenseCategory::class)->raw([
'name' => ''
]);
$response = $this->json('PUT', 'api/categories/'.$category->id, $category2);
$response->assertStatus(422)->assertJsonValidationErrors(['name']);
}
/** @test */
public function testDeleteCategory()
{
$category = factory(ExpenseCategory::class)->create();
$response = $this->json('DELETE', 'api/categories/'.$category->id);
$response
->assertOk()
->assertJson([
'success' => true
]);
}
}
$this->assertDeleted($category);
});

View File

@ -1,240 +1,126 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Crater\User;
use Crater\Expense;
use Crater\CompanySetting;
use Crater\ExpenseCategory;
use Laravel\Passport\Passport;
use SettingsSeeder;
use Crater\Models\User;
use Crater\Models\Expense;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use Crater\Http\Requests\ExpenseRequest;
use Crater\Http\Controllers\V1\Expense\ExpensesController;
use function Pest\Laravel\{postJson, putJson, getJson, deleteJson};
class ExpenseTest extends TestCase
{
use RefreshDatabase;
beforeEach(function () {
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
protected $user;
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Sanctum::actingAs(
$user,
['*']
);
});
public function setUp(): void
{
parent::setUp();
$this->seed();
$this->seed(SettingsSeeder::class);
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Passport::actingAs(
$user,
['*']
);
}
test('get expenses', function () {
getJson('api/v1/expenses?page=1')->assertOk();
});
test('create expense', function () {
$expense = Expense::factory()->raw();
/** @test */
public function testGetExpenses()
{
$response = $this->json('GET', 'api/expenses?page=1');
postJson('api/v1/expenses', $expense)->assertOk();
$response->assertOk();
}
$this->assertDatabaseHas('expenses', [
'notes' => $expense['notes'],
'expense_category_id' => $expense['expense_category_id'],
'amount' => $expense['amount'],
]);
});
/** @test */
public function testGetCreateExpenseData()
{
$response = $this->json('GET', 'api/expenses/create');
test('store validates using a form request', function () {
$this->assertActionUsesFormRequest(
ExpensesController::class,
'store',
ExpenseRequest::class
);
});
$response->assertOk();
}
test('get expense data', function () {
$expense = Expense::factory()->create([
'expense_date' => '2019-02-05'
]);
/** @test */
public function testCrateExpense()
{
$expense = factory(Expense::class)->raw();
getJson("api/v1/expenses/{$expense->id}")->assertOk();
$response = $this->json('POST', 'api/expenses', $expense);
$this->assertDatabaseHas('expenses', [
'id' => $expense->id,
'notes' => $expense['notes'],
'expense_category_id' => $expense['expense_category_id'],
'amount' => $expense['amount'],
]);
});
$expense2 = $response->decodeResponseJson()['expense'];
test('update expense', function () {
$expense = Expense::factory()->create([
'expense_date' => '2019-02-05'
]);
$response->assertOk();
$this->assertEquals($expense['notes'], $expense2['notes']);
$this->assertEquals($expense['expense_category_id'], $expense2['expense_category_id']);
$this->assertEquals($expense['amount'], $expense2['amount']);
}
$expense2 = Expense::factory()->raw();
/** @test */
public function testCreateExpenseRequiresExpanseDate()
{
$expense = factory(Expense::class)->raw([
'expense_date' => ''
putJson('api/v1/expenses/' . $expense->id, $expense2)->assertOk();
$this->assertDatabaseHas('expenses', [
'id' => $expense->id,
'notes' => $expense2['notes'],
'expense_category_id' => $expense2['expense_category_id'],
'amount' => $expense2['amount'],
]);
});
test('update validates using a form request', function () {
$this->assertActionUsesFormRequest(
ExpensesController::class,
'update',
ExpenseRequest::class
);
});
test('search expenses', function () {
$filters = [
'page' => 1,
'limit' => 15,
'expense_category_id' => 1,
'search' => 'cate',
'from_date' => '2020-07-18',
'to_date' => '2020-07-20'
];
$queryString = http_build_query($filters, '', '&');
$response = getJson('api/v1/expenses?' . $queryString);
$response->assertOk();
});
test('delete multiple expenses', function () {
$expenses = Expense::factory()->count(3)->create([
'expense_date' => '2019-02-05'
]);
$data = [
'ids' => $expenses->pluck('id')
];
$response = postJson('api/v1/expenses/delete', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
$response = $this->json('POST', 'api/expenses', $expense);
$response->assertStatus(422)->assertJsonValidationErrors(['expense_date']);
foreach ($expenses as $expense) {
$this->assertDeleted($expense);
}
/** @test */
public function testCreateExpenseRequiresAmount()
{
$expense = factory(Expense::class)->raw([
'amount' => ''
]);
$response = $this->json('POST', 'api/expenses', $expense);
$response->assertStatus(422)->assertJsonValidationErrors(['amount']);
}
/** @test */
public function testCreateExpenseRequiresCategory()
{
$expense = factory(Expense::class)->raw([
'expense_category_id' => ''
]);
$response = $this->json('POST', 'api/expenses', $expense);
$response->assertStatus(422)->assertJsonValidationErrors(['expense_category_id']);
}
/** @test */
public function testGetEditExpenseData()
{
$expense = factory(Expense::class)->create([
'expense_date' => '2019-02-05'
]);
$response = $this->json('GET', 'api/expenses/'.$expense->id.'/edit');
$response->assertOk();
}
/** @test */
public function testUpdateExpense()
{
$expense = factory(Expense::class)->create([
'expense_date' => '2019-02-05'
]);
$expense2 = factory(Expense::class)->raw();
$response = $this->json('PUT', 'api/expenses/'.$expense->id, $expense2);
$expense3 = $response->decodeResponseJson()['expense'];
$response->assertOk();
$this->assertEquals($expense3['notes'], $expense2['notes']);
$this->assertEquals($expense3['expense_category_id'], $expense2['expense_category_id']);
$this->assertEquals($expense3['amount'], $expense2['amount']);
}
/** @test */
public function testUpdateExpenseRequiresExpenseDate()
{
$expense = factory(Expense::class)->create([
'expense_date' => '2019-02-05'
]);
$expense2 = factory(Expense::class)->raw([
'expense_date' => ''
]);
$response = $this->json('PUT', 'api/expenses/'.$expense->id, $expense2);
$response->assertStatus(422)->assertJsonValidationErrors(['expense_date']);
}
/** @test */
public function testUpdateExpenseRequiresAmount()
{
$expense = factory(Expense::class)->create([
'expense_date' => '2019-02-05'
]);
$expense2 = factory(Expense::class)->raw([
'amount' => ''
]);
$response = $this->json('PUT', 'api/expenses/'.$expense->id, $expense2);
$response->assertStatus(422)->assertJsonValidationErrors(['amount']);
}
/** @test */
public function testUpdateExpenseRequiresCategory()
{
$expense = factory(Expense::class)->create([
'expense_date' => '2019-02-05'
]);
$expense2 = factory(Expense::class)->raw([
'expense_category_id' => ''
]);
$response = $this->json('PUT', 'api/expenses/'.$expense->id, $expense2);
$response->assertStatus(422)->assertJsonValidationErrors(['expense_category_id']);
}
/** @test */
public function testDeleteExpenses()
{
$expense = factory(Expense::class)->create([
'expense_date' => '2019-02-05'
]);
$response = $this->json('DELETE', 'api/expenses/'.$expense->id);
$response->assertOk()
->assertJson([
'success' => true
]);
}
/** @test */
public function testSearchExpenses()
{
$filters = [
'page' => 1,
'limit' => 15,
'expense_category_id' => 1,
'search' => 'cate',
'from_date' => '09/09/2016',
'to_date' => '10/09/2016'
];
$queryString = http_build_query($filters, '', '&');
$response = $this->json('GET', 'api/expenses?'.$queryString);
$response->dump();
$response->assertOk();
}
/** @test */
public function testDeleteMultipleExpenses()
{
$expenses = factory(Expense::class, 3)->create([
'expense_date' => '2019-02-05'
]);
$ids = $expenses->pluck('id');
$data = [
'id' => $ids,
'type' => 'expense'
];
$response = $this->json('POST', 'api/expenses/delete', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
}
}
});

View File

@ -0,0 +1,64 @@
<?php
use Crater\Models\User;
use Crater\Models\FileDisk;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use function Pest\Laravel\{postJson, putJson, getJson};
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('get file disks', function () {
$response = getJson('/api/v1/disks');
$response->assertOk();
});
test('create file disk', function () {
$disk = FileDisk::factory()->raw();
$response = postJson('/api/v1/disks', $disk);
$disk['credentials'] = json_encode($disk['credentials']);
$this->assertDatabaseHas('file_disks', $disk);
});
test('update file disk', function () {
$disk = FileDisk::factory()->create();
$disk2 = FileDisk::factory()->raw();
$response = putJson("/api/v1/disks/{$disk->id}", $disk2)->assertStatus(200);
$disk2['credentials'] = json_encode($disk2['credentials']);
$this->assertDatabaseHas('file_disks', $disk2);
});
test('get disk', function () {
$disk = FileDisk::factory()->create();
$response = getJson("/api/v1/disks/{$disk->driver}");
$response->assertStatus(200);
});
test('get drivers', function () {
$response = getJson("/api/v1/disk/drivers");
$response->assertStatus(200);
});

View File

@ -1,664 +1,252 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Crater\Tax;
use Crater\User;
use Crater\Invoice;
use Crater\CompanySetting;
use Crater\InvoiceItem;
use Laravel\Passport\Passport;
use SettingsSeeder;
use Crater\Models\User;
use Crater\Models\Invoice;
use Crater\Models\InvoiceItem;
use Crater\Models\Tax;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use Crater\Http\Requests\InvoicesRequest;
use Crater\Http\Controllers\V1\Invoice\InvoicesController;
class InvoiceTest extends TestCase
{
use RefreshDatabase;
use function Pest\Laravel\{postJson, putJson, getJson};
protected $user;
beforeEach(function () {
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
public function setUp(): void
{
parent::setUp();
$this->seed();
$this->seed(SettingsSeeder::class);
$user = User::find(1);
$this->withHeaders([
'company' => 1,
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Sanctum::actingAs(
$user,
['*']
);
});
test('testGetInvoices', function () {
$response = getJson('api/v1/invoices?page=1&type=OVERDUE&limit=20');
$response->assertOk();
});
test('create invoice', function () {
$invoice = Invoice::factory()
->raw([
'taxes' => [Tax::factory()->raw()],
'items' => [InvoiceItem::factory()->raw()]
]);
Passport::actingAs(
$user,
['*']
);
$response = postJson('api/v1/invoices', $invoice);
$response->assertOk();
$this->assertDatabaseHas('invoices', [
'invoice_template_id' => $invoice['invoice_template_id'],
'invoice_number' => $invoice['invoice_number'],
'sub_total' => $invoice['sub_total'],
'discount' => $invoice['discount'],
'user_id' => $invoice['user_id'],
'total' => $invoice['total'],
'tax' => $invoice['tax'],
]);
$this->assertDatabaseHas('invoice_items', $invoice['items'][0]);
$this->assertDatabaseHas('taxes', $invoice['taxes'][0]);
});
test('create invoice as sent', function () {
$invoice = Invoice::factory()
->raw([
'taxes' => [Tax::factory()->raw()],
'items' => [InvoiceItem::factory()->raw()]
]);
$response = postJson('api/v1/invoices', $invoice);
$response->assertOk();
$this->assertDatabaseHas('invoices', [
'invoice_number' => $invoice['invoice_number'],
'sub_total' => $invoice['sub_total'],
'total' => $invoice['total'],
'tax' => $invoice['tax'],
'discount' => $invoice['discount'],
'user_id' => $invoice['user_id'],
'invoice_template_id' => $invoice['invoice_template_id'],
]);
$this->assertDatabaseHas('invoice_items', $invoice['items'][0]);
$this->assertDatabaseHas('taxes', $invoice['taxes'][0]);
});
test('store validates using a form request', function () {
$this->assertActionUsesFormRequest(
InvoicesController::class,
'store',
InvoicesRequest::class
);
});
test('update invoice', function () {
$invoice = Invoice::factory()->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$invoice2 = Invoice::factory()
->raw([
'taxes' => [Tax::factory()->raw()],
'items' => [InvoiceItem::factory()->raw()]
]);
putJson('api/v1/invoices/' . $invoice->id, $invoice2)->assertOk();
$this->assertDatabaseHas('invoices', [
'invoice_number' => $invoice2['invoice_number'],
'sub_total' => $invoice2['sub_total'],
'total' => $invoice2['total'],
'tax' => $invoice2['tax'],
'discount' => $invoice2['discount'],
'user_id' => $invoice2['user_id'],
'invoice_template_id' => $invoice2['invoice_template_id'],
]);
$this->assertDatabaseHas('invoice_items', $invoice2['items'][0]);
$this->assertDatabaseHas('taxes', $invoice2['taxes'][0]);
});
test('update validates using a form request', function () {
$this->assertActionUsesFormRequest(
InvoicesController::class,
'update',
InvoicesRequest::class
);
});
test('send invoice to customer', function () {
$invoices = Invoice::factory()->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$data = [
'from' => 'john@example.com',
'to' => 'doe@example.com',
'subject' => 'email subject',
'body' => 'email body'
];
$response = postJson('api/v1/invoices/' . $invoices->id . '/send', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
$invoice2 = Invoice::find($invoices->id);
$this->assertEquals($invoice2->status, Invoice::STATUS_SENT);
});
test('invoice mark as paid', function () {
$invoice = Invoice::factory()->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$data = [
'status' => Invoice::STATUS_COMPLETED
];
$response = postJson('api/v1/invoices/' . $invoice->id . '/status', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
$this->assertEquals(Invoice::find($invoice->id)->paid_status, Invoice::STATUS_PAID);
});
test('invoice mark as sent', function () {
$invoice = Invoice::factory()->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$data = [
'status' => Invoice::STATUS_SENT
];
$response = postJson('api/v1/invoices/' . $invoice->id . '/status', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
$this->assertEquals(Invoice::find($invoice->id)->status, Invoice::STATUS_SENT);
});
test('search invoices', function () {
$filters = [
'page' => 1,
'limit' => 15,
'search' => 'doe',
'status' => Invoice::STATUS_DRAFT,
'from_date' => '2019-01-20',
'to_date' => '2019-01-27',
'invoice_number' => '000012'
];
$queryString = http_build_query($filters, '', '&');
$response = getJson('api/v1/invoices?' . $queryString);
$response->assertOk();
});
test('delete multiple invoices', function () {
$invoices = Invoice::factory()->count(3)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$ids = $invoices->pluck('id');
$data = [
'ids' => $ids
];
postJson('api/v1/invoices/delete', $data)
->assertOk()
->assertJson([
'success' => true
]);
foreach ($invoices as $invoice) {
$this->assertDeleted($invoice);
}
});
test('clone invoice', function () {
$invoices = Invoice::factory()->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
/** @test */
public function testGetInvoices()
{
$response = $this->json('GET', 'api/invoices?page=1&type=OVERDUE&limit=20');
$response = postJson("api/v1/invoices/{$invoices->id}/clone");
$response->assertOk();
}
/** @test */
public function testGetCreateInvoiceData()
{
$response = $this->json('GET', 'api/invoices/create');
$response->assertOk();
}
/** @test */
public function testCreateInvoice()
{
$invoice = factory(Invoice::class)->raw([
'items' => [
factory(InvoiceItem::class)->raw([
'taxes' => [
factory(Tax::class)->raw()
]
])
],
'taxes' => [
factory(Tax::class)->raw()
]
$response
->assertOk()
->assertJson([
'success' => true
]);
$response = $this->json('POST', 'api/invoices', $invoice);
$invoice2 = $response->decodeResponseJson()['invoice'];
$response->assertOk();
$this->assertEquals($invoice['invoice_number'], $invoice2['invoice_number']);
$this->assertEquals($invoice['sub_total'], $invoice2['sub_total']);
$this->assertEquals($invoice['total'], $invoice2['total']);
$this->assertEquals($invoice['tax'], $invoice2['tax']);
$this->assertEquals($invoice['discount'], $invoice2['discount']);
$this->assertEquals($invoice['discount_type'], $invoice2['discount_type']);
$this->assertEquals($invoice['discount_val'], $invoice2['discount_val']);
$this->assertEquals($invoice['notes'], $invoice2['notes']);
$this->assertEquals($invoice['user_id'], $invoice2['user_id']);
$this->assertEquals($invoice['invoice_template_id'], $invoice2['invoice_template_id']);
$this->assertEquals($invoice['items'][0]['name'], $invoice2['items'][0]['name']);
$this->assertEquals($invoice['items'][0]['description'], $invoice2['items'][0]['description']);
$this->assertEquals($invoice['items'][0]['price'], $invoice2['items'][0]['price']);
$this->assertEquals($invoice['items'][0]['quantity'], $invoice2['items'][0]['quantity']);
$this->assertEquals($invoice['items'][0]['discount_type'], $invoice2['items'][0]['discount_type']);
$this->assertEquals($invoice['items'][0]['discount'], $invoice2['items'][0]['discount']);
$this->assertEquals($invoice['items'][0]['total'], $invoice2['items'][0]['total']);
$this->assertEquals($invoice['items'][0]['item_id'], $invoice2['items'][0]['item_id']);
$this->assertEquals($invoice['taxes'][0]['tax_type_id'], $invoice2['taxes'][0]['tax_type_id']);
$this->assertEquals($invoice['taxes'][0]['percent'], $invoice2['taxes'][0]['percent']);
$this->assertEquals($invoice['taxes'][0]['amount'], $invoice2['taxes'][0]['amount']);
$this->assertEquals($invoice2['id'], $invoice2['taxes'][0]['invoice_id']);
}
/** @test */
public function testCreateInvoiceAsSent()
{
$invoice = factory(Invoice::class)->raw([
// 'invoiceSend' => true,
'items' => [
factory(InvoiceItem::class)->raw([
'taxes' => [
factory(Tax::class)->raw()
]
])
],
'taxes' => [
factory(Tax::class)->raw()
]
]);
$response = $this->json('POST', 'api/invoices', $invoice);
$invoice2 = $response->decodeResponseJson()['invoice'];
$response->assertOk();
$this->assertEquals($invoice['invoice_number'], $invoice2['invoice_number']);
$this->assertEquals($invoice['sub_total'], $invoice2['sub_total']);
$this->assertEquals($invoice['total'], $invoice2['total']);
$this->assertEquals($invoice['tax'], $invoice2['tax']);
$this->assertEquals($invoice['discount'], $invoice2['discount']);
$this->assertEquals($invoice['discount_type'], $invoice2['discount_type']);
$this->assertEquals($invoice['discount_val'], $invoice2['discount_val']);
$this->assertEquals($invoice['notes'], $invoice2['notes']);
$this->assertEquals($invoice['user_id'], $invoice2['user_id']);
$this->assertEquals($invoice['invoice_template_id'], $invoice2['invoice_template_id']);
$this->assertEquals($invoice['items'][0]['name'], $invoice2['items'][0]['name']);
$this->assertEquals($invoice['items'][0]['description'], $invoice2['items'][0]['description']);
$this->assertEquals($invoice['items'][0]['price'], $invoice2['items'][0]['price']);
$this->assertEquals($invoice['items'][0]['quantity'], $invoice2['items'][0]['quantity']);
$this->assertEquals($invoice['items'][0]['discount_type'], $invoice2['items'][0]['discount_type']);
$this->assertEquals($invoice['items'][0]['discount'], $invoice2['items'][0]['discount']);
$this->assertEquals($invoice['items'][0]['total'], $invoice2['items'][0]['total']);
$this->assertEquals($invoice['items'][0]['item_id'], $invoice2['items'][0]['item_id']);
$this->assertEquals($invoice['taxes'][0]['tax_type_id'], $invoice2['taxes'][0]['tax_type_id']);
$this->assertEquals($invoice['taxes'][0]['percent'], $invoice2['taxes'][0]['percent']);
$this->assertEquals($invoice['taxes'][0]['amount'], $invoice2['taxes'][0]['amount']);
$this->assertEquals($invoice2['id'], $invoice2['taxes'][0]['invoice_id']);
}
/** @test */
public function testCreateInvoiceRequiresInvoiceDate()
{
$invoice = factory(Invoice::class)->raw([
'invoice_date' => '',
'items' => [
factory(InvoiceItem::class)->raw()
]
]);
$response = $this->json('POST', 'api/invoices', $invoice);
$response->assertStatus(422)->assertJsonValidationErrors(['invoice_date']);
}
/** @test */
public function testCreateInvoiceExpiryDateRequired()
{
$invoice = factory(Invoice::class)->raw([
'due_date' => '',
'items' => [
factory(InvoiceItem::class)->raw()
]
]);
$response = $this->json('POST', 'api/invoices', $invoice);
$response->assertStatus(422)->assertJsonValidationErrors(['due_date']);
}
/** @test */
public function testCreateInvoiceRequiresInvoiceNumber()
{
$invoice = factory(Invoice::class)->raw([
'invoice_number' => '',
'items' => [
factory(InvoiceItem::class)->raw()
]
]);
$response = $this->json('POST', 'api/invoices', $invoice);
$response->assertStatus(422)->assertJsonValidationErrors(['invoice_number']);
}
/** @test */
public function testCreateInvoiceRequiresUser()
{
$invoice = factory(Invoice::class)->raw([
'user_id' => '',
'items' => [
factory(InvoiceItem::class)->raw()
]
]);
$response = $this->json('POST', 'api/invoices', $invoice);
$response->assertStatus(422)->assertJsonValidationErrors(['user_id']);
}
/** @test */
public function testCreateInvoiceRequiresDiscount()
{
$invoice = factory(Invoice::class)->raw([
'discount' => '',
'items' => [
factory(InvoiceItem::class)->raw()
]
]);
$response = $this->json('POST', 'api/invoices', $invoice);
$response->assertStatus(422)->assertJsonValidationErrors(['discount']);
}
/** @test */
public function testCreateInvoiceRequiresTemplate()
{
$invoice = factory(Invoice::class)->raw([
'invoice_template_id' => '',
'items' => [
factory(InvoiceItem::class)->raw()
]
]);
$response = $this->json('POST', 'api/invoices', $invoice);
$response->assertStatus(422)->assertJsonValidationErrors(['invoice_template_id']);
}
/** @test */
public function testCreateInvoiceRequiresItems()
{
$invoice = factory(Invoice::class)->raw(['items' => []]);
$response = $this->json('POST', 'api/invoices', $invoice);
$response->assertStatus(422)->assertJsonValidationErrors(['items']);
}
/** @test */
public function testCreateInvoiceRequiresItemsName()
{
$invoice = factory(Invoice::class)->raw([
'items' => [factory(InvoiceItem::class)->raw(['name' => ''])]
]);
$response = $this->json('POST', 'api/invoices', $invoice);
$response->assertStatus(422)->assertJsonValidationErrors(['items.0.name']);
}
/** @test */
public function testCreateInvoiceRequiresItemsQuantity()
{
$invoice = factory(Invoice::class)->raw([
'items' => [factory(InvoiceItem::class)->raw(['quantity' => null])]
]);
$response = $this->json('POST', 'api/invoices', $invoice);
$response->assertStatus(422)->assertJsonValidationErrors(['items.0.quantity']);
}
/** @test */
public function testCreateInvoiceRequiresItemsPrice()
{
$invoice = factory(Invoice::class)->raw([
'items' => [factory(InvoiceItem::class)->raw(['price' => null])]
]);
$response = $this->json('POST', 'api/invoices', $invoice);
$response->assertStatus(422)->assertJsonValidationErrors(['items.0.price']);
}
/** @test */
public function testGetEditInvoiceData()
{
$invoice = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$response = $this->json('GET', 'api/invoices/'.$invoice->id.'/edit');
$response->assertOk();
}
/** @test */
public function testUpdateInvoice()
{
$invoice = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$invoice2 = factory(Invoice::class)->raw([
'items' => [
factory(InvoiceItem::class)->raw([
'taxes' => [
factory(Tax::class)->raw()
]
])
],
'taxes' => [
factory(Tax::class)->raw(['tax_type_id' => $invoice->taxes[0]->tax_type_id])
]
]);
$response = $this->json('PUT', 'api/invoices/'.$invoice->id, $invoice2);
$invoice3 = $response->decodeResponseJson()['invoice'];
$response->assertOk();
$this->assertEquals($invoice3['invoice_number'], $invoice2['invoice_number']);
$this->assertEquals($invoice3['sub_total'], $invoice2['sub_total']);
$this->assertEquals($invoice3['total'], $invoice2['total']);
$this->assertEquals($invoice3['tax'], $invoice2['tax']);
$this->assertEquals($invoice3['discount'], $invoice2['discount']);
$this->assertEquals($invoice3['notes'], $invoice2['notes']);
$this->assertEquals($invoice3['user_id'], $invoice2['user_id']);
$this->assertEquals($invoice3['invoice_template_id'], $invoice2['invoice_template_id']);
$this->assertEquals($invoice3['items'][0]['name'], $invoice2['items'][0]['name']);
$this->assertEquals($invoice3['items'][0]['description'], $invoice2['items'][0]['description']);
$this->assertEquals($invoice3['items'][0]['price'], $invoice2['items'][0]['price']);
$this->assertEquals($invoice3['items'][0]['quantity'], $invoice2['items'][0]['quantity']);
$this->assertEquals($invoice3['items'][0]['discount_type'], $invoice2['items'][0]['discount_type']);
$this->assertEquals($invoice3['items'][0]['discount'], $invoice2['items'][0]['discount']);
$this->assertEquals($invoice3['items'][0]['total'], $invoice2['items'][0]['total']);
$this->assertEquals($invoice3['items'][0]['item_id'], $invoice2['items'][0]['item_id']);
$this->assertEquals($invoice3['taxes'][0]['tax_type_id'], $invoice2['taxes'][0]['tax_type_id']);
$this->assertEquals($invoice3['taxes'][0]['percent'], $invoice2['taxes'][0]['percent']);
$this->assertEquals($invoice3['taxes'][0]['amount'], $invoice2['taxes'][0]['amount']);
$this->assertEquals($invoice->id, $invoice3['taxes'][0]['invoice_id']);
}
/** @test */
public function testUpdateInvoiceRequiresInvoiceDate()
{
$invoice = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$invoice2 = factory(Invoice::class)->raw([
'invoice_date' => '',
'items' => [
factory(InvoiceItem::class)->raw()
]
]);
$response = $this->json('PUT', 'api/invoices/'.$invoice->id, $invoice2);
$response->assertStatus(422)->assertJsonValidationErrors(['invoice_date']);
}
/** @test */
public function testUpdateInvoiceExpiryDateRequired()
{
$invoice = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$invoice2 = factory(Invoice::class)->raw([
'due_date' => '',
'items' => [
factory(InvoiceItem::class)->raw()
]
]);
$response = $this->json('PUT', 'api/invoices/'.$invoice->id, $invoice2);
$response->assertStatus(422)->assertJsonValidationErrors(['due_date']);
}
/** @test */
public function testUpdateInvoiceRequiresInvoiceNumber()
{
$invoice = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$invoice2 = factory(Invoice::class)->raw([
'invoice_number' => '',
'items' => [
factory(InvoiceItem::class)->raw()
]
]);
$response = $this->json('PUT', 'api/invoices/'.$invoice->id, $invoice2);
$response->assertStatus(422)->assertJsonValidationErrors(['invoice_number']);
}
/** @test */
public function testUpdateInvoiceRequiresUser()
{
$invoice = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$invoice2 = factory(Invoice::class)->raw([
'user_id' => '',
'items' => [
factory(InvoiceItem::class)->raw()
]
]);
$response = $this->json('PUT', 'api/invoices/'.$invoice->id, $invoice2);
$response->assertStatus(422)->assertJsonValidationErrors(['user_id']);
}
/** @test */
public function testUpdateInvoiceRequiresDiscount()
{
$invoice = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$invoice2 = factory(Invoice::class)->raw([
'discount' => '',
'items' => [
factory(InvoiceItem::class)->raw()
]
]);
$response = $this->json('PUT', 'api/invoices/'.$invoice->id, $invoice2);
$response->assertStatus(422)->assertJsonValidationErrors(['discount']);
}
/** @test */
public function testUpdateInvoiceRequiresTemplate()
{
$invoice = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$invoice2 = factory(Invoice::class)->raw([
'invoice_template_id' => '',
'items' => [
factory(InvoiceItem::class)->raw()
]
]);
$response = $this->json('PUT', 'api/invoices/'.$invoice->id, $invoice2);
$response->assertStatus(422)->assertJsonValidationErrors(['invoice_template_id']);
}
/** @test */
public function testUpdateInvoiceRequiresItems()
{
$invoice = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$invoice2 = factory(Invoice::class)->raw(['items' => []]);
$response = $this->json('PUT', 'api/invoices/'.$invoice->id, $invoice2);
$response->assertStatus(422)->assertJsonValidationErrors(['items']);
}
/** @test */
public function testUpdateInvoiceRequiresItemsName()
{
$invoice = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$invoice2 = factory(Invoice::class)->raw([
'items' => [factory(InvoiceItem::class)->raw(['name' => ''])]
]);
$response = $this->json('PUT', 'api/invoices/'.$invoice->id, $invoice2);
$response->assertStatus(422)->assertJsonValidationErrors(['items.0.name']);
}
/** @test */
public function testUpdateInvoiceRequiresItemsQuantity()
{
$invoice = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$invoice2 = factory(Invoice::class)->raw([
'items' => [factory(InvoiceItem::class)->raw(['quantity' => null])]
]);
$response = $this->json('PUT', 'api/invoices/'.$invoice->id, $invoice2);
$response->assertStatus(422)->assertJsonValidationErrors(['items.0.quantity']);
}
/** @test */
public function testUpdateInvoiceRequiresItemsPrice()
{
$invoice = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$invoice2 = factory(Invoice::class)->raw([
'items' => [factory(InvoiceItem::class)->raw(['price' => null])]
]);
$response = $this->json('PUT', 'api/invoices/'.$invoice->id, $invoice2);
$response->assertStatus(422)->assertJsonValidationErrors(['items.0.price']);
}
/** @test */
public function testDeleteInvoice()
{
$invoice = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$response = $this->json('DELETE', 'api/invoices/'.$invoice->id);
$response
->assertOk()
->assertJson([
'success' => true
]);
$deleted = Invoice::find($invoice->id);
$this->assertNull($deleted);
}
/** @test */
public function testSendInvoiceToCustomer()
{
$invoices = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$data = [
'id' => $invoices->id
];
$response = $this->json('POST', 'api/invoices/send', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
$invoice2 = Invoice::find($invoices->id);
$this->assertEquals($invoice2->status, Invoice::STATUS_SENT);
}
/** @test */
public function testInvoiceMarkAsPaid()
{
$invoice = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$data = [
'id' => $invoice->id
];
$response = $this->json('POST', 'api/invoices/mark-as-paid', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
$invoice2 = Invoice::find($invoice->id);
$this->assertEquals($invoice2->status, Invoice::STATUS_PAID);
}
/** @test */
public function testInvoiceMarkAsSent()
{
$invoice = factory(Invoice::class)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$data = [
'id' => $invoice->id
];
$response = $this->json('POST', 'api/invoices/mark-as-sent', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
$invoice2 = Invoice::find($invoice->id);
$this->assertEquals($invoice2->status, Invoice::STATUS_SENT);
}
/** @test */
public function testSearchInvoices()
{
$filters = [
'page' => 1,
'limit' => 15,
'search' => 'doe',
'status' => Invoice::STATUS_DRAFT,
'from_date' => '01/09/2019',
'to_date' => '11/09/2019',
'invoice_number' => '000012'
];
$queryString = http_build_query($filters, '', '&');
$response = $this->json('GET', 'api/invoices?'.$queryString);
$response->assertOk();
}
/** @test */
public function testDeleteMultipleInvoices()
{
$invoices = factory(Invoice::class,3)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
]);
$ids = $invoices->pluck('id');
$data = [
'id' => $ids,
'type' => 'invoice'
];
$response = $this->json('POST', 'api/invoices/delete', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
}
}
});

View File

@ -1,182 +1,140 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Crater\Item;
use Crater\User;
use Crater\Tax;
use Laravel\Passport\Passport;
use SettingsSeeder;
use Crater\Models\User;
use Crater\Models\Item;
use Crater\Models\Tax;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use Crater\Http\Requests\ItemsRequest;
use Crater\Http\Controllers\V1\Item\ItemsController;
use function Pest\Laravel\{postJson, putJson, getJson, deleteJson};
class ItemTest extends TestCase
{
use RefreshDatabase;
beforeEach(function () {
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
protected $user;
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Sanctum::actingAs(
$user,
['*']
);
});
public function setUp(): void
{
parent::setUp();
$this->seed();
$this->seed(SettingsSeeder::class);
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Passport::actingAs(
$user,
['*']
);
test('get items', function () {
$response = getJson('api/v1/items?page=1');
$response->assertOk();
});
test('create item', function () {
$item = Item::factory()->raw([
'taxes' => [
Tax::factory()->raw(),
Tax::factory()->raw()
]
]);
$response = postJson('api/v1/items', $item);
$this->assertDatabaseHas('items', [
'name' => $item['name'],
'description' => $item['description'],
'price' => $item['price'],
'company_id' => $item['company_id']
]);
$this->assertDatabaseHas('taxes', [
'item_id' => $response->getData()->item->id,
]);
$response->assertOk();
});
test('store validates using a form request', function () {
$this->assertActionUsesFormRequest(
ItemsController::class,
'store',
ItemsRequest::class
);
});
test('get item', function () {
$item = Item::factory()->create();
$response = getJson("api/v1/items/{$item->id}");
$response->assertOk();
$this->assertDatabaseHas('items', [
'name' => $item['name'],
'description' => $item['description'],
'price' => $item['price'],
'company_id' => $item['company_id']
]);
});
test('update item', function () {
$item = Item::factory()->create();
$update_item = Item::factory()->raw([
'taxes' => [
Tax::factory()->raw()
]
]);
$response = putJson('api/v1/items/' . $item->id, $update_item);
$response->assertOk();
$this->assertDatabaseHas('items', [
'name' => $update_item['name'],
'description' => $update_item['description'],
'price' => $update_item['price'],
'company_id' => $update_item['company_id']
]);
$this->assertDatabaseHas('taxes', [
'item_id' => $item->id,
]);
});
test('update validates using a form request', function () {
$this->assertActionUsesFormRequest(
ItemsController::class,
'update',
ItemsRequest::class
);
});
test('delete multiple items', function () {
$items = Item::factory()->count(5)->create();
$data = [
'ids' => $items->pluck('id')
];
postJson("/api/v1/items/delete", $data)->assertOk();
foreach ($items as $item) {
$this->assertDeleted($item);
}
});
/** @test */
public function testGetItems()
{
$response = $this->json('GET', 'api/items?page=1');
test('search items', function () {
$filters = [
'page' => 1,
'limit' => 15,
'search' => 'doe',
'price' => 6,
'unit' => 'kg'
];
$response->assertOk();
}
$queryString = http_build_query($filters, '', '&');
/** @test */
public function testCreateItem()
{
$item = factory(Item::class)->raw([
'taxes' => [
factory(Tax::class)->raw(),
factory(Tax::class)->raw()
]
]);
$response = $this->json('POST', 'api/items', $item);
$response = getJson('api/v1/items?' . $queryString);
$response->assertOk();
}
/** @test */
public function testItemNameRequired()
{
$item = factory(Item::class)->raw(['name' => '']);
$response = $this->json('POST', 'api/items', $item);
$response->assertStatus(422)->assertJsonValidationErrors(['name']);
}
/** @test */
public function testItemPriceRequired()
{
$item = factory(Item::class)->raw(['price' => '']);
$response = $this->json('POST', 'api/items', $item);
$response->assertStatus(422)->assertJsonValidationErrors(['price']);
}
/** @test */
public function testGetEditItemData()
{
$item = factory(Item::class)->create();
$response = $this->json('GET', 'api/items/'.$item->id.'/edit');
$response
->assertOk()
->assertJson([
'item' => $item->toArray()
]);
}
/** @test */
public function testUpdateItem()
{
$item = factory(Item::class)->create();
$item2 = factory(Item::class)->raw([
'taxes' => [
factory(Tax::class)->raw(['tax_type_id' => $item->taxes[0]->tax_type_id]),
factory(Tax::class)->raw(['tax_type_id' => $item->taxes[1]->tax_type_id])
]
]);
$response = $this->json('PUT', 'api/items/'.$item->id, $item2);
$item3 = $response->decodeResponseJson()['item'];
$response->assertOk();
}
/** @test */
public function testUpdateItemNameRequired()
{
$item = factory(Item::class)->create();
$item2 = factory(Item::class)->raw(['name' => '']);
$response = $this->json('PUT', 'api/items/'.$item->id, $item2);
$response->assertStatus(422)->assertJsonValidationErrors(['name']);
}
/** @test */
public function testUpdateItemPriceRequired()
{
$item = factory(Item::class)->create();
$item2 = factory(Item::class)->raw(['price' => '']);
$response = $this->json('PUT', 'api/items/'.$item->id, $item2);
$response->assertStatus(422)->assertJsonValidationErrors(['price']);
}
/** @test */
public function testDeleteItem()
{
$item = factory(Item::class)->create();
$response = $this->json('DELETE', 'api/items/'.$item->id);
$response
->assertOk()
->assertJson([
'success' => true
]);
}
/** @test */
public function testSearchItems()
{
$filters = [
'page' => 1,
'limit' => 15,
'search' => 'doe',
'price' => 6,
'unit' => 'kg'
];
$queryString = http_build_query($filters, '', '&');
$response = $this->json('GET', 'api/items?'.$queryString);
$response->assertOk();
}
/** @test */
public function testDeleteMultipleItems()
{
$items = factory(Item::class, 3)->create();
$ids = $items->pluck('id');
$data = [
'id' => $ids,
'type' => 'item'
];
$response = $this->json('POST', 'api/items/delete', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
}
}
$response->assertOk();
});

View File

@ -1,27 +1,26 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Crater\Country;
use SettingsSeeder;
class LocationTest extends TestCase
{
use RefreshDatabase;
use Crater\Models\User;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use function Pest\Laravel\getJson;
public function setUp(): void
{
parent::setUp();
$this->seed();
$this->seed(SettingsSeeder::class);
}
beforeEach(function () {
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
/** @test */
public function testGetCountries()
{
$response = $this->json('GET', 'api/countries');
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Sanctum::actingAs(
$user,
['*']
);
});
$response->assertOk();
}
}
test('get countries', function () {
$response = getJson('api/v1/countries');
$response->assertOk();
});

View File

@ -1,22 +0,0 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class LoginTest extends TestCase
{
/** @test */
public function testLogin()
{
$data = array(
'username' => 'admin@crater.in',
'password' => 'admin@123'
);
$response = $this->json('POST', 'api/auth/login', $data);
$response->assertOk();
}
}

View File

@ -0,0 +1,46 @@
<?php
use Crater\Models\User;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use function Pest\Laravel\{getJson};
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('next number', function () {
$key = 'invoice';
$response = getJson('api/v1/next-number?key=' . $key);
$response->assertStatus(200)->assertJson([
'nextNumber' => '000001'
]);
$key = 'estimate';
$response = getJson('api/v1/next-number?key=' . $key);
$response->assertStatus(200)->assertJson([
'nextNumber' => '000001'
]);
$key = 'payment';
$response = getJson('api/v1/next-number?key=' . $key);
$response->assertStatus(200)->assertJson([
'nextNumber' => '000001'
]);
});

View File

@ -0,0 +1,66 @@
<?php
use Crater\Models\Note;
use Crater\Models\User;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use function Pest\Laravel\{getJson, postJson, putJson, deleteJson};
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('retrieve notes', function () {
getJson('/api/v1/notes')->assertStatus(200);
});
test('create note', function () {
$note = Note::factory()->raw();
postJson('/api/v1/notes', $note)->assertStatus(200);
$this->assertDatabaseHas('notes', $note);
});
test('retrieve note', function () {
$note = Note::factory()->create();
getJson("/api/v1/notes/{$note->id}")
->assertStatus(200)
->assertJson([
'note' => $note->toArray()
]);
});
test('update note', function () {
$note = Note::factory()->create();
$data = Note::factory()->raw();
putJson("/api/v1/notes/{$note->id}", $data)
->assertStatus(200);
$this->assertDatabaseHas('notes', $data);
});
test('delete note', function () {
$note = Note::factory()->create();
deleteJson("/api/v1/notes/{$note->id}")
->assertStatus(200)
->assertJson([
'success' => true
]);
$this->assertDeleted($note);
});

View File

@ -1,111 +0,0 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Crater\Address;
class OnboardingTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
}
/** @test */
public function testGetOnboardingData()
{
$this->seed();
$response = $this->json('GET', 'api/admin/onboarding');
$response->assertOk();
$status = $response->decodeResponseJson()['profile_complete'];
$this->assertEquals($status, 0);
}
/** @test */
public function testUpdateOnboardingProfile()
{
$user = [
'name' => 'Jane Doe',
'password' => 'admin@123',
'email' => 'admin@crater.in'
];
$response = $this->json('POST', 'api/admin/onboarding/profile', $user);
$user2 = $response->decodeResponseJson()['user'];
$response->assertOk();
$this->assertEquals($user['name'], $user2['name']);
$this->assertEquals($user['email'], $user2['email']);
}
/** @test */
public function testGetOnboardingDataAfterProfileUpdate()
{
$response = $this->json('GET', 'api/admin/onboarding');
$response->assertOk();
$status = $response->decodeResponseJson()['profile_complete'];
$this->assertEquals($status, '1');
}
/** @test */
public function testUpdateCompanyDetails()
{
$company = [
'name' => 'XYZ',
'country_id' => 2
];
$response = $this->json('POST', 'api/admin/onboarding/company', $company);
$response->assertOk();
$company2 = $response->decodeResponseJson()['user']['company'];
$address2 = $response->decodeResponseJson()['user']['addresses'][0];
$this->assertEquals($company['name'], $company2['name']);
$this->assertEquals($company['country_id'], $address2['country_id']);
}
/** @test */
public function testGetOnboardingDataAfterCompanyUpdate()
{
$response = $this->json('GET', 'api/admin/onboarding');
$response->assertOk();
$status = $response->decodeResponseJson()['profile_complete'];
$this->assertEquals($status, '2');
}
/** @test */
public function testUpdateCompanySettings()
{
$settings = [
'currency' => 1,
'time_zone' => 'Asia/Kolkata',
'language' => 'en',
'fiscal_year' => '1-12',
'carbon_date_format' => 'Y/m/d',
'moment_date_format' => 'YYYY/MM/DD'
];
$response = $this->json('POST', 'api/admin/onboarding/settings', $settings);
$response->assertOk();
}
/** @test */
public function testGetOnboardingDataAfterSettingsUpdate()
{
$response = $this->json('GET', 'api/admin/onboarding');
$response->assertOk();
$status = $response->decodeResponseJson()['profile_complete'];
$this->assertEquals($status, 'COMPLETED');
}
}

View File

@ -0,0 +1,104 @@
<?php
use Crater\Models\User;
use Crater\Models\PaymentMethod;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use Crater\Http\Requests\PaymentMethodRequest;
use Crater\Http\Controllers\V1\Payment\PaymentMethodsController;
use function Pest\Laravel\{postJson, putJson, getJson, deleteJson};
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('get payment methods', function () {
$response = getJson('api/v1/payment-methods?page=1');
$response->assertOk();
});
test('create payment method', function () {
$data = [
'name' => 'demo name',
'company_id' => User::find(1)->company_id,
];
$response = postJson('api/v1/payment-methods', $data);
$response->assertOk();
$method = $response->decodeResponseJson()['paymentMethod'];
$this->assertDatabaseHas('payment_methods', [
'name' => $data['name'],
'company_id' => $data['company_id']
]);
});
test('store validates using a form request', function () {
$this->assertActionUsesFormRequest(
PaymentMethodsController::class,
'store',
PaymentMethodRequest::class
);
});
test('get payment method', function () {
$method = PaymentMethod::factory()->create();
$response = getJson("api/v1/payment-methods/{$method->id}");
$response->assertOk();
$this->assertDatabaseHas('payment_methods', [
'id' => $method->id,
'name' => $method['name'],
'company_id' => $method['company_id'],
]);
});
test('update payment method', function () {
$method = PaymentMethod::factory()->create();
$data = [
'name' => 'updated name'
];
$response = putJson("api/v1/payment-methods/{$method->id}", $data);
$response->assertOk();
$this->assertDatabaseHas('payment_methods', [
'id' => $method->id,
'name' => $data['name']
]);
});
test('update validates using a form request', function () {
$this->assertActionUsesFormRequest(
PaymentMethodsController::class,
'update',
PaymentMethodRequest::class
);
});
test('delete payment method', function () {
$method = PaymentMethod::factory()->create();
$response = deleteJson('api/v1/payment-methods/' . $method->id);
$response->assertOk();
$this->assertDeleted($method);
});

View File

@ -1,277 +1,146 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Crater\User;
use Crater\Invoice;
use Crater\Payment;
use Laravel\Passport\Passport;
use SettingsSeeder;
class PaymentTest extends TestCase
{
use RefreshDatabase;
protected $user;
public function setUp(): void
{
parent::setUp();
$this->seed();
$this->seed(SettingsSeeder::class);
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Passport::actingAs(
$user,
['*']
);
}
/** @test */
public function testGetPayments()
{
$response = $this->json('GET', 'api/payments?page=1');
$response->assertOk();
}
/** @test */
public function testGetCreatePaymentData()
{
$response = $this->json('GET', 'api/payments/create');
$response->assertOk();
}
/** @test */
public function testCreatePayment()
{
$payment = factory(Payment::class)->raw();
$response = $this->json('POST', 'api/payments', $payment);
$payment2 = $response->decodeResponseJson()['payment'];
$response->assertOk();
$this->assertEquals($payment['payment_number'], $payment2['payment_number']);
$this->assertEquals($payment['user_id'], $payment2['user_id']);
$this->assertEquals($payment['amount'], $payment2['amount']);
}
/** @test */
public function testCreatePaymentRequiresPaymentNumber()
{
$payment = factory(Payment::class)->raw([
'payment_number' => ''
]);
$response = $this->json('POST', 'api/payments', $payment);
$response->assertStatus(422)->assertJsonValidationErrors(['payment_number']);
}
/** @test */
public function testCreatePaymentRequiresAmount()
{
$payment = factory(Payment::class)->raw([
'amount' => ''
]);
$response = $this->json('POST', 'api/payments', $payment);
$response->assertStatus(422)->assertJsonValidationErrors(['amount']);
}
/** @test */
public function testCreatePaymentRequiresUser()
{
$payment = factory(Payment::class)->raw([
'user_id' => ''
]);
$response = $this->json('POST', 'api/payments', $payment);
$response->assertStatus(422)->assertJsonValidationErrors(['user_id']);
}
/** @test */
public function testCreatePaymentRequiresPaymentDate()
{
$payment = factory(Payment::class)->raw([
'payment_date' => ''
]);
$response = $this->json('POST', 'api/payments', $payment);
$response->assertStatus(422)->assertJsonValidationErrors(['payment_date']);
}
/** @test */
public function testGetEditPaymentData()
{
$payment = factory(Payment::class)->create([
'payment_date' => '1988-07-18'
]);
$response = $this->json('GET', 'api/payments/'.$payment->id.'/edit');
$response->assertOk();
}
/** @test */
public function testUpdateEstimate()
{
$payment = factory(Payment::class)->create([
'payment_date' => '1988-07-18'
]);
use Crater\Models\User;
use Crater\Models\Invoice;
use Crater\Models\Payment;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use Crater\Http\Requests\PaymentRequest;
use Crater\Http\Controllers\V1\Payment\PaymentsController;
use function Pest\Laravel\{postJson, putJson, getJson, deleteJson};
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('get payments', function () {
$response = getJson('api/v1/payments?page=1');
$response->assertOk();
});
test('get payment', function () {
$payment = Payment::factory()->create();
$response = getJson("api/v1/payments/{$payment->id}");
$response->assertStatus(200);
});
test('create payment', function () {
$invoice = Invoice::factory()->create([
'due_amount' => 100
]);
$payment = Payment::factory()->raw([
'invoice_id' => $invoice->id,
'payment_number' => "PAY-000001"
]);
$response = postJson('api/v1/payments', $payment);
$response->assertOk();
$this->assertDatabaseHas('payments', [
'payment_number' => $payment['payment_number'],
'user_id' => $payment['user_id'],
'amount' => $payment['amount'],
'company_id' => $payment['company_id'],
]);
});
test('store validates using a form request', function () {
$this->assertActionUsesFormRequest(
PaymentsController::class,
'store',
PaymentRequest::class
);
});
test('update payment', function () {
$payment = Payment::factory()->create([
'payment_date' => '1988-08-18'
]);
$payment2 = Payment::factory()->raw([
'payment_number' => $payment->payment_number,
]);
$response = putJson("api/v1/payments/{$payment->id}", $payment2);
$response->assertOk();
$this->assertDatabaseHas('payments', [
'id' => $payment->id,
'payment_number' => $payment2['payment_number'],
'user_id' => $payment2['user_id'],
'amount' => $payment2['amount'],
]);
});
test('update validates using a form request', function () {
$this->assertActionUsesFormRequest(
PaymentsController::class,
'update',
PaymentRequest::class
);
});
test('search payments', function () {
$filters = [
'page' => 1,
'limit' => 15,
'search' => 'doe',
'payment_number' => 'PAY-000001',
'payment_mode' => 'OTHER'
];
$queryString = http_build_query($filters, '', '&');
$response = getJson('api/v1/payments?' . $queryString);
$response->assertOk();
});
test('send payment to customer', function () {
$payment = Payment::factory()->create();
$data = [
'subject' => 'test',
'body' => 'test',
'from' => 'john@example.com',
'to' => 'doe@example.com'
];
$payment2 = factory(Payment::class)->raw([
'payment_number' => $payment->payment_number
]);
$response = $this->json('PUT', 'api/payments/'.$payment->id, $payment2);
$payment3 = $response->decodeResponseJson()['payment'];
$response->assertOk();
$this->assertEquals($payment3['payment_number'], $payment2['payment_number']);
$this->assertEquals($payment3['user_id'], $payment2['user_id']);
$this->assertEquals($payment3['amount'], $payment2['amount']);
}
/** @test */
public function testUpdatePaymentRequiresPaymentDate()
{
$payment = factory(Payment::class)->create([
'payment_date' => '1988-07-18'
]);
$payment2 = factory(Payment::class)->raw(['payment_date' => '']);
$response = $this->json('PUT', 'api/payments/'.$payment->id, $payment2);
$response->assertStatus(422)->assertJsonValidationErrors(['payment_date']);
}
/** @test */
public function testUpdatePaymentRequiresPaymentNumber()
{
$payment = factory(Payment::class)->create([
'payment_date' => '1988-07-18'
]);
$payment2 = factory(Payment::class)->raw(['payment_number' => '']);
$response = $this->json('PUT', 'api/payments/'.$payment->id, $payment2);
$response->assertStatus(422)->assertJsonValidationErrors(['payment_number']);
}
/** @test */
public function testUpdatePaymentRequiresAmount()
{
$payment = factory(Payment::class)->create([
'payment_date' => '1988-07-18'
]);
$payment2 = factory(Payment::class)->raw(['amount' => '']);
$response = $this->json('PUT', 'api/payments/'.$payment->id, $payment2);
$response->assertStatus(422)->assertJsonValidationErrors(['amount']);
}
/** @test */
public function testUpdatePaymentRequiresUser()
{
$payment = factory(Payment::class)->create([
'payment_date' => '1988-07-18'
]);
$payment2 = factory(Payment::class)->raw(['user_id' => '']);
$response = $this->json('PUT', 'api/payments/'.$payment->id, $payment2);
$response->assertStatus(422)->assertJsonValidationErrors(['user_id']);
}
/** @test */
public function testDeletePayment()
{
$payment = factory(Payment::class)->create([
'payment_date' => '1988-07-18'
]);
$response = $this->json('DELETE', 'api/payments/'.$payment->id);
$response
->assertOk()
->assertJson([
'success' => true
]);
$payment = Payment::find($payment->id);
$this->assertNull($payment);
}
/** @test */
public function testSearchPayments()
{
$filters = [
'page' => 1,
'limit' => 15,
'search' => 'doe',
'payment_number' => 'PAY-000001',
'payment_mode' => 'OTHER'
];
$queryString = http_build_query($filters, '', '&');
$response = $this->json('GET', 'api/payments?'.$queryString);
$response->assertOk();
}
/** @test */
public function getUnpaidInvoicesOfUser()
{
$user = factory(User::class)->create();
$invoices = factory(Invoice::class, 2)->create([
'invoice_date' => '1988-07-18',
'due_date' => '1988-08-18',
'user_id' => $user->id
]);
$response = $this->json('GET', 'api/invoices/unpaid/'.$user->id);
$response->assertOk();
}
/** @test */
public function testDeleteMultiplePayments()
{
$payments = factory(Payment::class, 3)->create([
'payment_date' => '1988-07-18'
]);
$ids = $payments->pluck('id');
$data = [
'id' => $ids,
'type' => 'payment'
];
$response = $this->json('POST', 'api/payments/delete', $data);
$response
->assertOk()
->assertJson([
'success' => true
]);
}
}
$response = postJson("api/v1/payments/{$payment->id}/send", $data) ;
$response->assertJson([
'success' => true
]);
});
test('delete payment', function () {
$payments = Payment::factory()->count(5)->create();
$ids = $payments->pluck('id');
$data = [
'ids' => $ids
];
$response = postJson('api/v1/payments/delete', $data);
$response->assertJson([
'success' => true
]);
});

View File

@ -1,117 +1,96 @@
<?php
namespace Tests\Feature;
use Crater\Models\User;
use Crater\Models\Company;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use function Pest\Laravel\getJson;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Crater\User;
use Crater\Company;
use Laravel\Passport\Passport;
use SettingsSeeder;
beforeEach(function () {
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
class ReportTest extends TestCase
{
use RefreshDatabase;
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Sanctum::actingAs(
$user,
['*']
);
});
protected $user;
test('get customer sales report', function () {
$filters = [
'page' => 1,
'limit' => 15,
'from_date' => '2020-07-18',
'to_date' => '2020-07-20',
];
$queryString = http_build_query($filters, '', '&');
$queryString = Company::find(1)->unique_hash . '?' . $queryString;
public function setUp(): void
{
parent::setUp();
$this->seed();
$this->seed(SettingsSeeder::class);
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Passport::actingAs(
$user,
['*']
);
}
$response = getJson('reports/sales/customers/'. $queryString);
/** @test */
public function testGetCustomerSalesReport()
{
$filters = [
'page' => 1,
'limit' => 15,
'from_date' => '01/02/2019',
'to_date' => '10/02/2019',
];
$queryString = http_build_query($filters, '', '&');
$queryString = Company::find(1)->unique_hash . '?' . $queryString;
$response->assertOk();
});
$response = $this->json('GET', 'reports/sales/customers/'. $queryString);
test('get item sales report', function () {
$filters = [
'page' => 1,
'limit' => 15,
'from_date' => '2020-07-18',
'to_date' => '2020-07-20',
];
$queryString = http_build_query($filters, '', '&');
$queryString = Company::find(1)->unique_hash . '?' . $queryString;
$response->assertOk();
}
$response = getJson('reports/sales/items/' . $queryString);
/** @test */
public function testGetItemSalesReport()
{
$filters = [
'page' => 1,
'limit' => 15,
'from_date' => '01/02/2019',
'to_date' => '10/02/2019',
];
$queryString = http_build_query($filters, '', '&');
$queryString = Company::find(1)->unique_hash . '?' . $queryString;
$response->assertOk();
});
$response = $this->json('GET', 'reports/sales/items/' . $queryString);
test('get expenses report', function () {
$filters = [
'page' => 1,
'limit' => 15,
'from_date' => '2020-07-18',
'to_date' => '2020-07-20',
];
$queryString = http_build_query($filters, '', '&');
$queryString = Company::find(1)->unique_hash . '?' . $queryString;
$response->assertOk();
}
$response = getJson('reports/expenses/' . $queryString);
/** @test */
public function testGetExpensesReport()
{
$filters = [
'page' => 1,
'limit' => 15,
'from_date' => '01/02/2019',
'to_date' => '10/02/2019',
];
$queryString = http_build_query($filters, '', '&');
$queryString = Company::find(1)->unique_hash . '?' . $queryString;
$response->assertOk();
});
$response = $this->json('GET', 'reports/expenses/' . $queryString);
test('get tax summary', function () {
$filters = [
'page' => 1,
'limit' => 15,
'from_date' => '2020-07-18',
'to_date' => '2020-07-20',
];
$queryString = http_build_query($filters, '', '&');
$queryString = Company::find(1)->unique_hash . '?' . $queryString;
$response->assertOk();
}
$response = getJson('reports/tax-summary/' . $queryString);
/** @test */
public function testGetTaxSummary()
{
$filters = [
'page' => 1,
'limit' => 15,
'from_date' => '01/02/2019',
'to_date' => '10/02/2019',
];
$queryString = http_build_query($filters, '', '&');
$queryString = Company::find(1)->unique_hash . '?' . $queryString;
$response->assertOk();
});
$response = $this->json('GET', 'reports/tax-summary/' . $queryString);
test('get profit loss', function () {
$filters = [
'page' => 1,
'limit' => 15,
'from_date' => '2020-07-18',
'to_date' => '2020-07-20',
];
$queryString = http_build_query($filters, '', '&');
$queryString = Company::find(1)->unique_hash . '?' . $queryString;
$response->assertOk();
}
$response = getJson('reports/profit-loss/' . $queryString);
/** @test */
public function testGetProfitLoss()
{
$filters = [
'page' => 1,
'limit' => 15,
'from_date' => '01/02/2019',
'to_date' => '10/02/2019',
];
$queryString = http_build_query($filters, '', '&');
$queryString = Company::find(1)->unique_hash . '?' . $queryString;
$response->assertOk();
});
$response = $this->json('GET', 'reports/profit-loss/' . $queryString);
$response->assertOk();
}
}

View File

@ -1,138 +1,91 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Crater\User;
use Crater\TaxType;
use Laravel\Passport\Passport;
use SettingsSeeder;
use Crater\Models\User;
use Crater\Models\TaxType;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use Crater\Http\Requests\TaxTypeRequest;
use Crater\Http\Controllers\V1\Settings\TaxTypesController;
class TaxTypeTest extends TestCase
{
use RefreshDatabase;
use function Pest\Laravel\{postJson, putJson, getJson, deleteJson};
protected $user;
beforeEach(function () {
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
public function setUp(): void
{
parent::setUp();
$this->seed();
$this->seed(SettingsSeeder::class);
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
$user = User::find(1);
$this->withHeaders([
'company' => $user->company_id,
]);
Sanctum::actingAs(
$user,
['*']
);
});
test('get tax types', function () {
$response = getJson('api/v1/tax-types');
$response->assertOk();
});
test('create tax type', function () {
$taxType = TaxType::factory()->raw();
postJson('api/v1/tax-types', $taxType);
$this->assertDatabaseHas('tax_types', $taxType);
});
test('store validates using a form request', function () {
$this->assertActionUsesFormRequest(
TaxTypesController::class,
'store',
TaxTypeRequest::class
);
});
test('get tax type', function () {
$taxType = TaxType::factory()->create();
$response = getJson('api/v1/tax-types/' . $taxType->id);
$response->assertOk()
->assertJson([
'taxType' => $taxType->toArray()
]);
Passport::actingAs(
$user,
['*']
);
}
});
/** @test */
public function testGetTaxTypes()
{
$response = $this->json('GET', 'api/tax-types');
test('update tax type', function () {
$taxType = TaxType::factory()->create();
$response->assertOk();
}
$taxType1 = TaxType::factory()->raw();
/** @test */
public function testCreateTaxType()
{
$taxType = factory(TaxType::class)->raw();
$response = putJson('api/v1/tax-types/' . $taxType->id, $taxType1);
$response = $this->json('POST', 'api/tax-types', $taxType);
$response->assertOk()
->assertJson([
'taxType' => $taxType1
]);
});
$response->assertOk()
->assertJson([
'taxType' => $taxType
]);
}
test('update validates using a form request', function () {
$this->assertActionUsesFormRequest(
TaxTypesController::class,
'update',
TaxTypeRequest::class
);
});
/** @test */
public function testCreateTaxTypeRequiresName()
{
$taxType = factory(TaxType::class)->raw(['name' => '']);
test('delete tax type', function () {
$taxType = TaxType::factory()->create();
$response = $this->json('POST', 'api/tax-types', $taxType);
$response = deleteJson('api/v1/tax-types/' . $taxType->id);
$response->assertStatus(422)->assertJsonValidationErrors(['name']);
}
$response->assertOk()
->assertJson([
'success' => true
]);
/** @test */
public function testCreateTaxTypeRequiresPercent()
{
$taxType = factory(TaxType::class)->raw(['percent' => '']);
$response = $this->json('POST', 'api/tax-types', $taxType);
$response->assertStatus(422)->assertJsonValidationErrors(['percent']);
}
/** @test */
public function testGetEditTaxTypeData()
{
$taxType = factory(TaxType::class)->create();
$response = $this->json('GET', 'api/tax-types/'.$taxType->id.'/edit');
$response->assertOk()
->assertJson([
'taxType' => $taxType->toArray()
]);
}
/** @test */
public function testUpdateTaxType()
{
$taxType = factory(TaxType::class)->create();
$taxType2 = factory(TaxType::class)->raw();
$response = $this->json('PUT', 'api/tax-types/'.$taxType->id, $taxType2);
$response->assertOk()
->assertJson([
'taxType' => $taxType2
]);
}
/** @test */
public function testUpdateTaxTypeRequiresName()
{
$taxType = factory(TaxType::class)->create();
$taxType2 = factory(TaxType::class)->raw(['name' => '']);
$response = $this->json('PUT', 'api/tax-types/'.$taxType->id, $taxType2);
$response->assertStatus(422)->assertJsonValidationErrors(['name']);
}
/** @test */
public function testUpdateTaxTypeRequiresPercent()
{
$taxType = factory(TaxType::class)->create();
$taxType2 = factory(TaxType::class)->raw(['percent' => '']);
$response = $this->json('PUT', 'api/tax-types/'.$taxType->id, $taxType2);
$response->assertStatus(422)->assertJsonValidationErrors(['percent']);
}
/** @test */
public function testDeleteTaxType()
{
$taxType = factory(TaxType::class)->create();
$response = $this->json('DELETE', 'api/tax-types/'.$taxType->id);
$response->assertOk()
->assertJson([
'success' => true
]);
$this->assertNull(TaxType::find($taxType->id));
}
}
$this->assertDeleted($taxType);
});

View File

@ -0,0 +1,98 @@
<?php
use Crater\Models\User;
use Crater\Models\Unit;
use Illuminate\Support\Facades\Artisan;
use Laravel\Sanctum\Sanctum;
use Crater\Http\Requests\UnitRequest;
use Crater\Http\Controllers\V1\Item\UnitsController;
use function Pest\Laravel\{postJson, putJson, getJson, deleteJson};
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('get units', function () {
$response = getJson('api/v1/units?page=1');
$response->assertOk();
});
test('create unit', function () {
$data = [
'name' => 'unit name',
'company_id' => User::find(1)->company_id
];
$response = postJson('api/v1/units', $data);
$response->assertOk();
$this->assertDatabaseHas('units', $data);
});
test('store validates using a form request', function () {
$this->assertActionUsesFormRequest(
UnitsController::class,
'store',
UnitRequest::class
);
});
test('get unit', function () {
$unit = Unit::factory()->create();
$response = getJson("api/v1/units/{$unit->id}");
$response->assertOk();
$this->assertDatabaseHas('units', [
'id' => $unit->id,
'name' => $unit['name'],
]);
});
test('update unit', function () {
$unit = Unit::factory()->create();
$update_unit = [
'name' => 'new name',
];
$response = putJson("api/v1/units/{$unit->id}", $update_unit);
$response->assertOk();
$this->assertDatabaseHas('units', [
'id' => $unit->id,
'name' => $update_unit['name'],
]);
});
test('update validates using a form request', function () {
$this->assertActionUsesFormRequest(
UnitsController::class,
'update',
UnitRequest::class
);
});
test('delete unit', function () {
$unit = Unit::factory()->create();
$response = deleteJson("api/v1/units/{$unit->id}");
$response->assertOk();
$this->assertDeleted($unit);
});

View File

@ -0,0 +1,94 @@
<?php
use Crater\Http\Controllers\V1\Users\UsersController;
use Crater\Http\Requests\UserRequest;
use Crater\Models\User;
use Laravel\Sanctum\Sanctum;
use function Pest\Faker\faker;
use function Pest\Laravel\getJson;
use function Pest\Laravel\postJson;
use function Pest\Laravel\putJson;
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,
['*']
);
});
getJson('/api/v1/users')->assertOk();
test('store user using a form request', function () {
$this->assertActionUsesFormRequest(
UsersController::class,
'store',
UserRequest::class
);
});
test('store user', function () {
$data = [
'name' => faker()->name,
'email' => faker()->unique()->safeEmail,
'phone' => faker()->phoneNumber,
'password' => faker()->password
];
postJson('/api/v1/users', $data)->assertOk();
$this->assertDatabaseHas('users', [
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['phone'],
]);
});
test('get user', function () {
$user = User::factory()->create();
getJson("/api/v1/users/{$user->id}")->assertOk();
});
test('update user using a form request', function () {
$this->assertActionUsesFormRequest(
UsersController::class,
'update',
UserRequest::class
);
});
test('update user', function () {
$user = User::factory()->create();
$data = [
'name' => faker()->name,
'email' => faker()->unique()->safeEmail,
'phone' => faker()->phoneNumber,
'password' => faker()->password
];
putJson("/api/v1/users/{$user->id}", $data)->assertOk();
$this->assertDatabaseHas('users', [
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['phone'],
]);
});
test('delete users', function () {
$user = User::factory()->create();
$data['users'] = [$user->id];
postJson("/api/v1/users/delete", $data)->assertOk();
$this->assertDeleted($user);
});