Skip to content

Commit

Permalink
Merge pull request #1848 from hydephp/refactor-realtime-compiler-testing
Browse files Browse the repository at this point in the history
Internal: Refactor realtime compiler testing hydephp/develop@0047b70
  • Loading branch information
github-actions committed Jul 14, 2024
1 parent 80211cd commit 0f034b5
Showing 1 changed file with 149 additions and 144 deletions.
293 changes: 149 additions & 144 deletions tests/RealtimeCompilerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

declare(strict_types=1);

use Hyde\Testing\UnitTestCase;
use Desilva\Microserve\JsonResponse;
use Desilva\Microserve\Request;
use Desilva\Microserve\Response;
Expand All @@ -9,197 +12,199 @@
use Hyde\RealtimeCompiler\Http\HtmlResponse;
use Hyde\RealtimeCompiler\Http\HttpKernel;

define('BASE_PATH', realpath(__DIR__.'/../../../'));

if (BASE_PATH === false || ! file_exists(BASE_PATH.'/hyde')) {
throw new InvalidArgumentException('This test suite must be run from the root of the hydephp/develop monorepo.');
}

ob_start();

beforeEach(function () {
putenv('SERVER_LIVE_EDIT=false');
});

it('handles routes index page', function () {
putenv('SERVER_DASHBOARD=false');
mockRoute('');

$kernel = new HttpKernel();
$response = $kernel->handle(new Request());
class RealtimeCompilerTest extends UnitTestCase
{
public static function setUpBeforeClass(): void
{
putenv('SERVER_LIVE_EDIT=false');

expect($response)->toBeInstanceOf(Response::class);
define('BASE_PATH', realpath(__DIR__.'/../../../'));

expect($response->statusCode)->toBe(200);
expect($response->statusMessage)->toBe('OK');
expect($response->body)->toContain('<title>Welcome to HydePHP!</title>');
if (BASE_PATH === false || ! file_exists(BASE_PATH.'/hyde')) {
throw new InvalidArgumentException('This test suite must be run from the root of the hydephp/develop monorepo.');
}
}

expect(hyde()->path('_site/index.html'))->toBeFile()
->and(Filesystem::get('_site/index.html'))->toBe($response->body);
protected function setUp(): void
{
parent::setUp();
ob_start();
}

Filesystem::unlink('_site/index.html');
});
protected function tearDown(): void
{
parent::tearDown();
ob_end_clean();
}

it('handles routes custom pages', function () {
mockRoute('foo');
public function testHandlesRoutesIndexPage()
{
putenv('SERVER_DASHBOARD=false');
$this->mockRoute('');

Filesystem::put('_pages/foo.md', '# Hello World!');
$kernel = new HttpKernel();
$response = $kernel->handle(new Request());

$kernel = new HttpKernel();
$response = $kernel->handle(new Request());
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals(200, $response->statusCode);
$this->assertEquals('OK', $response->statusMessage);
$this->assertStringContainsString('<title>Welcome to HydePHP!</title>', $response->body);

expect($response)->toBeInstanceOf(Response::class)
->and($response->statusCode)->toBe(200)
->and($response->statusMessage)->toBe('OK');
$this->assertFileExists(hyde()->path('_site/index.html'));
$this->assertEquals($response->body, Filesystem::get('_site/index.html'));

expect($response->body)->toContain('<h1>Hello World!</h1>');
Filesystem::unlink('_site/index.html');
}

Filesystem::unlink('_pages/foo.md');
Filesystem::unlink('_site/foo.html');
});
public function testHandlesRoutesCustomPages()
{
$this->mockRoute('foo');

it('handles routes pages with .html extension', function () {
mockRoute('foo.html');
Filesystem::put('_pages/foo.md', '# Hello World!');

Filesystem::put('_pages/foo.md', '# Hello World!');
$kernel = new HttpKernel();
$response = $kernel->handle(new Request());

$kernel = new HttpKernel();
$response = $kernel->handle(new Request());
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals(200, $response->statusCode);
$this->assertEquals('OK', $response->statusMessage);
$this->assertStringContainsString('<h1>Hello World!</h1>', $response->body);

expect($response)->toBeInstanceOf(Response::class)
->and($response->statusCode)->toBe(200)
->and($response->statusMessage)->toBe('OK');
Filesystem::unlink('_pages/foo.md');
Filesystem::unlink('_site/foo.html');
}

expect($response->body)->toContain('<h1>Hello World!</h1>');
public function testHandlesRoutesPagesWithHtmlExtension()
{
$this->mockRoute('foo.html');

Filesystem::unlink('_pages/foo.md');
Filesystem::unlink('_site/foo.html');
});
Filesystem::put('_pages/foo.md', '# Hello World!');

it('handles routes static assets', function () {
mockRoute('media/app.css');
$kernel = new HttpKernel();
$response = $kernel->handle(new Request());

$kernel = new HttpKernel();
$response = $kernel->handle(new Request());
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals(200, $response->statusCode);
$this->assertEquals('OK', $response->statusMessage);
$this->assertStringContainsString('<h1>Hello World!</h1>', $response->body);

expect($response)->toBeInstanceOf(Response::class)
->and($response->statusCode)->toBe(200)
->and($response->statusMessage)->toBe('OK');
Filesystem::unlink('_pages/foo.md');
Filesystem::unlink('_site/foo.html');
}

expect($response->body)->toBe(file_get_contents(\Hyde\Hyde::path('_media/app.css')));
});
public function testHandlesRoutesStaticAssets()
{
$this->mockRoute('media/app.css');

it('handles throws route not found exception for missing route', function () {
mockRoute('missing');
$kernel = new HttpKernel();
$response = $kernel->handle(new Request());

$kernel = new HttpKernel();
$kernel->handle(new Request());
})->throws(RouteNotFoundException::class, 'Route [missing] not found');
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals(200, $response->statusCode);
$this->assertEquals('OK', $response->statusMessage);
$this->assertEquals(file_get_contents(\Hyde\Hyde::path('_media/app.css')), $response->body);
}

it('handles sends 404 error response for missing asset', function () {
mockRoute('missing.css');
public function testThrowsRouteNotFoundExceptionForMissingRoute()
{
$this->mockRoute('missing');

$kernel = new HttpKernel();
$response = $kernel->handle(new Request());
$kernel = new HttpKernel();

expect($response)->toBeInstanceOf(Response::class)
->and($response->statusCode)->toBe(404)
->and($response->statusMessage)->toBe('Not Found');
});
$this->expectException(RouteNotFoundException::class);
$this->expectExceptionMessage('Route [missing] not found');

//test('html responses contain the correct headers', function () {
// mockRoute('foo');
//
// Filesystem::put('_pages/foo.md', '# Hello World!');
//
// $kernel = new HttpKernel();
// $response = $kernel->handle(new Request());
//
// expect($response)->toBeInstanceOf(HtmlResponse::class)
// ->and($response->statusCode)->toBe(200)
// ->and($response->statusMessage)->toBe('OK')
// ->and($response->headers)->toContain('Content-Type', 'text/html')
// ->and($response->headers)->toContain('Content-Length', strlen($response->body));
//
// expect($response->body)->toContain('<h1>Hello World!</h1>');
//
// Filesystem::unlink('_pages/foo.md');
// Filesystem::unlink('_site/foo.html');
//})->skip('Underlying framework does not buffer headers (yet)');
$kernel->handle(new Request());
}

test('trailing slashes are normalized from route', function () {
mockRoute('foo/');
public function testSends404ErrorResponseForMissingAsset()
{
$this->mockRoute('missing.css');

Filesystem::put('_pages/foo.md', '# Hello World!');
$kernel = new HttpKernel();
$response = $kernel->handle(new Request());

$kernel = new HttpKernel();
$response = $kernel->handle(new Request());
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals(404, $response->statusCode);
$this->assertEquals('Not Found', $response->statusMessage);
}

expect($response)->toBeInstanceOf(Response::class)
->and($response->statusCode)->toBe(200)
->and($response->statusMessage)->toBe('OK');
public function testTrailingSlashesAreNormalizedFromRoute()
{
$this->mockRoute('foo/');

expect($response->body)->toContain('<h1>Hello World!</h1>');
Filesystem::put('_pages/foo.md', '# Hello World!');

Filesystem::unlink('_pages/foo.md');
Filesystem::unlink('_site/foo.html');
});
$kernel = new HttpKernel();
$response = $kernel->handle(new Request());

test('docs uri path is rerouted to docs/index', function () {
mockRoute('docs');
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals(200, $response->statusCode);
$this->assertEquals('OK', $response->statusMessage);
$this->assertStringContainsString('<h1>Hello World!</h1>', $response->body);

Filesystem::put('_docs/index.md', '# Hello World!');
Filesystem::unlink('_pages/foo.md');
Filesystem::unlink('_site/foo.html');
}

$kernel = new HttpKernel();
$response = $kernel->handle(new Request());
public function testDocsUriPathIsReroutedToDocsIndex()
{
$this->mockRoute('docs');

expect($response)->toBeInstanceOf(Response::class)
->and($response->statusCode)->toBe(200)
->and($response->statusMessage)->toBe('OK');
Filesystem::put('_docs/index.md', '# Hello World!');

expect($response->body)->toContain('HydePHP Docs');
$kernel = new HttpKernel();
$response = $kernel->handle(new Request());

Filesystem::unlink('_docs/index.md');
Filesystem::unlink('_site/docs/index.html');
});
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals(200, $response->statusCode);
$this->assertEquals('OK', $response->statusMessage);
$this->assertStringContainsString('HydePHP Docs', $response->body);

test('docs/search renders search page', function () {
mockRoute('docs/search');
Filesystem::unlink('_docs/index.md');
Filesystem::unlink('_site/docs/index.html');
}

$kernel = new HttpKernel();
$response = $kernel->handle(new Request());
public function testDocsSearchRendersSearchPage()
{
$this->mockRoute('docs/search');

expect($response)->toBeInstanceOf(HtmlResponse::class)
->and($response->statusCode)->toBe(200)
->and($response->statusMessage)->toBe('OK');
$kernel = new HttpKernel();
$response = $kernel->handle(new Request());

expect($response->body)->toContain('Search the documentation site');
$this->assertInstanceOf(HtmlResponse::class, $response);
$this->assertEquals(200, $response->statusCode);
$this->assertEquals('OK', $response->statusMessage);
$this->assertStringContainsString('Search the documentation site', $response->body);

Filesystem::unlink('_site/docs/search.html');
});
Filesystem::unlink('_site/docs/search.html');
}

test('ping route returns ping response', function () {
mockRoute('ping');
public function testPingRouteReturnsPingResponse()
{
$this->mockRoute('ping');

$kernel = new HttpKernel();
$response = $kernel->handle(new Request());
$kernel = new HttpKernel();
$response = $kernel->handle(new Request());

expect($response)->toBeInstanceOf(JsonResponse::class)
->and($response->statusCode)->toBe(200)
->and($response->statusMessage)->toBe('OK');
});
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals(200, $response->statusCode);
$this->assertEquals('OK', $response->statusMessage);
}

test('exception handling', function () {
$exception = new Exception('foo');
$response = ExceptionHandler::handle($exception);
public function testExceptionHandling()
{
$exception = new Exception('foo');
$response = ExceptionHandler::handle($exception);

expect($response)->toBeInstanceOf(Response::class)
->and($response->statusCode)->toBe(500)
->and($response->statusMessage)->toBe('Internal Server Error');
});
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals(500, $response->statusCode);
$this->assertEquals('Internal Server Error', $response->statusMessage);
}

function mockRoute(string $route, $method = 'GET'): void
{
$_SERVER['REQUEST_METHOD'] = $method;
$_SERVER['REQUEST_URI'] = "/$route";
protected function mockRoute(string $route, $method = 'GET'): void
{
$_SERVER['REQUEST_METHOD'] = $method;
$_SERVER['REQUEST_URI'] = "/$route";
}
}

0 comments on commit 0f034b5

Please sign in to comment.