improved factories and tests

This commit is contained in:
denisdulici 2020-01-07 01:28:05 +03:00
parent 472a4e00df
commit 751344f11a
13 changed files with 152 additions and 81 deletions

View File

@ -24,3 +24,15 @@ $factory->define(Account::class, function (Faker $faker) use ($company) {
'enabled' => $faker->boolean ? 1 : 0,
];
});
$factory->state(Account::class, 'enabled', function (Faker $faker) {
return [
'enabled' => 1,
];
});
$factory->state(Account::class, 'disabled', function (Faker $faker) {
return [
'enabled' => 0,
];
});

View File

@ -1,4 +1,5 @@
<?php
use App\Models\Auth\User;
use App\Models\Common\Contact;
use Faker\Generator as Faker;
@ -7,22 +8,44 @@ $user = User::first();
$company = $user->companies()->first();
$factory->define(Contact::class, function (Faker $faker) use ($company) {
setting()->setExtraColumns([
'company_id' => $company->id
]);
setting()->setExtraColumns(['company_id' => $company->id]);
return [
'company_id' => $company->id,
'type' => $faker->boolean ? 'customer' : 'vendor',
'name' => $faker->text(15),
'email' => '',
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'user_id' => null,
'tax_number' => null,
'phone' => null,
'address' => null,
'website' => null,
'tax_number' => $faker->randomNumber(9),
'phone' => $faker->phoneNumber,
'address' => $faker->address,
'website' => 'https://akaunting.com',
'currency_code' => setting('default.currency'),
'reference' => null,
'enabled' => $faker->boolean ? 1 : 0
'reference' => $faker->text(5),
'enabled' => $faker->boolean ? 1 : 0,
];
});
$factory->state(Contact::class, 'customer', function (Faker $faker) {
return [
'type' => 'customer',
];
});
$factory->state(Contact::class, 'vendor', function (Faker $faker) {
return [
'type' => 'vendor',
];
});
$factory->state(Contact::class, 'enabled', function (Faker $faker) {
return [
'enabled' => 1,
];
});
$factory->state(Contact::class, 'disabled', function (Faker $faker) {
return [
'enabled' => 0,
];
});

View File

@ -21,3 +21,15 @@ $factory->define(Item::class, function (Faker $faker) use ($company) {
'enabled' => $faker->boolean ? 1 : 0,
];
});
$factory->state(Item::class, 'enabled', function (Faker $faker) {
return [
'enabled' => 1,
];
});
$factory->state(Item::class, 'disabled', function (Faker $faker) {
return [
'enabled' => 0,
];
});

View File

