Skip to content

Commit 93d1a02

Browse files
committed
Add handling of whereJsonContains and orWhereJsonContains
Fixes #176
1 parent 613edc0 commit 93d1a02

File tree

12 files changed

+92
-6
lines changed

12 files changed

+92
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ 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.3.2] - 3 Nov 2018
8+
### Added
9+
- handling of `whereJsonContains()` and `orWhereJsonContains()`.
10+
11+
### Fixed
12+
- price field value generation in BookFactory to not exceed database field limits.
13+
714
## [0.3.1] - 7 Oct 2018
815
### Changed
916
- use of `cache()` helper method to `app("cache")` to allow for better Lumen compatibility. Thanks @nope7777!

phpunit.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,9 @@
3131
<env name="SESSION_DRIVER" value="array"/>
3232
<env name="QUEUE_DRIVER" value="sync"/>
3333
<env name="DB_CONNECTION" value="testing"/>
34+
<env name="DB_HOST" value="192.168.10.10"/>
35+
<env name="DB_DATABASE" value="testing"/>
36+
<env name="DB_USERNAME" value="homestead"/>
37+
<env name="DB_PASSWORD" value="secret"/>
3438
</php>
3539
</phpunit>

src/CacheKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected function getQueryColumns(array $columns) : string
9797

9898
protected function getTypeClause($where) : string
9999
{
100-
$type = in_array($where["type"], ["In", "NotIn", "Null", "NotNull", "between", "NotInSub", "InSub"])
100+
$type = in_array($where["type"], ["In", "NotIn", "Null", "NotNull", "between", "NotInSub", "InSub", "JsonContains"])
101101
? strtolower($where["type"])
102102
: strtolower($where["operator"]);
103103

tests/CreatesApplication.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ protected function getPackageProviders($app)
7474

7575
protected function getEnvironmentSetUp($app)
7676
{
77+
$app['config']->set('database.default', 'testing');
78+
$app['config']->set('database.connections.testbench', [
79+
'driver' => 'sqlite',
80+
'database' => ':memory:',
81+
'prefix' => '',
82+
]);
7783
$app['config']->set('database.redis.cache', [
7884
'host' => env('REDIS_HOST', '192.168.10.10'),
7985
]);

tests/Fixtures/Author.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ class Author extends Model
1010
{
1111
use Cachable;
1212

13+
protected $casts = [
14+
"finances" => "array",
15+
];
1316
protected $fillable = [
1417
'name',
1518
'email',
19+
"finances",
1620
];
1721

1822
public function books() : HasMany

tests/Fixtures/UncachedAuthor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77

88
class UncachedAuthor extends Model
99
{
10+
protected $casts = [
11+
"finances" => "array",
12+
];
1013
protected $fillable = [
1114
'name',
1215
'email',
16+
"finances",
1317
];
1418
protected $table = 'authors';
1519

tests/Integration/CachedBuilder/FirstOrCreateTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
class FirstOrCreateTest extends IntegrationTestCase
1919
{
20-
21-
2220
public function testFirstOrCreateFlushesCacheForModel()
2321
{
2422
(new Author)->truncate();

tests/Integration/CachedBuilder/FirstTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
class FirstTest extends IntegrationTestCase
1919
{
20-
21-
2220
public function testFirstReturnsAllAttributesForModel()
2321
{
2422
$author = (new Author)->where("id", "=", 1)->first();
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
6+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
7+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
8+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
9+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook;
10+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile;
11+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPublisher;
12+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedStore;
13+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Http\Resources\Author as AuthorResource;
14+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
15+
use Illuminate\Foundation\Testing\RefreshDatabase;
16+
use Illuminate\Support\Collection;
17+
use Illuminate\Support\Facades\Config;
18+
19+
class WhereJsonContainsTest extends IntegrationTestCase
20+
{
21+
use RefreshDatabase;
22+
23+
protected function getEnvironmentSetUp($app)
24+
{
25+
parent::getEnvironmentSetUp($app);
26+
27+
$app['config']->set('database.default', 'pgsql');
28+
}
29+
30+
public function setUp()
31+
{
32+
parent::setUp();
33+
34+
$this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');
35+
}
36+
37+
public function testWithInUsingCollectionQuery()
38+
{
39+
$key = sha1('genealabs:laravel-model-caching:pgsql:testing:genealabslaravelmodelcachingtestsfixturesauthor-finances->total_jsoncontains_5000');
40+
$tags = [
41+
'genealabs:laravel-model-caching:pgsql:testing:genealabslaravelmodelcachingtestsfixturesauthor',
42+
];
43+
44+
$authors = (new Author)
45+
->whereJsonContains("finances->total", 5000)
46+
->get();
47+
$liveResults = (new UncachedAuthor)
48+
->whereJsonContains("finances->total", 5000)
49+
->get();
50+
51+
$cachedResults = $this
52+
->cache()
53+
->tags($tags)
54+
->get($key)['value'];
55+
56+
$this->assertEquals($liveResults->pluck("id"), $authors->pluck("id"));
57+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
58+
}
59+
}

tests/database/factories/AuthorFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@
77
return [
88
'name' => $faker->name,
99
'email' => $faker->unique()->safeEmail,
10+
"finances" => [
11+
"total" => 5000,
12+
"weekly" => 100,
13+
"daily" => 20,
14+
],
1015
];
1116
});

tests/database/factories/BookFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
'title' => $faker->title,
99
'description' => $faker->optional()->paragraphs(3, true),
1010
'published_at' => $faker->dateTime,
11-
'price' => $faker->randomFloat(2),
11+
'price' => $faker->randomFloat(2, 0, 999999),
1212
];
1313
});

tests/database/migrations/2017_09_21_010055_create_authors.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function up()
1313

1414
$table->string('email');
1515
$table->string('name');
16+
$table->json("finances")->nullable();
1617
});
1718
}
1819

0 commit comments

Comments
 (0)