Skip to content

Commit c3efbc3

Browse files
authored
Coverage (#141)
* wip * Apply fixes from StyleCI (#140) * wip * Apply fixes from StyleCI (#142) * wip * Apply fixes from StyleCI (#143) * wip
1 parent cb48493 commit c3efbc3

File tree

9 files changed

+181
-13
lines changed

9 files changed

+181
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ html
77
output
88
docs/node_modules
99
docs/.vuepress/dist
10+
.phpunit.result.cache
1011

routes/api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
Route::post('/{repository}', 'RepositoryStoreController@handle');
55
Route::get('/{repository}/{repositoryId}', 'RepositoryShowController@handle');
66
Route::patch('/{repository}/{repositoryId}', 'RepositoryUpdateController@handle');
7+
Route::put('/{repository}/{repositoryId}', 'RepositoryUpdateController@handle');
78
Route::delete('/{repository}/{repositoryId}', 'RepositoryDestroyController@handle');

src/Exceptions/RestifyHandler.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Binaryk\LaravelRestify\Exceptions\UnauthorizedException as ActionUnauthorizedException;
1010
use Binaryk\LaravelRestify\Restify;
1111
use Closure;
12-
use Exception;
1312
use Illuminate\Auth\Access\AuthorizationException;
1413
use Illuminate\Auth\AuthenticationException;
1514
use Illuminate\Database\Eloquent\ModelNotFoundException;
@@ -54,8 +53,8 @@ class RestifyHandler extends ExceptionHandler
5453
/**
5554
* Render an exception into an HTTP response.
5655
*
57-
* @param Request $request
58-
* @param \Exception|Throwable $exception
56+
* @param Request $request
57+
* @param \Exception|Throwable $exception
5958
*
6059
* @return Response|\Symfony\Component\HttpFoundation\Response
6160
* @throws \Illuminate\Contracts\Container\BindingResolutionException
@@ -94,13 +93,13 @@ public function render($request, $exception)
9493
case $exception instanceof ValidationUnauthorized:
9594
case $exception instanceof UnauthorizedHttpException:
9695
case $exception instanceof UnauthenticateException:
97-
case $exception instanceof ActionUnauthorizedException:
98-
case $exception instanceof AuthorizationException:
9996
case $exception instanceof GatePolicy:
10097
case $exception instanceof AuthenticationException:
10198
$response->addError($exception->getMessage())->auth();
10299
break;
103100

101+
case $exception instanceof AuthorizationException:
102+
case $exception instanceof ActionUnauthorizedException:
104103
case $exception instanceof AccessDeniedHttpException:
105104
case $exception instanceof InvalidSignatureException:
106105
$response->addError($exception->getMessage())->forbidden();
@@ -121,7 +120,7 @@ public function render($request, $exception)
121120
/**
122121
* Report or log an exception.
123122
*
124-
* @param \Exception|Throwable $e
123+
* @param \Exception|Throwable $e
125124
* @return mixed
126125
*
127126
* @throws \Exception

src/Fields/Field.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function getAttribute()
153153

154154
/**
155155
* Validation rules for store.
156-
* @param callable|array|string $rules
156+
* @param $rules
157157
* @return Field
158158
*/
159159
public function storingRules($rules)
@@ -166,7 +166,7 @@ public function storingRules($rules)
166166
/**
167167
* Validation rules for update.
168168
*
169-
* @param callable|array|string $rules
169+
* @param $rules
170170
* @return Field
171171
*/
172172
public function updatingRules($rules)
@@ -178,7 +178,7 @@ public function updatingRules($rules)
178178

179179
/**
180180
* Validation rules for store.
181-
* @param callable|array|string $rules
181+
* @param $rules
182182
* @return Field
183183
*/
184184
public function rules($rules)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Tests\Controllers;
4+
5+
use Binaryk\LaravelRestify\Exceptions\RestifyHandler;
6+
use Binaryk\LaravelRestify\Tests\Fixtures\Post;
7+
use Binaryk\LaravelRestify\Tests\Fixtures\PostPolicy;
8+
use Binaryk\LaravelRestify\Tests\IntegrationTest;
9+
use Illuminate\Contracts\Debug\ExceptionHandler;
10+
use Illuminate\Support\Facades\Gate;
11+
12+
/**
13+
* @author Eduard Lupacescu <[email protected]>
14+
*/
15+
class RepositoryDestroyControllerTest extends IntegrationTest
16+
{
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
$this->authenticate();
21+
$this->app->bind(ExceptionHandler::class, RestifyHandler::class);
22+
}
23+
24+
public function test_destroy_works()
25+
{
26+
$post = factory(Post::class)->create(['user_id' => 1]);
27+
28+
$this->assertInstanceOf(Post::class, Post::find($post->id));
29+
30+
$this->withoutExceptionHandling()->delete('/restify-api/posts/'.$post->id, [
31+
'title' => 'Updated title',
32+
])
33+
->assertStatus(204);
34+
35+
$this->assertNull(Post::find($post->id));
36+
}
37+
38+
public function test_unathorized_to_destroy()
39+
{
40+
Gate::policy(Post::class, PostPolicy::class);
41+
42+
$post = factory(Post::class)->create(['user_id' => 1]);
43+
44+
$_SERVER['restify.post.deletable'] = false;
45+
46+
$this->delete('/restify-api/posts/'.$post->id, [
47+
'title' => 'Updated title',
48+
])->assertStatus(403)
49+
->assertJson([
50+
'errors' => ['This action is unauthorized.'],
51+
]);
52+
53+
$this->assertInstanceOf(Post::class, $post->refresh());
54+
}
55+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Tests\Controllers;
4+
5+
use Binaryk\LaravelRestify\Exceptions\RestifyHandler;
6+
use Binaryk\LaravelRestify\Tests\Fixtures\Post;
7+
use Binaryk\LaravelRestify\Tests\Fixtures\PostPolicy;
8+
use Binaryk\LaravelRestify\Tests\IntegrationTest;
9+
use Illuminate\Contracts\Debug\ExceptionHandler;
10+
use Illuminate\Support\Facades\Gate;
11+
12+
/**
13+
* @author Eduard Lupacescu <[email protected]>
14+
*/
15+
class RepositoryUpdateControllerTest extends IntegrationTest
16+
{
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
$this->authenticate();
21+
}
22+
23+
public function test_basic_update_works()
24+
{
25+
$post = factory(Post::class)->create(['user_id' => 1]);
26+
27+
$this->withoutExceptionHandling()->patch('/restify-api/posts/'.$post->id, [
28+
'title' => 'Updated title',
29+
])
30+
->assertStatus(200);
31+
32+
$updatedPost = Post::find($post->id);
33+
34+
$this->assertEquals($updatedPost->title, 'Updated title');
35+
}
36+
37+
public function test_put_works()
38+
{
39+
$post = factory(Post::class)->create(['user_id' => 1]);
40+
41+
$this->withoutExceptionHandling()->put('/restify-api/posts/'.$post->id, [
42+
'title' => 'Updated title',
43+
])
44+
->assertStatus(200);
45+
46+
$updatedPost = Post::find($post->id);
47+
48+
$this->assertEquals($updatedPost->title, 'Updated title');
49+
}
50+
51+
public function test_unathorized_to_update()
52+
{
53+
$this->app->bind(ExceptionHandler::class, RestifyHandler::class);
54+
55+
Gate::policy(Post::class, PostPolicy::class);
56+
57+
$post = factory(Post::class)->create(['user_id' => 1]);
58+
59+
$_SERVER['restify.post.updateable'] = false;
60+
61+
$this->patch('/restify-api/posts/'.$post->id, [
62+
'title' => 'Updated title',
63+
])->assertStatus(403)
64+
->assertJson([
65+
'errors' => ['This action is unauthorized.'],
66+
]);
67+
}
68+
}

tests/Fixtures/PostPolicy.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ public function store($user)
2222
{
2323
return $_SERVER['restify.post.creatable'] ?? true;
2424
}
25+
26+
public function update($user, $post)
27+
{
28+
return $_SERVER['restify.post.updateable'] ?? true;
29+
}
30+
31+
public function delete($user, $post)
32+
{
33+
return $_SERVER['restify.post.deletable'] ?? true;
34+
}
2535
}

