2017-02-21 14:26:46 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
class ApiComponentsCest
|
|
|
|
{
|
|
|
|
protected $faker;
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
public function _before(ApiTester $I)
|
|
|
|
{
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
$this->user = \App\Models\User::find(1);
|
|
|
|
|
|
|
|
$I->amBearerAuthenticated($I->getToken($this->user));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function indexComponents(ApiTester $I)
|
|
|
|
{
|
|
|
|
$I->wantTo('Get a list of components');
|
|
|
|
|
|
|
|
// setup
|
2017-06-12 17:39:03 -07:00
|
|
|
$components = factory(\App\Models\Component::class, 10)->create();
|
2017-02-21 14:26:46 -08:00
|
|
|
|
|
|
|
// call
|
|
|
|
$I->sendGET('/components');
|
|
|
|
$I->seeResponseIsJson();
|
|
|
|
$I->seeResponseCodeIs(200);
|
|
|
|
|
|
|
|
// sample verify
|
|
|
|
$component = $components->random();
|
|
|
|
$I->seeResponseContainsJson([
|
|
|
|
'name' => $component->name,
|
|
|
|
'qty' => $component->qty,
|
|
|
|
]);
|
2017-02-23 16:32:35 -08:00
|
|
|
|
|
|
|
$I->seeResponseContainsJson([
|
|
|
|
'total' => \App\Models\Component::count(),
|
|
|
|
]);
|
2017-02-21 14:26:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function createComponent(ApiTester $I)
|
|
|
|
{
|
|
|
|
$I->wantTo('Create a new component');
|
|
|
|
|
|
|
|
// setup
|
2017-06-12 17:39:03 -07:00
|
|
|
$category = factory(\App\Models\Category::class)->create(['user_id' => $this->user->id]);
|
|
|
|
$location = factory(\App\Models\Location::class)->create(['user_id' => $this->user->id]);
|
|
|
|
$company = factory(\App\Models\Company::class)->create();
|
2017-02-21 14:26:46 -08:00
|
|
|
|
|
|
|
$data = [
|
|
|
|
'category_id' => $category->id,
|
|
|
|
'company_id' => $company->id,
|
|
|
|
'location_id' => $location->id,
|
|
|
|
'name' => $this->faker->sentence(3),
|
|
|
|
'purchase_cost' => $this->faker->randomFloat(2, 0),
|
|
|
|
'purchase_date' => $this->faker->dateTime->format('Y-m-d'),
|
|
|
|
'qty' => rand(1, 10),
|
|
|
|
];
|
|
|
|
|
|
|
|
// create
|
|
|
|
$I->sendPOST('/components', $data);
|
|
|
|
$I->seeResponseIsJson();
|
|
|
|
$I->seeResponseCodeIs(200);
|
|
|
|
|
|
|
|
$response = json_decode($I->grabResponse());
|
|
|
|
$id = $response->payload->id;
|
|
|
|
|
|
|
|
$I->assertEquals('success', $response->status);
|
|
|
|
|
|
|
|
// verify
|
|
|
|
$I->sendGET('/components/' . $id);
|
|
|
|
$I->seeResponseIsJson();
|
|
|
|
$I->seeResponseCodeIs(200);
|
|
|
|
$I->seeResponseContainsJson([
|
2017-03-14 08:37:39 -07:00
|
|
|
'id' => (int) $id,
|
|
|
|
'name' => e($data['name']),
|
|
|
|
// 'serial_number' => e($component->serial),
|
|
|
|
'location' => [
|
|
|
|
'id' => (int) $data['location_id'],
|
|
|
|
'name' => e($location->name),
|
|
|
|
],
|
|
|
|
'qty' => number_format($data['qty']),
|
|
|
|
// 'min_amt' => e($component->min_amt),
|
2017-02-21 14:26:46 -08:00
|
|
|
'category' => [
|
2017-03-14 08:37:39 -07:00
|
|
|
'id' => (int) $data['category_id'],
|
2017-02-23 16:32:35 -08:00
|
|
|
'name' => e($category->name),
|
2017-02-21 14:26:46 -08:00
|
|
|
],
|
2017-03-14 08:37:39 -07:00
|
|
|
// 'order_number' => e($component->order_number),
|
|
|
|
'purchase_date' => \App\Helpers\Helper::getFormattedDateObject($data['purchase_date'], 'date'),
|
|
|
|
'purchase_cost' => \App\Helpers\Helper::formatCurrencyOutput($data['purchase_cost']),
|
|
|
|
// 'remaining' => (int) $component->numRemaining(),
|
2017-02-21 14:26:46 -08:00
|
|
|
'company' => [
|
2017-03-14 08:37:39 -07:00
|
|
|
'id' => (int) $data['company_id'],
|
2017-02-23 16:32:35 -08:00
|
|
|
'name' => e($company->name),
|
2017-02-21 14:26:46 -08:00
|
|
|
],
|
2017-03-14 08:37:39 -07:00
|
|
|
// 'created_at' => Helper::getFormattedDateObject($component->created_at, 'datetime'),
|
|
|
|
// 'updated_at' => Helper::getFormattedDateObject($component->updated_at, 'datetime'),
|
2017-02-21 14:26:46 -08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function updateComponentWithPatch(ApiTester $I)
|
|
|
|
{
|
|
|
|
$I->wantTo('Update a component with PATCH');
|
|
|
|
|
|
|
|
// create
|
2017-06-12 17:39:03 -07:00
|
|
|
$component = factory(\App\Models\Component::class)->create();
|
2017-02-21 14:26:46 -08:00
|
|
|
$I->assertInstanceOf(\App\Models\Component::class, $component);
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'name' => $this->faker->sentence(3),
|
|
|
|
'qty' => $this->faker->randomDigit + 1,
|
|
|
|
];
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
// verify
|
|
|
|
$I->sendGET('/components/' . $component->id);
|
|
|
|
$I->seeResponseIsJson();
|
|
|
|
$I->seeResponseCodeIs(200);
|
|
|
|
$I->seeResponseContainsJson([
|
|
|
|
'name' => $data['name'],
|
|
|
|
'id' => $component->id,
|
|
|
|
'qty' => $data['qty'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function updateComponentWithPut(ApiTester $I)
|
|
|
|
{
|
|
|
|
$I->wantTo('Update a component with PUT');
|
|
|
|
|
|
|
|
// create
|
2017-06-12 17:39:03 -07:00
|
|
|
$component = factory(\App\Models\Component::class)->create();
|
2017-02-21 14:26:46 -08:00
|
|
|
$I->assertInstanceOf(\App\Models\Component::class, $component);
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'name' => $this->faker->sentence(3),
|
|
|
|
];
|
|
|
|
|
|
|
|
$I->assertNotEquals($component->name, $data['name']);
|
|
|
|
|
|
|
|
// update
|
|
|
|
$I->sendPUT('/components/' . $component->id, $data);
|
|
|
|
$I->seeResponseIsJson();
|
|
|
|
$I->seeResponseCodeIs(200);
|
|
|
|
|
|
|
|
$response = json_decode($I->grabResponse());
|
|
|
|
$I->assertEquals('success', $response->status);
|
|
|
|
|
|
|
|
// verify
|
|
|
|
$I->sendGET('/components/' . $component->id);
|
|
|
|
$I->seeResponseIsJson();
|
|
|
|
$I->seeResponseCodeIs(200);
|
|
|
|
$I->seeResponseContainsJson([
|
|
|
|
'name' => e($data['name']),
|
|
|
|
'id' => e($component->id),
|
|
|
|
'qty' => e($component->qty),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
2017-02-23 16:32:35 -08:00
|
|
|
public function deleteComponentTest(ApiTester $I, $scenario)
|
2017-02-21 14:26:46 -08:00
|
|
|
{
|
|
|
|
$I->wantTo('Delete a component');
|
|
|
|
|
|
|
|
// create
|
2017-06-12 17:39:03 -07:00
|
|
|
$component = factory(\App\Models\Component::class)->create();
|
2017-02-21 14:26:46 -08:00
|
|
|
$I->assertInstanceOf(\App\Models\Component::class, $component);
|
|
|
|
|
|
|
|
// delete
|
|
|
|
$I->sendDELETE('/components/' . $component->id);
|
|
|
|
$I->seeResponseIsJson();
|
|
|
|
$I->seeResponseCodeIs(200);
|
|
|
|
|
2017-03-14 08:37:39 -07:00
|
|
|
// verify, expect a 200 with an error message
|
2017-02-21 14:26:46 -08:00
|
|
|
$I->sendGET('/components/' . $component->id);
|
2017-03-14 08:37:39 -07:00
|
|
|
$I->seeResponseCodeIs(200);
|
|
|
|
$I->seeResponseIsJson(); // @todo: response is not JSON
|
|
|
|
// $scenario->incomplete('Resource not found response should be JSON, receiving HTML instead');
|
2017-02-21 14:26:46 -08:00
|
|
|
}
|
|
|
|
}
|