cboissenin
1/3/2017 - 4:41 PM

PHPUnit Test

PHPUnit Test

<?php


// Execute PHP unit (inside main laravel root folder)
vendor/bin/phpunit 

// Execute PHP unit on a single file
vendor/bin/phpunit filepath

// Execute PHP unit on a single file - just one test
vendor/bin/phpunit --filter testSaveAndDrop EscalationGroupTest 


use App\Crisis;

class CrisisTest extends TestCase
{

  public function test_it_fetches_crisis()
  {
    $this->call('GET', 'api/v1/crisis');

    $this->assertResponseOk();
  }

  public function test_it_fetches_crisis_with_parameters()
  {
    $where = [
      'region' => 'Africa - East and Central',
      'status' => 'Alert',
    ];

    $params = [
      'number' => 2,
      'where' => json_encode($where),
    ];

    $response = $this->call('GET', 'api/v1/crisis', $params);

    $content = json_decode($response->getContent());
    dump($content->data[0]->id);

    $this->assertCount($params['number'], $content->data);

    foreach ($where as $key => $value) {
      $this->assertEquals($value, $content->data[0]->{$key});
    }
  }


  public function test_it_fetch_a_non_existing_crisis()
  {
    $response = $this->call('GET', 'api/v1/crisis/3094');

    $this->assertEquals(404, $response->status());
  }


  public function test_it_fetch_an_existing_crisis()
  {
    $crisis = Crisis::first();

    $this->get('api/v1/crisis/' . $crisis->_id)->seeJsonStructure([
      'data' => [
        'id',
        'name',
        'description',
      ]
    ]);

    $this->assertResponseOk();
  }

  public function test_using_include_event_should_include_events()
  {
    $crisis = Crisis::first();

    $this->get('api/v1/crisis/' . $crisis->_id . '?include=events')->seeJsonStructure([
      'data' => [
        'events' => [],
      ]
    ]);
  }

}