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

@ -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
]);
}
}
});