From 7ba71d17c55a7b9751b70e89caf556e3d5f8481c Mon Sep 17 00:00:00 2001 From: denisdulici Date: Fri, 22 Nov 2019 17:35:46 +0300 Subject: [PATCH] using factories in tests --- .../factories/{ItemFactory.php => Item.php} | 2 +- database/factories/ModelFactory.php | 24 ----------------- ...TransactionFactory.php => Transaction.php} | 6 +++-- database/factories/User.php | 21 +++++++++++++++ tests/Feature/Common/ItemsTest.php | 27 ++++--------------- 5 files changed, 31 insertions(+), 49 deletions(-) rename database/factories/{ItemFactory.php => Item.php} (96%) delete mode 100644 database/factories/ModelFactory.php rename database/factories/{TransactionFactory.php => Transaction.php} (84%) create mode 100644 database/factories/User.php diff --git a/database/factories/ItemFactory.php b/database/factories/Item.php similarity index 96% rename from database/factories/ItemFactory.php rename to database/factories/Item.php index 5e5df7147..0d2e48d90 100644 --- a/database/factories/ItemFactory.php +++ b/database/factories/Item.php @@ -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 ]; }); diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php deleted file mode 100644 index d04247abb..000000000 --- a/database/factories/ModelFactory.php +++ /dev/null @@ -1,24 +0,0 @@ -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), - ]; -}); diff --git a/database/factories/TransactionFactory.php b/database/factories/Transaction.php similarity index 84% rename from database/factories/TransactionFactory.php rename to database/factories/Transaction.php index 3584e41a1..5e65dd21e 100644 --- a/database/factories/TransactionFactory.php +++ b/database/factories/Transaction.php @@ -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, ]; }); diff --git a/database/factories/User.php b/database/factories/User.php new file mode 100644 index 000000000..1eece2a23 --- /dev/null +++ b/database/factories/User.php @@ -0,0 +1,21 @@ +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, + ]; +}); diff --git a/tests/Feature/Common/ItemsTest.php b/tests/Feature/Common/ItemsTest.php index 1f500eb26..a1b6309eb 100644 --- a/tests/Feature/Common/ItemsTest.php +++ b/tests/Feature/Common/ItemsTest.php @@ -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 - ]; - } }