2015-10-19 12:50:46 -04:00
|
|
|
<?php
|
|
|
|
|
2017-02-15 11:49:38 -05:00
|
|
|
namespace Aviat\AnimeClient\Tests;
|
|
|
|
|
2017-02-08 15:48:20 -05:00
|
|
|
use const Aviat\AnimeClient\SRC_DIR;
|
|
|
|
|
2015-10-19 12:50:46 -04:00
|
|
|
use Aura\Web\WebFactory;
|
2016-10-20 22:32:17 -04:00
|
|
|
use Aviat\AnimeClient\AnimeClient;
|
|
|
|
use Aviat\Ion\Json;
|
2017-01-12 15:41:20 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Zend\Diactoros\{
|
|
|
|
Response as HttpResponse,
|
|
|
|
ServerRequestFactory
|
|
|
|
};
|
2015-10-19 12:50:46 -04:00
|
|
|
|
2016-01-06 17:06:30 -05:00
|
|
|
define('ROOT_DIR', __DIR__ . '/../');
|
|
|
|
define('TEST_DATA_DIR', __DIR__ . '/test_data');
|
|
|
|
define('TEST_VIEW_DIR', __DIR__ . '/test_views');
|
|
|
|
|
2015-10-19 12:50:46 -04:00
|
|
|
/**
|
|
|
|
* Base class for TestCases
|
|
|
|
*/
|
2017-01-12 15:41:20 -05:00
|
|
|
class AnimeClient_TestCase extends TestCase {
|
2016-01-06 15:44:40 -05:00
|
|
|
// Test directory constants
|
2016-01-06 17:06:30 -05:00
|
|
|
const ROOT_DIR = ROOT_DIR;
|
2017-02-08 15:48:20 -05:00
|
|
|
const SRC_DIR = SRC_DIR;
|
2016-01-06 17:06:30 -05:00
|
|
|
const TEST_DATA_DIR = TEST_DATA_DIR;
|
|
|
|
const TEST_VIEW_DIR = TEST_VIEW_DIR;
|
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
|
|
|
|
2015-10-19 12:50:46 -04:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
// Use mock session handler
|
|
|
|
$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
|
2016-01-06 17:06:30 -05:00
|
|
|
$files = glob(_dir(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
|
|
|
}
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2015-10-20 15:59:51 -04:00
|
|
|
|
2016-01-06 15:44:40 -05:00
|
|
|
$ROOT_DIR = realpath(_dir(__DIR__, '/../'));
|
|
|
|
$APP_DIR = _dir($ROOT_DIR, 'app');
|
|
|
|
|
2015-10-19 12:50:46 -04:00
|
|
|
$config_array = [
|
|
|
|
'asset_path' => '//localhost/assets/',
|
2016-01-06 17:06:30 -05:00
|
|
|
'img_cache_path' => _dir(ROOT_DIR, 'public/images'),
|
|
|
|
'data_cache_path' => _dir(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
|
|
|
'route_config' => [
|
|
|
|
'asset_path' => '/assets'
|
2015-10-19 12:50:46 -04:00
|
|
|
],
|
2016-01-06 11:08:56 -05:00
|
|
|
'routes' => [
|
|
|
|
|
|
|
|
]
|
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
|
2016-01-06 15:44:40 -05:00
|
|
|
$di = require _dir($APP_DIR, '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
|
|
|
|
$container->set('session-handler', function() {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function setSuperGlobals($supers = [])
|
|
|
|
{
|
|
|
|
$default = [
|
2016-02-17 10:29:05 -05:00
|
|
|
'_SERVER' => $_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
|
|
|
|
|
|
|
$request = call_user_func_array(
|
|
|
|
['Zend\Diactoros\ServerRequestFactory', 'fromGlobals'],
|
|
|
|
array_merge($default, $supers)
|
|
|
|
);
|
2016-08-29 15:36:36 -04:00
|
|
|
$this->container->setInstance('request', $request);
|
|
|
|
$this->container->set('repsone', function() {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function getMockFile()
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
array_unshift($args, TEST_DATA_DIR);
|
|
|
|
$filePath = implode(DIRECTORY_SEPARATOR, $args);
|
|
|
|
|
|
|
|
return file_get_contents($filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simplify getting mocked test data
|
|
|
|
*
|
|
|
|
* Takes multiple path arguments
|
|
|
|
*
|
|
|
|
* @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
|
|
|
}
|
|
|
|
// End of AnimeClient_TestCase.php
|