Files
InvoiceShelf/tests/Unit/EstimateTest.php
mchev 3259173066 Laravel 11 (#84)
* Convert string references to `::class`

PHP 5.5.9 adds the new static `class` property which provides the fully qualified class name. This is preferred over using strings for class names since the `class` property references are checked by PHP.

* Use Faker methods

Accessing Faker properties was deprecated in Faker 1.14.

* Convert route options to fluent methods

Laravel 8 adopts the tuple syntax for controller actions. Since the old options array is incompatible with this syntax, Shift converted them to use modern, fluent methods.

* Adopt class based routes

* Remove default `app` files

* Shift core files

* Streamline config files

* Set new `ENV` variables

* Default new `bootstrap/app.php`

* Re-register HTTP middleware

* Consolidate service providers

* Re-register service providers

* Re-register routes

* Re-register scheduled commands

* Bump Composer dependencies

* Use `<env>` tags for configuration

`<env>` tags have a lower precedence than system environment variables making it easier to overwrite PHPUnit configuration values in additional environments, such a CI.

Review this blog post for more details on configuration precedence when testing Laravel: https://jasonmccreary.me/articles/laravel-testing-configuration-precedence/

* Adopt anonymous migrations

* Rename `password_resets` table

* Convert `$casts` property to method

* Adopt Laravel type hints

* Mark base controller as `abstract`

* Remove `CreatesApplication` testing trait

* Shift cleanup

* Fix shift first issues

* Updating Rules for laravel 11, sanctum config and pint

* Fix Carbon issue on dashboard

* Temporary fix for tests while migration is issue fixed on laravel side

* Carbon needs numerical values, not strings

* Minimum php version

* Fix domain installation step not fetching the correct company_id

* Fix Role Policy wasn't properly registered

---------
2024-06-05 11:33:52 +02:00

185 lines
5.1 KiB
PHP

<?php
use App\Http\Requests\EstimatesRequest;
use App\Models\Estimate;
use App\Models\EstimateItem;
use App\Models\Tax;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
beforeEach(function () {
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
Artisan::call('db:seed', ['--class' => 'DemoSeeder', '--force' => true]);
});
test('estimate has many estimate items', function () {
$estimate = Estimate::factory()->create();
$estimate = Estimate::factory()->hasItems(5)->create();
$this->assertCount(5, $estimate->items);
$this->assertTrue($estimate->items()->exists());
});
test('estimate belongs to customer', function () {
$estimate = Estimate::factory()->forCustomer()->create();
$this->assertTrue($estimate->customer()->exists());
});
test('estimate has many taxes', function () {
$estimate = Estimate::factory()->hasTaxes(5)->create();
$this->assertCount(5, $estimate->taxes);
$this->assertTrue($estimate->taxes()->exists());
});
test('create estimate', function () {
$estimate = Estimate::factory()->raw();
$item = EstimateItem::factory()->raw();
$estimate['items'] = [];
array_push($estimate['items'], $item);
$estimate['taxes'] = [];
array_push($estimate['taxes'], Tax::factory()->raw());
$request = new EstimatesRequest();
$request->replace($estimate);
$response = Estimate::createEstimate($request);
$this->assertDatabaseHas('estimate_items', [
'estimate_id' => $response->id,
'name' => $item['name'],
'description' => $item['description'],
'price' => $item['price'],
'quantity' => $item['quantity'],
'total' => $item['total'],
]);
$this->assertDatabaseHas('estimates', [
'estimate_number' => $estimate['estimate_number'],
'customer_id' => $estimate['customer_id'],
'template_name' => $estimate['template_name'],
'sub_total' => $estimate['sub_total'],
'total' => $estimate['total'],
'discount' => $estimate['discount'],
'discount_type' => $estimate['discount_type'],
'discount_val' => $estimate['discount_val'],
'tax' => $estimate['tax'],
'notes' => $estimate['notes'],
]);
});
test('update estimate', function () {
$estimate = Estimate::factory()->hasItems()->hasTaxes()->create();
$newEstimate = Estimate::factory()->raw();
$item = EstimateItem::factory()->raw([
'estimate_id' => $estimate->id,
]);
$newEstimate['items'] = [];
$newEstimate['taxes'] = [];
array_push($newEstimate['items'], $item);
array_push($newEstimate['taxes'], Tax::factory()->raw());
$request = new EstimatesRequest();
$request->replace($newEstimate);
$estimate_number = explode('-', $newEstimate['estimate_number']);
$number_attributes['estimate_number'] = $estimate_number[0].'-'.sprintf('%06d', intval($estimate_number[1]));
$estimate->updateEstimate($request);
$this->assertDatabaseHas('estimate_items', [
'estimate_id' => $estimate->id,
'name' => $item['name'],
'description' => $item['description'],
'price' => $item['price'],
'total' => $item['total'],
'quantity' => $item['quantity'],
]);
$this->assertDatabaseHas('estimates', [
'estimate_number' => $newEstimate['estimate_number'],
'customer_id' => $newEstimate['customer_id'],
'template_name' => $newEstimate['template_name'],
'sub_total' => $newEstimate['sub_total'],
'total' => $newEstimate['total'],
'discount' => $newEstimate['discount'],
'discount_type' => $newEstimate['discount_type'],
'discount_val' => $newEstimate['discount_val'],
'tax' => $newEstimate['tax'],
'notes' => $newEstimate['notes'],
]);
});
test('create items', function () {
$estimate = Estimate::factory()->create();
$items = [];
$item = EstimateItem::factory()->raw([
'invoice_id' => $estimate->id,
]);
array_push($items, $item);
$request = new Request();
$request->replace(['items' => $items]);
Estimate::createItems($estimate, $request, $estimate->exchange_rate);
$this->assertDatabaseHas('estimate_items', [
'estimate_id' => $estimate->id,
'description' => $item['description'],
'price' => $item['price'],
'tax' => $item['tax'],
'quantity' => $item['quantity'],
'total' => $item['total'],
]);
$this->assertCount(1, $estimate->items);
});
test('create taxes', function () {
$estimate = Estimate::factory()->create();
$taxes = [];
$tax1 = Tax::factory()->raw([
'estimate_id' => $estimate->id,
]);
$tax2 = Tax::factory()->raw([
'estimate_id' => $estimate->id,
]);
array_push($taxes, $tax1);
array_push($taxes, $tax2);
$request = new Request();
$request->replace(['taxes' => $taxes]);
Estimate::createTaxes($estimate, $request, $estimate->exchange_rate);
$this->assertCount(2, $estimate->taxes);
$this->assertDatabaseHas('taxes', [
'estimate_id' => $estimate->id,
'name' => $tax1['name'],
'amount' => $tax1['amount'],
]);
});