Fix issue where cache file doesn't exist, add tests for Menu Helper
This commit is contained in:
parent
def424da72
commit
32d20a9234
@ -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)
|
||||
|
@ -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))
|
||||
|
@ -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))
|
||||
{
|
||||
|
74
tests/AnimeClient/Helper/MenuHelperTest.php
Normal file
74
tests/AnimeClient/Helper/MenuHelperTest.php
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
1
tests/test_data/anime_list/anime-all-transformed.json
Normal file
1
tests/test_data/anime_list/anime-all-transformed.json
Normal file
File diff suppressed because one or more lines are too long
1
tests/test_data/anime_list/anime-all.json
Normal file
1
tests/test_data/anime_list/anime-all.json
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user