Passed
Push — master ( b7dddf...303190 )
by Caen
03:32 queued 13s
created

mockRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
use Desilva\Microserve\JsonResponse;
4
use Desilva\Microserve\Request;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Request. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
use Desilva\Microserve\Response;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Response. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Hyde\Facades\Filesystem;
7
use Hyde\Framework\Exceptions\RouteNotFoundException;
8
use Hyde\RealtimeCompiler\Http\ExceptionHandler;
9
use Hyde\RealtimeCompiler\Http\HtmlResponse;
10
use Hyde\RealtimeCompiler\Http\HttpKernel;
11
use Illuminate\Support\Facades\Blade;
12
13
define('BASE_PATH', realpath(__DIR__.'/../../../'));
14
15
if (BASE_PATH === false || ! file_exists(BASE_PATH.'/hyde')) {
16
    throw new InvalidArgumentException('This test suite must be run from the root of the hydephp/develop monorepo.');
17
}
18
19
ob_start();
20
21
test('handle routes index page', function () {
22
    putenv('SERVER_DASHBOARD=false');
23
    mockRoute('');
24
25
    $kernel = new HttpKernel();
26
    $response = $kernel->handle(new Request());
27
28
    expect($response)->toBeInstanceOf(Response::class);
29
30
    expect($response->statusCode)->toBe(200);
31
    expect($response->statusMessage)->toBe('OK');
32
    expect($response->body)->toContain('<title>Welcome to HydePHP!</title>');
33
34
    expect(hyde()->path('_site/index.html'))->toBeFile()
35
        ->and(Filesystem::get('_site/index.html'))->toBe($response->body);
0 ignored issues
show
Bug introduced by
Hyde\Facades\Filesystem::get('_site/index.html') of type string is incompatible with the type Pest\TValue expected by parameter $value of Pest\Expectation::and(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        ->and(/** @scrutinizer ignore-type */ Filesystem::get('_site/index.html'))->toBe($response->body);
Loading history...
36
37
    Filesystem::unlink('_site/index.html');
38
});
39
40
test('handle routes custom pages', function () {
41
    mockRoute('foo');
42
43
    Filesystem::put('_pages/foo.md', '# Hello World!');
44
45
    $kernel = new HttpKernel();
46
    $response = $kernel->handle(new Request());
47
48
    expect($response)->toBeInstanceOf(Response::class)
49
        ->and($response->statusCode)->toBe(200)
50
        ->and($response->statusMessage)->toBe('OK');
51
52
    expect($response->body)->toContain('<h1>Hello World!</h1>');
53
54
    Filesystem::unlink('_pages/foo.md');
55
    Filesystem::unlink('_site/foo.html');
56
});
57
58
test('handle routes pages with .html extension', function () {
59
    mockRoute('foo.html');
60
61
    Filesystem::put('_pages/foo.md', '# Hello World!');
62
63
    $kernel = new HttpKernel();
64
    $response = $kernel->handle(new Request());
65
66
    expect($response)->toBeInstanceOf(Response::class)
67
        ->and($response->statusCode)->toBe(200)
68
        ->and($response->statusMessage)->toBe('OK');
69
70
    expect($response->body)->toContain('<h1>Hello World!</h1>');
71
72
    Filesystem::unlink('_pages/foo.md');
73
    Filesystem::unlink('_site/foo.html');
74
});
75
76
test('handle routes static assets', function () {
77
    mockRoute('media/app.css');
78
79
    $kernel = new HttpKernel();
80
    $response = $kernel->handle(new Request());
81
82
    expect($response)->toBeInstanceOf(Response::class)
83
        ->and($response->statusCode)->toBe(200)
84
        ->and($response->statusMessage)->toBe('OK');
85
86
    expect($response->body)->toContain('/*! HydeFront v2.0.0');
87
});
88
89
test('handle throws route not found exception for missing route', function () {
90
    mockRoute('missing');
91
92
    $kernel = new HttpKernel();
93
    $kernel->handle(new Request());
94
})->throws(RouteNotFoundException::class, "Route not found: 'missing'");
95
96
test('handle sends 404 error response for missing asset', function () {
97
    mockRoute('missing.css');
98
99
    $kernel = new HttpKernel();
100
    $response = $kernel->handle(new Request());
101
102
    expect($response)->toBeInstanceOf(Response::class)
103
        ->and($response->statusCode)->toBe(404)
104
        ->and($response->statusMessage)->toBe('Not Found');
105
});
106
107
test('docs uri path is rerouted to docs/index', function () {
108
    mockRoute('docs');
109
110
    Filesystem::put('_docs/index.md', '# Hello World!');
111
112
    $kernel = new HttpKernel();
113
    $response = $kernel->handle(new Request());
114
115
    expect($response)->toBeInstanceOf(Response::class)
116
        ->and($response->statusCode)->toBe(200)
117
        ->and($response->statusMessage)->toBe('OK');
118
119
    expect($response->body)->toContain('HydePHP Docs');
120
121
    Filesystem::unlink('_docs/index.md');
122
    Filesystem::unlink('_site/docs/index.html');
123
});
124
125
test('docs/search renders search page', function () {
126
    mockRoute('docs/search');
127
128
    Blade::shouldReceive('render')->once()->andReturn('foo');
129
130
    $kernel = new HttpKernel();
131
    $response = $kernel->handle(new Request());
132
133
    expect($response)->toBeInstanceOf(HtmlResponse::class)
134
        ->and($response->statusCode)->toBe(200)
135
        ->and($response->statusMessage)->toBe('OK');
136
137
    expect($response->body)->toBe('foo');
138
});
139
140
test('ping route returns ping response', function () {
141
    mockRoute('ping');
142
143
    $kernel = new HttpKernel();
144
    $response = $kernel->handle(new Request());
145
146
    expect($response)->toBeInstanceOf(JsonResponse::class)
147
        ->and($response->statusCode)->toBe(200)
148
        ->and($response->statusMessage)->toBe('OK');
149
});
150
151
test('exception handling', function () {
152
    $exception = new Exception('foo');
153
    $response = ExceptionHandler::handle($exception);
154
155
    expect($response)->toBeInstanceOf(Response::class)
156
        ->and($response->statusCode)->toBe(500)
157
        ->and($response->statusMessage)->toBe('Internal Server Error');
158
});
159
160
function mockRoute(string $route, $method = 'GET'): void
161
{
162
    $_SERVER['REQUEST_METHOD'] = $method;
163
    $_SERVER['REQUEST_URI'] = "/$route";
164
}
165