diff --git a/database/seeds/Taxes.php b/database/seeds/Taxes.php index 20b8dda39..dc3cd6a80 100644 --- a/database/seeds/Taxes.php +++ b/database/seeds/Taxes.php @@ -4,7 +4,6 @@ namespace Database\Seeds; use App\Models\Model; use App\Models\Setting\Tax; - use Illuminate\Database\Seeder; class Taxes extends Seeder diff --git a/tests/Feature/Common/ItemsTest.php b/tests/Feature/Common/ItemsTest.php index 6510c3ebf..8068eb172 100644 --- a/tests/Feature/Common/ItemsTest.php +++ b/tests/Feature/Common/ItemsTest.php @@ -10,8 +10,7 @@ class ItemsTest extends FeatureTestCase { public function testItShouldBeShowTheItemsPage() { - $this - ->loginAs() + $this->loginAs() ->get(route('items.index')) ->assertStatus(200) ->assertSee('Items'); @@ -19,8 +18,7 @@ class ItemsTest extends FeatureTestCase public function testItShouldBeShowCreateItemPage() { - $this - ->loginAs() + $this->loginAs() ->get(route('items.create')) ->assertStatus(200) ->assertSee('New Item'); @@ -28,36 +26,20 @@ class ItemsTest extends FeatureTestCase public function testItShouldStoreAnItem() { - $picture = UploadedFile::fake()->create('image.jpg'); - - $item = [ - 'name' => $this->faker->title, - 'sku' => $this->faker->languageCode, - 'picture' => $picture, - 'description' => $this->faker->text(100), - 'purchase_price' => $this->faker->randomFloat(2,10,20), - 'sale_price' => $this->faker->randomFloat(2,10,20), - 'quantity' => $this->faker->randomNumber(2), - 'category_id' => $this->company->categories()->first()->id, - 'tax_id' => $this->company->taxes()->first()->id, - 'enabled' => $this->faker->boolean ? 1 : 0 - ]; - - $this - ->loginAs() - ->post(route('items.store'), $item) + $this->loginAs() + ->post(route('items.store'), $this->getItemRequest()) ->assertStatus(302) ->assertRedirect(route('items.index')); + $this->assertFlashLevel('success'); } public function testItShouldEditItem() { - $item = factory(Item::class)->create(); + $item = Item::create($this->getItemRequest()); - $this - ->loginAs() - ->get(route('items.edit', ['item' => $item])) + $this->loginAs() + ->get(route('items.edit', ['item' => $item->id])) ->assertStatus(200) ->assertSee($item->name); } @@ -66,12 +48,30 @@ class ItemsTest extends FeatureTestCase { $item = factory(Item::class)->create(); - $this - ->loginAs() + $this->loginAs() ->delete(route('items.destroy', ['item' => $item])) ->assertStatus(302) ->assertRedirect(route('items.index')); $this->assertFlashLevel('success'); } + + private function getItemRequest() + { + $picture = UploadedFile::fake()->create('image.jpg'); + + return [ + 'company_id' => $this->company->id, + 'name' => $this->faker->title, + 'sku' => $this->faker->languageCode, + 'picture' => $picture, + 'description' => $this->faker->text(100), + 'purchase_price' => $this->faker->randomFloat(2,10,20), + 'sale_price' => $this->faker->randomFloat(2,10,20), + 'quantity' => $this->faker->randomNumber(2), + 'category_id' => $this->company->categories()->first()->id, + 'tax_id' => $this->company->taxes()->first()->id, + 'enabled' => $this->faker->boolean ? 1 : 0 + ]; + } } \ No newline at end of file diff --git a/tests/Feature/FeatureTestCase.php b/tests/Feature/FeatureTestCase.php index 925dd38a1..9b6eaa309 100644 --- a/tests/Feature/FeatureTestCase.php +++ b/tests/Feature/FeatureTestCase.php @@ -1,14 +1,7 @@ faker = Factory::create(); $this->user = User::first(); $this->company = $this->user->first()->companies()->first(); @@ -44,18 +38,24 @@ abstract class FeatureTestCase extends TestCase */ public function loginAs(User $user = null, Company $company = null) { - if(!$user) $user = $this->user; - if(!$company) $company = $user->companies()->first(); + if (!$user) { + $user = $this->user; + } + + if (!$company) { + $company = $user->companies()->first(); + } + $this->startSession(); - return $this->actingAs($user) - ->withSession(['company_id' => $company->id]); + + return $this->actingAs($user)->withSession(['company_id' => $company->id]); } public function assertFlashLevel($excepted) { - $flash["level"] = null; - if($flashMessage = session('flash_notification')) - { + $flash['level'] = null; + + if ($flashMessage = session('flash_notification')) { $flash = $flashMessage->first(); } diff --git a/tests/Feature/Incomes/CustomersTest.php b/tests/Feature/Incomes/CustomersTest.php index b8dd1d505..0d86a44be 100644 --- a/tests/Feature/Incomes/CustomersTest.php +++ b/tests/Feature/Incomes/CustomersTest.php @@ -10,42 +10,46 @@ class CustomersTest extends FeatureTestCase { public function testItShouldCreateOnlyCustomerWithoutUser() { - $customer = $this->getCustomerData(); $this->loginAs() - ->post(route("customers.store"), $customer) + ->post(route('customers.store'), $this->getCustomerRequest()) ->assertStatus(302) - ->assertRedirect(route("customers.index")); - $this->assertFlashLevel("success"); + ->assertRedirect(route('customers.index')); + + $this->assertFlashLevel('success'); } public function testItShouldCreateCustomerWithUser() { - $customerWithUser = $this->getCustomerDataWithUser(); + $customer = $this->getCustomerRequestWithUser(); $this->loginAs() - ->post(route("customers.store"), $customerWithUser) + ->post(route('customers.store'), $customer) ->assertStatus(302) - ->assertRedirect(route("customers.index")); - $this->assertFlashLevel("success"); + ->assertRedirect(route('customers.index')); - $user = User::where("email", $customerWithUser["email"])->first(); + $this->assertFlashLevel('success'); + + $user = User::where('email', $customer['email'])->first(); + $this->assertNotNull($user); - $this->assertEquals($customerWithUser["email"], $user->email); + $this->assertEquals($customer['email'], $user->email); } public function testItShouldNotCreateCustomerWithExistsUser() { - $customerWithUser = $this->getCustomerDataWithUser(); - User::create($customerWithUser); + $customer = $this->getCustomerRequestWithUser(); + + User::create($customer); $this->loginAs() - ->post(route('customers.store'), $customerWithUser) + ->post(route('customers.store'), $customer) ->assertSessionHasErrors(['email']); } public function testItShouldBeSeeTheCustomersPage() { - $customer = Customer::create($this->getCustomerData()); + $customer = Customer::create($this->getCustomerRequest()); + $this ->loginAs() ->get(route('customers.index')) @@ -55,7 +59,8 @@ class CustomersTest extends FeatureTestCase public function testItShouldBeSeeTheEditCustomersPage() { - $customer = Customer::create($this->getCustomerData()); + $customer = Customer::create($this->getCustomerRequest()); + $this ->loginAs() ->get(route('customers.edit', ['customer' => $customer->id])) @@ -66,21 +71,24 @@ class CustomersTest extends FeatureTestCase public function testItShouldUpdateTheCustomer() { - $customerData = $this->getCustomerData(); - $customer = Customer::create($customerData); - $customerData["name"] = $this->faker->name; + $request = $this->getCustomerRequest(); + + $customer = Customer::create($request); + + $request['name'] = $this->faker->name; $this ->loginAs() - ->patch(route('customers.update', $customer->id), $customerData) + ->patch(route('customers.update', $customer->id), $request) ->assertStatus(302) ->assertRedirect(route('customers.index')); + $this->assertFlashLevel('success'); } public function testItShouldDeleteTheCustomer() { - $customer = Customer::create($this->getCustomerData()); + $customer = Customer::create($this->getCustomerRequest()); $this->loginAs() ->delete(route('customers.destroy', $customer->id)) @@ -97,8 +105,7 @@ class CustomersTest extends FeatureTestCase //TODO : This will write after done invoice and revenues tests. } - // Helpers - private function getCustomerData() + private function getCustomerRequest() { return [ 'company_id' => $this->company->id, @@ -113,11 +120,11 @@ class CustomersTest extends FeatureTestCase ]; } - private function getCustomerDataWithUser() + private function getCustomerRequestWithUser() { $password = $this->faker->password; - return $this->getCustomerData() + [ + return $this->getCustomerRequest() + [ 'create_user' => 1, 'locale' => 'en-GB', 'password' => $password, diff --git a/tests/TestCase.php b/tests/TestCase.php index 63c894417..2932d4a69 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,19 +2,9 @@ namespace Tests; -use Illuminate\Foundation\Testing\DatabaseMigrations; -use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; -use Illuminate\Support\Facades\Artisan; abstract class TestCase extends BaseTestCase { - use CreatesApplication, DatabaseMigrations; - - protected function setUp() - { - parent::setUp(); - Artisan::call('db:seed', ['--class' => '\Database\Seeds\TestCompany', '--force' => true]); - Artisan::call('company:seed',['company' => 1]); - } + use CreatesApplication; }