From c97e5d94db1f62b2ac98b0f7e007da065ad768fe Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Wed, 27 Mar 2024 18:21:27 -0400 Subject: [PATCH] Add tests for documentation examples --- tests/documentation/AutoloadingTest.php | 27 ++ tests/documentation/BasicUsageTest.php | 40 +++ tests/documentation/RoutingTest.php | 264 ++++++++++++++++++ .../app/controllers/MyController.php | 28 ++ .../app/utils/ArrayHelperUtil.php | 13 + .../autoloading-example/public/index.php | 5 + 6 files changed, 377 insertions(+) create mode 100644 tests/documentation/AutoloadingTest.php create mode 100644 tests/documentation/BasicUsageTest.php create mode 100644 tests/documentation/RoutingTest.php create mode 100644 tests/documentation/autoloading-example/app/controllers/MyController.php create mode 100644 tests/documentation/autoloading-example/app/utils/ArrayHelperUtil.php create mode 100644 tests/documentation/autoloading-example/public/index.php diff --git a/tests/documentation/AutoloadingTest.php b/tests/documentation/AutoloadingTest.php new file mode 100644 index 0000000..84bc909 --- /dev/null +++ b/tests/documentation/AutoloadingTest.php @@ -0,0 +1,27 @@ +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([]); + } +} diff --git a/tests/documentation/BasicUsageTest.php b/tests/documentation/BasicUsageTest.php new file mode 100644 index 0000000..83f0d88 --- /dev/null +++ b/tests/documentation/BasicUsageTest.php @@ -0,0 +1,40 @@ +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(); + } +} diff --git a/tests/documentation/RoutingTest.php b/tests/documentation/RoutingTest.php new file mode 100644 index 0000000..90e958b --- /dev/null +++ b/tests/documentation/RoutingTest.php @@ -0,0 +1,264 @@ +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(<<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']]; + } +} diff --git a/tests/documentation/autoloading-example/app/controllers/MyController.php b/tests/documentation/autoloading-example/app/controllers/MyController.php new file mode 100644 index 0000000..15153a1 --- /dev/null +++ b/tests/documentation/autoloading-example/app/controllers/MyController.php @@ -0,0 +1,28 @@ +