pedroroccon's avatar

Single test passing, but fails when running all tests (404 error)

Hello!

I'm facing a strange issue in my testing class. Well, I'm using Laravel 5.5 with PHPUnit 5.7 and testing a file upload. Here's my file EmpresaLogotipoImagemTest.php

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\RefreshDatabase;

class EmpresaLogotipoImagemTest extends TestCase
{
    use RefreshDatabase;

    public function setUp()
    {
        parent::setUp();
        $this->user = factory('App\User')->create();
        $this->empresa = factory('App\Empresa')->create();
    }

    /** @test */
    public function the_image_is_saved_when_uploaded()
    {
        Storage::fake('local');
        $arquivo = UploadedFile::fake()->image('logo.jpg', 1000, 1000);

        $response = $this->actingAs($this->user)->post($this->empresa->path() . '/logotipo', [
            'arquivo' => $arquivo
        ]);

        Storage::disk('local')->assertExists('empresa/' . $arquivo->hashName());
    }

    /** @test */
    public function the_old_image_is_removed_when_we_change_send_another_image()
    {
        Storage::fake('local');
        $antiga = $this->empresa->logotipo;
        $arquivo = UploadedFile::fake()->image('logo.jpg', 1000, 1000);

        $response = $this->actingAs($this->user)->post($this->empresa->path() . '/logotipo', [
            'arquivo' => $arquivo
        ]);

        Storage::disk('local')->assertExists('empresa/' . $arquivo->hashName());
        Storage::disk('local')->assertMissing($antiga);
    }

}


When I execute the two tests using --filter parameter, everything is ok. But when i execute the entire class (EmpresaLogotipoImagemTest), the second method always thrown an exception. Using dd($response->exeception) on the second method, I see that is an 404 error (.Symfony\Component\HttpKernel\Exception\NotFoundHttpException).

If I switch the methods order, the same error always happen on second method.

Anyone knows what is happening? Where is my controller.

<?php

namespace App\Http\Controllers\Admin\Empresa;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Empresa;
use Storage;

class LogotipoController extends Controller
{

    public function upload(Request $request, Empresa $empresa)
    {

        $arquivo = $request->file('arquivo');
        $caminho = $arquivo->store('empresa');

    if(!is_null($empresa->logotipo)):
            Storage::delete($empresa->logotipo);
        endif;

        $empresa->logotipo = $caminho;
        $empresa->update();
    }
}

Thanks! :)

0 likes
3 replies
Tray2's avatar

Are you doing anything in the setUp() that should be reset after each test? You should try to reset the values in a tearDown().

pedroroccon's avatar

@Tray2 Thanks for reply!

I'm doing only this:

public function setUp()
{
    parent::setUp();
    $this->user = factory('App\User')->create();
    $this->empresa = factory('App\Empresa')->create();
}

I tried to make a GET request without parameters, and received the same 404 error again, but if i run the second method alone, it gets returns me green. It's seems that afther the first method the tests always will return 404.

public function the_old_image_is_removed_when_we_change_send_another_image()
{
    Storage::fake('local');
    $antiga = $this->empresa->logotipo;
    $arquivo = UploadedFile::fake()->image('logo.jpg', 1000, 1000);

    $response = $this->actingAs($this->user)->get('painel');

    dd($response->exception); // 404 error

    Storage::disk('local')->assertExists('empresa/' . $arquivo->hashName());
    Storage::disk('local')->assertMissing($antiga);
}
pedroroccon's avatar

I wrote two new tests. They make a get request to 'admin' url. I ran the tests one by one and get green! But when I try to run all together, the second method always fails saying that an error 404 was thrown.

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\RefreshDatabase;

class EmpresaLogotipoImagemTest extends TestCase
{
    use RefreshDatabase;

    /** @test */
    function my_first_request()
    {
        $response = $this->get('admin'); // Ok
        $response->assertStatus(200);
    }

    /** @test */
    function my_second_request()
    {
        $response = $this->get('admin'); // Fails
        $response->assertStatus(200);
    }
}

Please or to participate in this conversation.