Misc fixes

This commit is contained in:
Timothy Warren 2021-02-26 14:42:07 -05:00
parent f0eacc2f93
commit 0b9fbbf042
8 changed files with 50 additions and 31 deletions

View File

@ -26,6 +26,7 @@ setlocale(LC_CTYPE, 'en_US');
require_once __DIR__ . '/vendor/autoload.php';
Debugger::$strictMode = true;
Debugger::$showBar = false;
Debugger::enable(Debugger::DEVELOPMENT, __DIR__ . '/app/logs');
// Define base directories

View File

@ -126,6 +126,7 @@ class Controller {
/**
* Set the current url in the session as the target of a future redirect
*
* @codeCoverageIgnore
* @param string|NULL $url
* @throws ContainerException
* @throws NotFoundException
@ -166,6 +167,7 @@ class Controller {
*
* If one is not set, redirect to default url
*
* @codeCoverageIgnore
* @throws InvalidArgumentException
* @return void
*/
@ -179,6 +181,7 @@ class Controller {
/**
* Check if the current user is authenticated, else error and exit
* @codeCoverageIgnore
*/
protected function checkAuth(): void
{
@ -195,6 +198,7 @@ class Controller {
/**
* Get the string output of a partial template
*
* @codeCoverageIgnore
* @param HtmlView $view
* @param string $template
* @param array $data
@ -226,6 +230,7 @@ class Controller {
/**
* Render a template with header and footer
*
* @codeCoverageIgnore
* @param HtmlView $view
* @param string $template
* @param array $data
@ -256,6 +261,7 @@ class Controller {
/**
* 404 action
*
* @codeCoverageIgnore
* @param string $title
* @param string $message
* @throws InvalidArgumentException
@ -276,6 +282,7 @@ class Controller {
/**
* Display a generic error page
*
* @codeCoverageIgnore
* @param int $httpCode
* @param string $title
* @param string $message
@ -295,6 +302,7 @@ class Controller {
/**
* Redirect to the default controller/url from an empty path
*
* @codeCoverageIgnore
* @throws InvalidArgumentException
* @return void
*/
@ -308,6 +316,7 @@ class Controller {
* Set a session flash variable to display a message on
* next page load
*
* @codeCoverageIgnore
* @param string $message
* @param string $type
* @return void
@ -343,6 +352,7 @@ class Controller {
/**
* Add a message box to the page
*
* @codeCoverageIgnore
* @param HtmlView $view
* @param string $type
* @param string $message
@ -360,6 +370,7 @@ class Controller {
/**
* Output a template to HTML, using the provided data
*
* @codeCoverageIgnore
* @param string $template
* @param array $data
* @param HtmlView|NULL $view
@ -381,6 +392,7 @@ class Controller {
/**
* Output a JSON Response
*
* @codeCoverageIgnore
* @param mixed $data
* @param int $code - the http status code
* @throws DoubleRenderException
@ -397,6 +409,7 @@ class Controller {
/**
* Redirect to the selected page
*
* @codeCoverageIgnore
* @param string $url
* @param int $code
* @return void

View File

@ -314,10 +314,8 @@ final class Dispatcher extends RoutingBase {
/**
* Get the appropriate params for the error page
* passed on the failed route
*
* @return array|false
*/
protected function getErrorParams()
protected function getErrorParams(): array
{
$logger = $this->container->getLogger();
$failure = $this->matcher->getFailedRoute();

View File

@ -66,7 +66,7 @@ class Container implements ContainerInterface {
*
* @return mixed Entry.
*/
public function get($id): mixed
public function get(string $id): mixed
{
if ( ! \is_string($id))
{
@ -94,12 +94,12 @@ class Container implements ContainerInterface {
* Get a new instance of the specified item
*
* @param string $id - Identifier of the entry to look for.
* @param array $args - Optional arguments for the factory callable
* @param array|null $args - Optional arguments for the factory callable
* @throws NotFoundException - No entry was found for this identifier.
* @throws ContainerException - Error while retrieving the entry.
* @return mixed
*/
public function getNew($id, array $args = NULL): mixed
public function getNew(string $id, ?array $args = NULL): mixed
{
if ( ! \is_string($id))
{
@ -159,7 +159,7 @@ class Container implements ContainerInterface {
* @param string $id Identifier of the entry to look for.
* @return boolean
*/
public function has($id): bool
public function has(string $id): bool
{
return array_key_exists($id, $this->container);
}

View File

@ -36,7 +36,7 @@ interface ContainerInterface {
* @throws Exception\ContainerException Error while retrieving the entry.
* @return mixed Entry.
*/
public function get($id);
public function get(string $id): mixed;
/**
* Returns true if the container can return an entry for the given identifier.
@ -45,7 +45,7 @@ interface ContainerInterface {
* @param string $id Identifier of the entry to look for.
* @return boolean
*/
public function has($id): bool;
public function has(string $id): bool;
/**
* Add a factory to the container
@ -63,7 +63,7 @@ interface ContainerInterface {
* @param mixed $value
* @return ContainerInterface
*/
public function setInstance(string $id, $value): ContainerInterface;
public function setInstance(string $id, mixed $value): ContainerInterface;
/**
* Get a new instance of the specified item
@ -71,7 +71,7 @@ interface ContainerInterface {
* @param string $id
* @return mixed
*/
public function getNew($id);
public function getNew(string $id): mixed;
/**
* Determine whether a logger channel is registered

View File

@ -16,6 +16,7 @@
namespace Aviat\AnimeClient\Tests;
use Aviat\Ion\Di\ContainerAware;
use Aviat\Ion\Di\ContainerInterface;
use function Aviat\Ion\_dir;
@ -27,10 +28,16 @@ use Laminas\Diactoros\{
ServerRequestFactory
};
use const Aviat\AnimeClient\{
SLUG_PATTERN,
DEFAULT_CONTROLLER,
};
/**
* Base class for TestCases
*/
class AnimeClientTestCase extends TestCase {
use ContainerAware;
use MatchesSnapshots;
// Test directory constants
@ -39,17 +46,10 @@ class AnimeClientTestCase extends TestCase {
public const TEST_DATA_DIR = __DIR__ . '/test_data';
public const TEST_VIEW_DIR = __DIR__ . '/test_views';
protected $container;
protected static $staticContainer;
protected static $session_handler;
protected ContainerInterface $container;
public static function setUpBeforeClass(): void
{
// Use mock session handler
//$session_handler = new TestSessionHandler();
//session_set_save_handler($session_handler, TRUE);
//self::$session_handler = $session_handler;
// Remove test cache files
$files = glob(_dir(self::TEST_DATA_DIR, 'cache', '*.json'));
array_map('unlink', $files);
@ -90,9 +90,7 @@ class AnimeClientTestCase extends TestCase {
'file' => ':memory:',
]
],
'routes' => [
],
'routes' => [ ],
];
// Set up DI container
@ -159,7 +157,7 @@ class AnimeClientTestCase extends TestCase {
* @param array $args
* @return mixed - the decoded data
*/
public function getMockFileData(...$args)
public function getMockFileData(mixed ...$args): mixed
{
$rawData = $this->getMockFile(...$args);

View File

@ -21,13 +21,14 @@ use Aviat\AnimeClient\Controller;
use Aviat\AnimeClient\Dispatcher;
use Aviat\AnimeClient\UrlGenerator;
use Aviat\Ion\Config;
use Aviat\Ion\Di\ContainerInterface;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
class DispatcherTest extends AnimeClientTestCase {
protected $container;
protected ContainerInterface $container;
protected $router;
protected $config;
protected $urlGenerator;

View File

@ -23,6 +23,8 @@ use Monolog\Logger;
use Monolog\Handler\{TestHandler, NullHandler};
use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Di\Exception\NotFoundException;
use Throwable;
use TypeError;
class FooTest {
@ -49,13 +51,11 @@ class ContainerTest extends IonTestCase {
return [
'Bad index type: number' => [
'id' => 42,
'exception' => ContainerException::class,
'message' => 'Id must be a string'
'exception' => TypeError::class,
],
'Bad index type: array' => [
'id' => [],
'exception' => ContainerException::class,
'message' => 'Id must be a string'
'exception' => TypeError::class,
],
'Non-existent id' => [
'id' => 'foo',
@ -68,7 +68,7 @@ class ContainerTest extends IonTestCase {
/**
* @dataProvider dataGetWithException
*/
public function testGetWithException($id, $exception, $message): void
public function testGetWithException(mixed $id, $exception, ?string $message = NULL): void
{
try
{
@ -79,15 +79,23 @@ class ContainerTest extends IonTestCase {
$this->assertInstanceOf($exception, $e);
$this->assertEquals($message, $e->getMessage());
}
catch(Throwable $e)
{
$this->assertInstanceOf($exception, $e);
}
}
/**
* @dataProvider dataGetWithException
*/
public function testGetNewWithException($id, $exception, $message): void
public function testGetNewWithException(mixed $id, $exception, ?string $message = NULL): void
{
$this->expectException($exception);
if ($message !== NULL)
{
$this->expectExceptionMessage($message);
}
$this->container->getNew($id);
}