Skip to content

Commit

Permalink
wip.
Browse files Browse the repository at this point in the history
  • Loading branch information
rennokki committed Nov 23, 2019
1 parent 2563824 commit 2a8f705
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 4 deletions.
19 changes: 19 additions & 0 deletions database/factories/KidFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/

use Illuminate\Support\Str;

$factory->define(\Rennokki\QueryCache\Test\Models\Kid::class, function () {
return [
'name' => 'Kid'.Str::random(5),
];
});
7 changes: 3 additions & 4 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ protected function getFromQueryCache(string $method = 'get', $columns = ['*'])
}

$key = $this->getCacheKey('get');
$seconds = $this->cacheTime;
$cache = $this->getCache();
$callback = $this->getQueryCacheCallback($method, $columns);

Expand Down Expand Up @@ -202,12 +201,12 @@ protected function getCache()
/**
* Indicate that the query results should be cached.
*
* @param \DateTime|int $seconds
* @param \DateTime|int $time
* @return \Rennokki\QueryCache\Query\Builder
*/
public function cacheFor($seconds)
public function cacheFor($time)
{
$this->cacheTime = $seconds;
$this->cacheTime = $time;

return $this;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/MethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Rennokki\QueryCache\Test;

use DB;
use Cache;
use Rennokki\QueryCache\Test\Models\Kid;
use Rennokki\QueryCache\Test\Models\Post;

class MethodsTest extends TestCase
Expand Down Expand Up @@ -68,4 +70,13 @@ public function test_cache_flush_without_the_right_tag()
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
$this->assertNotNull($cache);
}

public function test_hashed_key()
{
$kid = factory(Kid::class)->create();
$storedKid = Kid::cacheFor(now()->addHours(1))->first();
$cache = Cache::get('leqc:156667fa9bcb7fb8abb01018568648406f251ef65736e89e6fd27d08bc48b5bb');

$this->assertNotNull($cache);
}
}
15 changes: 15 additions & 0 deletions tests/Models/Kid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rennokki\QueryCache\Test\Models;

use Illuminate\Database\Eloquent\Model;
use Rennokki\QueryCache\Traits\QueryCacheable;

class Kid extends Model
{
use QueryCacheable;

protected $fillable = [
'name',
];
}
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function getEnvironmentSetUp($app)
]);
$app['config']->set('auth.providers.users.model', User::class);
$app['config']->set('auth.providers.posts.model', Post::class);
$app['config']->set('auth.providers.kids.model', Kid::class);
$app['config']->set('app.key', 'wslxrEFGWY6GfGhvN9L3wH3KSRJQQpBD');
}

Expand Down
32 changes: 32 additions & 0 deletions tests/database/migrations/2018_07_14_183253_kids.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class Kids extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('kids', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('kids');
}
}

0 comments on commit 2a8f705

Please sign in to comment.