Skip to content

Commit

Permalink
Add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldiron committed Aug 6, 2023
1 parent 893ffb7 commit 4447ecf
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ public function testCanGetResources(): void
->param('x', 'x-def', new Text(200), 'x param', true)
->param('y', 'y-def', new Text(200), 'y param', true)
->action(function ($x, $y, $rand) {
echo $x.'-'.$y.'-'.$rand;
echo $x . '-' . $y . '-' . $rand;
});

\ob_start();
$this->app->execute($route, new Request(), new Response());
$result = \ob_get_contents();
\ob_end_clean();

$this->assertEquals('x-def-y-def-'.$resource, $result);
$this->assertEquals('x-def-y-def-' . $resource, $result);
}

public function testCanExecuteRoute(): void
Expand All @@ -123,7 +123,7 @@ public function testCanExecuteRoute(): void
->error()
->inject('error')
->action(function ($error) {
echo 'error: '.$error->getMessage();
echo 'error: ' . $error->getMessage();
});

// Default Params
Expand All @@ -133,7 +133,7 @@ public function testCanExecuteRoute(): void
->param('x', 'x-def', new Text(200), 'x param', true)
->param('y', 'y-def', new Text(200), 'y param', true)
->action(function ($x, $y) {
echo $x.'-'.$y;
echo $x . '-' . $y;
});

\ob_start();
Expand All @@ -150,12 +150,12 @@ public function testCanExecuteRoute(): void
->param('y', 'y-def', new Text(200), 'y param', true)
->inject('rand')
->param('z', 'z-def', function ($rand) {
echo $rand.'-';
echo $rand . '-';

return new Text(200);
}, 'z param', true, ['rand'])
->action(function ($x, $y, $z, $rand) {
echo $x.'-', $y;
echo $x . '-', $y;
});

\ob_start();
Expand All @@ -165,7 +165,7 @@ public function testCanExecuteRoute(): void
$result = \ob_get_contents();
\ob_end_clean();

$this->assertEquals($resource.'-param-x-param-y', $result);
$this->assertEquals($resource . '-param-x-param-y', $result);

// With Error

Expand All @@ -175,7 +175,7 @@ public function testCanExecuteRoute(): void
->param('x', 'x-def', new Text(1, min: 0), 'x param', false)
->param('y', 'y-def', new Text(1, min: 0), 'y param', false)
->action(function ($x, $y) {
echo $x.'-', $y;
echo $x . '-', $y;
});

\ob_start();
Expand All @@ -193,7 +193,7 @@ public function testCanExecuteRoute(): void
->init()
->inject('rand')
->action(function ($rand) {
echo 'init-'.$rand.'-';
echo 'init-' . $rand . '-';
});

$this->app
Expand Down Expand Up @@ -237,7 +237,7 @@ public function testCanExecuteRoute(): void
->param('x', 'x-def', new Text(200), 'x param', false)
->param('y', 'y-def', new Text(200), 'y param', false)
->action(function ($x, $y) {
echo $x.'-', $y;
echo $x . '-', $y;
});

$homepage = new Route('GET', '/path');
Expand All @@ -247,7 +247,7 @@ public function testCanExecuteRoute(): void
->param('x', 'x-def', new Text(200), 'x param', false)
->param('y', 'y-def', new Text(200), 'y param', false)
->action(function ($x, $y) {
echo $x.'*', $y;
echo $x . '*', $y;
});

\ob_start();
Expand All @@ -257,7 +257,7 @@ public function testCanExecuteRoute(): void
$result = \ob_get_contents();
\ob_end_clean();

$this->assertEquals('init-'.$resource.'-(init-api)-param-x-param-y-(shutdown-api)-shutdown', $result);
$this->assertEquals('init-' . $resource . '-(init-api)-param-x-param-y-(shutdown-api)-shutdown', $result);

\ob_start();
$request = new UtopiaRequestTest();
Expand All @@ -266,7 +266,7 @@ public function testCanExecuteRoute(): void
$result = \ob_get_contents();
\ob_end_clean();

$this->assertEquals('init-'.$resource.'-(init-homepage)-param-x*param-y-(shutdown-homepage)-shutdown', $result);
$this->assertEquals('init-' . $resource . '-(init-homepage)-param-x*param-y-(shutdown-homepage)-shutdown', $result);
}

public function testCanAddAndExecuteHooks()
Expand Down Expand Up @@ -321,14 +321,14 @@ public function testCanHookThrowExceptions()
->init()
->param('y', '', new Text(5), 'y param', false)
->action(function ($y) {
echo '(init)-'.$y.'-';
echo '(init)-' . $y . '-';
});

$this->app
->error()
->inject('error')
->action(function ($error) {
echo 'error-'.$error->getMessage();
echo 'error-' . $error->getMessage();
});

$this->app
Expand Down Expand Up @@ -517,9 +517,15 @@ public function testWildcardRoute(): void
$_SERVER['REQUEST_URI'] = '/unknown_path';

App::init()
->action(function () {
->inject('request')
->inject('response')
->action(function (Request $request, Response $response) {
$route = $this->app->getRoute();
App::setResource('myRoute', fn () => $route);

if ($request->getURI() === '/init_response') {
$response->send('THIS IS RESPONSE FROM INIT!');
}
});

App::options()
Expand All @@ -541,7 +547,7 @@ public function testWildcardRoute(): void
->inject('myRoute')
->inject('response')
->action(function (mixed $myRoute, $response) {
if($myRoute == null) {
if ($myRoute == null) {
$response->send('ROUTE IS NULL!');
} else {
$response->send('HELLO');
Expand All @@ -564,8 +570,17 @@ public function testWildcardRoute(): void

$this->assertEquals('', $result);

$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/init_response';

\ob_start();
@$this->app->run(new Request(), new Response());
$result = \ob_get_contents();
\ob_end_clean();

$this->assertEquals('THIS IS RESPONSE FROM INIT!', $result);

$_SERVER['REQUEST_METHOD'] = $method;
$_SERVER['REQUEST_URI'] = $uri;

}
}

0 comments on commit 4447ecf

Please sign in to comment.