Skip to content

Commit e899728

Browse files
committed
Adapt unit test
1 parent 94e95b7 commit e899728

14 files changed

+243
-21
lines changed

src/Repositories/Crudable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function destroy(RestifyRequest $request, $repositoryId)
157157
* @param array $payload
158158
* @return mixed
159159
*/
160-
public function allowToUpdate(RestifyRequest $request, $payload = [])
160+
public function allowToUpdate(RestifyRequest $request, $payload = null)
161161
{
162162
$this->authorizeToUpdate($request);
163163

@@ -171,9 +171,9 @@ public function allowToUpdate(RestifyRequest $request, $payload = [])
171171
* @param array $payload
172172
* @return mixed
173173
*/
174-
public function allowToStore(RestifyRequest $request, $payload = [])
174+
public function allowToStore(RestifyRequest $request, $payload = null)
175175
{
176-
self::authorizeToCreate($request);
176+
self::authorizeToStore($request);
177177

178178
$validator = self::validatorForStoring($request, $payload);
179179

src/Repositories/ResponseResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function resolveDetailsMeta($request)
5959
{
6060
return [
6161
'authorizedToShow' => $this->authorizedToShow($request),
62-
'authorizedToCreate' => $this->authorizedToCreate($request),
62+
'authorizedToStore' => $this->authorizedToStore($request),
6363
'authorizedToUpdate' => $this->authorizedToUpdate($request),
6464
'authorizedToDelete' => $this->authorizedToDelete($request),
6565
];

src/Traits/AuthorizableModels.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,30 +92,30 @@ public function authorizedToShow(Request $request)
9292
}
9393

9494
/**
95-
* Determine if the current user can create new repositories or throw an exception.
95+
* Determine if the current user can store new repositories or throw an exception.
9696
*
9797
* @param \Illuminate\Http\Request $request
9898
* @return void
9999
*
100100
* @throws \Illuminate\Auth\Access\AuthorizationException
101101
*/
102-
public static function authorizeToCreate(Request $request)
102+
public static function authorizeToStore(Request $request)
103103
{
104-
if (! static::authorizedToCreate($request)) {
105-
throw new AuthorizationException('Unauthorized to create.');
104+
if (! static::authorizedToStore($request)) {
105+
throw new AuthorizationException('Unauthorized to store.');
106106
}
107107
}
108108

109109
/**
110-
* Determine if the current user can create new repositories.
110+
* Determine if the current user can store new repositories.
111111
*
112112
* @param \Illuminate\Http\Request $request
113113
* @return bool
114114
*/
115-
public static function authorizedToCreate(Request $request)
115+
public static function authorizedToStore(Request $request)
116116
{
117117
if (static::authorizable()) {
118-
return Gate::check('create', static::$model);
118+
return Gate::check('store', static::$model);
119119
}
120120

121121
return true;

tests/Controllers/RepositoryIndexControllerTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ public function users()
7474
public function test_search_query_works()
7575
{
7676
$users = $this->mockUsers(10, ['[email protected]']);
77-
$request = Mockery::mock(RestifyRequest::class);
7877
$model = $users->where('email', '[email protected]')->first(); //find manually the model
7978
$repository = Restify::repositoryForModel(get_class($model));
80-
$expected = $repository::resolveWith($model)->toArray(request());
79+
$expected = $repository::resolveWith($model)->toArray(resolve(RestifyRequest::class));
8180
unset($expected['relationships']);
8281

8382
$r = $this->withExceptionHandling()
@@ -171,6 +170,10 @@ public function test_that_match_param_works()
171170
->andReturnFalse();
172171
$request->shouldReceive('get')
173172
->andReturnFalse();
173+
$request->shouldReceive('isDetailRequest')
174+
->andReturnFalse();
175+
$request->shouldReceive('isIndexRequest')
176+
->andReturnTrue();
174177

175178
$model = $users->where('email', '[email protected]')->first();
176179
$repository = Restify::repositoryForModel(get_class($model));
@@ -210,6 +213,10 @@ public function test_that_with_param_works()
210213
->andReturnTrue();
211214
$request->shouldReceive('get')
212215
->andReturn('posts');
216+
$request->shouldReceive('isDetailRequest')
217+
->andReturnFalse();
218+
$request->shouldReceive('isIndexRequest')
219+
->andReturnTrue();
213220

214221
$r = $this->withExceptionHandling()
215222
->getJson('/restify-api/users?with=posts')

tests/Controllers/RepositoryStoreControllerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@ public function test_basic_validation_works()
3737

3838
public function test_unauthorized_store()
3939
{
40-
$_SERVER['restify.user.creatable'] = false;
40+
$_SERVER['restify.post.creatable'] = false;
4141

4242
Gate::policy(Post::class, PostPolicy::class);
4343

4444
$this->withExceptionHandling()->post('/restify-api/posts', [
4545
'title' => 'Title',
4646
'description' => 'Title',
4747
])->assertStatus(403)
48-
->assertJson(['errors' => ['Unauthorized to create.']]);
48+
->assertJson(['errors' => ['Unauthorized to store.']]);
4949
}
5050

5151
public function test_success_storing()
5252
{
5353
$user = $this->mockUsers()->first();
54-
$r = json_decode($this->withExceptionHandling()->post('/restify-api/posts', [
54+
$r = json_decode($this->withoutExceptionHandling()->post('/restify-api/posts', [
5555
'user_id' => $user->id,
5656
'title' => 'Some post title',
5757
'description' => 'A very short description',

tests/Factories/BookFactory.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use Faker\Generator as Faker;
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Model Factories
8+
|--------------------------------------------------------------------------
9+
|
10+
| This directory should contain each of the model factory definitions for
11+
| your application. Factories provide a convenient way to generate new
12+
| model instances for testing / seeding your application's database.
13+
|
14+
*/
15+
16+
$factory->define(Binaryk\LaravelRestify\Tests\Fixtures\Book::class, function (Faker $faker) {
17+
return [
18+
'title' => $faker->text(30),
19+
'description' => $faker->text,
20+
'author' => $faker->name,
21+
'price' => $faker->randomFloat(),
22+
'stock' => $faker->numberBetween(0, 100),
23+
];
24+
});

tests/FieldResolversTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Tests;
4+
5+
use Binaryk\LaravelRestify\Fields\Field;
6+
use Binaryk\LaravelRestify\Tests\Fixtures\Book;
7+
use Binaryk\LaravelRestify\Tests\Fixtures\BookRepository;
8+
9+
/**
10+
* @package Binaryk\LaravelRestify\Tests;
11+
* @author Eduard Lupacescu <[email protected]>
12+
*/
13+
class FieldResolversTest extends IntegrationTest
14+
{
15+
public function test_show_callback_change_details_value()
16+
{
17+
tap($this->basicField(), function (Field $field) {
18+
$book = factory(Book::class)->create();
19+
$repository = $this->basicRepository();
20+
$repository->resource = $book;
21+
22+
$field->showCallback(function ($value, $repo) use ($book, $repository) {
23+
$this->assertInstanceOf(get_class($repository), $repo);
24+
$this->assertSame($value, $book->title); //assert that the value is read from the database
25+
return 'something else';
26+
});
27+
28+
$this->assertSame($field->resolveForShow($repository), 'something else');
29+
});
30+
}
31+
32+
public function basicField()
33+
{
34+
return Field::make('title');
35+
}
36+
37+
public function basicRepository()
38+
{
39+
return new class extends BookRepository {
40+
};
41+
}
42+
}

tests/Fixtures/Book.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Tests\Fixtures;
4+
5+
use Binaryk\LaravelRestify\Contracts\RestifySearchable;
6+
use Binaryk\LaravelRestify\Traits\InteractWithSearch;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
/**
10+
* @author Eduard Lupacescu <[email protected]>
11+
*/
12+
class Book extends Model implements RestifySearchable
13+
{
14+
use InteractWithSearch;
15+
16+
protected $fillable = [
17+
'id',
18+
'title',
19+
'description',
20+
'author',
21+
'price',
22+
'stock',
23+
];
24+
}

tests/Fixtures/BookPolicy.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Tests\Fixtures;
4+
5+
/**
6+
* @author Eduard Lupacescu <[email protected]>
7+
*/
8+
class BookPolicy
9+
{
10+
/**
11+
* Determine if the given user can view resources.
12+
*/
13+
public function viewAny($user)
14+
{
15+
return $_SERVER['restify.book.viewAnyable'] ?? true;
16+
}
17+
18+
/**
19+
* Determine if users can be created.
20+
*/
21+
public function store($user)
22+
{
23+
return $_SERVER['restify.book.storable'] ?? true;
24+
}
25+
26+
/**
27+
* Determine if books can be updated.
28+
*/
29+
public function update($user, $book)
30+
{
31+
return $_SERVER['restify.book.updateable'] ?? true;
32+
}
33+
34+
/**
35+
* Determine if book can be shown
36+
*/
37+
public function show($user, $book)
38+
{
39+
return $_SERVER['restify.book.showable'] ?? true;
40+
}
41+
42+
/**
43+
* Determine if books can be deleted
44+
*/
45+
public function destroy($user)
46+
{
47+
return $_SERVER['restify.book.destroyable'] ?? true;
48+
}
49+
}

tests/Fixtures/BookRepository.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Tests\Fixtures;
4+
5+
use Binaryk\LaravelRestify\Fields\Field;
6+
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
7+
use Binaryk\LaravelRestify\Repositories\Repository;
8+
9+
/**
10+
* @author Eduard Lupacescu <[email protected]>
11+
*/
12+
class BookRepository extends Repository
13+
{
14+
public static $model = Book::class;
15+
16+
/**
17+
* Get the URI key for the resource.
18+
*
19+
* @return string
20+
*/
21+
public static function uriKey()
22+
{
23+
return 'books';
24+
}
25+
26+
/**
27+
* @param RestifyRequest $request
28+
* @return array
29+
*/
30+
public function fields(RestifyRequest $request)
31+
{
32+
return [
33+
Field::make('title')->storingRules('required')->messages([
34+
'required' => 'This field is required',
35+
]),
36+
];
37+
}
38+
}

0 commit comments

Comments
 (0)