-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#111. Add more coverage on CacheTrait/OptionsTrait
- Loading branch information
1 parent
2e75bba
commit ffb4e7d
Showing
4 changed files
with
169 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |