Fix issue where cache file doesn't exist, add tests for Menu Helper

This commit is contained in:
Timothy Warren 2015-10-21 11:57:58 -04:00
parent def424da72
commit 32d20a9234
6 changed files with 94 additions and 6 deletions

View File

@ -81,7 +81,9 @@ class MenuGenerator extends UrlGenerator {
foreach ($menu_config as $title => $path)
{
$selected = $this->string($path)->contains($this->path());
$has = $this->string($path)->contains($this->path());
$selected = ($has && strlen($path) >= strlen($this->path()));
$link = $this->helper->a($this->url($path), $title);
$attrs = ($selected)

View File

@ -42,12 +42,17 @@ class Anime extends API {
* Update the selected anime
*
* @param array $data
* @return array
* @return array|false
*/
public function update($data)
{
// @TODO use Hummingbird Auth class
$data['auth_token'] = '';
$auth = $this->container->get('auth');
if ( ! $auth->is_authenticated())
{
return FALSE;
}
$data['auth_token'] = $auth->get_auth_token();
$response = $this->client->post("libraries/{$data['id']}", [
'body' => $data
@ -193,7 +198,9 @@ class Anime extends API {
$cache_file = _dir($this->config->get('data_cache_path'), "anime-{$status}.json");
$transformed_cache_file = _dir($this->config->get('data_cache_path'), "anime-{$status}-transformed.json");
$cached = json_decode(file_get_contents($cache_file), TRUE);
$cached = (file_exists($cache_file))
? json_decode(file_get_contents($cache_file), TRUE)
: [];
$api_data = json_decode($response->getBody(), TRUE);
if ($api_data === $cached && file_exists($transformed_cache_file))

View File

@ -126,7 +126,10 @@ class Manga extends API {
$cache_file = _dir($this->config->get('data_cache_path'), 'manga.json');
$transformed_cache_file = _dir($this->config->get('data_cache_path'), 'manga-transformed.json');
$cached_data = json_decode(file_get_contents($cache_file), TRUE);
$cached_data = file_exists($cache_file)
? json_decode(file_get_contents($cache_file), TRUE)
: [];
if ($cached_data === $api_data && file_exists($transformed_cache_file))
{

View File

@ -0,0 +1,74 @@
<?php
use Aviat\AnimeClient\Helper\Menu as MenuHelper;
class MenuHelperTest extends AnimeClient_TestCase {
public function setUp()
{
parent::setUp();
$this->helper = $this->container->get('html-helper');
$this->urlGenerator = $this->container->get('url-generator');
}
public function testInvoke()
{
$menus = [
'no selection' => [
'route_prefix' => '/foo',
'items' => [
'bar' => '/bar'
]
],
'selected' => [
'route_prefix' => '',
'items' => [
'index' => '/foobar'
]
]
];
$expected = [];
// No selection
$link = $this->helper->a($this->urlGenerator->url('/foo/bar'), 'Bar');
$this->helper->ul()->rawItem($link);
$expected['no selection'] = $this->helper->ul()->__toString();
// selected
$link = $this->helper->a($this->urlGenerator->url('/foobar'), 'Index');
$this->helper->ul()->rawItem($link, ['class' => 'selected']);
$expected['selected'] = $this->helper->ul()->__toString();
// Set config for tests
$config = $this->container->get('config');
$config->set('menus', $menus);
$this->container->set('config', $config);
foreach($menus as $case => $config)
{
if ($case === 'selected')
{
$this->setSuperGlobals([
'_SERVER' => [
'HTTP_HOST' => 'localhost',
'REQUEST_URI' => '/foobar'
]
]);
}
else
{
$this->setSuperGlobals([
'_SERVER' => [
'HTTP_HOST' => 'localhost',
'REQUEST_URI' => '/applesauceisgreat'
]
]);
}
$helper = new MenuHelper();
$helper->setContainer($this->container);
$this->assertEquals($expected[$case], (string)$helper($case));
}
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long