Add tests for documentation examples

documentation-tests
fadrian06 10 months ago
parent 4fe54ea6de
commit c97e5d94db

@ -0,0 +1,27 @@
<?php
use app\utils\ArrayHelperUtil;
use PHPUnit\Framework\TestCase;
class AutoloadingTest extends TestCase
{
public function testBasicExample(): void
{
require __DIR__ . '/autoloading-example/public/index.php';
$this->assertTrue(class_exists('MyController'));
$this->expectOutputString('Doing something');
(new MyController)->index();
}
public function testNamespaces(): void {
Flight::path(__DIR__ . '/autoloading-example');
$this->assertTrue(class_exists('app\utils\ArrayHelperUtil'));
$this->assertTrue(class_exists('app\controllers\MyController'));
$this->expectOutputString('Doing something');
(new ArrayHelperUtil)->changeArrayCase([]);
}
}

@ -0,0 +1,40 @@
<?php
use PHPUnit\Framework\TestCase;
class BasicUsageTest extends TestCase
{
protected function setUp(): void
{
Flight::init();
}
protected function tearDown(): void
{
Flight::clear();
}
public function testReadmeBasicUsage(): void
{
$this->expectOutputString('Hello, World!');
Flight::route('/', function (): void {
echo 'Hello, World!';
});
Flight::start();
}
public function testQuickStart(): void
{
$this->expectOutputString('{"Hello":"World!"}');
Flight::route('/api', function (): void {
Flight::json(['Hello' => 'World!']);
});
Flight::request()->url = '/api';
Flight::start();
}
}