@ -10,22 +10,29 @@ $company = $user->companies()->first();
$factory->define(Transaction::class, function (Faker $faker) use ($company) {
setting()->setExtraColumns(['company_id' => $company->id]);
$type = $faker->boolean ? 'income' : 'expense';
return [
'company_id' => $company->id,
'type' => 'income',
'type' => $type,
'account_id' => setting('default.account'),
'paid_at' => $faker->date(),
'paid_at' => $faker->dateTimeBetween(now()->startOfYear(), now()->endOfYear())->format('Y-m-d'),
'amount' => $faker->randomFloat(2, 2, 1000),
'currency_code' => setting('default.currency'),
'currency_rate' => '1',
'description' => $faker->text(5),
'category_id' => $company->categories()->type('income')->pluck('id')->first(),
'category_id' => $company->categories()->type($type)->pluck('id')->first(),
'reference' => $faker->text(5),
'payment_method' => setting('default.payment_method'),
];
});
$factory->state(Transaction::class, 'income', []);
$factory->state(Transaction::class, 'income', function (Faker $faker) use ($company) {
return [
'type' => 'income',
'category_id' => $company->categories()->type('income')->pluck('id')->first(),
];
});
$factory->state(Transaction::class, 'expense', function (Faker $faker) use ($company) {
return [

View File

@ -19,3 +19,15 @@ $factory->define(User::class, function (Faker $faker) {
'enabled' => $this->faker->boolean ? 1 : 0,
];
});
$factory->state(User::class, 'enabled', function (Faker $faker) {
return [
'enabled' => 1,
];
});
$factory->state(User::class, 'disabled', function (Faker $faker) {
return [
'enabled' => 0,
];
});

View File

@ -27,7 +27,7 @@ class UsersTest extends FeatureTestCase
public function testItShouldCreateUser()
{
$this->loginAs()
->post(route('users.store'), factory(User::class)->raw())
->post(route('users.store'), $this->getRequest())
->assertStatus(200);
$this->assertFlashLevel('success');
@ -35,7 +35,7 @@ class UsersTest extends FeatureTestCase
public function testItShouldSeeUserUpdatePage()
{
$user = $this->dispatch(new CreateUser(factory(User::class)->raw()));
$user = $this->dispatch(new CreateUser($this->getRequest()));
$this->loginAs()
->get(route('users.edit', ['user' => $user->id]))
@ -45,7 +45,7 @@ class UsersTest extends FeatureTestCase
public function testItShouldUpdateUser()
{
$request = factory(User::class)->raw();
$request = $this->getRequest();
$user = $this->dispatch(new CreateUser($request));
@ -60,7 +60,7 @@ class UsersTest extends FeatureTestCase
public function testItShouldDeleteUser()
{
$user = $this->dispatch(new CreateUser(factory(User::class)->raw()));
$user = $this->dispatch(new CreateUser($this->getRequest()));
$this->loginAs()
->delete(route('users.destroy', $user->id))
@ -78,7 +78,7 @@ class UsersTest extends FeatureTestCase
public function testItShouldLoginUser()
{
$user = $this->dispatch(new CreateUser(factory(User::class)->raw()));
$user = $this->dispatch(new CreateUser($this->getRequest()));
$this->post(route('login'), ['email' => $user->email, 'password' => $user->password])
->assertStatus(200);
@ -88,7 +88,7 @@ class UsersTest extends FeatureTestCase
public function testItShouldNotLoginUser()
{
$user = $this->dispatch(new CreateUser(factory(User::class)->raw()));
$user = $this->dispatch(new CreateUser($this->getRequest()));
$this->post(route('login'), ['email' => $user->email, 'password' => $this->faker->password()])
->assertStatus(200);
@ -98,7 +98,7 @@ class UsersTest extends FeatureTestCase
public function testItShouldLogoutUser()
{
$user = $this->dispatch(new CreateUser(factory(User::class)->raw()));
$user = $this->dispatch(new CreateUser($this->getRequest()));
$this->loginAs()
->get(route('logout', $user->id))
@ -107,4 +107,9 @@ class UsersTest extends FeatureTestCase
$this->assertGuest();
}
public function getRequest()
{
return factory(User::class)->states('enabled')->raw();
}
}

View File

@ -27,7 +27,7 @@ class AccountsTest extends FeatureTestCase
public function testItShouldCreateAccount()
{
$this->loginAs()
->post(route('accounts.index'), factory(Account::class)->raw())
->post(route('accounts.index'), $this->getRequest())
->assertStatus(200);
$this->assertFlashLevel('success');
@ -35,7 +35,7 @@ class AccountsTest extends FeatureTestCase
public function testItShouldSeeAccountUpdatePage()
{
$account = $this->dispatch(new CreateAccount(factory(Account::class)->raw()));
$account = $this->dispatch(new CreateAccount($this->getRequest()));
$this->loginAs()
->get(route('accounts.edit', ['account' => $account->id]))
@ -45,7 +45,7 @@ class AccountsTest extends FeatureTestCase
public function testItShouldUpdateAccount()
{
$request = factory(Account::class)->raw();
$request = $this->getRequest();
$account = $this->dispatch(new CreateAccount($request));
@ -60,7 +60,7 @@ class AccountsTest extends FeatureTestCase
public function testItShouldDeleteAccount()
{
$account = $this->dispatch(new CreateAccount(factory(Account::class)->raw()));
$account = $this->dispatch(new CreateAccount($this->getRequest()));
$this->loginAs()
->delete(route('accounts.destroy', ['account' => $account]))
@ -68,4 +68,9 @@ class AccountsTest extends FeatureTestCase
$this->assertFlashLevel('success');
}
public function getRequest()
{
return factory(Account::class)->states('enabled')->raw();
}
}

View File

@ -69,11 +69,11 @@ class TransfersTest extends FeatureTestCase
$this->assertFlashLevel('success');
}
private function getRequest()
public function getRequest()
{
$income_transaction = factory(Transaction::class)->create();
$income_transaction = factory(Transaction::class)->states('income')->create();
$expense_transaction = factory(Transaction::class)->state('expense')->create();
$expense_transaction = factory(Transaction::class)->states('expense')->create();
return [
'company_id' => $this->company->id,

View File

@ -27,7 +27,7 @@ class ItemsTest extends FeatureTestCase
public function testItShouldCreateItem()
{
$this->loginAs()
->post(route('items.store'), factory(Item::class)->raw())
->post(route('items.store'), $this->getRequest())
->assertStatus(200);
$this->assertFlashLevel('success');
@ -35,7 +35,7 @@ class ItemsTest extends FeatureTestCase
public function testItShouldSeeItemUpdatePage()
{
$item = $this->dispatch(new CreateItem(factory(Item::class)->raw()));
$item = $this->dispatch(new CreateItem($this->getRequest()));
$this->loginAs()
->get(route('items.edit', ['item' => $item->id]))
@ -45,7 +45,7 @@ class ItemsTest extends FeatureTestCase
public function testItShouldUpdateItem()
{
$request = factory(Item::class)->raw();
$request = $this->getRequest();
$item = $this->dispatch(new CreateItem($request));
@ -60,7 +60,7 @@ class ItemsTest extends FeatureTestCase
public function testItShouldDeleteItem()
{
$item = $this->dispatch(new CreateItem(factory(Item::class)->raw()));
$item = $this->dispatch(new CreateItem($this->getRequest()));
$this->loginAs()
->delete(route('items.destroy', ['item' => $item]))
@ -68,4 +68,9 @@ class ItemsTest extends FeatureTestCase
$this->assertFlashLevel('success');
}
public function getRequest()
{
return factory(Item::class)->states('enabled')->raw();
}
}

View File

@ -27,7 +27,7 @@ class PaymentsTest extends FeatureTestCase
public function testItShouldCreatePayment()
{
$this->loginAs()
->post(route('payments.store'), factory(Transaction::class)->states('expense')->raw())
->post(route('payments.store'), $this->getRequest())
->assertStatus(200);
$this->assertFlashLevel('success');
@ -35,7 +35,7 @@ class PaymentsTest extends FeatureTestCase
public function testItShouldUpdatePayment()
{
$request = factory(Transaction::class)->states('expense')->raw();
$request = $this->getRequest();
$payment = $this->dispatch(new CreateTransaction($request));
@ -50,7 +50,7 @@ class PaymentsTest extends FeatureTestCase
public function testItShouldDeletePayment()
{
$payment = $this->dispatch(new CreateTransaction(factory(Transaction::class)->states('expense')->raw()));
$payment = $this->dispatch(new CreateTransaction($this->getRequest()));
$this->loginAs()
->delete(route('payments.destroy', $payment->id))
@ -58,4 +58,9 @@ class PaymentsTest extends FeatureTestCase
$this->assertFlashLevel('success');
}
public function getRequest()
{
return factory(Transaction::class)->states('expense')->raw();
}
}

View File

@ -3,6 +3,7 @@
namespace Tests\Feature\Purchases;
use App\Jobs\Common\CreateContact;
use App\Models\Common\Contact;
use Tests\Feature\FeatureTestCase;
class VendorsTest extends FeatureTestCase
@ -26,7 +27,7 @@ class VendorsTest extends FeatureTestCase
public function testItShouldCreateVendor()
{
$this->loginAs()
->post(route('vendors.store'), $this->getVendorRequest())
->post(route('vendors.store'), $this->getRequest())
->assertStatus(200);
$this->assertFlashLevel('success');
@ -34,7 +35,7 @@ class VendorsTest extends FeatureTestCase
public function testItShouldSeeVendorDetailPage()
{
$vendor = $this->dispatch(new CreateContact($this->getVendorRequest()));
$vendor = $this->dispatch(new CreateContact($this->getRequest()));
$this->loginAs()
->get(route('vendors.show', ['vendor' => $vendor->id]))
@ -44,7 +45,7 @@ class VendorsTest extends FeatureTestCase
public function testItShouldSeeVendorUpdatePage()
{
$vendor = $this->dispatch(new CreateContact($this->getVendorRequest()));
$vendor = $this->dispatch(new CreateContact($this->getRequest()));
$this->loginAs()
->get(route('vendors.edit', ['vendor' => $vendor->id]))
@ -55,7 +56,7 @@ class VendorsTest extends FeatureTestCase
public function testItShouldUpdateVendor()
{
$request = $this->getVendorRequest();
$request = $this->getRequest();
$vendor = $this->dispatch(new CreateContact($request));
@ -70,7 +71,7 @@ class VendorsTest extends FeatureTestCase
public function testItShouldDeleteVendor()
{
$vendor = $this->dispatch(new CreateContact($this->getVendorRequest()));
$vendor = $this->dispatch(new CreateContact($this->getRequest()));
$this->loginAs()
->delete(route('vendors.destroy', $vendor->id))
@ -79,19 +80,8 @@ class VendorsTest extends FeatureTestCase
$this->assertFlashLevel('success');
}
private function getVendorRequest()
public function getRequest()
{
return [
'company_id' => $this->company->id,
'type' => 'vendor',
'name' => $this->faker->name,
'email' => $this->faker->email,
'tax_number' => $this->faker->randomNumber(9),
'phone' => $this->faker->phoneNumber,
'address' => $this->faker->address,
'website' => 'www.akaunting.com',
'currency_code' => $this->company->currencies()->enabled()->first()->code,
'enabled' => $this->faker->boolean ? 1 : 0
];
return factory(Contact::class)->states('vendor', 'enabled')->raw();
}
}

View File

@ -4,6 +4,7 @@ namespace Tests\Feature\Sales;
use App\Jobs\Common\CreateContact;
use App\Models\Auth\User;
use App\Models\Common\Contact;
use Tests\Feature\FeatureTestCase;
class CustomersTest extends FeatureTestCase
@ -27,7 +28,7 @@ class CustomersTest extends FeatureTestCase
public function testItShouldCreateOnlyCustomerWithoutUser()
{
$this->loginAs()
->post(route('customers.store'), $this->getCustomerRequest())
->post(route('customers.store'), $this->getRequest())
->assertStatus(200);
$this->assertFlashLevel('success');
@ -35,7 +36,7 @@ class CustomersTest extends FeatureTestCase
public function testItShouldCreateCustomerWithUser()
{
$customer = $this->getCustomerRequestWithUser();
$customer = $this->getRequestWithUser();
$this->loginAs()
->post(route('customers.store'), $customer)
@ -51,7 +52,7 @@ class CustomersTest extends FeatureTestCase
public function testItShouldSeeCustomerDetailPage()
{
$customer = $this->dispatch(new CreateContact($this->getCustomerRequest()));
$customer = $this->dispatch(new CreateContact($this->getRequest()));
$this->loginAs()
->get(route('customers.show', ['customer' => $customer->id]))
@ -61,7 +62,7 @@ class CustomersTest extends FeatureTestCase
public function testItShouldSeeCustomerUpdatePage()
{
$customer = $this->dispatch(new CreateContact($this->getCustomerRequest()));
$customer = $this->dispatch(new CreateContact($this->getRequest()));
$this->loginAs()
->get(route('customers.edit', ['customer' => $customer->id]))
@ -72,7 +73,7 @@ class CustomersTest extends FeatureTestCase
public function testItShouldUpdateCustomer()
{
$request = $this->getCustomerRequest();
$request = $this->getRequest();
$customer = $this->dispatch(new CreateContact($request));
@ -87,7 +88,7 @@ class CustomersTest extends FeatureTestCase
public function testItShouldDeleteCustomer()
{
$customer = $this->dispatch(new CreateContact($this->getCustomerRequest()));
$customer = $this->dispatch(new CreateContact($this->getRequest()));
$this->loginAs()
->delete(route('customers.destroy', $customer->id))
@ -103,31 +104,20 @@ class CustomersTest extends FeatureTestCase
//TODO : This will write after done invoice and revenues tests.
}
private function getCustomerRequest()
{
return [
'company_id' => $this->company->id,
'type' => 'customer',
'name' => $this->faker->name,
'email' => $this->faker->email,
'tax_number' => $this->faker->randomNumber(9),
'phone' => $this->faker->phoneNumber,
'address' => $this->faker->address,
'website' => 'www.akaunting.com',
'currency_code' => $this->company->currencies()->enabled()->first()->code,
'enabled' => $this->faker->boolean ? 1 : 0
];
}
public function getRequest()
{
return factory(Contact::class)->states('customer', 'enabled')->raw();
}
private function getCustomerRequestWithUser()
public function getRequestWithUser()
{
$password = $this->faker->password;
return $this->getCustomerRequest() + [
return $this->getRequest() + [
'create_user' => 1,
'locale' => 'en-GB',
'password' => $password,
'password_confirmation' => $password
'password_confirmation' => $password,
];
}
}

View File

@ -27,7 +27,7 @@ class RevenuesTest extends FeatureTestCase
public function testItShouldCreateRevenue()
{
$this->loginAs()
->post(route('revenues.store'), factory(Transaction::class)->raw())
->post(route('revenues.store'), $this->getRequest())
->assertStatus(200);
$this->assertFlashLevel('success');
@ -35,7 +35,7 @@ class RevenuesTest extends FeatureTestCase
public function testItShouldUpdateRevenue()
{
$request = factory(Transaction::class)->raw();
$request = $this->getRequest();
$revenue = $this->dispatch(new CreateTransaction($request));
@ -50,7 +50,7 @@ class RevenuesTest extends FeatureTestCase
public function testItShouldDeleteRevenue()
{
$revenue = $this->dispatch(new CreateTransaction(factory(Transaction::class)->raw()));
$revenue = $this->dispatch(new CreateTransaction($this->getRequest()));
$this->loginAs()
->delete(route('revenues.destroy', $revenue->id))
@ -58,4 +58,9 @@ class RevenuesTest extends FeatureTestCase
$this->assertFlashLevel('success');
}
public function getRequest()
{
return factory(Transaction::class)->states('income')->raw();
}
}