tests/HandlerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Binaryk\LaravelRestify\Exceptions\Guard\GatePolicy;
88
use Binaryk\LaravelRestify\Exceptions\RestifyHandler;
99
use Binaryk\LaravelRestify\Exceptions\UnauthenticateException;
10+
use Binaryk\LaravelRestify\Restify;
1011
use Illuminate\Auth\AuthenticationException;
1112
use Illuminate\Database\Eloquent\ModelNotFoundException;
1213
use Illuminate\Foundation\Testing\Concerns\InteractsWithContainer;
@@ -150,4 +151,16 @@ public function test_default_unhandled_exception_production()
150151
$this->assertObjectHasAttribute('errors', $response->getData());
151152
$this->assertEquals($response->getData()->errors[0], __('messages.something_went_wrong'));
152153
}
154+
155+
public function test_can_inject_custom_handler_but_handler_will_continue_handle()
156+
{
157+
Restify::exceptionHandler(function ($request, $exception) {
158+
$this->assertInstanceOf(NotFoundHttpException::class, $exception);
159+
});
160+
161+
$response = $this->handler->render($this->request, new NotFoundHttpException('This message is not visible'));
162+
$this->assertInstanceOf(JsonResponse::class, $response);
163+
$this->assertEquals($response->getData()->errors[0], __('messages.not_found'));
164+
$this->assertEquals($response->getStatusCode(), 404);
165+
}
153166
}

