Skip to content

Commit a482d7d

Browse files
author
Daniel Mason
authored
Merge pull request #2 from GeneaLabs/master
merge
2 parents b023112 + be138bc commit a482d7d

21 files changed

+701
-1
lines changed

Diff for: CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.8.2] - 2020-04-13
8+
### Fixed
9+
- issue with incorrectly adding to currentBinding if item was not in query
10+
binding. Thanks @irvine1231!
11+
712
## [0.8.1] - 2020-04-09
813
### Fixed
914
- `jsonContains()` with array values. Thanks @dmason30!

Diff for: composer.json

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
"email": "[email protected]"
99
}
1010
],
11+
"repositories": [
12+
{
13+
"type": "composer",
14+
"url": "https://nova.laravel.com"
15+
}
16+
],
1117
"require": {
1218
"php": ">=7.2.5",
1319
"genealabs/laravel-pivot-events": "^0.3.0",
@@ -22,6 +28,7 @@
2228
},
2329
"require-dev": {
2430
"fzaninotto/faker": "^1.9",
31+
"laravel/nova": "3.*",
2532
"orchestra/testbench-browser-kit": "^5.0",
2633
"orchestra/testbench": "^5.0",
2734
"php-coveralls/php-coveralls" : "^2.2",

Diff for: src/CacheKey.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ protected function getInAndNotInClauses(array $where) : string
234234
$type = strtolower($where["type"]);
235235
$subquery = $this->getValuesFromWhere($where);
236236
$values = collect($this->query->bindings["where"][$this->currentBinding] ?? []);
237-
$this->currentBinding += count($where["values"]);
237+
238+
if (Str::startsWith($subquery, $values->first())) {
239+
$this->currentBinding += count($where["values"]);
240+
}
238241

239242
if (! is_numeric($subquery) && ! is_numeric(str_replace("_", "", $subquery))) {
240243
try {

Diff for: tests/EnvironmentSetup.php

+19
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)
@@ -31,5 +36,19 @@ protected function getEnvironmentSetUp($app)
3136
'connection' => 'model-cache',
3237
]);
3338
$app['config']->set('laravel-model-caching.store', 'model');
39+
$app['config']->set("nova", [
40+
'name' => 'Nova Site',
41+
'url' => env('APP_URL', '/'),
42+
'path' => '/nova',
43+
'guard' => env('NOVA_GUARD', null),
44+
'middleware' => [
45+
'web',
46+
Authenticate::class,
47+
DispatchServingNovaEvent::class,
48+
BootTools::class,
49+
Authorize::class,
50+
],
51+
'pagination' => 'simple',
52+
]);
3453
}
3554
}

Diff for: tests/Feature/Nova/BelongsToManyTest.php

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Feature\Nova;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
5+
6+
class BelongsToManyTest extends NovaTestCase
7+
{
8+
/** @group test */
9+
public function testAttachRelationFlushesCache()
10+
{
11+
$beforeStore = Store::with(['books'])->get()->first();
12+
$beforeBooks = $beforeStore->books;
13+
14+
/** @var Book $beforeBook */
15+
$beforeBook = $beforeStore->books->first()->replicate();
16+
$beforeBook->title = 'new foo';
17+
$beforeBook->save();
18+
19+
$this->postJson('nova-api/books/' . $beforeBook->id . '/attach/stores' , [
20+
'stores' => $beforeStore->id,
21+
'viaRelationship' => 'stores'
22+
]);
23+
24+
$this->response->assertStatus(200);
25+
26+
$store = Store::with(['books'])->all()->first();
27+
$books = $store->books;
28+
$book = $store->books->sortByDesc('id')->first();
29+
30+
$this->assertTrue($beforeStore->is($store));
31+
$this->assertTrue($beforeBook->is($book));
32+
$this->assertCount(1, $beforeBooks);
33+
$this->assertCount(2, $books);
34+
$this->assertCount(0, $beforeBook->stores);
35+
$this->assertSame('new foo', $book->title);
36+
}
37+
38+
/** @group test */
39+
public function testDetachRelationFlushesCache()
40+
{
41+
/** @var Store $store */
42+
$store = Store::with(['books'])->get()->first();
43+
44+
/** @var Book $beforeBook */
45+
$newBook = $store->books->first()->replicate();
46+
$newBook->title = 'new foo';
47+
$newBook->save();
48+
49+
$store->books()->attach($newBook);
50+
51+
$beforeStore = Store::with(['books'])->get()->first();
52+
$beforeBooks = $beforeStore->books;
53+
$beforeBook = $beforeBooks->first();
54+
55+
$this->deleteJson('/nova-api/stores/detach?viaResource=books&viaResourceId=' . $beforeBook->id . '&viaRelationship=stores' , [
56+
'resources' => [$beforeStore->id],
57+
]);
58+
59+
$this->response->assertStatus(200);
60+
61+
$store = Store::with(['books'])->all()->first();
62+
$books = $store->books;
63+
64+
$this->assertTrue($beforeStore->is($store));
65+
$this->assertCount(2, $beforeBooks);
66+
$this->assertCount(1, $books);
67+
}
68+
69+
/** @group test */
70+
public function testUpdateRelationFlushesCache()
71+
{
72+
$beforeStore = Store::with(['books'])->get()->first();
73+
$beforeBook = $beforeStore->books->first();
74+
75+
$this->putJson('nova-api/books/' . $beforeBook->id, [
76+
'title' => 'foo',
77+
]);
78+
79+
$store = Store::with(['books'])->all()->first();
80+
$book = $store->books->first();
81+
82+
$this->response->assertStatus(200);
83+
84+
$this->assertTrue($beforeStore->is($store));
85+
$this->assertTrue($beforeBook->is($book));
86+
$this->assertSame('foo', $book->title);
87+
}
88+
89+
/** @group test */
90+
public function testDeleteRelationFlushesCache()
91+
{
92+
$beforeStore = Store::with(['books'])->get()->first();
93+
$beforeBooks = $beforeStore->books;
94+
$beforeBook = $beforeBooks->first();
95+
96+
$this->deleteJson('nova-api/books', ['resources' => [$beforeBook->id]]);
97+
98+
$store = Store::with(['books'])->all()->first();
99+
$books = $store->books;
100+
101+
$this->response->assertStatus(200);
102+
103+
$this->assertTrue($beforeStore->is($store));
104+
$this->assertCount(1, $beforeBooks);
105+
$this->assertCount(0, $books);
106+
$this->assertNull(Book::find($beforeBook->id));
107+
}
108+
}

Diff for: tests/Feature/Nova/CreateTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Feature\Nova;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
5+
class CreateTest extends NovaTestCase
6+
{
7+
public function testCreateFlushesCacheForModel()
8+
{
9+
$beforeAuthors = (new Author)->get();
10+
11+
$this->postJson('nova-api/authors', [
12+
'name' => 'foo',
13+
'email' => '[email protected]',
14+
]);
15+
16+
$authors = (new Author)->get();
17+
18+
$this->response->assertStatus(201);
19+
$this->assertCount(10, $beforeAuthors);
20+
$this->assertCount(11, $authors);
21+
}
22+
}

Diff for: tests/Feature/Nova/DeleteTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Feature\Nova;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
5+
class DeleteTest extends NovaTestCase
6+
{
7+
public function testDeleteFlushesCacheForModel()
8+
{
9+
$beforeAuthors = (new Author)->get();
10+
$deleteAuthor = $beforeAuthors->first();
11+
12+
$this->deleteJson('nova-api/authors', ['resources' => [$deleteAuthor->id]]);
13+
14+
$authors = (new Author)->get();
15+
16+
$this->response->assertStatus(200);
17+
$this->assertCount(10, $beforeAuthors);
18+
$this->assertCount(9, $authors);
19+
}
20+
}

