Skip to content

Commit 31de346

Browse files
authored
Merge pull request #36 from georgehanson/master
Add inertia() helper function
2 parents 1f0a6f6 + 522fde2 commit 31de346

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
"autoload": {
1515
"psr-4": {
1616
"Inertia\\": "src"
17-
}
17+
},
18+
"files": [
19+
"./helpers.php"
20+
]
1821
},
1922
"autoload-dev": {
2023
"psr-4": {

helpers.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
if (! function_exists('inertia')) {
4+
/**
5+
* Inertia helper.
6+
*
7+
* @param null|string $component
8+
* @param array $props
9+
* @return \Inertia\ResponseFactory|\Inertia\Response
10+
*/
11+
function inertia($component = null, $props = []) {
12+
$instance = \Inertia\Inertia::getFacadeRoot();
13+
14+
if ($component) {
15+
return $instance->render($component, $props);
16+
}
17+
18+
return $instance;
19+
}
20+
}

tests/HelperTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Inertia\Inertia;
6+
use Inertia\ResponseFactory;
7+
use Inertia\Response;
8+
9+
class HelperTest extends TestCase
10+
{
11+
public function test_the_helper_function_returns_an_instance_of_the_response_factory()
12+
{
13+
$this->assertInstanceOf(ResponseFactory::class, inertia());
14+
}
15+
16+
public function test_the_helper_function_returns_a_response_instance()
17+
{
18+
$this->assertInstanceOf(Response::class, inertia('User/Edit', ['user' => ['name' => 'George']]));
19+
}
20+
21+
public function test_the_instance_is_the_same_as_the_facade_instance()
22+
{
23+
Inertia::share('key', 'value');
24+
$this->assertEquals('value', inertia()->getShared('key'));
25+
}
26+
}

0 commit comments

Comments
 (0)