From d20f5d2d56e038fd8f6867f67c4008ea99df4d0c Mon Sep 17 00:00:00 2001 From: EnesSacid-Buker Date: Thu, 15 Dec 2022 12:21:04 +0300 Subject: [PATCH 1/5] Tests are refactored --- tests/Feature/Banking/TransactionsTest.php | 21 +++++++++++---------- tests/Feature/Common/ItemsTest.php | 21 +++++++++++---------- tests/Feature/Purchases/BillsTest.php | 21 +++++++++++---------- tests/Feature/Purchases/VendorsTest.php | 21 +++++++++++---------- tests/Feature/Sales/CustomersTest.php | 21 +++++++++++---------- tests/Feature/Sales/InvoicesTest.php | 21 +++++++++++---------- 6 files changed, 66 insertions(+), 60 deletions(-) diff --git a/tests/Feature/Banking/TransactionsTest.php b/tests/Feature/Banking/TransactionsTest.php index 18b65e2a8..eabe05ef6 100644 --- a/tests/Feature/Banking/TransactionsTest.php +++ b/tests/Feature/Banking/TransactionsTest.php @@ -7,6 +7,7 @@ use App\Jobs\Banking\CreateTransaction; use App\Models\Banking\Transaction; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\File; +use Maatwebsite\Excel\Facades\Excel; use Tests\Feature\FeatureTestCase; class TransactionsTest extends FeatureTestCase @@ -119,16 +120,16 @@ class TransactionsTest extends FeatureTestCase $count = 5; Transaction::factory()->income()->count($count)->create(); - \Excel::fake(); + Excel::fake(); $this->loginAs() ->get(route('transactions.export')) ->assertStatus(200); - \Excel::matchByRegex(); + Excel::matchByRegex(); - \Excel::assertDownloaded( - '/' . \Str::filename(trans_choice('general.transactions', 2)) . '-\d{10}\.xlsx/', + Excel::assertDownloaded( + '/' . str()->filename(trans_choice('general.transactions', 2), '-') . '-\d{10}\.xlsx/', function (Export $export) use ($count) { // Assert that the correct export is downloaded. return $export->collection()->count() === $count; @@ -143,7 +144,7 @@ class TransactionsTest extends FeatureTestCase $transactions = Transaction::factory()->income()->count($create_count)->create(); - \Excel::fake(); + Excel::fake(); $this->loginAs() ->post( @@ -152,10 +153,10 @@ class TransactionsTest extends FeatureTestCase ) ->assertStatus(200); - \Excel::matchByRegex(); + Excel::matchByRegex(); - \Excel::assertDownloaded( - '/' . \Str::filename(trans_choice('general.transactions', 2)) . '-\d{10}\.xlsx/', + Excel::assertDownloaded( + '/' . str()->filename(trans_choice('general.transactions', 2), '-') . '-\d{10}\.xlsx/', function (Export $export) use ($select_count) { return $export->collection()->count() === $select_count; } @@ -164,7 +165,7 @@ class TransactionsTest extends FeatureTestCase public function testItShouldImportTransactions() { - \Excel::fake(); + Excel::fake(); $this->loginAs() ->post( @@ -178,7 +179,7 @@ class TransactionsTest extends FeatureTestCase ) ->assertStatus(200); - \Excel::assertImported('transactions.xlsx'); + Excel::assertImported('transactions.xlsx'); $this->assertFlashLevel('success'); } diff --git a/tests/Feature/Common/ItemsTest.php b/tests/Feature/Common/ItemsTest.php index e83900502..d1038b589 100644 --- a/tests/Feature/Common/ItemsTest.php +++ b/tests/Feature/Common/ItemsTest.php @@ -7,6 +7,7 @@ use App\Jobs\Common\CreateItem; use App\Models\Common\Item; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\File; +use Maatwebsite\Excel\Facades\Excel; use Tests\Feature\FeatureTestCase; class ItemsTest extends FeatureTestCase @@ -90,16 +91,16 @@ class ItemsTest extends FeatureTestCase $count = 5; Item::factory()->count($count)->create(); - \Excel::fake(); + Excel::fake(); $this->loginAs() ->get(route('items.export')) ->assertStatus(200); - \Excel::matchByRegex(); + Excel::matchByRegex(); - \Excel::assertDownloaded( - '/' . \Str::filename(trans_choice('general.items', 2)) . '-\d{10}\.xlsx/', + Excel::assertDownloaded( + '/' . str()->filename(trans_choice('general.items', 2), '-') . '-\d{10}\.xlsx/', function (Export $export) use ($count) { // Assert that the correct export is downloaded. return $export->sheets()[0]->collection()->count() === $count; @@ -114,7 +115,7 @@ class ItemsTest extends FeatureTestCase $items = Item::factory()->count($create_count)->create(); - \Excel::fake(); + Excel::fake(); $this->loginAs() ->post( @@ -123,10 +124,10 @@ class ItemsTest extends FeatureTestCase ) ->assertStatus(200); - \Excel::matchByRegex(); + Excel::matchByRegex(); - \Excel::assertDownloaded( - '/' . \Str::filename(trans_choice('general.items', 2)) . '-\d{10}\.xlsx/', + Excel::assertDownloaded( + '/' . str()->filename(trans_choice('general.items', 2), '-') . '-\d{10}\.xlsx/', function (Export $export) use ($select_count) { // Assert that the correct export is downloaded. return $export->sheets()[0]->collection()->count() === $select_count; @@ -136,7 +137,7 @@ class ItemsTest extends FeatureTestCase public function testItShouldImportItems() { - \Excel::fake(); + Excel::fake(); $this->loginAs() ->post( @@ -150,7 +151,7 @@ class ItemsTest extends FeatureTestCase ) ->assertStatus(200); - \Excel::assertImported('items.xlsx'); + Excel::assertImported('items.xlsx'); $this->assertFlashLevel('success'); } diff --git a/tests/Feature/Purchases/BillsTest.php b/tests/Feature/Purchases/BillsTest.php index ed5d35c7a..050b20d25 100644 --- a/tests/Feature/Purchases/BillsTest.php +++ b/tests/Feature/Purchases/BillsTest.php @@ -9,6 +9,7 @@ use Illuminate\Http\UploadedFile; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Storage; +use Maatwebsite\Excel\Facades\Excel; use Tests\Feature\FeatureTestCase; class BillsTest extends FeatureTestCase @@ -170,16 +171,16 @@ class BillsTest extends FeatureTestCase $count = 5; Document::factory()->bill()->count($count)->create(); - \Excel::fake(); + Excel::fake(); $this->loginAs() ->get(route('bills.export')) ->assertStatus(200); - \Excel::matchByRegex(); + Excel::matchByRegex(); - \Excel::assertDownloaded( - '/' . \Str::filename(trans_choice('general.bills', 2)) . '-\d{10}\.xlsx/', + Excel::assertDownloaded( + '/' . str()->filename(trans_choice('general.bills', 2), '-') . '-\d{10}\.xlsx/', function (Export $export) use ($count) { // Assert that the correct export is downloaded. return $export->sheets()[0]->collection()->count() === $count; @@ -194,7 +195,7 @@ class BillsTest extends FeatureTestCase $bills = Document::factory()->bill()->count($create_count)->create(); - \Excel::fake(); + Excel::fake(); $this->loginAs() ->post( @@ -203,10 +204,10 @@ class BillsTest extends FeatureTestCase ) ->assertStatus(200); - \Excel::matchByRegex(); + Excel::matchByRegex(); - \Excel::assertDownloaded( - '/' . \Str::filename(trans_choice('general.bills', 2)) . '-\d{10}\.xlsx/', + Excel::assertDownloaded( + '/' . str()->filename(trans_choice('general.bills', 2), '-') . '-\d{10}\.xlsx/', function (Export $export) use ($select_count) { return $export->sheets()[0]->collection()->count() === $select_count; } @@ -215,7 +216,7 @@ class BillsTest extends FeatureTestCase public function testItShouldImportBills() { - \Excel::fake(); + Excel::fake(); $this->loginAs() ->post( @@ -229,7 +230,7 @@ class BillsTest extends FeatureTestCase ) ->assertStatus(200); - \Excel::assertImported('bills.xlsx'); + Excel::assertImported('bills.xlsx'); $this->assertFlashLevel('success'); } diff --git a/tests/Feature/Purchases/VendorsTest.php b/tests/Feature/Purchases/VendorsTest.php index 2a14a4e37..19f2acb58 100644 --- a/tests/Feature/Purchases/VendorsTest.php +++ b/tests/Feature/Purchases/VendorsTest.php @@ -7,6 +7,7 @@ use App\Jobs\Common\CreateContact; use App\Models\Common\Contact; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\File; +use Maatwebsite\Excel\Facades\Excel; use Tests\Feature\FeatureTestCase; class VendorsTest extends FeatureTestCase @@ -116,16 +117,16 @@ class VendorsTest extends FeatureTestCase Contact::factory()->vendor()->count(5)->create(); $count = Contact::vendor()->count(); - \Excel::fake(); + Excel::fake(); $this->loginAs() ->get(route('vendors.export')) ->assertStatus(200); - \Excel::matchByRegex(); + Excel::matchByRegex(); - \Excel::assertDownloaded( - '/' . \Str::filename(trans_choice('general.vendors', 2)) . '-\d{10}\.xlsx/', + Excel::assertDownloaded( + '/' . str()->filename(trans_choice('general.vendors', 2), '-') . '-\d{10}\.xlsx/', function (Export $export) use ($count) { // Assert that the correct export is downloaded. return $export->collection()->count() === $count; @@ -140,7 +141,7 @@ class VendorsTest extends FeatureTestCase $vendors = Contact::factory()->vendor()->count($create_count)->create(); - \Excel::fake(); + Excel::fake(); $this->loginAs() ->post( @@ -149,10 +150,10 @@ class VendorsTest extends FeatureTestCase ) ->assertStatus(200); - \Excel::matchByRegex(); + Excel::matchByRegex(); - \Excel::assertDownloaded( - '/' . \Str::filename(trans_choice('general.vendors', 2)) . '-\d{10}\.xlsx/', + Excel::assertDownloaded( + '/' . str()->filename(trans_choice('general.vendors', 2), '-') . '-\d{10}\.xlsx/', function (Export $export) use ($select_count) { return $export->collection()->count() === $select_count; } @@ -161,7 +162,7 @@ class VendorsTest extends FeatureTestCase public function testItShouldImportVendors() { - \Excel::fake(); + Excel::fake(); $this->loginAs() ->post( @@ -175,7 +176,7 @@ class VendorsTest extends FeatureTestCase ) ->assertStatus(200); - \Excel::assertImported('vendors.xlsx'); + Excel::assertImported('vendors.xlsx'); $this->assertFlashLevel('success'); } diff --git a/tests/Feature/Sales/CustomersTest.php b/tests/Feature/Sales/CustomersTest.php index ac2b19f3b..965d07039 100644 --- a/tests/Feature/Sales/CustomersTest.php +++ b/tests/Feature/Sales/CustomersTest.php @@ -8,6 +8,7 @@ use App\Models\Auth\User; use App\Models\Common\Contact; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\File; +use Maatwebsite\Excel\Facades\Excel; use Tests\Feature\FeatureTestCase; class CustomersTest extends FeatureTestCase @@ -137,16 +138,16 @@ class CustomersTest extends FeatureTestCase Contact::factory()->customer()->count(5)->create(); $count = Contact::customer()->count(); - \Excel::fake(); + Excel::fake(); $this->loginAs() ->get(route('customers.export')) ->assertStatus(200); - \Excel::matchByRegex(); + Excel::matchByRegex(); - \Excel::assertDownloaded( - '/' . \Str::filename(trans_choice('general.customers', 2)) . '-\d{10}\.xlsx/', + Excel::assertDownloaded( + '/' . str()->filename(trans_choice('general.customers', 2), '-') . '-\d{10}\.xlsx/', function (Export $export) use ($count) { // Assert that the correct export is downloaded. return $export->collection()->count() === $count; @@ -161,7 +162,7 @@ class CustomersTest extends FeatureTestCase $customers = Contact::factory()->customer()->count($create_count)->create(); - \Excel::fake(); + Excel::fake(); $this->loginAs() ->post( @@ -170,10 +171,10 @@ class CustomersTest extends FeatureTestCase ) ->assertStatus(200); - \Excel::matchByRegex(); + Excel::matchByRegex(); - \Excel::assertDownloaded( - '/' . \Str::filename(trans_choice('general.customers', 2)) . '-\d{10}\.xlsx/', + Excel::assertDownloaded( + '/' . str()->filename(trans_choice('general.customers', 2), '-') . '-\d{10}\.xlsx/', function (Export $export) use ($select_count) { return $export->collection()->count() === $select_count; } @@ -182,7 +183,7 @@ class CustomersTest extends FeatureTestCase public function testItShouldImportCustomers() { - \Excel::fake(); + Excel::fake(); $this->loginAs() ->post( @@ -196,7 +197,7 @@ class CustomersTest extends FeatureTestCase ) ->assertStatus(200); - \Excel::assertImported('customers.xlsx'); + Excel::assertImported('customers.xlsx'); $this->assertFlashLevel('success'); } diff --git a/tests/Feature/Sales/InvoicesTest.php b/tests/Feature/Sales/InvoicesTest.php index 0e1558391..095824a72 100644 --- a/tests/Feature/Sales/InvoicesTest.php +++ b/tests/Feature/Sales/InvoicesTest.php @@ -9,6 +9,7 @@ use Illuminate\Http\UploadedFile; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Storage; +use Maatwebsite\Excel\Facades\Excel; use Tests\Feature\FeatureTestCase; class InvoicesTest extends FeatureTestCase @@ -181,16 +182,16 @@ class InvoicesTest extends FeatureTestCase $count = 5; Document::factory()->invoice()->count($count)->create(); - \Excel::fake(); + Excel::fake(); $this->loginAs() ->get(route('invoices.export')) ->assertStatus(200); - \Excel::matchByRegex(); + Excel::matchByRegex(); - \Excel::assertDownloaded( - '/' . \Str::filename(trans_choice('general.invoices', 2)) . '-\d{10}\.xlsx/', + Excel::assertDownloaded( + '/' . str()->filename(trans_choice('general.invoices', 2), '-') . '-\d{10}\.xlsx/', function (Export $export) use ($count) { // Assert that the correct export is downloaded. return $export->sheets()[0]->collection()->count() === $count; @@ -205,7 +206,7 @@ class InvoicesTest extends FeatureTestCase $invoices = Document::factory()->invoice()->count($create_count)->create(); - \Excel::fake(); + Excel::fake(); $this->loginAs() ->post( @@ -214,10 +215,10 @@ class InvoicesTest extends FeatureTestCase ) ->assertStatus(200); - \Excel::matchByRegex(); + Excel::matchByRegex(); - \Excel::assertDownloaded( - '/' . \Str::filename(trans_choice('general.invoices', 2)) . '-\d{10}\.xlsx/', + Excel::assertDownloaded( + '/' . str()->filename(trans_choice('general.invoices', 2), '-') . '-\d{10}\.xlsx/', function (Export $export) use ($select_count) { return $export->sheets()[0]->collection()->count() === $select_count; } @@ -226,7 +227,7 @@ class InvoicesTest extends FeatureTestCase public function testItShouldImportInvoices() { - \Excel::fake(); + Excel::fake(); $this->loginAs() ->post( @@ -240,7 +241,7 @@ class InvoicesTest extends FeatureTestCase ) ->assertStatus(200); - \Excel::assertImported('invoices.xlsx'); + Excel::assertImported('invoices.xlsx'); $this->assertFlashLevel('success'); } From 7cb22d5d2570f21afe33a6bad01274a84b5d3c13 Mon Sep 17 00:00:00 2001 From: EnesSacid-Buker Date: Fri, 16 Dec 2022 10:26:41 +0300 Subject: [PATCH 2/5] Transfer test refactored --- database/factories/Transfer.php | 47 ++++++------------------- tests/Feature/Banking/TransfersTest.php | 47 ++++++++++--------------- 2 files changed, 29 insertions(+), 65 deletions(-) diff --git a/database/factories/Transfer.php b/database/factories/Transfer.php index fce6ce92c..233bdb73c 100644 --- a/database/factories/Transfer.php +++ b/database/factories/Transfer.php @@ -4,15 +4,10 @@ namespace Database\Factories; use App\Abstracts\Factory; use App\Models\Banking\Account; -use App\Models\Banking\Transaction; use App\Models\Banking\Transfer as Model; -use App\Models\Setting\Category; -use App\Traits\Categories; class Transfer extends Factory { - use Categories; - /** * The name of the factory's corresponding model. * @@ -27,42 +22,20 @@ class Transfer extends Factory */ public function definition() { - $accounts = Account::enabled()->get(); + $from_account = Account::factory()->enabled()->default_currency()->create(); - if ($accounts->count() >= 2) { - $random = $accounts->random(2); - - $expense_account = $random->first(); - $income_account = $random->last(); - } else { - $expense_account = $accounts->first(); - - $income_account = Account::factory()->enabled()->default_currency()->create(); - } - - $request = [ - 'amount' => $this->faker->randomFloat(2, 1, 1000), - 'paid_at' => $this->faker->dateTimeBetween(now()->startOfYear(), now()->endOfYear())->format('Y-m-d'), - 'category_id' => $this->getTransferCategoryId(), - 'description' => $this->faker->text(20), - 'reference' => $this->faker->text(20), - 'created_from' => 'core::factory', - ]; - - $expense_transaction = Transaction::factory()->create(array_merge($request, [ - 'type' => Transaction::EXPENSE_TRANSFER_TYPE, - 'account_id' => $expense_account->id, - ])); - - $income_transaction = Transaction::factory()->create(array_merge($request, [ - 'type' => Transaction::INCOME_TRANSFER_TYPE, - 'account_id' => $income_account->id, - ])); + $to_account = Account::factory()->enabled()->default_currency()->create(); return [ 'company_id' => $this->company->id, - 'expense_transaction_id' => $expense_transaction->id, - 'income_transaction_id' => $income_transaction->id, + 'from_account_id' => $from_account->id, + 'to_account_id' => $to_account->id, + 'amount' => $this->faker->randomFloat(2, 1, 1000), + 'transferred_at' => $this->faker->date(), + 'description'=> $this->faker->text(20), + 'payment_method' => setting('default.payment_method'), + 'reference' => $this->faker->text(20), + 'created_from' => 'core::factory', ]; } } diff --git a/tests/Feature/Banking/TransfersTest.php b/tests/Feature/Banking/TransfersTest.php index 2276e7fa4..8a429bb8c 100644 --- a/tests/Feature/Banking/TransfersTest.php +++ b/tests/Feature/Banking/TransfersTest.php @@ -4,10 +4,10 @@ namespace Tests\Feature\Banking; use App\Exports\Banking\Transfers as Export; use App\Jobs\Banking\CreateTransfer; -use App\Models\Banking\Account; use App\Models\Banking\Transfer; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\File; +use Maatwebsite\Excel\Facades\Excel; use Tests\Feature\FeatureTestCase; class TransfersTest extends FeatureTestCase @@ -76,18 +76,20 @@ class TransfersTest extends FeatureTestCase public function testItShouldExportTransfers() { $count = 5; - Transfer::factory()->count($count)->create(); + foreach (Transfer::factory()->count($count)->raw() as $request) { + $this->dispatch(new CreateTransfer($request)); + } - \Excel::fake(); + Excel::fake(); $this->loginAs() ->get(route('transfers.export')) ->assertStatus(200); - \Excel::matchByRegex(); + Excel::matchByRegex(); - \Excel::assertDownloaded( - '/' . \Str::filename(trans_choice('general.transfers', 2)) . '-\d{10}\.xlsx/', + Excel::assertDownloaded( + '/' . str()->filename(trans_choice('general.transfers', 2)) . '-\d{10}\.xlsx/', function (Export $export) use ($count) { // Assert that the correct export is downloaded. return $export->collection()->count() === $count; @@ -100,21 +102,23 @@ class TransfersTest extends FeatureTestCase $create_count = 5; $select_count = 3; - $transfers = Transfer::factory()->count($create_count)->create(); + foreach (Transfer::factory()->count($create_count)->raw() as $request) { + $responses[] = $this->dispatch(new CreateTransfer($request)); + } - \Excel::fake(); + Excel::fake(); $this->loginAs() ->post( route('bulk-actions.action', ['group' => 'banking', 'type' => 'transfers']), - ['handle' => 'export', 'selected' => $transfers->take($select_count)->pluck('id')->toArray()] + ['handle' => 'export', 'selected' => collect($responses)->take($select_count)->pluck('id')->toArray()] ) ->assertStatus(200); - \Excel::matchByRegex(); + Excel::matchByRegex(); - \Excel::assertDownloaded( - '/' . \Str::filename(trans_choice('general.transfers', 2)) . '-\d{10}\.xlsx/', + Excel::assertDownloaded( + '/' . str()->filename(trans_choice('general.transfers', 2)) . '-\d{10}\.xlsx/', function (Export $export) use ($select_count) { return $export->collection()->count() === $select_count; } @@ -123,7 +127,7 @@ class TransfersTest extends FeatureTestCase public function testItShouldImportTransfers() { - \Excel::fake(); + Excel::fake(); $this->loginAs() ->post( @@ -137,26 +141,13 @@ class TransfersTest extends FeatureTestCase ) ->assertStatus(200); - \Excel::assertImported('transfers.xlsx'); + Excel::assertImported('transfers.xlsx'); $this->assertFlashLevel('success'); } public function getRequest() { - $from_account = Account::factory()->enabled()->default_currency()->create(); - - $to_account = Account::factory()->enabled()->default_currency()->create(); - - return [ - 'company_id' => $this->company->id, - 'from_account_id' => $from_account->id, - 'to_account_id' => $to_account->id, - 'amount' => $this->faker->randomFloat(2, 1, 1000), - 'transferred_at' => $this->faker->date(), - 'description'=> $this->faker->text(20), - 'payment_method' => setting('default.payment_method'), - 'reference' => $this->faker->text(20), - ]; + return Transfer::factory()->raw(); } } From dcc714ff0c432ea53d5cf02e6d4dfacb82ea7a08 Mon Sep 17 00:00:00 2001 From: EnesSacid-Buker Date: Fri, 16 Dec 2022 10:36:57 +0300 Subject: [PATCH 3/5] Code refactoring (filename func arg2 already assigned by default) --- tests/Feature/Banking/TransactionsTest.php | 4 ++-- tests/Feature/Common/ItemsTest.php | 4 ++-- tests/Feature/Purchases/BillsTest.php | 4 ++-- tests/Feature/Purchases/VendorsTest.php | 4 ++-- tests/Feature/Sales/CustomersTest.php | 4 ++-- tests/Feature/Sales/InvoicesTest.php | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/Feature/Banking/TransactionsTest.php b/tests/Feature/Banking/TransactionsTest.php index eabe05ef6..2a644a890 100644 --- a/tests/Feature/Banking/TransactionsTest.php +++ b/tests/Feature/Banking/TransactionsTest.php @@ -129,7 +129,7 @@ class TransactionsTest extends FeatureTestCase Excel::matchByRegex(); Excel::assertDownloaded( - '/' . str()->filename(trans_choice('general.transactions', 2), '-') . '-\d{10}\.xlsx/', + '/' . str()->filename(trans_choice('general.transactions', 2)) . '-\d{10}\.xlsx/', function (Export $export) use ($count) { // Assert that the correct export is downloaded. return $export->collection()->count() === $count; @@ -156,7 +156,7 @@ class TransactionsTest extends FeatureTestCase Excel::matchByRegex(); Excel::assertDownloaded( - '/' . str()->filename(trans_choice('general.transactions', 2), '-') . '-\d{10}\.xlsx/', + '/' . str()->filename(trans_choice('general.transactions', 2)) . '-\d{10}\.xlsx/', function (Export $export) use ($select_count) { return $export->collection()->count() === $select_count; } diff --git a/tests/Feature/Common/ItemsTest.php b/tests/Feature/Common/ItemsTest.php index d1038b589..c1338b3d9 100644 --- a/tests/Feature/Common/ItemsTest.php +++ b/tests/Feature/Common/ItemsTest.php @@ -100,7 +100,7 @@ class ItemsTest extends FeatureTestCase Excel::matchByRegex(); Excel::assertDownloaded( - '/' . str()->filename(trans_choice('general.items', 2), '-') . '-\d{10}\.xlsx/', + '/' . str()->filename(trans_choice('general.items', 2)) . '-\d{10}\.xlsx/', function (Export $export) use ($count) { // Assert that the correct export is downloaded. return $export->sheets()[0]->collection()->count() === $count; @@ -127,7 +127,7 @@ class ItemsTest extends FeatureTestCase Excel::matchByRegex(); Excel::assertDownloaded( - '/' . str()->filename(trans_choice('general.items', 2), '-') . '-\d{10}\.xlsx/', + '/' . str()->filename(trans_choice('general.items', 2)) . '-\d{10}\.xlsx/', function (Export $export) use ($select_count) { // Assert that the correct export is downloaded. return $export->sheets()[0]->collection()->count() === $select_count; diff --git a/tests/Feature/Purchases/BillsTest.php b/tests/Feature/Purchases/BillsTest.php index 050b20d25..bd03a11ba 100644 --- a/tests/Feature/Purchases/BillsTest.php +++ b/tests/Feature/Purchases/BillsTest.php @@ -180,7 +180,7 @@ class BillsTest extends FeatureTestCase Excel::matchByRegex(); Excel::assertDownloaded( - '/' . str()->filename(trans_choice('general.bills', 2), '-') . '-\d{10}\.xlsx/', + '/' . str()->filename(trans_choice('general.bills', 2)) . '-\d{10}\.xlsx/', function (Export $export) use ($count) { // Assert that the correct export is downloaded. return $export->sheets()[0]->collection()->count() === $count; @@ -207,7 +207,7 @@ class BillsTest extends FeatureTestCase Excel::matchByRegex(); Excel::assertDownloaded( - '/' . str()->filename(trans_choice('general.bills', 2), '-') . '-\d{10}\.xlsx/', + '/' . str()->filename(trans_choice('general.bills', 2)) . '-\d{10}\.xlsx/', function (Export $export) use ($select_count) { return $export->sheets()[0]->collection()->count() === $select_count; } diff --git a/tests/Feature/Purchases/VendorsTest.php b/tests/Feature/Purchases/VendorsTest.php index 19f2acb58..ed651ccd8 100644 --- a/tests/Feature/Purchases/VendorsTest.php +++ b/tests/Feature/Purchases/VendorsTest.php @@ -126,7 +126,7 @@ class VendorsTest extends FeatureTestCase Excel::matchByRegex(); Excel::assertDownloaded( - '/' . str()->filename(trans_choice('general.vendors', 2), '-') . '-\d{10}\.xlsx/', + '/' . str()->filename(trans_choice('general.vendors', 2)) . '-\d{10}\.xlsx/', function (Export $export) use ($count) { // Assert that the correct export is downloaded. return $export->collection()->count() === $count; @@ -153,7 +153,7 @@ class VendorsTest extends FeatureTestCase Excel::matchByRegex(); Excel::assertDownloaded( - '/' . str()->filename(trans_choice('general.vendors', 2), '-') . '-\d{10}\.xlsx/', + '/' . str()->filename(trans_choice('general.vendors', 2)) . '-\d{10}\.xlsx/', function (Export $export) use ($select_count) { return $export->collection()->count() === $select_count; } diff --git a/tests/Feature/Sales/CustomersTest.php b/tests/Feature/Sales/CustomersTest.php index 965d07039..5864dc60d 100644 --- a/tests/Feature/Sales/CustomersTest.php +++ b/tests/Feature/Sales/CustomersTest.php @@ -147,7 +147,7 @@ class CustomersTest extends FeatureTestCase Excel::matchByRegex(); Excel::assertDownloaded( - '/' . str()->filename(trans_choice('general.customers', 2), '-') . '-\d{10}\.xlsx/', + '/' . str()->filename(trans_choice('general.customers', 2)) . '-\d{10}\.xlsx/', function (Export $export) use ($count) { // Assert that the correct export is downloaded. return $export->collection()->count() === $count; @@ -174,7 +174,7 @@ class CustomersTest extends FeatureTestCase Excel::matchByRegex(); Excel::assertDownloaded( - '/' . str()->filename(trans_choice('general.customers', 2), '-') . '-\d{10}\.xlsx/', + '/' . str()->filename(trans_choice('general.customers', 2)) . '-\d{10}\.xlsx/', function (Export $export) use ($select_count) { return $export->collection()->count() === $select_count; } diff --git a/tests/Feature/Sales/InvoicesTest.php b/tests/Feature/Sales/InvoicesTest.php index 095824a72..5fb575024 100644 --- a/tests/Feature/Sales/InvoicesTest.php +++ b/tests/Feature/Sales/InvoicesTest.php @@ -191,7 +191,7 @@ class InvoicesTest extends FeatureTestCase Excel::matchByRegex(); Excel::assertDownloaded( - '/' . str()->filename(trans_choice('general.invoices', 2), '-') . '-\d{10}\.xlsx/', + '/' . str()->filename(trans_choice('general.invoices', 2)) . '-\d{10}\.xlsx/', function (Export $export) use ($count) { // Assert that the correct export is downloaded. return $export->sheets()[0]->collection()->count() === $count; @@ -218,7 +218,7 @@ class InvoicesTest extends FeatureTestCase Excel::matchByRegex(); Excel::assertDownloaded( - '/' . str()->filename(trans_choice('general.invoices', 2), '-') . '-\d{10}\.xlsx/', + '/' . str()->filename(trans_choice('general.invoices', 2)) . '-\d{10}\.xlsx/', function (Export $export) use ($select_count) { return $export->sheets()[0]->collection()->count() === $select_count; } From 9fe6a60f51a062b87e78bd05b7674f0ab23f4e1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihan=20=C5=9Eent=C3=BCrk?= <53110792+CihanSenturk@users.noreply.github.com> Date: Fri, 16 Dec 2022 11:24:57 +0300 Subject: [PATCH 4/5] landing page field validations --- app/Http/Requests/Auth/User.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Requests/Auth/User.php b/app/Http/Requests/Auth/User.php index 3c739a2ee..f8b764798 100644 --- a/app/Http/Requests/Auth/User.php +++ b/app/Http/Requests/Auth/User.php @@ -69,6 +69,7 @@ class User extends FormRequest 'companies' => $companies, 'roles' => $roles, 'picture' => $picture, + 'landing_page' => 'required|string', ]; } From 7bef3d1569d42a2866ff3d9b769ecda10fd9ed51 Mon Sep 17 00:00:00 2001 From: EnesSacid-Buker Date: Fri, 16 Dec 2022 14:09:06 +0300 Subject: [PATCH 5/5] Company test added --- database/factories/Company.php | 47 +-------- tests/Feature/Common/CompaniesTest.php | 126 +++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 43 deletions(-) create mode 100644 tests/Feature/Common/CompaniesTest.php diff --git a/database/factories/Company.php b/database/factories/Company.php index ad51d0e75..f27cf9296 100644 --- a/database/factories/Company.php +++ b/database/factories/Company.php @@ -3,9 +3,7 @@ namespace Database\Factories; use App\Abstracts\Factory; -use App\Models\Auth\User; use App\Models\Common\Company as Model; -use Illuminate\Support\Facades\Artisan; class Company extends Factory { @@ -24,8 +22,11 @@ class Company extends Factory public function definition() { return [ + 'name' => $this->faker->company, + 'email' => $this->faker->freeEmail, + 'currency' => $this->faker->randomElement(array_keys(\Akaunting\Money\Currency::getCurrencies())), + 'country' => $this->faker->randomElement(array_keys(trans('countries'))), 'enabled' => $this->faker->boolean ? 1 : 0, - 'created_from' => 'core::factory', ]; } @@ -52,44 +53,4 @@ class Company extends Factory 'enabled' => 0, ]); } - - /** - * Configure the model factory. - * - * @return $this - */ - public function configure() - { - return $this->afterCreating(function (Model $company) { - $company->makeCurrent(); - - app()->setLocale('en-GB'); - - // Company seeds - Artisan::call('company:seed', [ - 'company' => $company->id - ]); - - $user = User::first(); - - $user->companies()->attach($company->id); - - // User seeds - Artisan::call('user:seed', [ - 'user' => $user->id, - 'company' => $company->id, - ]); - - setting()->set([ - 'company.name' => $this->faker->text(15), - 'company.address' => 'New Street 1254', - 'company.city' => 'London', - 'company.country' => $this->faker->countryCode, - 'default.currency' => 'USD', - 'default.locale' => 'en-GB', - ]); - - setting()->save(); - }); - } } diff --git a/tests/Feature/Common/CompaniesTest.php b/tests/Feature/Common/CompaniesTest.php new file mode 100644 index 000000000..153e6e8e7 --- /dev/null +++ b/tests/Feature/Common/CompaniesTest.php @@ -0,0 +1,126 @@ +loginAs() + ->get(route('dashboard')) + ->assertOk() + ->assertSeeText($this->company->name) + ->assertSeeText(trans('general.title.manage', ['type' => trans_choice('general.companies', 2)])); + } + + public function testItShouldSeeCompanyListPage() + { + $this->loginAs() + ->get(route('companies.index')) + ->assertOk() + ->assertSeeText(trans_choice('general.companies', 2)); + } + + public function testItShouldSeeCompanyCreatePage() + { + $this->loginAs() + ->get(route('companies.create')) + ->assertOk() + ->assertSeeText(trans('general.title.new', ['type' => trans_choice('general.companies', 1)])); + } + + public function testItShouldCreateCompany() + { + $request = $this->getRequest(); + + $response = $this->loginAs() + ->post(route('companies.store'), $request) + ->assertOk(); + + $this->assertFlashLevel('success'); + + $this->assertHasCompany($response->baseResponse->original['data']->id, $request); + } + + public function testItShouldSwitchCompany() + { + $request = $this->getRequest(); + + $company = $this->createCompany($request); + + $this->loginAs() + ->get(route('companies.switch', $company->id)) + ->assertStatus(302); + + $this->assertEquals($company->id, company_id()); + } + + public function testItShouldSeeCompanyUpdatePage() + { + $request = $this->getRequest(); + + $company = $this->createCompany($request); + + $this->loginAs() + ->get(route('companies.edit', $company->id)) + ->assertOk() + ->assertSee($company->name); + } + + public function testItShouldUpdateCompany() + { + $request = $this->getRequest(); + + $company = $this->createCompany($request); + + $request['name'] = $this->faker->text(15); + + $this->loginAs() + ->patch(route('companies.update', $company->id), $request) + ->assertOk(); + + $this->assertFlashLevel('success'); + + $this->assertHasCompany($company->id, $request); + } + + public function testItShouldDeleteCompany() + { + $request = $this->getRequest(); + + $company = $this->createCompany($request); + + $this->loginAs() + ->delete(route('companies.destroy', $company->id)) + ->assertOk(); + + $this->assertFlashLevel('success'); + } + + public function getRequest(): array + { + return Company::factory()->enabled()->raw(); + } + + public function createCompany(array $request): Company + { + $response = $this->loginAs() + ->post(route('companies.store'), $request) + ->assertOk(); + + return $response->baseResponse->original['data']; + } + + public function assertHasCompany(int $id, array $request): void + { + company($id)->makeCurrent(); + + $this->assertEquals(setting('company.name'), $request['name']); + $this->assertEquals(setting('company.email'), $request['email']); + $this->assertEquals(setting('company.country'), $request['country']); + $this->assertEquals(setting('default.currency'), $request['currency']); + } +}