trifek's avatar

First feature test - opinions

Hi, I write my first features test in Laravel:

class CompanyTest extends TestCase
{
    use RefreshDatabase,
        WithFaker;

    public function setUp(): void
    {
        parent::setUp();
        $this->withoutExceptionHandling();
        $this->refreshDatabase();
        $this->userRepository = app(UserRepositoryInterface::class);
        $this->seed('DatabaseSeeder');
        $this->company = $this->userRepository->findBy('is_company', true);
    }

    public function testCompanyVewIsWorkingCorrectly()
    {
        $response = $this->get(route('company', $this->company->slug));
        $response->assertSeeText('Nadaj przesyłkę');
        $response->assertStatus(200);
    }

    public function testSendContactForm()
    {
        $this->actingAs($this->company);
        $response = $this->post(route('contactWithCompany', $this->company->slug), [
            'name' => $this->faker->text(150),
            'email' => $this->faker->email,
            'theme' => $this->faker->text(150),
            'message' => $this->faker->text(500)
        ]);

        $response->assertSessionHas('success', 'Wiadomość została wysłana poprawnie!');
        $response->assertSessionHasNoErrors();
        $response->assertRedirect();
    }

    public function testPackageOrderForm()
    {
        $user = $this->userRepository->findBy('is_company', false);
        $this->actingAs($user);

        $response = $this->post(route('orderPackage', $this->company->slug), [
            'order_name' => $this->faker->firstName,
            'order_surname' => $this->faker->lastName,
            'order_address' => $this->faker->address,
            'order_city' => $this->faker->city,
            'building_number' => $this->faker->buildingNumber,
            'order_email' => $this->faker->email,
            'shipping_method' => 1,
            'package_type' => 1,
            'package_size' => 1,
            'order_sending_parcel' => 'RUM001',
            'order_recipient_parcel' => 'RUM001',
            'shipping_company' => 1,
            'user_id' => 1,
            'order_phone' => '111223344',
            'order_post_code' => $this->faker->postcode,
        ]);

        $response->assertSessionHasNoErrors();
        $response->assertRedirect();
    }
}

Could I have your opinions, please? Is everything ok or would you change something?

0 likes
1 reply
martinbean's avatar

@trifek You seem to be testing many features in one file. Sending a contact form, and ordering a package are definitely two different features and as such, would move those test cases to their own files (CompanyContactFormTest, CompanyPackageOrderFormTest, etc) so it’s clear from each filename what it is that file is actually testing.

Please or to participate in this conversation.