Good practice with Phpunit tests

Asked

Viewed 172 times

0

I’m starting tests, and I wanted to know if I’m following correctly, use Portable, and have had some dilemmas to develop a test of a service that I have. He does some operations in an app via API, but since I have no way to make a "mock" of this api I had to do the test "sequential" ( in production ):

<?php

namespace Tests\Feature;


use App\Exceptions\AppConnectionException;
use App\Exceptions\AppErrorException;
use App\Exceptions\AppNullException;
use App\Services\App\AppService;
use App\Services\App\Responses\Card;
use Tests\TestCase;

class AppServiceTest extends TestCase
{
  /** @var AppService */
  private $appService;
  /** @var int */
  private $pipeId,$destinationPhase;

  protected function setUp(): void {
    parent::setUp();
    $this->appService = new AppService();
    $this->pipeId = 533822;
    $this->destinationPhase = 3656118;

  }

  /**
   * @test
   * @throws AppConnectionException
   * @throws AppErrorException
   * @throws AppNullException
   */
  public function test(){
//  CREATE
    $title = 'teste '. now()->toTimeString();
    $fields = ['nome_completo'=>'igor'];

    $card = $this->appService->createCard($this->pipeId,$title,$fields);
    $this->assertInstanceOf(Card::class,$card);
    $this->assertEquals($title,$card->title);

//  SHOW CARD
    $card = $this->appService->showCard($card->id);
    $this->assertInstanceOf(Card::class,$card);
    $this->assertIsArray($card->rawData); // TODO: melhorar acerto verificando os fields criados acima

//    UPDATE FIELD
    $fielid = array_keys($fields)[0];
    $newvalue = $fielid . " updated";
    $mutation = $this->appService->updateField($card->id,$fielid,$newvalue);
    $this->assertTrue($mutation);

//    MOVE CARD
    $card = $this->appService->moveCardToPhase($card->id,$this->destinationPhase);
    $this->assertInstanceOf(Card::class,$card);
    $this->assertEquals($card->currentPhase['id'],$this->destinationPhase);

  }

}

I imagine that each test should be individual, right ? this sequence is very wrong ?

2 answers

2

To improve these tests it is more appropriate to separate them into several methods, so everything is more organized and it is possible to have a better sense of everything that is being tested. Follow the remade class:

<?php

namespace Tests\Feature;


use App\Exceptions\AppConnectionException;
use App\Exceptions\AppErrorException;
use App\Exceptions\AppNullException;
use App\Services\App\AppService;
use App\Services\App\Responses\Card;
use Tests\TestCase;

class AppServiceTest extends TestCase
{

  private $appService;
  private $pipeId;
  private $destinationPhase;
  private $card;
  private $fields;

  protected function setUp(): void
  {

    parent::setUp();
    $this->appService       = new AppService();
    $this->pipeId           = 533822;
    $this->destinationPhase = 3656118;
    $this->fields           = ['nome_completo'=>'igor'];
   
  }

  public function testCardCreation(): void
  {
    
     $title      = 'teste '. now()->toTimeString();
     $this->card = $this->appService->createCard($this->pipeId, $title, $this->fields);

     $this->assertInstanceOf(Card::class, $this->card);
     $this->assertEquals($title, $this->card->title);

  }
  
  public function testShowCard(): void
  {

     $card = $this->appService->showCard($this->card->id);

     $this->assertInstanceOf(Card::class, $card);
     $this->assertIsArray($card->rawData); // TODO: melhorar acerto verificando os fields criados acima

  }
    
  public function testCardUpdate(): void
  {

    
     $fielid   = array_keys($this->fields)[0];
     $newvalue = $fielid . " updated";
     $mutation = $this->appService->updateField($this->card->id, $fielid, $newvalue);

     $this->assertTrue($mutation);
    
  }

  public function testCardMove(): void
  {

     $card = $this->appService->moveCardToPhase($this->card->id, $this->destinationPhase);

     $this->assertInstanceOf(Card::class, $card);
     $this->assertEquals($card->currentPhase['id'], $this->destinationPhase);

  }

}

-1

Start by dividing the test into smaller tests to make it easier to visualize:

public function testCreate(){
    // code
  }

  public function testShowCard(){
    // code
  }

  public function testUpdateField(){
    // code
  }
  function testMoveCard(){
    // code
  }

From there you start to define attributes for your test class, and to define them, create a function called setUp (she will be called before each test).

If in doubt, always remember: each function should be responsible for testing A functionality of the project. Here it becomes easier to verify in which functionality the error occurred. And read the documentation of the PHPUnit and of Laravel to improve the tests.

Good studies ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.