akaunting/tests/Feature/Sales/CustomersTest.php

222 lines
6.1 KiB
PHP
Raw Normal View History

2018-07-14 13:29:16 +03:00
<?php
2019-12-31 15:49:09 +03:00
namespace Tests\Feature\Sales;
2018-07-14 13:29:16 +03:00
use App\Exports\Sales\Customers as Export;
2019-11-17 15:06:00 +03:00
use App\Jobs\Common\CreateContact;
2018-07-14 13:29:16 +03:00
use App\Models\Auth\User;
2020-01-07 01:28:05 +03:00
use App\Models\Common\Contact;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File;
2022-12-15 12:21:04 +03:00
use Maatwebsite\Excel\Facades\Excel;
2018-07-14 13:29:16 +03:00
use Tests\Feature\FeatureTestCase;
class CustomersTest extends FeatureTestCase
{
2021-02-12 13:38:33 +03:00
public function testItShouldSeeCustomerListPage()
{
$this->loginAs()
->get(route('customers.index'))
->assertStatus(200)
->assertSeeText(trans_choice('general.customers', 2));
}
2021-06-30 12:56:46 +03:00
public function testItShouldSeeCustomerShowPage()
{
$request = $this->getRequest();
$customer = $this->dispatch(new CreateContact($request));
$this->loginAs()
->get(route('customers.show', $customer->id))
->assertStatus(200)
->assertSee($customer->email);
}
2021-02-12 13:38:33 +03:00
public function testItShouldSeeCustomerCreatePage()
{
$this->loginAs()
->get(route('customers.create'))
->assertStatus(200)
->assertSeeText(trans('general.title.new', ['type' => trans_choice('general.customers', 1)]));
}
public function testItShouldCreateCustomer()
{
$request = $this->getRequest();
$this->loginAs()
->post(route('customers.store'), $request)
->assertStatus(200);
$this->assertFlashLevel('success');
2020-07-25 17:22:50 +03:00
$this->assertDatabaseHas('contacts', $request);
2021-02-12 13:38:33 +03:00
}
2018-07-14 13:29:16 +03:00
2021-02-12 13:38:33 +03:00
public function testItShouldCreateCustomerWithUser()
{
2020-07-25 17:22:50 +03:00
$request = $this->getRequestWithUser();
2018-07-14 13:29:16 +03:00
2021-02-12 13:38:33 +03:00
$this->loginAs()
->post(route('customers.store'), $request)
->assertStatus(200);
2018-07-14 13:29:16 +03:00
2021-02-12 13:38:33 +03:00
$this->assertFlashLevel('success');
2018-09-29 17:28:34 +03:00
2021-02-12 13:38:33 +03:00
$user = User::where('email', $request['email'])->first();
2020-01-13 10:55:19 +03:00
2021-02-12 13:38:33 +03:00
$this->assertNotNull($user);
$this->assertEquals($request['email'], $user->email);
}
2018-07-14 13:29:16 +03:00
2021-02-12 13:38:33 +03:00
public function testItShouldSeeCustomerDetailPage()
{
$request = $this->getRequest();
2020-07-25 17:22:50 +03:00
2021-02-12 13:38:33 +03:00
$customer = $this->dispatch(new CreateContact($request));
2018-09-29 17:28:34 +03:00
2021-02-12 13:38:33 +03:00
$this->loginAs()
->get(route('customers.show', $customer->id))
->assertStatus(200)
->assertSee($customer->email);
}
2018-07-14 13:29:16 +03:00
2021-02-12 13:38:33 +03:00
public function testItShouldSeeCustomerUpdatePage()
{
$request = $this->getRequest();
2020-07-25 17:22:50 +03:00
2021-02-12 13:38:33 +03:00
$customer = $this->dispatch(new CreateContact($request));
2018-09-29 17:28:34 +03:00
2021-02-12 13:38:33 +03:00
$this->loginAs()
->get(route('customers.edit', $customer->id))
->assertStatus(200)
->assertSee($customer->email);
}
2018-07-14 13:29:16 +03:00
2021-02-12 13:38:33 +03:00
public function testItShouldUpdateCustomer()
{
$request = $this->getRequest();
2018-09-29 17:28:34 +03:00
2021-02-12 13:38:33 +03:00
$customer = $this->dispatch(new CreateContact($request));
2018-09-29 17:28:34 +03:00
2022-06-01 10:15:55 +03:00
$request['email'] = $this->faker->freeEmail;
2018-07-14 13:29:16 +03:00
2021-02-12 13:38:33 +03:00
$this->loginAs()
->patch(route('customers.update', $customer->id), $request)
->assertStatus(200)
->assertSee($request['email']);
2018-09-29 17:28:34 +03:00
2021-02-12 13:38:33 +03:00
$this->assertFlashLevel('success');
2020-07-25 17:22:50 +03:00
$this->assertDatabaseHas('contacts', $request);
2021-02-12 13:38:33 +03:00
}
2018-07-14 13:29:16 +03:00
2021-02-12 13:38:33 +03:00
public function testItShouldDeleteCustomer()
{
2020-07-25 17:22:50 +03:00
$request = $this->getRequest();
2021-02-12 13:38:33 +03:00
$customer = $this->dispatch(new CreateContact($request));
2018-07-14 13:29:16 +03:00
2021-02-12 13:38:33 +03:00
$this->loginAs()
->delete(route('customers.destroy', $customer->id))
->assertStatus(200);
2020-01-13 10:55:19 +03:00
2021-02-12 13:38:33 +03:00
$this->assertFlashLevel('success');
2018-07-14 13:29:16 +03:00
2020-07-25 17:22:50 +03:00
$this->assertSoftDeleted('contacts', $request);
2021-02-12 13:38:33 +03:00
}
2018-07-14 13:29:16 +03:00
2021-02-12 13:38:33 +03:00
public function testItShouldNotDeleteCustomerIfHasRelations()
2020-01-07 01:28:05 +03:00
{
2021-02-12 13:38:33 +03:00
$this->assertTrue(true);
//TODO : This will write after done invoice and revenues tests.
2020-01-07 01:28:05 +03:00
}
2018-07-14 13:29:16 +03:00
public function testItShouldExportCustomers()
{
Contact::factory()->customer()->count(5)->create();
2022-11-01 13:10:08 +03:00
$count = Contact::customer()->count();
2022-12-15 12:21:04 +03:00
Excel::fake();
$this->loginAs()
->get(route('customers.export'))
->assertStatus(200);
2022-12-15 12:21:04 +03:00
Excel::matchByRegex();
2021-05-23 17:13:13 +03:00
2022-12-15 12:21:04 +03:00
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;
}
);
}
public function testItShouldExportSelectedCustomers()
{
2021-05-23 17:13:13 +03:00
$create_count = 5;
$select_count = 3;
$customers = Contact::factory()->customer()->count($create_count)->create();
2022-12-15 12:21:04 +03:00
Excel::fake();
$this->loginAs()
->post(
route('bulk-actions.action', ['group' => 'sales', 'type' => 'customers']),
2021-05-23 17:13:13 +03:00
['handle' => 'export', 'selected' => $customers->take($select_count)->pluck('id')->toArray()]
)
->assertStatus(200);
2022-12-15 12:21:04 +03:00
Excel::matchByRegex();
2021-05-23 17:13:13 +03:00
2022-12-15 12:21:04 +03:00
Excel::assertDownloaded(
'/' . str()->filename(trans_choice('general.customers', 2)) . '-\d{10}\.xlsx/',
2021-05-23 17:13:13 +03:00
function (Export $export) use ($select_count) {
return $export->collection()->count() === $select_count;
}
);
}
public function testItShouldImportCustomers()
{
2022-12-15 12:21:04 +03:00
Excel::fake();
$this->loginAs()
->post(
route('customers.import'),
[
'import' => UploadedFile::fake()->createWithContent(
'customers.xlsx',
File::get(public_path('files/import/customers.xlsx'))
),
]
)
->assertStatus(200);
2022-12-15 12:21:04 +03:00
Excel::assertImported('customers.xlsx');
$this->assertFlashLevel('success');
}
2021-02-12 13:38:33 +03:00
public function getRequest()
{
return Contact::factory()->customer()->enabled()->raw();
}
public function getRequestWithUser()
{
$password = $this->faker->password;
return $this->getRequest() + [
'create_user' => 'true',
'locale' => 'en-GB',
'password' => $password,
'password_confirmation' => $password,
];
}
2019-04-02 18:51:06 +03:00
}