-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use custom attachers * Apply fixes from StyleCI (#196)
- Loading branch information
Showing
9 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Binaryk\LaravelRestify\Tests\Controllers; | ||
|
||
use Binaryk\LaravelRestify\Tests\Fixtures\Role\Role; | ||
use Binaryk\LaravelRestify\Tests\IntegrationTest; | ||
|
||
class RepositoryAttachInterceptorTest extends IntegrationTest | ||
{ | ||
public function test_can_intercept_attach_method() | ||
{ | ||
$role = factory(Role::class)->create(); | ||
$user = $this->mockUsers()->first(); | ||
|
||
$this->postJson('restify-api/roles/'.$role->id.'/attach/users', [ | ||
'users' => $user->id, | ||
]) | ||
->assertCreated(); | ||
|
||
$this->assertDatabaseCount('model_has_roles', 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
use Binaryk\LaravelRestify\Tests\Fixtures\Role\Role; | ||
use Faker\Generator as Faker; | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Model Factories | ||
|-------------------------------------------------------------------------- | ||
| | ||
| This directory should contain each of the model factory definitions for | ||
| your application. Factories provide a convenient way to generate new | ||
| model instances for testing / seeding your application's database. | ||
| | ||
*/ | ||
|
||
$factory->define(Role::class, function (Faker $faker) { | ||
return [ | ||
'name' => $faker->word, | ||
'guard_name' => 'web', | ||
]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Binaryk\LaravelRestify\Tests\Fixtures\Role; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class ModelHasRole extends Model | ||
{ | ||
public $timestamps = false; | ||
|
||
protected $table = 'model_has_roles'; | ||
|
||
protected $guarded = ['id']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Binaryk\LaravelRestify\Tests\Fixtures\Role; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Role extends Model | ||
{ | ||
protected $table = 'roles'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Binaryk\LaravelRestify\Tests\Fixtures\Role; | ||
|
||
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; | ||
use Binaryk\LaravelRestify\Repositories\Repository; | ||
use Binaryk\LaravelRestify\Tests\Fixtures\User\User; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class RoleRepository extends Repository | ||
{ | ||
public static $model = Role::class; | ||
|
||
public function attachUsers(RestifyRequest $request, Repository $repository, Model $model) | ||
{ | ||
ModelHasRole::create([ | ||
'role_id' => $model->id, | ||
'model_type' => User::class, | ||
'model_id' => $request->get('users'), | ||
]); | ||
|
||
return $this->response()->created(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateRolesTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('roles', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->string('name'); | ||
$table->string('guard_name'); | ||
$table->timestamps(); | ||
}); | ||
|
||
Schema::create('model_has_roles', function (Blueprint $table) { | ||
$table->unsignedBigInteger('role_id'); | ||
|
||
$table->string('model_type'); | ||
$table->unsignedBigInteger('model_id'); | ||
$table->index(['model_id', 'model_type'], 'model_has_roles_model_id_model_type_index'); | ||
|
||
$table->foreign('role_id') | ||
->references('id') | ||
->on('roles') | ||
->onDelete('cascade'); | ||
|
||
$table->primary(['role_id', 'model_id', 'model_type'], | ||
'model_has_roles_role_model_type_primary'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('roles'); | ||
} | ||
} |