DEV Community

Cover image for [php-curtas]: Testando FormRequest no Laravel
Tadeu Barbosa
Tadeu Barbosa

Posted on

3 1

[php-curtas]: Testando FormRequest no Laravel

Há uma maneira de testar que o seu Controller irá receber exatamente o que você precisa, e isso você poderá fazer tanto antes de criar um Request (com TDD), ou após a criação para garantir que nada irá mudar!

Para testar você vai criar o seu teste normalmente:

use Tests\TestCase;

class CreateContactRequestTest extends TestCase
{
  public function testEnsureAValidIdWilBePassed()
  {
    //
  }
}
Enter fullscreen mode Exit fullscreen mode

Após isso adicione o seguinte método setUp:

public function setUp(): void
{
  parent::setUp();
  $request = new CreateContactRequest();
  $this->rules     = $request->rules();
  $this->validator = $this->app['validator'];
}
Enter fullscreen mode Exit fullscreen mode

Adicione também esses dois métodos para validação do Request:

protected function getFieldValidator($field, $value)
{
  return $this->validator->make(
    [$field => $value],
    [$field => $this->rules[$field]]
  );
}

protected function validateField($field, $value)
{
  return $this->getFieldValidator($field, $value)->passes();
}
Enter fullscreen mode Exit fullscreen mode

Por fim, teste os fields que serão passados:

public function testEnsureAValidIdWilBePassed(): void
{
  $this->assertFalse($this->validateField('id', ''));
  $this->assertFalse($this->validateField('id', null));
  $this->assertTrue($this->validateField('id', 'any_id'));
}
Enter fullscreen mode Exit fullscreen mode

Juntando tudo, teremos:

use Tests\TestCase;

class CreateContactRequestTest extends TestCase
{
  public function setUp(): void
  {
    parent::setUp();
    $request = new CreateContactRequest();
    $this->rules     = $request->rules();
    $this->validator = $this->app['validator'];
  }

  public function testEnsureAValidIdWilBePassed()
  {
    $this->assertFalse($this->validateField('id', ''));
    $this->assertFalse($this->validateField('id', null));
    $this->assertTrue($this->validateField('id', 'any_id'));
  }

  protected function getFieldValidator($field, $value)
  {
    return $this->validator->make(
      [$field => $value],
      [$field => $this->rules[$field]]
    );
  }

  protected function validateField($field, $value)
  {
    return $this->getFieldValidator($field, $value)->passes();
  }
}
Enter fullscreen mode Exit fullscreen mode

Prontinho! :D
Espero ter ajudado! Deixe seu like ;)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay