Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Desu's avatar
Level 4

What do you actually test?

It can be obvious but for me it is not. I watched series about testing and even read a book, but I don't get it. Lets pretend I am building project on my own. How can I specify acceptance tests etc.? I don't know how to start and I feel a little bit overwhelmed.

What to test? Should I test CRUD actions? Its pretty obvious that I should test complicated calculations etc. but I don't see anything complicated in calling save method on Eloquent. And come on Taylor has already tested this code, so should I test controllers calling eloquent models etc.? Should I wait until I involve my own business logic and then start testing?

Lets make it by example. I have to build a quiz. The first step is showing a feed of questions based on weather user is logged in or not. Questions table has column requires_login, so should I test if eloquent works?! Cause this comes down to check weather Auth facade works, and weather eloquent build right query.. Testing this doesn't make much sense to me.

The second step is adding a new question and again, should I test weather the eloquent acts right?

Please provide me with some real world example, cause I think this will be my AHA moment. We don't use TDD at work so it is hard for me to get this concept right.

0 likes
2 replies
ohffs's avatar
ohffs
Best Answer
Level 50

If your code isn't (yet!) very complicated then you're probably best doing 'acceptance' style tests - ie, replicating browser-like actions that you'd do yourself to check the code was working. So in your test you might have something along the lines of :

// test login
// create a user 'tester'
$this->visit('/login')
  ->type('tester', 'username')
  ->type('thepassword', 'password')
  ->press('login')
  ->see('Welcome tester');

// test quiz
// create a quiz question record
// create a test user
$this->actingAs($user)
  ->visit('/quiz')
  ->see($question1->title)
  ->select($question1->correct_answer_id)
  ->press('Answer')
  ->see('Correct!');
// and the equivalent for picking the wrong answer

And you write those tests before the code for your app in TDD. Once the code gets a bit bigger and you've maybe looking to extract/refactor things then you can look at writing 'unit' tests for the way you expect that code to run - then write the code. Then you can still run your original acceptance tests and see they pass along with the new ones for your new code.

Generally you wouldn't test the basic functions of eloquent (or other aspects of the framework). But, for a contrived example, maybe your app creates a DB record in a mailing list table when a user registers so you might want to do a test of :

$this->seeInDatabase('mailinglist', ['email' => $testuser->email]);
2 likes
MikeHopley's avatar

Ideally, you should test everything that you think might fail. In particular, consider how changes to the code could potentially break something. You want tests to alert you when this happens.

You should not be testing whether Eloquent works. We know that Eloquent works. But you may wish to test whether you've used Eloquent correctly, especially if some CRUD operations are more involved than usual.

Ultimately it's up to you. The most important thing is to start testing. Start with the things you find most useful. As time goes on, you may want to test more things.

Whenever possible, I recommend adding a failing test before you fix a bug. This means the bug won't recur (or at least, you'll know about it when it does!).

1 like

Please or to participate in this conversation.