-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathAbstractKleinTest.php
More file actions
124 lines (107 loc) · 3.43 KB
/
AbstractKleinTest.php
File metadata and controls
124 lines (107 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* Klein (klein.php) - A fast & flexible router for PHP
*
* @author Chris O'Hara <cohara87@gmail.com>
* @author Trevor Suarez (Rican7) (contributor and v2 refactorer)
* @copyright (c) Chris O'Hara
* @link https://github.com/klein/klein.php
* @license MIT
*/
namespace Klein\Tests;
use Klein\Klein;
use Klein\Request;
use Klein\Response;
use Klein\Tests\Mocks\HeadersNoOp;
use PHPUnit\Framework\TestCase;
/**
* AbstractKleinTest
*
* Base test class for PHP Unit testing
*/
abstract class AbstractKleinTest extends TestCase
{
/**
* The automatically created test Klein instance
* (for easy testing and less boilerplate)
*
* @type Klein
*/
protected $klein_app;
/**
* Setup our test
* (runs before each test)
*
* @return void
*/
protected function setUp(): void
{
// Create a new klein app,
// since we need one pretty much everywhere
$this->klein_app = new Klein();
}
/**
* Quick method for dispatching and returning our output from our shared Klein instance
*
* This is mostly useful, since the tests would otherwise have to make a bunch of calls
* concerning the argument order and constants. DRY, bitch. ;)
*
* @param Request $request Custom Klein "Request" object
* @param Response $response Custom Klein "Response" object
* @return mixed The output of the dispatch call
*/
protected function dispatchAndReturnOutput($request = null, $response = null)
{
return $this->klein_app->dispatch(
$request,
$response,
false,
Klein::DISPATCH_CAPTURE_AND_RETURN
);
}
/**
* Runs a callable and asserts that the output from the executed callable
* matches the passed in expected output
*
* @param mixed $expected The expected output
* @param callable $callback The callable function
* @param string $message (optional) A message to display if the assertion fails
* @return void
*/
protected function assertOutputSame($expected, $callback, $message = '')
{
// Start our output buffer so we can capture our output
ob_start();
call_user_func($callback);
// Grab our output from our buffer
$out = ob_get_contents();
// Clean our buffer and destroy it, so its like no output ever happened. ;)
ob_end_clean();
// Use PHPUnit's built in assertion
$this->assertSame($expected, $out, $message);
}
/**
* Loads externally defined routes under the filename's namespace
*
* @param Klein $app_context The application context to attach the routes to
* @return array
*/
protected function loadExternalRoutes(Klein $app_context = null)
{
// Did we not pass an instance?
if (is_null($app_context)) {
$app_context = $this->klein_app ?: new Klein();
}
$route_directory = __DIR__ . '/routes/';
$route_files = scandir($route_directory);
$route_namespaces = array();
foreach ($route_files as $file) {
if (is_file($route_directory . $file)) {
$route_namespace = '/' . basename($file, '.php');
$route_namespaces[] = $route_namespace;
$app_context->with($route_namespace, $route_directory . $file);
}
}
return $route_namespaces;
}
}