From 7bef3d1569d42a2866ff3d9b769ecda10fd9ed51 Mon Sep 17 00:00:00 2001 From: EnesSacid-Buker Date: Fri, 16 Dec 2022 14:09:06 +0300 Subject: [PATCH] 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']); + } +}