From ff5ecf61829f2f99e9d30f9eed3a613fddc108e8 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 7 Apr 2015 19:58:49 +0200 Subject: [PATCH] More tests. --- app/Helpers/Help/Help.php | 85 ++++++++++++++++ app/Helpers/Help/HelpInterface.php | 46 +++++++++ app/Http/Controllers/HelpController.php | 54 ++--------- app/Providers/FireflyServiceProvider.php | 1 + .../controllers/GoogleChartControllerTest.php | 1 + tests/controllers/HelpControllerTest.php | 96 +++++++++++++++++++ 6 files changed, 237 insertions(+), 46 deletions(-) create mode 100644 app/Helpers/Help/Help.php create mode 100644 app/Helpers/Help/HelpInterface.php create mode 100644 tests/controllers/HelpControllerTest.php diff --git a/app/Helpers/Help/Help.php b/app/Helpers/Help/Help.php new file mode 100644 index 0000000000..f7bf4a7690 --- /dev/null +++ b/app/Helpers/Help/Help.php @@ -0,0 +1,85 @@ + '

There is no help for this route!

', + 'title' => $route, + ]; + try { + $content['text'] = file_get_contents($uri); + } catch (ErrorException $e) { + Log::error(trim($e->getMessage())); + } + if (strlen(trim($content['text'])) == 0) { + $content['text'] = '

There is no help for this route.

'; + } + $converter = new CommonMarkConverter(); + $content['text'] = $converter->convertToHtml($content['text']); + + return $content; + + } + + /** + * @return boolean + */ + public function hasRoute($route) + { + return Route::has($route); + } + + /** + * @param $title + * @param array $content + * + * @return void + */ + public function putInCache($route, array $content) + { + Cache::put('help.' . $route . '.text', $content['text'], 10080); // a week. + Cache::put('help.' . $route . '.title', $content['title'], 10080); + } + + /** + * @param $route + * + * @return bool + */ + public function inCache($route) + { + return Cache::has('help.' . $route . '.title') && Cache::has('help.' . $route . '.text'); + } +} \ No newline at end of file diff --git a/app/Helpers/Help/HelpInterface.php b/app/Helpers/Help/HelpInterface.php new file mode 100644 index 0000000000..c550a9faf5 --- /dev/null +++ b/app/Helpers/Help/HelpInterface.php @@ -0,0 +1,46 @@ + '

There is no help for this route!

', 'title' => 'Help', ]; - if (!Route::has($route)) { + if (!$help->hasRoute($route)) { Log::error('No such route: ' . $route); return Response::json($content); } - if ($this->inCache($route)) { + if ($help->inCache($route)) { $content = [ - 'text' => Cache::get('help.' . $route . '.text'), - 'title' => Cache::get('help.' . $route . '.title'), + 'text' => $help->getFromCache('help.' . $route . '.text'), + 'title' => $help->getFromCache('help.' . $route . '.title'), ]; return Response::json($content); } - $content = $this->getFromGithub($route); + $content = $help->getFromGithub($route); - - Cache::put('help.' . $route . '.text', $content['text'], 10080); // a week. - Cache::put('help.' . $route . '.title', $content['title'], 10080); + $help->putInCache($route, $content); return Response::json($content); } - /** - * @param $route - * - * @return bool - */ - protected function inCache($route) - { - return Cache::has('help.' . $route . '.title') && Cache::has('help.' . $route . '.text'); - } - - /** - * @param $route - * - * @return array - */ - protected function getFromGithub($route) - { - $uri = 'https://raw.githubusercontent.com/JC5/firefly-iii-help/master/' . e($route) . '.md'; - $content = [ - 'text' => '

There is no help for this route!

', - 'title' => $route, - ]; - try { - $content['text'] = file_get_contents($uri); - } catch (ErrorException $e) { - Log::error(trim($e->getMessage())); - } - if (strlen(trim($content['text'])) == 0) { - $content['text'] = '

There is no help for this route.

