Skip to content

Commit

Permalink
test: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Aug 13, 2024
1 parent 4eb459e commit 006bee3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 41 deletions.
18 changes: 0 additions & 18 deletions tests/app.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
app()->config('test', false);
app()->config('mode', 'TEST');

app()->set404(function () {
});

app()->script('TEST', function () {
app()->config('test', true);
});
Expand Down Expand Up @@ -65,21 +62,6 @@
app()->config('app.down', false);
});

test('swap out leaf response', function () {
class TestResponse extends \Leaf\Http\Response
{
public function customMethod()
{
return 'This is some test response';
}
}

$leafInstance1 = new \Leaf\App();
$leafInstance1->setResponseClass(TestResponse::class);

expect($leafInstance1->response->customMethod())->toBe((new TestResponse())->customMethod());
});

test('get route info', function () {
$routePath = '/getRouteInfo';

Expand Down
46 changes: 23 additions & 23 deletions tests/middleware.test.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
<?php

class StaticTestClassMid
{
public static $called = false;
}

afterEach(function () {
StaticTestClassMid::$called = false;
});

test('leaf middleware', function () {
app()->config('anotherKey', false);

class AppMid extends \Leaf\Middleware
{
public function call()
{
app()->config('anotherKey', true);
StaticTestClassMid::$called = true;
$this->next();
}
}
Expand All @@ -16,46 +24,42 @@ public function call()
$_SERVER['REQUEST_URI'] = '/';

app()->use(new AppMid());
app()->get('/', function () {
});
app()->get('/', function () {});
app()->run();

expect(app()->config('anotherKey'))->toBe(true);
expect(StaticTestClassMid::$called)->toBe(true);
});

test('in-route middleware', function () {
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/';

$app = new Leaf\App();
$app->config('useMiddleware', false);

$m = function () use ($app) {
$app->config('useMiddleware', true);
StaticTestClassMid::$called = true;
};

$app->get('/', ['middleware' => $m, function () {
}]);
$app->get('/', ['middleware' => $m, function () {}]);
$app->run();

expect($app->config('useMiddleware'))->toBe(true);
expect(StaticTestClassMid::$called)->toBe(true);
});

test('in-route named middleware', function () {
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/';

$app = new Leaf\App();
$app->config('useNamedMiddleware', false);

$app->registerMiddleware('mid1', function () use ($app) {
$app->config('useNamedMiddleware', true);
StaticTestClassMid::$called = true;
});

$app->get('/', ['middleware' => 'mid1', function () {
}]);
$app->get('/', ['middleware' => 'mid1', function () {}]);
$app->run();

expect($app->config('useNamedMiddleware'))->toBe(true);
expect(StaticTestClassMid::$called)->toBe(true);
});

test('in-route middleware + group', function () {
Expand All @@ -70,8 +74,7 @@ public function call()
};

$app->group('/group-test', function () use ($app, $m) {
$app->get('/', ['middleware' => $m, function () {
}]);
$app->get('/', ['middleware' => $m, function () {}]);
});

$app->run();
Expand All @@ -90,8 +93,7 @@ public function call()
});

$app->group('/groups', function () use ($app) {
$app->get('/test', ['middleware' => 'mid2', function () {
}]);
$app->get('/test', ['middleware' => 'mid2', function () {}]);
});

$app->run();
Expand All @@ -109,8 +111,7 @@ public function call()
$app->before('GET', '/', function () use ($app) {
$app->config('inTest', 'false');
});
$app->get('/', function () {
});
$app->get('/', function () {});
$app->run();

expect($app->config('inTest'))->toBe('false');
Expand All @@ -127,8 +128,7 @@ public function call()
$app->before('GET', '/.*', function () use ($app) {
$app->config('inTest2', 'false');
});
$app->get('/test', function () {
});
$app->get('/test', function () {});
$app->run();

expect($app->config('inTest2'))->toBe('false');
Expand Down

0 comments on commit 006bee3

Please sign in to comment.