snipe-it/tests/api/ApiComponentsCest.php
Daniel Meltzer 7de8f71f58 Api tests (#5096)
* Use the formated date helper to clean up verifications.

* Add Checkin/Checkout api tests.

* Accessories api test

* Add Companies API Test.

* Return ModelNotFound as a 404.

* Cleanups/simplficiations/updates.

* Locations api test.

* currency and image should be fillable on location.

* Update components api test.

* Use findOrFail so we return a 404 instead of a 200.  Matches other item types.

* order_number should be fillable in component.

* Add updated_at and permissions to information returned from api for a user.

* Add users test and flesh out factory and fillable fields.

* Add test for assets method

* API status label test.

* Disable php7.2 for now on travis until the count(null) issues are remedied

* Add serial to update.

* API model not found should return a 200
2018-02-24 19:01:34 -08:00

156 lines
5.6 KiB
PHP

<?php
use App\Helpers\Helper;
use App\Http\Transformers\ComponentsTransformer;
use App\Models\Component;
use App\Models\Setting;
use Illuminate\Support\Facades\Auth;
class ApiComponentsCest
{
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 indexComponents(ApiTester $I)
{
$I->wantTo('Get a list of components');
// call
$I->sendGET('/components?limit=10');
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
$response = json_decode($I->grabResponse(), true);
// sample verify
$component = App\Models\Component::orderByDesc('created_at')->take(10)->get()->shuffle()->first();
$I->seeResponseContainsJson((new ComponentsTransformer)->transformComponent($component));
}
/** @test */
public function createComponent(ApiTester $I, $scenario)
{
$I->wantTo('Create a new component');
$temp_component = factory(\App\Models\Component::class)->states('ram-crucial4')->make([
'name' => "Test Component Name",
'company_id' => 2
]);
// setup
$data = [
'category_id' => $temp_component->category_id,
'company_id' => $temp_component->company->id,
'location_id' => $temp_component->location_id,
'manufacturer_id' => $temp_component->manufacturer_id,
'model_number' => $temp_component->model_number,
'name' => $temp_component->name,
'order_number' => $temp_component->order_number,
'purchase_cost' => $temp_component->purchase_cost,
'purchase_date' => $temp_component->purchase_date,
'qty' => $temp_component->qty,
'serial' => $temp_component->serial
];
// create
$I->sendPOST('/components', $data);
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
}
// Put is routed to the same method in the controller
// DO we actually need to test both?
/** @test */
public function updateComponentWithPatch(ApiTester $I, $scenario)
{
$I->wantTo('Update an component with PATCH');
// create
$component = factory(\App\Models\Component::class)->states('ram-crucial4')->create([
'name' => 'Original Component Name',
'company_id' => 2,
'location_id' => 3
]);
$I->assertInstanceOf(\App\Models\Component::class, $component);
$temp_component = factory(\App\Models\Component::class)->states('ssd-crucial240')->make([
'company_id' => 3,
'name' => "updated component name",
'location_id' => 1,
]);
$data = [
'category_id' => $temp_component->category_id,
'company_id' => $temp_component->company->id,
'location_id' => $temp_component->location_id,
'min_amt' => $temp_component->min_amt,
'name' => $temp_component->name,
'order_number' => $temp_component->order_number,
'purchase_cost' => $temp_component->purchase_cost,
'purchase_date' => $temp_component->purchase_date,
'qty' => $temp_component->qty,
'serial' => $temp_component->serial,
];
$I->assertNotEquals($component->name, $data['name']);
// update
$I->sendPATCH('/components/' . $component->id, $data);
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
$response = json_decode($I->grabResponse());
$I->assertEquals('success', $response->status);
$I->assertEquals(trans('admin/components/message.update.success'), $response->messages);
$I->assertEquals($component->id, $response->payload->id); // component id does not change
$I->assertEquals($temp_component->company_id, $response->payload->company_id); // company_id updated
$I->assertEquals($temp_component->name, $response->payload->name); // component name updated
$I->assertEquals($temp_component->location_id, $response->payload->location_id); // component location_id updated
$temp_component->created_at = Carbon::parse($response->payload->created_at);
$temp_component->updated_at = Carbon::parse($response->payload->updated_at);
$temp_component->id = $component->id;
// verify
$I->sendGET('/components/' . $component->id);
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
$I->seeResponseContainsJson((new ComponentsTransformer)->transformComponent($temp_component));
}
/** @test */
public function deleteComponentTest(ApiTester $I, $scenario)
{
$I->wantTo('Delete an component');
// create
$component = factory(\App\Models\Component::class)->states('ram-crucial4')->create([
'name' => "Soon to be deleted"
]);
$I->assertInstanceOf(\App\Models\Component::class, $component);
// delete
$I->sendDELETE('/components/' . $component->id);
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
$response = json_decode($I->grabResponse());
// dd($response);
$I->assertEquals('success', $response->status);
$I->assertEquals(trans('admin/components/message.delete.success'), $response->messages);
// verify, expect a 200
$I->sendGET('/components/' . $component->id);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
}
}