@ -0,0 +1,264 @@
<?php
use Dice\Dice;
use flight\core\Dispatcher;
use flight\database\PdoWrapper;
use flight\net\Route;
use PHPUnit\Framework\TestCase;
class Greeting
{
public static function hello(): void
{
echo 'Hello, World from static method!';
}
}
class Greeting2
{
private string $name;
public function __construct()
{
$this->name = 'John Doe';
}
public function hello(): void
{
echo "Hello, {$this->name}!";
}
}
class Greeting3
{
private PdoWrapper $pdo;
public function __construct(PdoWrapper $pdo)
{
$this->pdo = $pdo;
$this->pdo->query(<<<SQL
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(20) NOT NULL UNIQUE
)
SQL);
$this->pdo->exec('DELETE FROM users');
$this->pdo->exec("INSERT INTO users (id, name) VALUES (1, 'John'), (2, 'Mike')");
}
public function hello(int $id): void
{
$name = $this->pdo->fetchField("SELECT name FROM users WHERE id = ?", [$id]);
echo "Hello, world! My name is $name!";
}
}
class RoutingTest extends TestCase
{
protected function setUp(): void
{
Flight::init();
}
protected function tearDown(): void
{
Flight::clear();
Flight::registerContainerHandler(Dispatcher::class);
}
public function testRouting(): void
{
Flight::route('/', function (): void {
echo 'Hello, World from callable!';
});
Flight::route('/', function (): void {
echo 'Hello, World from callable!!';
});
$this->expectOutputString('Hello, World from callable!');
Flight::start();
}
public function testCallbacksAndFunctions(): void
{
function hello(): void
{
echo 'Hello, World from function!';
}
Flight::route('/', 'hello');
$this->expectOutputString('Hello, World from function!');
Flight::start();
}
public function testStaticMethods(): void
{
Flight::route('/', ['Greeting', 'hello']);
$this->expectOutputString('Hello, World from static method!');
Flight::start();
}
public function testInstanceMethod(): void
{
$greeting = new Greeting2();
Flight::route('/', [$greeting, 'hello']);
$this->expectOutputString('Hello, John Doe!');
Flight::start();
}
public function testInstanceMethodLikeStaticMethod(): void
{
Flight::route('/', ['Greeting2', 'hello']);
$this->expectOutputString('Hello, John Doe!');
Flight::start();
}
public function testDependencyInjection(): void
{
// Setup the container with whatever params you need
// See the Dependency Injection page for more information on PSR-11
$dice = new Dice();
// Don't forget to reassign the variable with '$dice = '!!!!!
$dice = $dice->addRule(PdoWrapper::class, [
'shared' => true,
'constructParams' => ['sqlite::memory:']
]);
// Register the container handler
Flight::registerContainerHandler(function (string $class, array $params) use ($dice) {
return $dice->create($class, $params);
});
$cases = [
// Routes like normal
['Greeting3', 'hello'],
// or
'Greeting3->hello',
// or
'Greeting3::hello'
];
foreach ($cases as $case) {
Flight::route('/hello/@id', $case);
Flight::request()->url = '/hello/1';
$this->expectOutputString('Hello, world! My name is John!');
Flight::start();
}
}
/** @dataProvider methodsDataProvider */
public function testMethodRouting(string $method): void
{
Flight::route("$method /", function () use ($method): void {
echo "I received a $method request";
});
Flight::request()->method = $method;
$this->expectOutputString("I received a $method request");
Flight::start();
}
/** @dataProvider methodsDataProvider */
public function testMethodsRoutingShorthands(string $method): void
{
if ($method === 'GET') {
$this->assertTrue(true);
return;
}
$lowerCaseMethod = strtolower($method);
Flight::{$lowerCaseMethod}('/', function () use ($method): void {
echo "I received a $method request.";
});
Flight::request()->method = $method;
$this->expectOutputString("I received a $method request.");
Flight::start();
}
/** @dataProvider methodsDataProvider */
public function testMultipleMethodsRouting(string $method): void
{
$methods = join('|', array_map(function (array $caseParams): string {
return $caseParams[0];
}, self::methodsDataProvider()));
Flight::route("$methods /", function () use ($method): void {
echo "I received a $method request.";
});
Flight::request()->method = $method;
$this->expectOutputString("I received a $method request.");
Flight::start();
}
/** @dataProvider methodsDataProvider */
public function testRouterObject(string $method): void {
$router = Flight::router();
$router->map('/', function () use ($method): void {
echo "I received a $method request.";
});
Flight::request()->method = $method;
$this->expectOutputString("I received a $method request.");
Flight::start();
}
/** @dataProvider methodsDataProvider */
public function testRouterObjectWithShorthands(string $method): void
{
if ($method === 'GET') {
$this->assertTrue(true);
return;
}
$lowerCaseMethod = strtolower($method);
$router = Flight::router();
$router->{$lowerCaseMethod}('/', function () use ($method): void {
echo "I received a $method request.";
});
Flight::request()->method = $method;
$this->expectOutputString("I received a $method request.");
Flight::start();
}
public static function methodsDataProvider(): array
{
return [['GET'], ['POST'], ['PUT'], ['PATCH'], ['DELETE']];
}
}

@ -0,0 +1,28 @@
<?php
namespace {
// All autoloaded classes are recommended to be Pascal Case (each word capitalized, no spaces)
// It is a requirement that you cannot have an underscore in your class name
class MyController
{
public function index()
{
echo 'Doing something';
}
}
}
// namespaces are required
// namespaces are the same as the directory structure
// namespaces must follow the same case as the directory structure
// namespaces and directories cannot have any underscores
namespace app\controllers {
// All autoloaded classes are recommended to be Pascal Case (each word capitalized, no spaces)
// It is a requirement that you cannot have an underscore in your class name
class MyController
{
public function index() {
echo __CLASS__ . ' is doing something';
}
}
}

@ -0,0 +1,13 @@
<?php
// namespace must match the directory structure and case (note the UTILS directory is all caps
// like in the file tree above)
namespace app\utils;
class ArrayHelperUtil
{
public function changeArrayCase(array $array)
{
echo 'Doing something';
}
}

@ -0,0 +1,5 @@
<?php
// Add a path to the autoloader
Flight::path(__DIR__ . '/../app/controllers/');
Flight::path(__DIR__ . '/../app/utils/');
Loading…
Cancel
Save