2017-02-16 11:09:37 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2017-02-16 11:09:37 -05:00
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
2017-02-16 11:09:37 -05:00
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2021-01-13 01:52:03 -05:00
|
|
|
* @copyright 2015 - 2021 Timothy J. Warren
|
2017-02-16 11:09:37 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2017-03-07 20:53:58 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2017-02-16 11:09:37 -05:00
|
|
|
*/
|
2015-10-19 12:50:46 -04:00
|
|
|
|
2017-02-15 11:49:38 -05:00
|
|
|
namespace Aviat\AnimeClient\Tests;
|
|
|
|
|
2017-03-30 16:57:58 -04:00
|
|
|
use function Aviat\Ion\_dir;
|
2017-02-22 15:08:29 -05:00
|
|
|
|
2016-10-20 22:32:17 -04:00
|
|
|
use Aviat\Ion\Json;
|
2017-01-12 15:41:20 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-04-06 11:59:53 -04:00
|
|
|
use Spatie\Snapshots\MatchesSnapshots;
|
2020-03-12 09:52:45 -04:00
|
|
|
use Laminas\Diactoros\{
|
2017-01-12 15:41:20 -05:00
|
|
|
Response as HttpResponse,
|
|
|
|
ServerRequestFactory
|
|
|
|
};
|
2015-10-19 12:50:46 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for TestCases
|
|
|
|
*/
|
2017-02-15 16:40:18 -05:00
|
|
|
class AnimeClientTestCase extends TestCase {
|
2017-04-06 11:59:53 -04:00
|
|
|
use MatchesSnapshots;
|
|
|
|
|
2016-01-06 15:44:40 -05:00
|
|
|
// Test directory constants
|
2021-02-03 10:27:04 -05:00
|
|
|
public const ROOT_DIR = AC_TEST_ROOT_DIR;
|
2020-03-12 09:52:45 -04:00
|
|
|
public const SRC_DIR = SRC_DIR;
|
|
|
|
public const TEST_DATA_DIR = __DIR__ . '/test_data';
|
|
|
|
public const TEST_VIEW_DIR = __DIR__ . '/test_views';
|
2016-01-06 15:44:40 -05:00
|
|
|
|
2015-10-19 12:50:46 -04:00
|
|
|
protected $container;
|
|
|
|
protected static $staticContainer;
|
|
|
|
protected static $session_handler;
|
2015-10-20 15:59:51 -04:00
|
|
|
|
2019-12-06 09:15:49 -05:00
|
|
|
public static function setUpBeforeClass(): void
|
2015-10-19 12:50:46 -04:00
|
|
|
{
|
|
|
|
// Use mock session handler
|
2017-12-08 22:32:00 -05:00
|
|
|
//$session_handler = new TestSessionHandler();
|
|
|
|
//session_set_save_handler($session_handler, TRUE);
|
|
|
|
//self::$session_handler = $session_handler;
|
2015-11-17 16:45:41 -05:00
|
|
|
|
|
|
|
// Remove test cache files
|
2020-03-12 09:52:45 -04:00
|
|
|
$files = glob(_dir(self::TEST_DATA_DIR, 'cache', '*.json'));
|
2015-11-17 16:45:41 -05:00
|
|
|
array_map('unlink', $files);
|
2015-10-19 12:50:46 -04:00
|
|
|
}
|
|
|
|
|
2019-12-06 09:15:49 -05:00
|
|
|
public function setUp(): void
|
2015-10-19 12:50:46 -04:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2015-10-20 15:59:51 -04:00
|
|
|
|
2015-10-19 12:50:46 -04:00
|
|
|
$config_array = [
|
2018-10-05 14:32:05 -04:00
|
|
|
'asset_path' => '/assets',
|
2021-02-03 10:27:04 -05:00
|
|
|
'img_cache_path' => _dir(self::ROOT_DIR, 'public/images'),
|
2020-03-12 09:52:45 -04:00
|
|
|
'data_cache_path' => _dir(self::TEST_DATA_DIR, 'cache'),
|
2017-01-10 21:13:44 -05:00
|
|
|
'cache' => [
|
|
|
|
'driver' => 'null',
|
|
|
|
'connection' => []
|
|
|
|
],
|
2015-12-08 16:39:49 -05:00
|
|
|
'database' => [
|
|
|
|
'collection' => [
|
|
|
|
'type' => 'sqlite',
|
|
|
|
'host' => '',
|
|
|
|
'user' => '',
|
|
|
|
'pass' => '',
|
|
|
|
'port' => '',
|
|
|
|
'name' => 'default',
|
|
|
|
'database' => '',
|
|
|
|
'file' => ':memory:',
|
2016-07-28 10:44:13 -04:00
|
|
|
],
|
|
|
|
'cache' => [
|
|
|
|
'type' => 'sqlite',
|
|
|
|
'host' => '',
|
|
|
|
'user' => '',
|
|
|
|
'pass' => '',
|
|
|
|
'port' => '',
|
|
|
|
'name' => 'default',
|
|
|
|
'database' => '',
|
|
|
|
'file' => ':memory:',
|
2015-12-08 16:39:49 -05:00
|
|
|
]
|
|
|
|
],
|
2015-10-19 12:50:46 -04:00
|
|
|
'routes' => [
|
2016-01-06 11:08:56 -05:00
|
|
|
|
2017-03-29 15:14:30 -04:00
|
|
|
],
|
2015-10-19 12:50:46 -04:00
|
|
|
];
|
2015-10-20 15:59:51 -04:00
|
|
|
|
2015-10-19 12:50:46 -04:00
|
|
|
// Set up DI container
|
2020-03-12 09:52:45 -04:00
|
|
|
$di = require _dir(self::ROOT_DIR, 'app', 'bootstrap.php');
|
2015-10-19 12:50:46 -04:00
|
|
|
$container = $di($config_array);
|
2016-08-29 15:36:36 -04:00
|
|
|
|
|
|
|
// Use mock session handler
|
2020-03-12 09:52:45 -04:00
|
|
|
$container->set('session-handler', static function() {
|
2016-08-29 15:36:36 -04:00
|
|
|
$session_handler = new TestSessionHandler();
|
|
|
|
session_set_save_handler($session_handler, TRUE);
|
|
|
|
return $session_handler;
|
|
|
|
});
|
2015-10-19 12:50:46 -04:00
|
|
|
|
|
|
|
$this->container = $container;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set arbitrary superglobal values for testing purposes
|
|
|
|
*
|
|
|
|
* @param array $supers
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-01-07 09:08:00 -05:00
|
|
|
public function setSuperGlobals($supers = []): void
|
2015-10-19 12:50:46 -04:00
|
|
|
{
|
|
|
|
$default = [
|
2020-12-11 14:26:54 -05:00
|
|
|
'_SERVER' => $GLOBALS['_SERVER'],
|
2015-10-19 12:50:46 -04:00
|
|
|
'_GET' => $_GET,
|
|
|
|
'_POST' => $_POST,
|
|
|
|
'_COOKIE' => $_COOKIE,
|
|
|
|
'_FILES' => $_FILES
|
|
|
|
];
|
2016-02-17 10:29:05 -05:00
|
|
|
|
2018-01-15 14:43:15 -05:00
|
|
|
$request = \call_user_func_array(
|
|
|
|
[ServerRequestFactory::class, 'fromGlobals'],
|
2020-12-11 14:26:54 -05:00
|
|
|
array_values(array_merge($default, $supers)),
|
2016-02-17 10:29:05 -05:00
|
|
|
);
|
2016-08-29 15:36:36 -04:00
|
|
|
$this->container->setInstance('request', $request);
|
2019-12-06 09:15:49 -05:00
|
|
|
$this->container->set('response', static function() {
|
2016-08-29 15:36:36 -04:00
|
|
|
return new HttpResponse();
|
|
|
|
});
|
2015-10-19 12:50:46 -04:00
|
|
|
}
|
2015-10-21 15:43:51 -04:00
|
|
|
|
2016-08-01 13:02:26 -04:00
|
|
|
/**
|
|
|
|
* Simplify getting test data
|
|
|
|
*
|
|
|
|
* Takes multiple path arguments
|
|
|
|
*
|
|
|
|
* @return string - contents of the data file
|
|
|
|
*/
|
2019-01-07 09:08:00 -05:00
|
|
|
public function getMockFile(): string
|
2016-08-01 13:02:26 -04:00
|
|
|
{
|
|
|
|
$args = func_get_args();
|
2020-03-12 09:52:45 -04:00
|
|
|
array_unshift($args, self::TEST_DATA_DIR);
|
2016-08-01 13:02:26 -04:00
|
|
|
$filePath = implode(DIRECTORY_SEPARATOR, $args);
|
|
|
|
|
|
|
|
return file_get_contents($filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simplify getting mocked test data
|
|
|
|
*
|
|
|
|
* Takes multiple path arguments
|
|
|
|
*
|
2019-12-06 09:15:49 -05:00
|
|
|
* @param array $args
|
2016-08-01 13:02:26 -04:00
|
|
|
* @return mixed - the decoded data
|
|
|
|
*/
|
2017-01-27 15:41:52 -05:00
|
|
|
public function getMockFileData(...$args)
|
2016-08-01 13:02:26 -04:00
|
|
|
{
|
2017-01-27 15:41:52 -05:00
|
|
|
$rawData = $this->getMockFile(...$args);
|
2016-08-01 13:02:26 -04:00
|
|
|
|
|
|
|
return Json::decode($rawData);
|
|
|
|
}
|
2015-10-19 12:50:46 -04:00
|
|
|
}
|
2017-02-15 16:40:18 -05:00
|
|
|
// End of AnimeClientTestCase.php
|