HummingBirdAnimeClient/tests/AnimeClient_TestCase.php

110 lines
2.5 KiB
PHP
Raw Normal View History

2015-10-19 12:50:46 -04:00
<?php
use Aura\Web\WebFactory;
2015-10-21 15:43:51 -04:00
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
2015-10-19 12:50:46 -04:00
use Aviat\AnimeClient\Config;
/**
* Base class for TestCases
*/
class AnimeClient_TestCase extends PHPUnit_Framework_TestCase {
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
$files = glob(_dir(TEST_DATA_DIR, 'cache', '*.json'));
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
2015-10-19 12:50:46 -04:00
$config_array = [
'asset_path' => '//localhost/assets/',
2015-11-17 16:45:41 -05:00
'img_cache_path' => _dir(ROOT_DIR, 'public/images'),
'data_cache_path' => _dir(TEST_DATA_DIR, 'cache'),
2015-12-08 16:39:49 -05:00
'database' => [
'collection' => [
'type' => 'sqlite',
'host' => '',
'user' => '',
'pass' => '',
'port' => '',
'name' => 'default',
'database' => '',
'file' => ':memory:',
]
],
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
$di = require _dir(APP_DIR, 'bootstrap.php');
$container = $di($config_array);
$container->set('error-handler', new MockErrorHandler());
$container->set('session-handler', self::$session_handler);
$this->container = $container;
}
/**
* Set arbitrary superglobal values for testing purposes
*
* @param array $supers
* @return void
*/
public function setSuperGlobals($supers = [])
{
$default = [
'_GET' => $_GET,
'_POST' => $_POST,
'_COOKIE' => $_COOKIE,
'_SERVER' => $_SERVER,
'_FILES' => $_FILES
];
$web_factory = new WebFactory(array_merge($default,$supers));
$this->container->set('request', $web_factory->newRequest());
$this->container->set('response', $web_factory->newResponse());
}
2015-10-21 15:43:51 -04:00
/**
* Create a mock guzzle client for testing
* api call methods
*
* @param int $code The status code
* @param array $headers
* @param string $body
* @return Client
*/
public function getMockClient($code, $headers, $body)
{
$mock = new MockHandler([
new Response($code, $headers, $body)
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
return $client;
}
2015-10-19 12:50:46 -04:00
}
// End of AnimeClient_TestCase.php