Misc fixes

This commit is contained in:
Timothy Warren 2021-02-26 14:42:07 -05:00
parent 50bb525f60
commit d387b793ea
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'; require_once __DIR__ . '/vendor/autoload.php';
Debugger::$strictMode = true; Debugger::$strictMode = true;
Debugger::$showBar = false;
Debugger::enable(Debugger::DEVELOPMENT, __DIR__ . '/app/logs'); Debugger::enable(Debugger::DEVELOPMENT, __DIR__ . '/app/logs');
// Define base directories // 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 * Set the current url in the session as the target of a future redirect
* *
* @codeCoverageIgnore
* @param string|NULL $url * @param string|NULL $url
* @throws ContainerException * @throws ContainerException
* @throws NotFoundException * @throws NotFoundException
@ -166,6 +167,7 @@ class Controller {
* *
* If one is not set, redirect to default url * If one is not set, redirect to default url
* *
* @codeCoverageIgnore
* @throws InvalidArgumentException * @throws InvalidArgumentException
* @return void * @return void
*/ */
@ -179,6 +181,7 @@ class Controller {
/** /**
* Check if the current user is authenticated, else error and exit * Check if the current user is authenticated, else error and exit
* @codeCoverageIgnore
*/ */
protected function checkAuth(): void protected function checkAuth(): void
{ {
@ -195,6 +198,7 @@ class Controller {
/** /**
* Get the string output of a partial template * Get the string output of a partial template
* *
* @codeCoverageIgnore
* @param HtmlView $view * @param HtmlView $view
* @param string $template * @param string $template
* @param array $data * @param array $data
@ -226,6 +230,7 @@ class Controller {
/** /**
* Render a template with header and footer * Render a template with header and footer
* *
* @codeCoverageIgnore
* @param HtmlView $view * @param HtmlView $view
* @param string $template * @param string $template
* @param array $data * @param array $data
@ -256,6 +261,7 @@ class Controller {
/** /**
* 404 action * 404 action
* *
* @codeCoverageIgnore
* @param string $title * @param string $title
* @param string $message * @param string $message
* @throws InvalidArgumentException * @throws InvalidArgumentException
@ -276,6 +282,7 @@ class Controller {
/** /**
* Display a generic error page * Display a generic error page
* *
* @codeCoverageIgnore
* @param int $httpCode * @param int $httpCode
* @param string $title * @param string $title
* @param string $message * @param string $message
@ -295,6 +302,7 @@ class Controller {
/** /**
* Redirect to the default controller/url from an empty path * Redirect to the default controller/url from an empty path
* *
* @codeCoverageIgnore
* @throws InvalidArgumentException * @throws InvalidArgumentException
* @return void * @return void
*/ */
@ -308,6 +316,7 @@ class Controller {
* Set a session flash variable to display a message on * Set a session flash variable to display a message on
* next page load * next page load
* *
* @codeCoverageIgnore
* @param string $message * @param string $message
* @param string $type * @param string $type
* @return void * @return void
@ -343,6 +352,7 @@ class Controller {
/** /**
* Add a message box to the page * Add a message box to the page
* *
* @codeCoverageIgnore
* @param HtmlView $view * @param HtmlView $view
* @param string $type * @param string $type
* @param string $message * @param string $message
@ -360,6 +370,7 @@ class Controller {
/** /**
* Output a template to HTML, using the provided data * Output a template to HTML, using the provided data
* *
* @codeCoverageIgnore
* @param string $template * @param string $template
* @param array $data * @param array $data
* @param HtmlView|NULL $view * @param HtmlView|NULL $view
@ -381,6 +392,7 @@ class Controller {
/** /**
* Output a JSON Response * Output a JSON Response
* *
* @codeCoverageIgnore
* @param mixed $data * @param mixed $data
* @param int $code - the http status code * @param int $code - the http status code
* @throws DoubleRenderException * @throws DoubleRenderException
@ -397,6 +409,7 @@ class Controller {
/** /**
* Redirect to the selected page * Redirect to the selected page
* *
* @codeCoverageIgnore
* @param string $url * @param string $url
* @param int $code * @param int $code
* @return void * @return void

View File

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

View File

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

View File

@ -36,7 +36,7 @@ interface ContainerInterface {
* @throws Exception\ContainerException Error while retrieving the entry. * @throws Exception\ContainerException Error while retrieving the entry.
* @return mixed 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. * 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. * @param string $id Identifier of the entry to look for.
* @return boolean * @return boolean
*/ */
public function has($id): bool; public function has(string $id): bool;
/** /**
* Add a factory to the container * Add a factory to the container
@ -63,7 +63,7 @@ interface ContainerInterface {
* @param mixed $value * @param mixed $value
* @return ContainerInterface * @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 * Get a new instance of the specified item
@ -71,7 +71,7 @@ interface ContainerInterface {
* @param string $id * @param string $id
* @return mixed * @return mixed
*/ */
public function getNew($id); public function getNew(string $id): mixed;
/** /**
* Determine whether a logger channel is registered * Determine whether a logger channel is registered

View File

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

View File

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

View File

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