using factories in tests

This commit is contained in:
denisdulici 2019-11-22 17:35:46 +03:00
parent a4135ec902
commit 7ba71d17c5
5 changed files with 31 additions and 49 deletions

View File

@ -15,7 +15,7 @@ $factory->define(Item::class, function (Faker $faker) {
'purchase_price' => $faker->randomFloat(2, 10, 20),
'sale_price' => $faker->randomFloat(2, 10, 20),
'category_id' => $company->categories()->type('item')->pluck('id')->first(),
'tax_id' => '',
'tax_id' => 0,
'enabled' => $faker->boolean ? 1 : 0
];
});

View File

@ -1,24 +0,0 @@
<?php
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(\App\Models\Auth\User::class, function (Faker\Generator $faker) {
static $password;
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => \Str::random(10),
];
});

View File

@ -9,6 +9,8 @@ $factory->define(Transaction::class, function (Faker $faker) {
$user = User::first();
$company = $user->companies()->first();
setting()->setExtraColumns(['company_id' => $company->id]);
$attachment = UploadedFile::fake()->create('image.jpg');
return [
@ -16,13 +18,13 @@ $factory->define(Transaction::class, function (Faker $faker) {
'type' => 'income',
'account_id' => setting('default.account'),
'paid_at' => $faker->date(),
'amount' => $faker->randomFloat(2, 2),
'amount' => $faker->randomFloat(2, 2, 1000),
'currency_code' => setting('default.currency'),
'currency_rate' => '1',
'description' => $faker->text(5),
'category_id' => $company->categories()->type('income')->first()->id,
'reference' => $faker->text(5),
'payment_method' => setting('default.payment_method'),
'attachment' => $attachment,
//'attachment' => $attachment,
];
});

View File

@ -0,0 +1,21 @@
<?php
use App\Models\Auth\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;
$factory->define(User::class, function (Faker $faker) {
$password = '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi'; // password
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password,
'password_confirmation' => $password,
'remember_token' => Str::random(10),
'locale' => 'en-GB',
'companies' => ['1'],
'roles' => ['1'],
'enabled' => $this->faker->boolean ? 1 : 0,
];
});

View File

@ -3,7 +3,7 @@
namespace Tests\Feature\Common;
use App\Jobs\Common\CreateItem;
use Illuminate\Http\UploadedFile;
use App\Models\Common\Item;
use Tests\Feature\FeatureTestCase;
class ItemsTest extends FeatureTestCase
@ -27,7 +27,7 @@ class ItemsTest extends FeatureTestCase
public function testItShouldCreateItem()
{
$this->loginAs()
->post(route('items.store'), $this->getItemRequest())
->post(route('items.store'), factory(Item::class)->raw())
->assertStatus(200);
$this->assertFlashLevel('success');
@ -35,7 +35,7 @@ class ItemsTest extends FeatureTestCase
public function testItShouldSeeItemUpdatePage()
{
$item = $this->dispatch(new CreateItem($this->getItemRequest()));
$item = $this->dispatch(new CreateItem(factory(Item::class)->raw()));
$this->loginAs()
->get(route('items.edit', ['item' => $item->id]))
@ -45,7 +45,7 @@ class ItemsTest extends FeatureTestCase
public function testItShouldUpdateItem()
{
$request = $this->getItemRequest();
$request = factory(Item::class)->raw();
$item = $this->dispatch(new CreateItem($request));
@ -60,7 +60,7 @@ class ItemsTest extends FeatureTestCase
public function testItShouldDeleteItem()
{
$item = $this->dispatch(new CreateItem($this->getItemRequest()));
$item = $this->dispatch(new CreateItem(factory(Item::class)->raw()));
$this->loginAs()
->delete(route('items.destroy', ['item' => $item]))
@ -68,21 +68,4 @@ class ItemsTest extends FeatureTestCase
$this->assertFlashLevel('success');
}
private function getItemRequest()
{
$picture = UploadedFile::fake()->create('image.jpg');
return [
'company_id' => $this->company->id,
'name' => $this->faker->text(15),
'picture' => $picture,
'description' => $this->faker->text(100),
'purchase_price' => $this->faker->randomFloat(2, 10, 20),
'sale_price' => $this->faker->randomFloat(2, 10, 20),
'category_id' => $this->company->categories()->type('item')->pluck('id')->first(),
'tax_id' => '',
'enabled' => $this->faker->boolean ? 1 : 0
];
}
}