Skip to content

Commit c4a6d48

Browse files
committed
Complete setup for adding nova tests
1 parent 802290d commit c4a6d48

File tree

7 files changed

+106
-14
lines changed

7 files changed

+106
-14
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"require-dev": {
3030
"fzaninotto/faker": "^1.9",
31-
"laravel/nova": "2.*",
31+
"laravel/nova": "3.*",
3232
"orchestra/testbench-browser-kit": "^5.0",
3333
"orchestra/testbench": "^5.0",
3434
"php-coveralls/php-coveralls" : "^2.2",

tests/CreatesApplication.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests;
22

33
use GeneaLabs\LaravelModelCaching\Providers\Service as LaravelModelCachingService;
4-
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Providers\NovaServiceProvider;
54

65
trait CreatesApplication
76
{
@@ -54,7 +53,6 @@ protected function getPackageProviders($app)
5453
{
5554
return [
5655
LaravelModelCachingService::class,
57-
NovaServiceProvider::class,
5856
];
5957
}
6058
}

tests/EnvironmentSetup.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests;
22

3+
use Illuminate\Auth\Middleware\Authenticate;
4+
use Laravel\Nova\Http\Middleware\Authorize;
5+
use Laravel\Nova\Http\Middleware\BootTools;
6+
use Laravel\Nova\Http\Middleware\DispatchServingNovaEvent;
7+
38
trait EnvironmentSetup
49
{
510
protected function getEnvironmentSetUp($app)
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Feature\Nova;
22

3-
use GeneaLabs\LaravelModelCaching\Tests\FeatureTestCase;
4-
5-
class BelongsToManyTest extends FeatureTestCase
3+
class BelongsToManyTest extends NovaTestCase
64
{
75
/** @group test */
8-
public function testCacheCanBeDisabledOnModel()
6+
public function testBasicNovaIndexRequest()
97
{
10-
dd();
11-
$result = $this->visit("/nova");
8+
$this->getJson('nova-api/authors');
9+
// $response->dump();
1210

13-
dd($result);
11+
$this->response->assertStatus(200)
12+
->assertJsonCount(10, 'resources');
1413
}
1514
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace GeneaLabs\LaravelModelCaching\Tests\Feature\Nova;
4+
5+
use GeneaLabs\LaravelModelCaching\Tests\FeatureTestCase;
6+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Nova\AuthorResource;
7+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Providers\NovaServiceProvider;
8+
use Illuminate\Contracts\Auth\Authenticatable;
9+
use Illuminate\Testing\TestResponse;
10+
use Laravel\Nova\Nova;
11+
use Laravel\Nova\NovaCoreServiceProvider;
12+
13+
abstract class NovaTestCase extends FeatureTestCase
14+
{
15+
/** @var TestResponse */
16+
protected $response;
17+
18+
protected $authenticatedAs;
19+
20+
public function setUp(): void
21+
{
22+
parent::setUp();
23+
Nova::$tools = [];
24+
Nova::$resources = [];
25+
26+
Nova::resources([
27+
AuthorResource::class
28+
]);
29+
30+
Nova::auth(function () {
31+
return true;
32+
});
33+
34+
$this->authenticate();
35+
}
36+
37+
protected function authenticate()
38+
{
39+
$this->actingAs($this->authenticatedAs = \Mockery::mock(Authenticatable::class));
40+
41+
$this->authenticatedAs->shouldReceive('getAuthIdentifier')->andReturn(1);
42+
$this->authenticatedAs->shouldReceive('getKey')->andReturn(1);
43+
44+
return $this;
45+
}
46+
47+
protected function getPackageProviders($app)
48+
{
49+
return array_merge(parent::getPackageProviders($app), [
50+
NovaCoreServiceProvider::class,
51+
\Laravel\Nova\NovaServiceProvider::class,
52+
NovaServiceProvider::class,
53+
]);
54+
}
55+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures\Nova;
4+
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
6+
use Illuminate\Http\Request;
7+
use Laravel\Nova\Fields\ID;
8+
use Laravel\Nova\Fields\Text;
9+
10+
class AuthorResource extends Resource
11+
{
12+
public static $model = Author::class;
13+
14+
public static $search = ['id'];
15+
16+
/**
17+
* @inheritDoc
18+
*/
19+
public function fields(Request $request)
20+
{
21+
return [
22+
ID::make('ID', 'id'),
23+
24+
Text::make('Name', 'name'),
25+
];
26+
}
27+
28+
public static function uriKey()
29+
{
30+
return 'authors';
31+
}
32+
}

tests/Fixtures/Providers/NovaServiceProvider.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures\Providers;
22

3-
use App\User;
43
use Illuminate\Support\Facades\Gate;
54
use Laravel\Nova\Nova;
65
use Laravel\Nova\NovaApplicationServiceProvider;
@@ -30,8 +29,7 @@ protected function routes()
3029
{
3130
Nova::routes()
3231
->withAuthenticationRoutes()
33-
->withPasswordResetRoutes()
34-
->register();
32+
->withPasswordResetRoutes();
3533
}
3634

3735
/**
@@ -43,7 +41,7 @@ protected function routes()
4341
*/
4442
protected function gate()
4543
{
46-
Gate::define('viewNova', function (User $user) {
44+
Gate::define('viewNova', function () {
4745
return true;
4846
});
4947
}
@@ -78,4 +76,9 @@ public function register()
7876
{
7977
//
8078
}
79+
80+
protected function resources()
81+
{
82+
// See NovaTestCase
83+
}
8184
}

0 commit comments

Comments
 (0)