Skip to content
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
3 changes: 1 addition & 2 deletions src/Http/Controllers/ProfileAvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ public function __invoke(ProfileAvatarRequest $request)
$user->{$request::$userAvatarAttribute} = $path;
$user->save();


$user->{ProfileAvatarRequest::$userAvatarAttribute} = url($user->{ProfileAvatarRequest::$userAvatarAttribute});

return $this->response()->data($user);
return $this->response()->model($user);
}
}
2 changes: 1 addition & 1 deletion src/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public function __invoke(RestifyRequest $request)
$user->{ProfileAvatarRequest::$userAvatarAttribute} = url($user->{ProfileAvatarRequest::$userAvatarAttribute});
}

return $this->response()->data($user);
return $this->response()->model($user);
}
}
2 changes: 0 additions & 2 deletions src/Http/Controllers/RepositoryIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public function __invoke(RepositoryIndexRequest $request)
->dump($e, $request->isDev());
} catch (UnauthorizedException $e) {
return $this->response()->forbidden()->addError($e->getMessage())->dump($e, $request->isDev());
} catch (\Throwable $e) {
return $this->response()->error()->dump($e, $request->isDev());
}
}
}
6 changes: 6 additions & 0 deletions src/Http/Requests/InteractWithRepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Binaryk\LaravelRestify\Repositories\Repository;
use Binaryk\LaravelRestify\Restify;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pipeline\Pipeline;

/**
* @author Eduard Lupacescu <eduard.lupacescu@binarcode.com>
Expand Down Expand Up @@ -49,6 +50,11 @@ public function repository($key = null): ?Repository
'name' => $repository,
]), 403);
}

app(Pipeline::class)
->send($this)
->through($repository::collectMiddlewares()->toArray())
->thenReturn();
});

return $repository::resolveWith($repository::newModel());
Expand Down
1 change: 0 additions & 1 deletion src/Http/Requests/RestifyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Binaryk\LaravelRestify\Http\Requests;

use Binaryk\LaravelRestify\Restify;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\App;

Expand Down
12 changes: 12 additions & 0 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ abstract class Repository implements RestifySearchable, JsonSerializable
*/
public static $globalSearchResults = 5;

/**
* The list of middlewares for the current repository.
*
* @var array
*/
public static $middlewares = [];

/**
* Get the underlying model instance for the resource.
*
Expand Down Expand Up @@ -731,4 +738,9 @@ public function availableFilters(RestifyRequest $request)
return collect($this->filter($this->filters($request)))->each(fn (Filter $filter) => $filter->authorizedToSee($request))
->values();
}

public static function collectMiddlewares(): Collection
{
return collect(static::$middlewares);
}
}
3 changes: 2 additions & 1 deletion tests/Controllers/GlobalSearchControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public function test_global_search_filter_will_filter_with_index_query()

$response = $this
->withoutExceptionHandling()
->getJson('/restify-api/search?search=1');
->getJson('/restify-api/search?search=1')
->dump();

$this->assertCount(1, $response->json('data'));

Expand Down
75 changes: 75 additions & 0 deletions tests/Controllers/RepositoryMiddlewaresTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Binaryk\LaravelRestify\Tests\Controllers;

use Binaryk\LaravelRestify\Restify;
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostAbortMiddleware;
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostWithCustomMiddlewareRepository;
use Binaryk\LaravelRestify\Tests\IntegrationTest;
use Mockery as m;

class RepositoryMiddlewaresTest extends IntegrationTest
{
protected function tearDown(): void
{
m::close();
}

public function test_repository_can_have_custom_middleware()
{
$middleware = m::mock(PostAbortMiddleware::class);

$nextParam = null;

$middleware
->expects('handle')
->once();

PostWithCustomMiddlewareRepository::$middlewares = [
$middleware,
];

Restify::repositories([
PostWithCustomMiddlewareRepository::class,
]);

$this->getJson('restify-api/post-with-middleware')
->assertStatus(200);
}

public function test_request_fails_if_middleware_abort()
{
PostWithCustomMiddlewareRepository::$middlewares = [
PostAbortMiddleware::class,
];

Restify::repositories([
PostWithCustomMiddlewareRepository::class,
]);

$this->getJson('restify-api/post-with-middleware')
->assertStatus(404);
}

public function test_foreign_repository_middleware_should_not_be_invoked()
{
$middleware = m::mock(PostAbortMiddleware::class);

$nextParam = null;

$middleware
->expects('handle')
->never();

PostWithCustomMiddlewareRepository::$middlewares = [
$middleware,
];

Restify::repositories([
PostWithCustomMiddlewareRepository::class,
]);

$this->getJson('restify-api/posts')
->assertStatus(200);
}
}
15 changes: 15 additions & 0 deletions tests/Fixtures/Post/PostAbortMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Binaryk\LaravelRestify\Tests\Fixtures\Post;

use Illuminate\Http\Request;

class PostAbortMiddleware
{
public function handle(Request $request, $next)
{
abort(404);

$next($request);
}
}
25 changes: 25 additions & 0 deletions tests/Fixtures/Post/PostWithCustomMiddlewareRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Binaryk\LaravelRestify\Tests\Fixtures\Post;

use Binaryk\LaravelRestify\Fields\Field;
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
use Binaryk\LaravelRestify\Repositories\Repository;

class PostWithCustomMiddlewareRepository extends Repository
{
public static $model = Post::class;

public static $middlewares = [
PostAbortMiddleware::class,
];

public static $uriKey = 'post-with-middleware';

public function fields(RestifyRequest $request)
{
return [
Field::new('title'),
];
}
}
2 changes: 2 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ protected function setUp(): void
{
parent::setUp();
DB::enableQueryLog();

Hash::driver('bcrypt')->setRounds(4);

$this->repositoryMock();
$this->loadMigrations();
$this->loadRoutes();
Expand Down