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,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);
});