Are you doing anything in the setUp() that should be reset after each test? You should try to reset the values in a tearDown().
Aug 2, 2018
3
Level 10
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! :)
Please or to participate in this conversation.