From 35c11c12ed29df9a31661d7e5f848de6795e8658 Mon Sep 17 00:00:00 2001 From: denisdulici Date: Mon, 13 Jan 2020 02:56:14 +0300 Subject: [PATCH] added currency factory --- database/factories/Currency.php | 39 +++++++++++++++++++++++ tests/Feature/Settings/CurrenciesTest.php | 24 ++++---------- tests/Feature/Wizard/CurrenciesTest.php | 25 ++++----------- 3 files changed, 51 insertions(+), 37 deletions(-) create mode 100644 database/factories/Currency.php diff --git a/database/factories/Currency.php b/database/factories/Currency.php new file mode 100644 index 000000000..e3e9b6739 --- /dev/null +++ b/database/factories/Currency.php @@ -0,0 +1,39 @@ +companies()->first(); + +$factory->define(Currency::class, function (Faker $faker) use ($company) { + setting()->setExtraColumns(['company_id' => $company->id]); + + $currencies = config('money'); + $random = $faker->randomElement($currencies); + + $filtered = array_filter($currencies, function ($value) use ($random) { + return ($value['code'] == $random['code']); + }); + + $code = key($filtered); + $currency = $filtered[$code]; + + return [ + 'company_id' => $company->id, + 'name' => $currency['name'], + 'code' => $code, + 'rate' => $faker->randomFloat($currency['precision'], 1, 10), + 'precision' => $currency['precision'], + 'symbol' => $currency['symbol'], + 'symbol_first' => $currency['symbol_first'], + 'decimal_mark' => $currency['decimal_mark'], + 'thousands_separator' => $currency['thousands_separator'], + 'enabled' => $faker->boolean ? 1 : 0, + ]; +}); + +$factory->state(Currency::class, 'enabled', ['enabled' => 1]); + +$factory->state(Currency::class, 'disabled', ['enabled' => 0]); diff --git a/tests/Feature/Settings/CurrenciesTest.php b/tests/Feature/Settings/CurrenciesTest.php index 96de411d6..59b883f8a 100644 --- a/tests/Feature/Settings/CurrenciesTest.php +++ b/tests/Feature/Settings/CurrenciesTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Settings; use App\Jobs\Setting\CreateCurrency; +use App\Models\Setting\Currency; use Tests\Feature\FeatureTestCase; class CurrenciesTest extends FeatureTestCase @@ -26,7 +27,7 @@ class CurrenciesTest extends FeatureTestCase public function testItShouldCreateCurrency() { $this->loginAs() - ->post(route('currencies.store'), $this->getCurrencyRequest()) + ->post(route('currencies.store'), $this->getRequest()) ->assertStatus(200); $this->assertFlashLevel('success'); @@ -34,7 +35,7 @@ class CurrenciesTest extends FeatureTestCase public function testItShouldUpdateCurrency() { - $request = $this->getCurrencyRequest(); + $request = $this->getRequest(); $currency = $this->dispatch(new CreateCurrency($request)); @@ -49,7 +50,7 @@ class CurrenciesTest extends FeatureTestCase public function testItShouldDeleteCurrency() { - $currency = $this->dispatch(new CreateCurrency($this->getCurrencyRequest())); + $currency = $this->dispatch(new CreateCurrency($this->getRequest())); $this->loginAs() ->delete(route('currencies.destroy', $currency->id)) @@ -58,21 +59,8 @@ class CurrenciesTest extends FeatureTestCase $this->assertFlashLevel('success'); } - private function getCurrencyRequest() + public function getRequest() { - return [ - 'company_id' => $this->company->id, - 'name' => $this->faker->text(15), - 'code' => $this->faker->text(strtoupper(5)), - 'rate' => $this->faker->boolean(1), - 'precision' => $this->faker->text(5), - 'symbol' => $this->faker->text(5), - 'symbol_first' => 1, - 'symbol_position' => 'after_amount', - 'decimal_mark' => $this->faker->text(5), - 'thousands_separator' => $this->faker->text(5), - 'enabled' => $this->faker->boolean ? 1 : 0, - 'default_currency' => 0, - ]; + return factory(Currency::class)->states('enabled')->raw(); } } diff --git a/tests/Feature/Wizard/CurrenciesTest.php b/tests/Feature/Wizard/CurrenciesTest.php index 63277fd12..49be849d4 100644 --- a/tests/Feature/Wizard/CurrenciesTest.php +++ b/tests/Feature/Wizard/CurrenciesTest.php @@ -2,11 +2,11 @@ namespace Tests\Feature\Wizard; use App\Jobs\Setting\CreateCurrency; +use App\Models\Setting\Currency; use Tests\Feature\FeatureTestCase; class CurrenciesTest extends FeatureTestCase { - public function testItShouldSeeCurrencyListPage() { $this->loginAs() @@ -18,7 +18,7 @@ class CurrenciesTest extends FeatureTestCase public function testItShouldCreateCurrency() { $this->loginAs() - ->post(route('wizard.currencies.store'), $this->getCurrencyRequest()) + ->post(route('wizard.currencies.store'), $this->getRequest()) ->assertStatus(200); $this->assertFlashLevel('success'); @@ -26,7 +26,7 @@ class CurrenciesTest extends FeatureTestCase public function testItShouldUpdateCurrency() { - $request = $this->getCurrencyRequest(); + $request = $this->getRequest(); $currency = $this->dispatch(new CreateCurrency($request)); @@ -41,7 +41,7 @@ class CurrenciesTest extends FeatureTestCase public function testItShouldDeleteCurrency() { - $currency = $this->dispatch(new CreateCurrency($this->getCurrencyRequest())); + $currency = $this->dispatch(new CreateCurrency($this->getRequest())); $this->loginAs() ->delete(route('wizard.currencies.delete', $currency->id)) @@ -50,21 +50,8 @@ class CurrenciesTest extends FeatureTestCase $this->assertFlashLevel('success'); } - private function getCurrencyRequest() + public function getRequest() { - return [ - 'company_id' => $this->company->id, - 'name' => $this->faker->text(15), - 'code' => $this->faker->text(strtoupper(5)), - 'rate' => $this->faker->boolean(1), - 'precision' => $this->faker->text(5), - 'symbol' => $this->faker->text(5), - 'symbol_first' => 1, - 'symbol_position' => 'after_amount', - 'decimal_mark' => $this->faker->text(5), - 'thousands_separator' => $this->faker->text(5), - 'enabled' => $this->faker->boolean ? 1 : 0, - 'default_currency' => 0 - ]; + return factory(Currency::class)->states('enabled')->raw(); } }