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

Skyles's avatar

PHP unit testing

I am having this weird problem with phpunit. When i run it, it gives me failure. However on the browser everything is working fine. I then commented part of the test code and then run, still got error. Then I uncommented the whole thing, it then suddenly works fine with no error. Anyone having this kind of issue?

And one more thing, why do we do unit testing when we can just use the browser to get any error or using postman?

New to Laravel. :)

0 likes
5 replies
Sergiu17's avatar

Would be nice to show us your test

1 like
Tray2's avatar

Unit test are much faster and it's not only for testing urls.

Let's say that you have an authors table containing last name and first name.

Like this

1 | Jordan | Robert

And you want to display it like this in your view

Jordan, Robert

Then you can write a test for that like this

/**
    * @test
    */
    public function name_property_returns_the_authors_last_name_and_first_name()
    {
        $author = factory(Author::class)->create([
            'first_name' => 'Robert',
            'last_name' => 'Jordan'
        ]);

        $this->assertEquals('Jordan, Robert', $author->name);
    }

As you see this does not even hit a route, it just tests the method that it returns the desired value. You can use the browser to test this yes but if you have hundreds of this little helpers and such it will take a long time to test everything manually.

You can test your validation of your request to make sure you have the correct validation rules.

/**
    * @test
    */
    public function author_first_name_is_required()
    {
        $author = factory(Author::class)->make([
            'first_name' => null,
            'last_name' => 'Jordan'
        ]);

        $this->post('/authors', $author->toArray())->assertSessionHasErrors('first_name');
        $this->assertEquals(0, Author::count());
    }

If you need to implement a new feature then you can do so and then check if you have broken anything quite fast instead of once again testing everything in the browser.

You also use it when you make changes to your code to make it cleaner aka refactoring.

2 likes
Skyles's avatar

@sergiu17 Here it is sir

<?php

namespace Tests\Feature;

use Tests\TestCase;
//use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class ThreadsTest extends TestCase
{
    use DatabaseMigrations;
    /**@test */
    public function setUp() :void{
        parent::setUp();
        $this->thread = factory('App\Thread')->create();
    }

    public function test_a_user_can_view_all_threads()
    {
        
        $this->get('/threads')->assertSee($this->thread->title);

        
    }

    public function test_a_user_can_view_one_thread()
    {
        
        $this->get('/threads/' . $this->thread->id)->assertSee($this->thread->title);

        
    }

    public function test_a_user_can_read_replies_associated_with_a_thread()
    {

        $reply = factory('App\Reply')->create(['thread_id' => $this->thread->id]);
        $this->get('/threads/' . $this->thread->id)->assertSee($reply->body);

    }
}
Tray2's avatar

The setup method should not have a test annotation.

 use DatabaseMigrations;
    /**@test */
    public function setUp() :void{

Should be

 use DatabaseMigrations;

    public function setUp() :void{

And you really should add

protected $thread;

Above the setUp method.

1 like

Please or to participate in this conversation.