Skip to content

Firefly III: a personal finances manager

More tests.

James Coleff5ecf61829f
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 @@
+<?php
+
+namespace FireflyIII\Helpers\Help;
+
+use Cache;
+use ErrorException;
+use League\CommonMark\CommonMarkConverter;
+use Log;
+use Route;
+
+/**
+ * Class Help
+ *
+ * @package FireflyIII\Helpers\Help
+ */
+class Help implements HelpInterface
+{
+
+ /**
+ * @param $key
+ *
+ * @return string
+ */
+ public function getFromCache($key)
+ {
+ return Cache::get($key);
+ }
+
+ /**
+ * @param $route
+ *
+ * @return array
+ */
+ public function getFromGithub($route)
+ {
+ $uri = 'https://raw.githubusercontent.com/JC5/firefly-iii-help/master/' . e($route) . '.md';
+ $content = [
+ 'text' => '<p>There is no help for this route!</p>',
+ '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'] = '<p>There is no help for this route.</p>';
+ }
+ $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 @@
+<?php
+
+namespace FireflyIII\Helpers\Help;
+
+/**
+ * Interface HelpInterface
+ *
+ * @package FireflyIII\Helpers\Help
+ */
+interface HelpInterface
+{
+
+ /**
+ * @param $key
+ *
+ * @return string
+ */
+ public function getFromCache($key);
+
+ /**
+ * @return boolean
+ */
+ public function hasRoute($route);
+
+ /**
+ * @param $route
+ *
+ * @return array
+ */
+ public function getFromGithub($route);
+
+ /**
+ * @param $route
+ * @param array $content
+ *
+ * @return void
+ */
+ public function putInCache($route, array $content);
+
+ /**
+ * @param $route
+ *
+ * @return bool
+ */
+ public function inCache($route);
+}
\ No newline at end of file
diff --git a/app/Http/Controllers/HelpController.php b/app/Http/Controllers/HelpController.php
index 09e99e2b4b..414e015944 100644
--- a/app/Http/Controllers/HelpController.php
+++ b/app/Http/Controllers/HelpController.php
@@ -2,6 +2,7 @@
use Cache;
use ErrorException;
+use FireflyIII\Helpers\Help\HelpInterface;
use League\CommonMark\CommonMarkConverter;
use Log;
use Response;
@@ -20,73 +21,34 @@ class HelpController extends Controller
*
* @return \Illuminate\Http\JsonResponse
*/
- public function show($route)
+ public function show($route, HelpInterface $help)
{
$content = [
'text' => '<p>There is no help for this route!</p>',
'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' => '<p>There is no help for this route!</p>',
- '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'] = '<p>There is no help for this route.</p>';
- }
- $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 @@
+<?php
+
+use League\FactoryMuffin\Facade as FactoryMuffin;
+
+class HelpControllerTest extends TestCase
+{
+ /**
+ * Sets up the fixture, for example, opens a network connection.
+ * This method is called before a test is executed.
+ */
+ public function setUp()
+ {
+ parent::setUp();
+ }
+
+ /**
+ * This method is called before the first test of this test class is run.
+ *
+ * @since Method available since Release 3.4.0
+ */
+ public static function setUpBeforeClass()
+ {
+ parent::setUpBeforeClass();
+ }
+
+ /**
+ * Tears down the fixture, for example, closes a network connection.
+ * This method is called after a test is executed.
+ */
+ public function tearDown()
+ {
+ parent::tearDown();
+
+ }
+
+ /**
+ * Everything present and accounted for, and in cache:
+ */
+ public function testGetHelpText()
+ {
+ // login
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $this->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