2018-02-24 19:01:34 -08:00
< ? php
use App\Helpers\Helper ;
use App\Http\Transformers\CompaniesTransformer ;
use App\Models\Company ;
use App\Models\Setting ;
use Illuminate\Support\Facades\Auth ;
class ApiCompaniesCest
{
protected $user ;
protected $timeFormat ;
public function _before ( ApiTester $I )
{
$this -> user = \App\Models\User :: find ( 1 );
$I -> haveHttpHeader ( 'Accept' , 'application/json' );
$I -> amBearerAuthenticated ( $I -> getToken ( $this -> user ));
}
/** @test */
public function indexCompanys ( ApiTester $I )
{
$I -> wantTo ( 'Get a list of companies' );
// call
$I -> sendGET ( '/companies?order_by=id&limit=10' );
$I -> seeResponseIsJson ();
$I -> seeResponseCodeIs ( 200 );
$response = json_decode ( $I -> grabResponse (), true );
// dd($response);
// sample verify
2021-06-10 13:15:52 -07:00
$company = App\Models\Company :: withCount ( 'assets as assets_count' , 'licenses as licenses_count' , 'accessories as accessories_count' , 'consumables as consumables_count' , 'components as components_count' , 'users as users_count' )
2018-02-24 19:01:34 -08:00
-> orderByDesc ( 'created_at' ) -> take ( 10 ) -> get () -> shuffle () -> first ();
2018-07-16 14:41:51 -07:00
$I -> seeResponseContainsJson ( $I -> removeTimestamps (( new CompaniesTransformer ) -> transformCompany ( $company )));
2018-02-24 19:01:34 -08:00
}
/** @test */
public function createCompany ( ApiTester $I , $scenario )
{
$I -> wantTo ( 'Create a new company' );
2021-06-10 13:17:44 -07:00
$temp_company = \App\Models\Company :: factory () -> make ([
2021-06-10 13:15:52 -07:00
'name' => 'Test Company Tag' ,
2018-02-24 19:01:34 -08:00
]);
// setup
$data = [
'name' => $temp_company -> name ,
];
// create
$I -> sendPOST ( '/companies' , $data );
$I -> seeResponseIsJson ();
$I -> seeResponseCodeIs ( 200 );
}
// Put is routed to the same method in the controller
// DO we actually need to test both?
2021-06-10 13:15:52 -07:00
2018-02-24 19:01:34 -08:00
/** @test */
public function updateCompanyWithPatch ( ApiTester $I , $scenario )
{
$I -> wantTo ( 'Update an company with PATCH' );
// create
2021-06-10 13:17:44 -07:00
$company = \App\Models\Company :: factory () -> create ([
2018-02-24 19:01:34 -08:00
'name' => 'Original Company Name' ,
]);
$I -> assertInstanceOf ( \App\Models\Company :: class , $company );
2021-06-10 13:17:44 -07:00
$temp_company = \App\Models\Company :: factory () -> make ([
2021-06-10 13:15:52 -07:00
'name' => 'updated company name' ,
2018-02-24 19:01:34 -08:00
]);
$data = [
'name' => $temp_company -> name ,
];
$I -> assertNotEquals ( $company -> name , $data [ 'name' ]);
// update
2021-06-10 13:15:52 -07:00
$I -> sendPATCH ( '/companies/' . $company -> id , $data );
2018-02-24 19:01:34 -08:00
$I -> seeResponseIsJson ();
$I -> seeResponseCodeIs ( 200 );
$response = json_decode ( $I -> grabResponse ());
$I -> assertEquals ( 'success' , $response -> status );
$I -> assertEquals ( trans ( 'admin/companies/message.update.success' ), $response -> messages );
$I -> assertEquals ( $company -> id , $response -> payload -> id ); // company id does not change
$I -> assertEquals ( $temp_company -> name , $response -> payload -> name ); // company name updated
// Some manual copying to compare against
$temp_company -> created_at = Carbon :: parse ( $response -> payload -> created_at -> datetime );
$temp_company -> updated_at = Carbon :: parse ( $response -> payload -> updated_at -> datetime );
$temp_company -> id = $company -> id ;
// verify
2021-06-10 13:15:52 -07:00
$I -> sendGET ( '/companies/' . $company -> id );
2018-02-24 19:01:34 -08:00
$I -> seeResponseIsJson ();
$I -> seeResponseCodeIs ( 200 );
$I -> seeResponseContainsJson (( new CompaniesTransformer ) -> transformCompany ( $temp_company ));
}
/** @test */
public function deleteCompanyTest ( ApiTester $I , $scenario )
{
$I -> wantTo ( 'Delete an company' );
// create
2021-06-10 13:17:44 -07:00
$company = \App\Models\Company :: factory () -> create ([
2021-06-10 13:15:52 -07:00
'name' => 'Soon to be deleted' ,
2018-02-24 19:01:34 -08:00
]);
$I -> assertInstanceOf ( \App\Models\Company :: class , $company );
// delete
2021-06-10 13:15:52 -07:00
$I -> sendDELETE ( '/companies/' . $company -> id );
2018-02-24 19:01:34 -08:00
$I -> seeResponseIsJson ();
$I -> seeResponseCodeIs ( 200 );
$response = json_decode ( $I -> grabResponse ());
$I -> assertEquals ( 'success' , $response -> status );
$I -> assertEquals ( trans ( 'admin/companies/message.delete.success' ), $response -> messages );
// verify, expect a 200
2021-06-10 13:15:52 -07:00
$I -> sendGET ( '/companies/' . $company -> id );
2018-02-24 19:01:34 -08:00
$I -> seeResponseCodeIs ( 200 );
$I -> seeResponseIsJson ();
}
}