From ffb4e7d141b039b57611616860ec3fedef7a98a0 Mon Sep 17 00:00:00 2001 From: zeusakm Date: Sun, 10 Jun 2018 14:41:16 +0300 Subject: [PATCH] #111. Add more coverage on CacheTrait/OptionsTrait --- src/extension/CacheTrait.php | 6 +- tests/_data/ArticleFixture.php | 2 +- tests/unit/extension/CacheTraitTest.php | 89 +++++++++++++++++++++++ tests/unit/extension/OptionsTraitTest.php | 76 +++++++++++++++++++ 4 files changed, 169 insertions(+), 4 deletions(-) create mode 100644 tests/unit/extension/CacheTraitTest.php create mode 100644 tests/unit/extension/OptionsTraitTest.php diff --git a/src/extension/CacheTrait.php b/src/extension/CacheTrait.php index dcbae84..c2f0579 100644 --- a/src/extension/CacheTrait.php +++ b/src/extension/CacheTrait.php @@ -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) { @@ -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()); @@ -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); } /** diff --git a/tests/_data/ArticleFixture.php b/tests/_data/ArticleFixture.php index 0c04fac..ee0a404 100644 --- a/tests/_data/ArticleFixture.php +++ b/tests/_data/ArticleFixture.php @@ -33,6 +33,6 @@ public static function createAndGet() : Article */ public static function delete($id) : void { - Article::where('id', $id)->first(['*'])->delete(); + Article::destroy($id); } } \ No newline at end of file diff --git a/tests/unit/extension/CacheTraitTest.php b/tests/unit/extension/CacheTraitTest.php new file mode 100644 index 0000000..ed40a2c --- /dev/null +++ b/tests/unit/extension/CacheTraitTest.php @@ -0,0 +1,89 @@ +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(); + } +} \ No newline at end of file diff --git a/tests/unit/extension/OptionsTraitTest.php b/tests/unit/extension/OptionsTraitTest.php new file mode 100644 index 0000000..119f4ea --- /dev/null +++ b/tests/unit/extension/OptionsTraitTest.php @@ -0,0 +1,76 @@ +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()); + } +} \ No newline at end of file