Diff for: tests/Feature/Nova/NovaTestCase.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\Nova\BookResource;
8+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Nova\StoreResource;
9+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Providers\NovaServiceProvider;
10+
use Illuminate\Contracts\Auth\Authenticatable;
11+
use Illuminate\Testing\TestResponse;
12+
use Laravel\Nova\Nova;
13+
use Laravel\Nova\NovaCoreServiceProvider;
14+
15+
abstract class NovaTestCase extends FeatureTestCase
16+
{
17+
/** @var TestResponse */
18+
protected $response;
19+
20+
protected $authenticatedAs;
21+
22+
public function setUp(): void
23+
{
24+
parent::setUp();
25+
Nova::$tools = [];
26+
Nova::$resources = [];
27+
28+
Nova::resources([
29+
AuthorResource::class,
30+
BookResource::class,
31+
StoreResource::class,
32+
]);
33+
34+
Nova::auth(function () {
35+
return true;
36+
});
37+
38+
$this->authenticate();
39+
}
40+
41+
protected function authenticate()
42+
{
43+
$this->actingAs($this->authenticatedAs = \Mockery::mock(Authenticatable::class));
44+
45+
$this->authenticatedAs->shouldReceive('getAuthIdentifier')->andReturn(1);
46+
$this->authenticatedAs->shouldReceive('getKey')->andReturn(1);
47+
48+
return $this;
49+
}
50+
51+
protected function getPackageProviders($app)
52+
{
53+
return array_merge(parent::getPackageProviders($app), [
54+
NovaCoreServiceProvider::class,
55+
\Laravel\Nova\NovaServiceProvider::class,
56+
NovaServiceProvider::class,
57+
]);
58+
}
59+
}

Diff for: tests/Feature/Nova/UpdateTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Feature\Nova;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
5+
class UpdateTest extends NovaTestCase
6+
{
7+
public function testUpdateFlushesCacheForModel()
8+
{
9+
$beforeAuthors = (new Author)->get();
10+
11+
/** @var Author $author */
12+
$author = $beforeAuthors->first();
13+
14+
$this->putJson('nova-api/authors/' . $author->id, [
15+
'name' => 'foo',
16+
'email' => '[email protected]',
17+
]);
18+
19+
$authors = (new Author)->get();
20+
21+
$this->response->assertStatus(200);
22+
$this->assertCount(10, $beforeAuthors);
23+
$this->assertCount(10, $authors);
24+
25+
/** @var Author $updatedAuthor */
26+
$updatedAuthor = $authors->first();
27+
$this->assertTrue($updatedAuthor->is($author));
28+
$this->assertSame('foo', $updatedAuthor->name);
29+
$this->assertSame('[email protected]', $updatedAuthor->email);
30+
31+
$author->refresh();
32+
$this->assertSame('foo', $author->name);
33+
$this->assertSame('[email protected]', $author->email);
34+
}
35+
}

Diff for: tests/Fixtures/Nova/AuthorResource.php

+32
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+
Text::make('Name', 'name'),
24+
Text::make('E-Mail', 'email'),
25+
];
26+
}
27+
28+
public static function uriKey()
29+
{
30+
return 'authors';
31+
}
32+
}

Diff for: tests/Fixtures/Nova/BookResource.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures\Nova;
4+
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
6+
use Illuminate\Http\Request;
7+
use Laravel\Nova\Fields\BelongsToMany;
8+
use Laravel\Nova\Fields\ID;
9+
use Laravel\Nova\Fields\Text;
10+
11+
class BookResource extends Resource
12+
{
13+
public static $model = Book::class;
14+
15+
public static $search = ['id'];
16+
17+
/**
18+
* @inheritDoc
19+
*/
20+
public function fields(Request $request)
21+
{
22+
return [
23+
ID::make('ID', 'id'),
24+
Text::make('Title', 'title'),
25+
BelongsToMany::make('Stores', 'stores', StoreResource::class),
26+
];
27+
}
28+
29+
public static function uriKey()
30+
{
31+
return 'books';
32+
}
33+
}

0 commit comments

Comments
 (0)