Skip to content

Commit

Permalink
#111. Add more coverage on CacheTrait/OptionsTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Jun 10, 2018
1 parent 2e75bba commit ffb4e7d
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/extension/CacheTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private function set(string $key, $val, int $ttl = 0)
* @param string $key
* @return mixed
*/
private function get(string $key)
private function getSource(string $key)
{
$data = Redis::get($key);
if ($data === NULL) {
Expand Down Expand Up @@ -70,7 +70,7 @@ private function getCached(Request $request, SqlOptions $sqlOptions)
private function getStdCached(Request $request, SqlOptions $sqlOptions)
{
$hashKey = $this->getKeyHash($request);
$items = $this->get($hashKey);
$items = $this->getSource($hashKey);
if ($items === null) {
if ($sqlOptions->getId() > 0) {
$items = $this->getEntity($sqlOptions->getId(), $sqlOptions->getData());
Expand Down Expand Up @@ -98,7 +98,7 @@ private function getXFetched(Request $request, SqlOptions $sqlOptions)
return $this->recompute($sqlOptions, $hashKey, $ttl);
}

return $this->get($hashKey);
return $this->getSource($hashKey);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/_data/ArticleFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public static function createAndGet() : Article
*/
public static function delete($id) : void
{
Article::where('id', $id)->first(['*'])->delete();
Article::destroy($id);
}
}
89 changes: 89 additions & 0 deletions tests/unit/extension/CacheTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace rjapitest\unit\extensions;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Modules\V2\Entities\Article;
use rjapi\extension\CacheTrait;
use rjapi\helpers\ConfigOptions;
use rjapi\helpers\SqlOptions;
use rjapi\types\ModelsInterface;
use rjapitest\_data\ArticleFixture;
use rjapitest\unit\TestCase;

/**
* Class CacheTraitTest
* @package rjapitest\unit\extensions
*
* @property ConfigOptions configOptions
* @property SqlOptions sqlOptions
*/
class CacheTraitTest extends TestCase
{
use CacheTrait;

private const HASH = '8f0072d875ea7422d2fd4387621a6be8';
private const CACHE_TTL = 60;
private const METHOD_INDEX = 'index';

private $configOptions;
private $sqlOptions;
private $req;
private $item;

public function setUp()
{
parent::setUp();
$this->configOptions = new ConfigOptions();
$this->configOptions->setCalledMethod(self::METHOD_INDEX);
$this->sqlOptions = new SqlOptions();
$this->req = new Request();
$this->item = ArticleFixture::createAndGet();
$this->sqlOptions->setId($this->item->id);
$this->set(self::HASH, $this->item, self::CACHE_TTL);
}

/**
* @test
*/
public function it_sets_and_gets_exp_val()
{
$val = $this->getStdCached($this->req, $this->sqlOptions);
$this->assertInstanceOf(Article::class, $val);
$this->req->server->set('REQUEST_URI', 'foo=bar');
$this->sqlOptions->setId(0);
$val = $this->getStdCached($this->req, $this->sqlOptions);
$this->assertNotEmpty($val);
}

/**
* @test
*/
public function it_gets_x_fetched()
{
$this->configOptions->setCacheTtl(self::CACHE_TTL);
$this->configOptions->setIsXFetch(true);
$val = $this->getCached($this->req, $this->sqlOptions);
$this->assertInstanceOf(Article::class, $val);
}

/**
* Params needed for internal calls
* @param $id
* @param array $data
* @return Article
*/
private function getEntity($id, array $data = ModelsInterface::DEFAULT_DATA)
{
return ArticleFixture::createAndGet();
}

public function tearDown()
{
ArticleFixture::delete($this->item->id);
Redis::flushall();
parent::tearDown();
}
}
76 changes: 76 additions & 0 deletions tests/unit/extension/OptionsTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace rjapitest\unit\extensions;


use Illuminate\Http\Request;
use rjapi\extension\JSONApiInterface;
use rjapi\extension\OptionsTrait;
use rjapi\helpers\ConfigOptions;
use rjapi\types\ModelsInterface;
use rjapitest\unit\TestCase;

/**
* Class OptionsTraitTest
* @package rjapitest\unit\extensions
*
* @property ConfigOptions configOptions
*/
class OptionsTraitTest extends TestCase
{
use OptionsTrait;

private $configOptions;
private $entity;

public function setUp()
{
parent::setUp();
$this->entity = 'article'; // test cache etc
}

/**
* @test
*/
public function it_sets_default()
{
$this->setDefaults();
$this->assertEquals(ModelsInterface::DEFAULT_PAGE, $this->defaultPage);
// check query_params preset from config
$this->assertEquals(15, $this->defaultLimit);
$this->assertEquals('desc', $this->defaultSort);
}

/**
* @test
*/
public function it_sets_sql_options()
{
$req = new Request();
$sqlOptions = $this->setSqlOptions($req);
$this->assertEquals(20, $sqlOptions->getLimit());
$this->assertEquals(1, $sqlOptions->getPage());
}

/**
* @test
*/
public function it_sets_config_options()
{
$this->setConfigOptions(JSONApiInterface::URI_METHOD_INDEX);
$this->assertEquals(JSONApiInterface::URI_METHOD_INDEX, $this->configOptions->getCalledMethod());
$this->assertTrue($this->configOptions->isCached());
// check fsm, spelling
$this->setConfigOptions(JSONApiInterface::URI_METHOD_CREATE);
$this->setConfigOptions(JSONApiInterface::URI_METHOD_UPDATE);
$this->assertTrue($this->configOptions->isStateMachine());
$this->assertTrue($this->configOptions->isSpellCheck());

// test jwt etc
$this->entity = 'user';
$this->setConfigOptions(JSONApiInterface::URI_METHOD_INDEX);
$this->assertTrue($this->configOptions->getIsJwtAction());
// bit-mask
$this->assertTrue($this->configOptions->isBitMask());
}
}

0 comments on commit ffb4e7d

Please sign in to comment.