request header shortcut and lots of unit test fixes for phpcs

pull/533/head
n0nag0n 1 year ago
parent 84877b34f4
commit 537ea032d8

@ -61,6 +61,7 @@ use flight\net\Route;
* @method void etag($id, string $type = 'strong') Handles ETag HTTP caching.
* @method void lastModified(int $time) Handles last modified HTTP caching.
*/
// phpcs:ignoreFile Generic.Files.LineLength.TooLong, PSR2.Methods.MethodDeclaration.Underscore
class Engine
{
/**

@ -58,7 +58,8 @@ use flight\net\Route;
* @method static void etag($id, $type = 'strong') Performs ETag HTTP caching.
* @method static void lastModified($time) Performs last modified HTTP caching.
*/
class Flight
// phpcs:ignoreFile Generic.Files.LineLength.TooLong, PSR1.Classes.ClassDeclaration.MissingNamespace
class Flight
{
/**
* Framework engine.

@ -302,6 +302,19 @@ class Request
return $_SERVER[$var] ?? $default;
}
/**
* This will pull a header from the request.
*
* @param string $header Header name. Can be caps, lowercase, or mixed.
* @param string $default Default value if the header does not exist
* @return string
*/
public static function getHeader(string $header, $default = ''): string
{
$header = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
return self::getVar($header, $default);
}
/**
* Parse query parameters from a URL.
*

@ -312,15 +312,15 @@ class Response
\strlen($this->body);
}
/**
* Gets the response body
*
* @return string
*/
public function getBody(): string
{
return $this->body;
}
/**
* Gets the response body
*
* @return string
*/
public function getBody(): string
{
return $this->body;
}
/**
* Gets whether response body was sent.

@ -1,5 +1,6 @@
<?php
// This file is only here so that the PHP8 attribute for doesn't throw an error in files
// phpcs:ignoreFile PSR1.Classes.ClassDeclaration.MissingNamespace
class ReturnTypeWillChange
{
}

@ -7,9 +7,12 @@
* @license MIT, http://flightphp.com/license
*/
namespace tests;
use flight\Engine;
use tests\classes\User;
class AutoloadTest extends PHPUnit\Framework\TestCase
class AutoloadTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Engine
@ -25,7 +28,7 @@ class AutoloadTest extends PHPUnit\Framework\TestCase
// Autoload a class
public function testAutoload()
{
$this->app->register('user', 'User');
$this->app->register('user', User::class);
$loaders = spl_autoload_functions();

@ -6,7 +6,10 @@
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license MIT, http://flightphp.com/license
*/
class CollectionTest extends PHPUnit\Framework\TestCase
namespace tests;
class CollectionTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \flight\util\Collection

@ -7,9 +7,13 @@
* @license MIT, http://flightphp.com/license
*/
namespace tests;
use Exception;
use flight\core\Dispatcher;
use tests\classes\Hello;
class DispatcherTest extends PHPUnit\Framework\TestCase
class DispatcherTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Dispatcher|null
@ -95,7 +99,7 @@ class DispatcherTest extends PHPUnit\Framework\TestCase
// Map a static function
public function testStaticFunctionMapping()
{
$this->dispatcher->set('map2', 'Hello::sayHi');
$this->dispatcher->set('map2', 'tests\classes\Hello::sayHi');
$result = $this->dispatcher->run('map2');
@ -117,7 +121,7 @@ class DispatcherTest extends PHPUnit\Framework\TestCase
// Map a static class method
public function testStaticClassMethodMapping()
{
$this->dispatcher->set('map4', ['Hello', 'sayBye']);
$this->dispatcher->set('map4', ['\tests\classes\Hello', 'sayBye']);
$result = $this->dispatcher->run('map4');

@ -6,11 +6,14 @@
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license MIT, http://flightphp.com/license
*/
namespace tests;
use Exception;
use flight\Engine;
use flight\net\Response;
use PHPUnit\Framework\TestCase;
// phpcs:ignoreFile PSR2.Methods.MethodDeclaration.Underscore
class EngineTest extends TestCase
{
public function setUp(): void

@ -7,9 +7,11 @@
* @license MIT, http://flightphp.com/license
*/
namespace tests;
use flight\Engine;
class FilterTest extends PHPUnit\Framework\TestCase
class FilterTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Engine

@ -1,18 +1,24 @@
<?php
use flight\Engine;
use flight\net\Request;
use flight\net\Response;
use flight\net\Router;
use flight\template\View;
/**
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license MIT, http://flightphp.com/license
*/
class FlightTest extends PHPUnit\Framework\TestCase
namespace tests;
use Exception;
use Flight;
use flight\Engine;
use flight\net\Request;
use flight\net\Response;
use flight\net\Router;
use flight\template\View;
use tests\classes\User;
class FlightTest extends \PHPUnit\Framework\TestCase
{
protected function setUp(): void
{
@ -70,7 +76,7 @@ class FlightTest extends PHPUnit\Framework\TestCase
{
Flight::path(__DIR__ . '/classes');
Flight::register('user', 'User');
Flight::register('user', User::class);
$user = Flight::user();
$loaders = spl_autoload_functions();
@ -79,11 +85,11 @@ class FlightTest extends PHPUnit\Framework\TestCase
self::assertIsObject($user);
self::assertInstanceOf(User::class, $user);
Flight::unregister('user');
Flight::unregister('user');
self::expectException(Exception::class);
self::expectExceptionMessage('user must be a mapped method.');
$user = Flight::user();
self::expectException(Exception::class);
self::expectExceptionMessage('user must be a mapped method.');
$user = Flight::user();
}
// Map a function

@ -7,8 +7,13 @@
* @license MIT, http://flightphp.com/license
*/
namespace tests;
use flight\core\Loader;
use tests\classes\Factory;
use tests\classes\User;
use PHPUnit\Framework\TestCase;
use tests\classes\TesterClass;
class LoaderTest extends TestCase
{
@ -23,7 +28,7 @@ class LoaderTest extends TestCase
// Autoload a class
public function testAutoload()
{
$this->loader->register('tests', 'User');
$this->loader->register('tests', User::class);
$test = $this->loader->load('tests');
@ -34,7 +39,7 @@ class LoaderTest extends TestCase
// Register a class
public function testRegister()
{
$this->loader->register('a', 'User');
$this->loader->register('a', User::class);
$user = $this->loader->load('a');
@ -46,7 +51,7 @@ class LoaderTest extends TestCase
// Register a class with constructor parameters
public function testRegisterWithConstructor()
{
$this->loader->register('b', 'User', ['Bob']);
$this->loader->register('b', User::class, ['Bob']);
$user = $this->loader->load('b');
@ -58,7 +63,7 @@ class LoaderTest extends TestCase
// Register a class with initialization
public function testRegisterWithInitialization()
{
$this->loader->register('c', 'User', ['Bob'], function ($user) {
$this->loader->register('c', User::class, ['Bob'], function ($user) {
$user->name = 'Fred';
});
@ -72,7 +77,7 @@ class LoaderTest extends TestCase
// Get a non-shared instance of a class
public function testSharedInstance()
{
$this->loader->register('d', 'User');
$this->loader->register('d', User::class);
$user1 = $this->loader->load('d');
$user2 = $this->loader->load('d');
@ -85,7 +90,7 @@ class LoaderTest extends TestCase
// Gets an object from a factory method
public function testRegisterUsingCallable()
{
$this->loader->register('e', ['Factory', 'create']);
$this->loader->register('e', ['\tests\classes\Factory', 'create']);
$obj = $this->loader->load('e');
@ -119,9 +124,9 @@ class LoaderTest extends TestCase
public function testUnregisterClass()
{
$this->loader->register('g', 'User');
$this->loader->register('g', User::class);
$current_class = $this->loader->get('g');
$this->assertEquals([ 'User', [], null ], $current_class);
$this->assertEquals([ User::class, [], null ], $current_class);
$this->loader->unregister('g');
$unregistered_class_result = $this->loader->get('g');
$this->assertNull($unregistered_class_result);
@ -129,7 +134,7 @@ class LoaderTest extends TestCase
public function testNewInstance6Params()
{
$TesterClass = $this->loader->newInstance('TesterClass', ['Bob','Fred', 'Joe', 'Jane', 'Sally', 'Suzie']);
$TesterClass = $this->loader->newInstance(TesterClass::class, ['Bob','Fred', 'Joe', 'Jane', 'Sally', 'Suzie']);
$this->assertEquals('Bob', $TesterClass->param1);
$this->assertEquals('Fred', $TesterClass->param2);
$this->assertEquals('Joe', $TesterClass->param3);

@ -7,7 +7,11 @@
* @license MIT, http://flightphp.com/license
*/
namespace tests;
use Exception;
use flight\Engine;
use tests\classes\Hello;
use PHPUnit\Framework\TestCase;
class MapTest extends TestCase
@ -58,7 +62,7 @@ class MapTest extends TestCase
// Map a static class method
public function testStaticClassMethodMapping()
{
$this->app->map('map4', ['Hello', 'sayBye']);
$this->app->map('map4', [Hello::class, 'sayBye']);
$result = $this->app->map4();

@ -1,7 +1,5 @@
<?php
use flight\database\PdoWrapper;
/**
* Flight: An extensible micro-framework.
*
@ -9,9 +7,12 @@ use flight\database\PdoWrapper;
* @license MIT, http://flightphp.com/license
*/
namespace tests;
use flight\database\PdoWrapper;
use PDOStatement;
class PdoWrapperTest extends PHPUnit\Framework\TestCase
class PdoWrapperTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Pdo_Wrapper

@ -7,9 +7,11 @@
* @license MIT, http://flightphp.com/license
*/
namespace tests;
use flight\Engine;
class RedirectTest extends PHPUnit\Framework\TestCase
class RedirectTest extends \PHPUnit\Framework\TestCase
{
private Engine $app;

@ -7,7 +7,10 @@
* @license MIT, http://flightphp.com/license
*/
namespace tests;
use flight\Engine;
use tests\classes\User;
use PHPUnit\Framework\TestCase;
class RegisterTest extends TestCase
@ -22,7 +25,7 @@ class RegisterTest extends TestCase
// Register a class
public function testRegister()
{
$this->app->register('reg1', 'User');
$this->app->register('reg1', User::class);
$user = $this->app->reg1();
@ -34,7 +37,7 @@ class RegisterTest extends TestCase
// Register a class with constructor parameters
public function testRegisterWithConstructor()
{
$this->app->register('reg2', 'User', ['Bob']);
$this->app->register('reg2', User::class, ['Bob']);
$user = $this->app->reg2();
@ -46,7 +49,7 @@ class RegisterTest extends TestCase
// Register a class with initialization
public function testRegisterWithInitialization()
{
$this->app->register('reg3', 'User', ['Bob'], function ($user) {
$this->app->register('reg3', User::class, ['Bob'], function ($user) {
$user->name = 'Fred';
});
@ -60,7 +63,7 @@ class RegisterTest extends TestCase
// Get a non-shared instance of a class
public function testSharedInstance()
{
$this->app->register('reg4', 'User');
$this->app->register('reg4', User::class);
$user1 = $this->app->reg4();
$user2 = $this->app->reg4();
@ -73,7 +76,7 @@ class RegisterTest extends TestCase
// Map method takes precedence over register
public function testMapOverridesRegister()
{
$this->app->register('reg5', 'User');
$this->app->register('reg5', User::class);
$user = $this->app->reg5();

@ -7,9 +7,11 @@
* @license MIT, http://flightphp.com/license
*/
namespace tests;
use flight\Engine;
class RenderTest extends PHPUnit\Framework\TestCase
class RenderTest extends \PHPUnit\Framework\TestCase
{
private Engine $app;

@ -7,10 +7,12 @@
* @license MIT, http://flightphp.com/license
*/
namespace tests;
use flight\net\Request;
use flight\util\Collection;
class RequestTest extends PHPUnit\Framework\TestCase
class RequestTest extends \PHPUnit\Framework\TestCase
{
private Request $request;
@ -198,4 +200,18 @@ class RequestTest extends PHPUnit\Framework\TestCase
$this->assertEquals([ 'foo' => 'bar' ], $request->data->getData());
$this->assertEquals('{"foo":"bar"}', $request->getBody());
}
public function testGetHeader()
{
$_SERVER['HTTP_X_CUSTOM_HEADER'] = 'custom header value';
$request = new Request();
$this->assertEquals('custom header value', $request->getHeader('X-Custom-Header'));
// or the headers that are already in $_SERVER
$this->assertEquals('XMLHttpRequest', $request->getHeader('X-REqUesTed-WiTH'));
$this->assertEquals('32.32.32.32', $request->getHeader('X-Forwarded-For'));
// default values
$this->assertEquals('default value', $request->getHeader('X-Non-Existent-Header', 'default value'));
}
}

@ -7,11 +7,12 @@
* @license MIT, http://flightphp.com/license
*/
use flight\net\Request;
namespace tests;
use Exception;
use flight\net\Response;
use flight\util\Collection;
class ResponseTest extends PHPUnit\Framework\TestCase
class ResponseTest extends \PHPUnit\Framework\TestCase
{
protected function setUp(): void
{

@ -7,11 +7,13 @@
* @license MIT, http://flightphp.com/license
*/
namespace tests;
use flight\core\Dispatcher;
use flight\net\Request;
use flight\net\Router;
class RouterTest extends PHPUnit\Framework\TestCase
class RouterTest extends \PHPUnit\Framework\TestCase
{
private Router $router;

@ -6,7 +6,10 @@
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license MIT, http://flightphp.com/license
*/
class VariableTest extends PHPUnit\Framework\TestCase
namespace tests;
class VariableTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \flight\Engine

@ -1,5 +1,8 @@
<?php
namespace tests;
use Exception;
use flight\template\View;
use PHPUnit\Framework\TestCase;

@ -1,5 +1,7 @@
<?php
namespace tests\classes;
class Factory
{
// Cannot be instantiated

@ -1,5 +1,7 @@
<?php
namespace tests\classes;
class Hello
{
public function sayHi()

@ -1,5 +1,7 @@
<?php
namespace tests\classes;
class TesterClass
{
public $param1;

@ -1,5 +1,7 @@
<?php
namespace tests\classes;
class User
{
public $name;

Loading…
Cancel
Save