snipe-it/tests/api/ApiComponentsAssetsCest.php
Daniel Meltzer 5d4920c741 [WIP] Improvements to unit tests. (#3574)
* Improvemenets to unit tests.

* Break up modelfactory into multiple files, populate many states.
* Begin testing validation at the unit test level, test relationships.
* Add tests for Asset::availableForCheckout.
* Model factories now generate all needed relationships on demand,
  which allows us to unit test with a empty database.
* To faciliate the empty database, we move to using sqlite in memory as
  the unit testing database.

* Fix bug with logs of checkouts to non users.

* Fix location finding for assets.  Also Fix location show page to show users associated with location.  Still need some work to show assets.

* More test and generator improvements

* More unit test fixes. PermissionsTest is borked still.

* More Updates

* Rewrite permissionstest.  Check that we have access on the model level rather than via web requests.  Also test delete permissions.

* Fix seeders.

* Make the default asset model factory generate assets that are rtd for testing.

* Save progress.

* Rebase tests, fix department unit test, update database for functional tests.

* Update functional and api tests to use new modelfactory signatures.
2017-06-12 17:39:03 -07:00

81 lines
2.7 KiB
PHP

<?php
class ApiComponentsAssetsCest
{
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 indexComponentsAssets(ApiTester $I)
{
$I->wantTo('Get a list of assets related to a component');
// generate component
$component = factory(\App\Models\Component::class)
->create(['user_id' => $this->user->id, 'qty' => 20]);
// generate assets and associate component
$assets = factory(\App\Models\Asset::class, 2)
->create(['user_id' => $this->user->id])
->each(function ($asset) use ($component) {
$component->assets()->attach($component->id, [
'component_id' => $component->id,
'user_id' => $this->user->id,
'created_at' => date('Y-m-d H:i:s'),
'assigned_qty' => 2,
'asset_id' => $asset->id
]);
});
// verify
$I->sendGET('/components/' . $component->id . '/assets/');
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
$response = json_decode($I->grabResponse());
$I->assertEquals(2, $response->total);
$I->assertInstanceOf(\Illuminate\Database\Eloquent\Collection::class, $assets);
$I->seeResponseContainsJson(['rows' => [
0 => [
'name' => $assets[0]->name,
'id' => $assets[0]->id,
'created_at' => $assets[0]->created_at->format('Y-m-d'),
],
1 => [
'name' => $assets[1]->name,
'id' => $assets[1]->id,
'created_at' => $assets[1]->created_at->format('Y-m-d'),
],
]
]);
}
/** @test */
public function expectEmptyResponseWithoutAssociatedAssets(ApiTester $I, $scenario)
{
$I->wantTo('See an empty response when there are no associated assets to a component');
$component = factory(\App\Models\Component::class)
->create(['user_id' => $this->user->id, 'qty' => 20]);
$I->sendGET('/components/' . $component->id . '/assets');
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$response = json_decode($I->grabResponse());
$I->assertEquals(0, $response->total);
$I->assertEquals([], $response->rows);
$I->seeResponseContainsJson(['total' => 0, 'rows' => []]);
}
}