Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Components Assets view + stubbed API tests #3325

Merged
merged 18 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/Http/Controllers/Api/ComponentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Transformers\AssetsTransformer;
use App\Http\Transformers\ComponentsTransformer;
use App\Http\Transformers\ComponentsAssetsTransformer;
use App\Models\Component;
use App\Models\Company;
use App\Helpers\Helper;
Expand Down Expand Up @@ -131,4 +133,26 @@ public function destroy($id)
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/components/message.delete.success')));
}

/**
* Display all assets attached to a component
*
* @author [A. Bergamasco] [@vjandrea]
* @since [v4.0]
* @param Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function getAssets(Request $request, $id)
{
$this->authorize('index', Asset::class);

$component = Component::findOrFail($id);
$assets = $component->assets();

$offset = request('offset', 0);
$limit = $request->input('limit', 50);
$total = $assets->count();
$assets = $assets->skip($offset)->take($limit)->get();
return (new ComponentsAssetsTransformer)->transformAssets($assets, $total);
}
}
54 changes: 54 additions & 0 deletions app/Http/Transformers/ComponentsAssetsTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
namespace App\Http\Transformers;

use App\Models\Asset;
use Illuminate\Database\Eloquent\Collection;
use App\Http\Transformers\UsersTransformer;
use Gate;


class ComponentsAssetsTransformer
{
public function transformAssets (Collection $assets, $total)
{
$array = array();
foreach ($assets as $asset) {
$array[] = self::transformAsset($asset);
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
}


public function transformAsset (Asset $asset)
{
$array = [
'id' => $asset->id,
'name' => e($asset->name),
'created_at' => $asset->created_at->format('Y-m-d'),
'qty' => $asset->components()->count(),
'can_checkout' => $asset->availableForCheckout(),
];

$permissions_array['available_actions'] = [
'checkout' => Gate::allows('checkout', Asset::class) ? true : false,
'checkin' => Gate::allows('checkin', Asset::class) ? true : false,
'update' => Gate::allows('update', Asset::class) ? true : false,
'delete' => Gate::allows('delete', Asset::class) ? true : false,
];

$array += $permissions_array;

if ($asset->model->fieldset) {
foreach ($asset->model->fieldset->fields as $field) {
$fields_array = [$field->name => $asset->{$field->convertUnicodeDbSlug()}];
$array += $fields_array;
}
}

return $array;
}

public function transformAssetsDatatable ($assets) {
return (new DatatablesTransformer)->transformDatatables($assets);
}
}
4 changes: 2 additions & 2 deletions resources/views/components/view.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@
name="component_users"
class="table table-striped snipe-table"
id="table"
data-url="{{route('api.components.show', $component->id)}}"
data-url="{{route('api.components.assets', $component->id)}}"
data-cookie="true"
data-click-to-select="true"
data-cookie-id-table="componentDetailTable-{{ config('version.hash_version') }}"
>
<thead>
<tr>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="name">{{ trans('general.asset') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="name" data-formatter="hardwareLinkFormatter">{{ trans('general.asset') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="qty">{{ trans('general.qty') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="created_at">{{ trans('general.date') }}</th>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/hardware/view.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
@if ($asset->company)
<tr>
<td>{{ trans('general.company') }}</td>
<td>{{ $asset->company->name }}</td>
<td><a href="{{ url('/companies/' . $asset->company->id) }}">{{ $asset->company->name }}</a></td>
</tr>
@endif

Expand Down
3 changes: 3 additions & 0 deletions resources/views/settings/api.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
@if (!config('app.lock_passwords'))
<passport-clients></passport-clients>
<passport-authorized-clients></passport-authorized-clients>
@if(env('APP_ENV') != 'production')
<passport-personal-access-tokens></passport-personal-access-tokens>
@endif
@else
<p class="help-block">{{ trans('general.feature_disabled') }}</p>
@endif
Expand Down
5 changes: 5 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@
]
);

Route::get('components/{id}/assets', [
'as' =>'api.components.assets',
'uses' => 'ComponentsController@getAssets',
]);


Route::resource('suppliers', 'SuppliersController',
['names' =>
Expand Down
46 changes: 46 additions & 0 deletions tests/_support/ApiTester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php


/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL)
*
* @SuppressWarnings(PHPMD)
*/
class ApiTester extends \Codeception\Actor
{
use _generated\ApiTesterActions;

/**
* Define custom actions here
*/

public function getToken(\App\Models\User $user)
{
$client_repository = new \Laravel\Passport\ClientRepository();
$client = $client_repository->createPersonalAccessClient($user->id, 'Codeception API Test Client',
'https://localhost/');

\Illuminate\Support\Facades\DB::table('oauth_personal_access_clients')->insert([
'client_id' => $client->id,
'created_at' => new DateTime,
'updated_at' => new DateTime,
]);

$user->permissions = json_encode(['superuser' => true]);
$user->save();

$token = $user->createToken('CodeceptionAPItestToken')->accessToken;

return $token;
}
}
10 changes: 10 additions & 0 deletions tests/_support/Helper/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Helper;

// here you can define custom actions
// all public methods declared in helper class will be available in $I

class Api extends \Codeception\Module
{

}
13 changes: 13 additions & 0 deletions tests/api.suite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class_name: ApiTester
modules:
enabled:
- \Helper\Api
- REST:
url: /api/v1
depends: Laravel5
- Asserts
config:
- Laravel5:
environment_file: .env.testing
disable_middleware: true
cleanup: true
59 changes: 59 additions & 0 deletions tests/api/ApiComponentsAssetsCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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 = factory(\App\Models\Component::class, 'component')
->create(['user_id' => $this->user->id, 'qty' => 20]);

$assets = factory(\App\Models\Asset::class, 'asset', 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
]);
});

$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'),
],
]
]);
}
}
Loading