2018-02-25 12:10:02 -08:00
< ? php
use App\Helpers\Helper ;
use App\Http\Transformers\CategoriesTransformer ;
use App\Models\Category ;
use App\Models\Setting ;
use Illuminate\Support\Facades\Auth ;
class ApiCategoriesCest
{
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 indexCategorys ( ApiTester $I )
{
$I -> wantTo ( 'Get a list of categories' );
// call
$I -> sendGET ( '/categories?order_by=id&limit=10' );
$I -> seeResponseIsJson ();
$I -> seeResponseCodeIs ( 200 );
$response = json_decode ( $I -> grabResponse (), true );
// sample verify
2021-06-10 13:15:52 -07:00
$category = App\Models\Category :: withCount ( 'assets as assets_count' , 'accessories as accessories_count' , 'consumables as consumables_count' , 'components as components_count' , 'licenses as licenses_count' )
2018-02-25 12:10:02 -08:00
-> orderByDesc ( 'created_at' ) -> take ( 10 ) -> get () -> shuffle () -> first ();
2018-07-16 14:41:51 -07:00
$I -> seeResponseContainsJson ( $I -> removeTimestamps (( new CategoriesTransformer ) -> transformCategory ( $category )));
2018-02-25 12:10:02 -08:00
}
/** @test */
public function createCategory ( ApiTester $I , $scenario )
{
$I -> wantTo ( 'Create a new category' );
2021-06-10 13:17:44 -07:00
$temp_category = \App\Models\Category :: factory () -> assetLaptopCategory () -> make ([
2021-06-10 13:15:52 -07:00
'name' => 'Test Category Tag' ,
2018-02-25 12:10:02 -08:00
]);
// setup
$data = [
'category_type' => $temp_category -> category_type ,
'checkin_email' => $temp_category -> checkin_email ,
'eula_text' => $temp_category -> eula_text ,
'name' => $temp_category -> name ,
'require_acceptance' => $temp_category -> require_acceptance ,
'use_default_eula' => $temp_category -> use_default_eula ,
];
// create
$I -> sendPOST ( '/categories' , $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-25 12:10:02 -08:00
/** @test */
public function updateCategoryWithPatch ( ApiTester $I , $scenario )
{
$I -> wantTo ( 'Update an category with PATCH' );
// create
2021-06-10 13:17:44 -07:00
$category = \App\Models\Category :: factory () -> assetLaptopCategory ()
2018-02-25 12:10:02 -08:00
-> create ([
'name' => 'Original Category Name' ,
]);
$I -> assertInstanceOf ( \App\Models\Category :: class , $category );
2021-06-10 13:17:44 -07:00
$temp_category = \App\Models\Category :: factory () -> accessoryMouseCategory () -> make ([
2021-06-10 13:15:52 -07:00
'name' => 'updated category name' ,
2018-02-25 12:10:02 -08:00
]);
$data = [
'category_type' => $temp_category -> category_type ,
'checkin_email' => $temp_category -> checkin_email ,
'eula_text' => $temp_category -> eula_text ,
'name' => $temp_category -> name ,
'require_acceptance' => $temp_category -> require_acceptance ,
'use_default_eula' => $temp_category -> use_default_eula ,
];
$I -> assertNotEquals ( $category -> name , $data [ 'name' ]);
// update
2021-06-10 13:15:52 -07:00
$I -> sendPATCH ( '/categories/' . $category -> id , $data );
2018-02-25 12:10:02 -08:00
$I -> seeResponseIsJson ();
$I -> seeResponseCodeIs ( 200 );
$response = json_decode ( $I -> grabResponse ());
$I -> assertEquals ( 'success' , $response -> status );
$I -> assertEquals ( trans ( 'admin/categories/message.update.success' ), $response -> messages );
$I -> assertEquals ( $category -> id , $response -> payload -> id ); // category id does not change
$I -> assertEquals ( $temp_category -> name , $response -> payload -> name ); // category name updated
// Some manual copying to compare against
$temp_category -> created_at = Carbon :: parse ( $response -> payload -> created_at );
$temp_category -> updated_at = Carbon :: parse ( $response -> payload -> updated_at );
$temp_category -> id = $category -> id ;
// verify
2021-06-10 13:15:52 -07:00
$I -> sendGET ( '/categories/' . $category -> id );
2018-02-25 12:10:02 -08:00
$I -> seeResponseIsJson ();
$I -> seeResponseCodeIs ( 200 );
$I -> seeResponseContainsJson (( new CategoriesTransformer ) -> transformCategory ( $temp_category ));
}
/** @test */
public function deleteCategoryTest ( ApiTester $I , $scenario )
{
$I -> wantTo ( 'Delete an category' );
// create
2021-06-10 13:17:44 -07:00
$category = \App\Models\Category :: factory () -> assetLaptopCategory () -> create ([
2021-06-10 13:15:52 -07:00
'name' => 'Soon to be deleted' ,
2018-02-25 12:10:02 -08:00
]);
$I -> assertInstanceOf ( \App\Models\Category :: class , $category );
// delete
2021-06-10 13:15:52 -07:00
$I -> sendDELETE ( '/categories/' . $category -> id );
2018-02-25 12:10:02 -08:00
$I -> seeResponseIsJson ();
$I -> seeResponseCodeIs ( 200 );
$response = json_decode ( $I -> grabResponse ());
$I -> assertEquals ( 'success' , $response -> status );
$I -> assertEquals ( trans ( 'admin/categories/message.delete.success' ), $response -> messages );
// verify, expect a 200
2021-06-10 13:15:52 -07:00
$I -> sendGET ( '/categories/' . $category -> id );
2018-02-25 12:10:02 -08:00
$I -> seeResponseCodeIs ( 200 );
$I -> seeResponseIsJson ();
}
}