Skip to content

Laravel 5.5 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 3, 2017
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
9 changes: 9 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
* text=auto

/.github export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/phpunit.xml export-ignore
/tests export-ignore
15 changes: 6 additions & 9 deletions src/CachedBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,16 @@ protected function getQueryColumns(array $columns) : string

protected function getWhereClauses() : string
{
// dump($this->query->wheres);
return collect($this->query->wheres)->reduce(function ($carry, $where) {
if (! $where['column'] ?? false) {
return $carry . '';
}

$value = $where['value'] ?? '';
$value = array_get($where, 'value');

if ($where['values'] ?? false) {
$value .= 'in_' . implode('_', $where['values']);
if (in_array($where['type'], ['In', 'Null', 'NotNull'])) {
$value = strtolower($where['type']);
}

$value = $where['type'] === 'Null' ? 'null' : $value;
if (is_array(array_get($where, 'values'))) {
$value .= '_' . implode('_', $where['values']);
}

return "{$carry}-{$where['column']}_{$value}";
}) ?: '';
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/UncachedBook.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;

use GeneaLabs\LaravelModelCaching\CachedModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class UncachedBook extends CachedModel
class UncachedBook extends Model
{
protected $dates = [
'published_at',
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/UncachedProfile.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;

use GeneaLabs\LaravelModelCaching\CachedModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class UncachedProfile extends CachedModel
class UncachedProfile extends Model
{
protected $fillable = [
'first_name',
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/UncachedPublisher.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;

use GeneaLabs\LaravelModelCaching\CachedModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class UncachedPublisher extends CachedModel
class UncachedPublisher extends Model
{
protected $fillable = [
'name',
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/UncachedStore.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;

use GeneaLabs\LaravelModelCaching\CachedModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class UncachedStore extends CachedModel
class UncachedStore extends Model
{
protected $fillable = [
'address',
Expand Down
24 changes: 9 additions & 15 deletions tests/Unit/CachedBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,26 +415,20 @@ public function testValueModelResultsCreatesCache()

public function testNestedRelationshipEagerloading()
{
$authors = collect()->push(
(new Author)->with('books.publisher')
->first()
);
$authors = collect([(new Author)->with('books.publisher')
->first()]);

$key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-books.publisher-first';
$tags = [
'genealabslaravelmodelcachingtestsfixturesauthor',
'genealabslaravelmodelcachingtestsfixturesbook',
'genealabslaravelmodelcachingtestsfixturespublisher',
];

$cachedResults = collect()->push(
cache()->tags($tags)
->get($key)
);

$liveResults = collect()->push(
(new UncachedAuthor)->with('books.publisher')
->first()
);
$cachedResults = collect([cache()->tags($tags)
->get($key)]);
$liveResults = collect([(new UncachedAuthor)->with('books.publisher')
->first()]);

$this->assertEmpty($authors->diffAssoc($cachedResults));
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
Expand All @@ -443,7 +437,7 @@ public function testNestedRelationshipEagerloading()
public function testLazyLoadedRelationshipResolvesThroughCachedBuilder()
{
$books = (new Author)->first()->books;
$key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1';
$key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull';
$tags = [
'genealabslaravelmodelcachingtestsfixturesbook',
];
Expand All @@ -458,7 +452,7 @@ public function testLazyLoadedRelationshipResolvesThroughCachedBuilder()
public function testLazyLoadingOnResourceIsCached()
{
$books = (new AuthorResource((new Author)->first()))->books;
$key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1';
$key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull';
$tags = [
'genealabslaravelmodelcachingtestsfixturesbook',
];
Expand Down