tests/RepositoryWithRoutesTest.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ protected function setUp(): void
2121
WithCustomPrefix::class,
2222
WithCustomMiddleware::class,
2323
WithCustomNamespace::class,
24+
WithoutGroup::class,
2425
]);
2526

2627
parent::setUp();
@@ -62,15 +63,27 @@ public function test_can_use_custom_namespace()
6263
],
6364
]);
6465
}
66+
67+
public function test_routes_default_wrapped()
68+
{
69+
$this->withoutExceptionHandling()->getJson(route('no.group.default.options'))
70+
->assertStatus(200)
71+
->assertJson([
72+
'meta' => [
73+
'message' => 'From the sayHello method',
74+
],
75+
]);
76+
}
6577
}
6678

6779
class RepositoryWithRoutes extends Repository
6880
{
6981
/**
7082
* @param Router $router
7183
* @param array $attributes
84+
* @param bool $wrap
7285
*/
73-
public static function routes(Router $router, $attributes)
86+
public static function routes(Router $router, $attributes, $wrap = false)
7487
{
7588
$router->group($attributes, function ($router) {
7689
$router->get('/main-testing', function () {
@@ -89,7 +102,7 @@ public static function uriKey()
89102

90103
class WithCustomPrefix extends RepositoryWithRoutes
91104
{
92-
public static function routes(Router $router, $attributes)
105+
public static function routes(Router $router, $attributes, $wrap = false)
93106
{
94107
$attributes['prefix'] = 'custom-prefix';
95108

@@ -115,7 +128,7 @@ public function handle($request, $next)
115128

116129
class WithCustomMiddleware extends RepositoryWithRoutes
117130
{
118-
public static function routes(Router $router, $options)
131+
public static function routes(Router $router, $options, $wrap = false)
119132
{
120133
$options['middleware'] = [MiddlewareFail::class];
121134

@@ -131,7 +144,7 @@ public static function routes(Router $router, $options)
131144

132145
class WithCustomNamespace extends RepositoryWithRoutes
133146
{
134-
public static function routes(Router $router, $options)
147+
public static function routes(Router $router, $options, $wrap = false)
135148
{
136149
$options['namespace'] = 'Binaryk\LaravelRestify\Tests';
137150

@@ -141,6 +154,14 @@ public static function routes(Router $router, $options)
141154
}
142155
}
143156

157+
class WithoutGroup extends RepositoryWithRoutes
158+
{
159+
public static function routes(Router $router, $options = [], $wrap = true)
160+
{
161+
$router->get('default-options', '\\'.HandleController::class.'@sayHello')->name('no.group.default.options');
162+
}
163+
}
164+
144165
class HandleController extends RestController
145166
{
146167
/**

0 commit comments

Comments
 (0)