More tests.

This commit is contained in:
James Cole
2015-04-07 19:58:49 +02:00
parent 05670cf393
commit ff5ecf6182
6 changed files with 237 additions and 46 deletions

85
app/Helpers/Help/Help.php Normal file
View File

@@ -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');
}
}

View File

@@ -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);
}

View File

@@ -2,6 +2,7 @@
use Cache; use Cache;
use ErrorException; use ErrorException;
use FireflyIII\Helpers\Help\HelpInterface;
use League\CommonMark\CommonMarkConverter; use League\CommonMark\CommonMarkConverter;
use Log; use Log;
use Response; use Response;
@@ -20,73 +21,34 @@ class HelpController extends Controller
* *
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
*/ */
public function show($route) public function show($route, HelpInterface $help)
{ {
$content = [ $content = [
'text' => '<p>There is no help for this route!</p>', 'text' => '<p>There is no help for this route!</p>',
'title' => 'Help', 'title' => 'Help',
]; ];
if (!Route::has($route)) { if (!$help->hasRoute($route)) {
Log::error('No such route: ' . $route); Log::error('No such route: ' . $route);
return Response::json($content); return Response::json($content);
} }
if ($this->inCache($route)) { if ($help->inCache($route)) {
$content = [ $content = [
'text' => Cache::get('help.' . $route . '.text'), 'text' => $help->getFromCache('help.' . $route . '.text'),
'title' => Cache::get('help.' . $route . '.title'), 'title' => $help->getFromCache('help.' . $route . '.title'),
]; ];
return Response::json($content); return Response::json($content);
} }
$content = $this->getFromGithub($route); $content = $help->getFromGithub($route);
$help->putInCache($route, $content);
Cache::put('help.' . $route . '.text', $content['text'], 10080); // a week.
Cache::put('help.' . $route . '.title', $content['title'], 10080);
return Response::json($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;
}
} }

View File

@@ -66,6 +66,7 @@ class FireflyServiceProvider extends ServiceProvider
$this->app->bind('FireflyIII\Support\Search\SearchInterface', 'FireflyIII\Support\Search\Search'); $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\Reminders\ReminderHelperInterface', 'FireflyIII\Helpers\Reminders\ReminderHelper');
$this->app->bind('FireflyIII\Helpers\Report\ReportHelperInterface', 'FireflyIII\Helpers\Report\ReportHelper'); $this->app->bind('FireflyIII\Helpers\Report\ReportHelperInterface', 'FireflyIII\Helpers\Report\ReportHelper');
$this->app->bind('FireflyIII\Helpers\Report\ReportQueryInterface', 'FireflyIII\Helpers\Report\ReportQuery'); $this->app->bind('FireflyIII\Helpers\Report\ReportQueryInterface', 'FireflyIII\Helpers\Report\ReportQuery');

View File

@@ -193,6 +193,7 @@ class GoogleChartControllerTest extends TestCase
$repetition = FactoryMuffin::create('FireflyIII\Models\LimitRepetition'); $repetition = FactoryMuffin::create('FireflyIII\Models\LimitRepetition');
$repetition->startdate = Carbon::now()->startOfMonth(); $repetition->startdate = Carbon::now()->startOfMonth();
$repetition->enddate = Carbon::now()->endOfMonth(); $repetition->enddate = Carbon::now()->endOfMonth();
$repetition->save();
$budget = $repetition->budgetlimit->budget; $budget = $repetition->budgetlimit->budget;
$this->be($budget->user); $this->be($budget->user);
///chart/budget/{budget}/{limitrepetition} ///chart/budget/{budget}/{limitrepetition}

View File

@@ -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();
}
}