'; - } - $converter = new CommonMarkConverter(); - $content['text'] = $converter->convertToHtml($content['text']); - - return $content; - - - } } diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php index 113a25c433..a00ffc289e 100644 --- a/app/Providers/FireflyServiceProvider.php +++ b/app/Providers/FireflyServiceProvider.php @@ -66,6 +66,7 @@ class FireflyServiceProvider extends ServiceProvider $this->app->bind('FireflyIII\Support\Search\SearchInterface', 'FireflyIII\Support\Search\Search'); + $this->app->bind('FireflyIII\Helpers\Help\HelpInterface', 'FireflyIII\Helpers\Help\Help'); $this->app->bind('FireflyIII\Helpers\Reminders\ReminderHelperInterface', 'FireflyIII\Helpers\Reminders\ReminderHelper'); $this->app->bind('FireflyIII\Helpers\Report\ReportHelperInterface', 'FireflyIII\Helpers\Report\ReportHelper'); $this->app->bind('FireflyIII\Helpers\Report\ReportQueryInterface', 'FireflyIII\Helpers\Report\ReportQuery'); diff --git a/tests/controllers/GoogleChartControllerTest.php b/tests/controllers/GoogleChartControllerTest.php index 3600a2eebb..d6cf2bf489 100644 --- a/tests/controllers/GoogleChartControllerTest.php +++ b/tests/controllers/GoogleChartControllerTest.php @@ -193,6 +193,7 @@ class GoogleChartControllerTest extends TestCase $repetition = FactoryMuffin::create('FireflyIII\Models\LimitRepetition'); $repetition->startdate = Carbon::now()->startOfMonth(); $repetition->enddate = Carbon::now()->endOfMonth(); + $repetition->save(); $budget = $repetition->budgetlimit->budget; $this->be($budget->user); ///chart/budget/{budget}/{limitrepetition} diff --git a/tests/controllers/HelpControllerTest.php b/tests/controllers/HelpControllerTest.php new file mode 100644 index 0000000000..1bacdbdcb0 --- /dev/null +++ b/tests/controllers/HelpControllerTest.php @@ -0,0 +1,96 @@ +be($user); + // mock some stuff. + $interface = $this->mock('FireflyIII\Helpers\Help\HelpInterface'); + $interface->shouldReceive('hasRoute')->once()->with('accounts.index')->andReturn(true); + $interface->shouldReceive('getFromCache')->once()->with('help.accounts.index.title')->andReturn('Title.'); + $interface->shouldReceive('getFromCache')->once()->with('help.accounts.index.text')->andReturn('Text'); + $interface->shouldReceive('inCache')->andReturn(true); + + + $this->call('GET', '/help/accounts.index'); + $this->assertResponseOk(); + } + + /** + * Everything present and accounted for, but not cached + */ + public function testGetHelpTextNoCache() + { + // login + $user = FactoryMuffin::create('FireflyIII\User'); + $content = ['title' => 'Bla', 'text' => 'Bla']; + + $this->be($user); + // mock some stuff. + $interface = $this->mock('FireflyIII\Helpers\Help\HelpInterface'); + $interface->shouldReceive('hasRoute')->once()->with('accounts.index')->andReturn(true); + $interface->shouldReceive('getFromGithub')->once()->with('accounts.index')->andReturn($content); + $interface->shouldReceive('putInCache')->once()->withArgs(['accounts.index', $content]); + $interface->shouldReceive('inCache')->once()->andReturn(false); + + + $this->call('GET', '/help/accounts.index'); + $this->assertResponseOk(); + } + + /** + * No such route. + */ + public function testGetHelpTextNoRoute() + { + // login + $user = FactoryMuffin::create('FireflyIII\User'); + $content = ['title' => 'Bla', 'text' => 'Bla']; + + $this->be($user); + // mock some stuff. + $interface = $this->mock('FireflyIII\Helpers\Help\HelpInterface'); + $interface->shouldReceive('hasRoute')->once()->with('accounts.index')->andReturn(false); + + + $this->call('GET', '/help/accounts.index'); + $this->assertResponseOk(); + } +} \ No newline at end of file