Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Inertia\Support\Header constants instead of strings #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<report>
<clover outputFile="coverage/phpunit/clover.xml"/>
<html outputDirectory="coverage/phpunit/html" lowUpperBound="35" highLowerBound="70"/>
Expand All @@ -22,10 +19,13 @@
<env name="MAIL_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>

<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>

<env name="AWS_DEFAULT_REGION" value="us-west-2"/>
</php>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
14 changes: 8 additions & 6 deletions src/Modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Routing\Route;
use Illuminate\Support\Str;
use Inertia\Support\Header;
use Momentum\Modal\Support\ModalHeader;

class Modal implements Responsable
{
Expand Down Expand Up @@ -53,9 +55,9 @@ public function render(): mixed
inertia()->share(['modal' => $this->component()]);

// render background component on first visit
if (request()->header('X-Inertia') && request()->header('X-Inertia-Partial-Component')) {
if (request()->header(Header::INERTIA) && request()->header(Header::PARTIAL_COMPONENT)) {
/** @phpstan-ignore-next-line */
return inertia()->render(request()->header('X-Inertia-Partial-Component'));
return inertia()->render(request()->header(Header::PARTIAL_COMPONENT));
}

/** @var Request $originalRequest */
Expand Down Expand Up @@ -108,20 +110,20 @@ protected function component(): array
'baseURL' => $this->baseURL,
'redirectURL' => $this->redirectURL(),
'props' => $this->props,
'key' => request()->header('X-Inertia-Modal-Key', Str::uuid()->toString()),
'key' => request()->header(ModalHeader::KEY, Str::uuid()->toString()),
'nonce' => Str::uuid()->toString(),
];
}

protected function redirectURL(): string
{
if (request()->header('X-Inertia-Modal-Redirect')) {
return request()->header('X-Inertia-Modal-Redirect');
if (request()->header(ModalHeader::REDIRECT)) {
return request()->header(ModalHeader::REDIRECT);
}

$referer = request()->headers->get('referer');

if (request()->header('X-Inertia') && $referer && $referer != url()->current()) {
if (request()->header(Header::INERTIA) && $referer && $referer != url()->current()) {
return $referer;
}

Expand Down
11 changes: 11 additions & 0 deletions src/Support/ModalHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Momentum\Modal\Support;

class ModalHeader
{
public const KEY = 'X-Inertia-Modal-Key';
public const REDIRECT = 'X-Inertia-Modal-Redirect';
}
10 changes: 6 additions & 4 deletions tests/Pest/ModalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\Support\Facades\Route;
use Inertia\Support\Header;
use Inertia\Testing\AssertableInertia;
use Momentum\Modal\Support\ModalHeader;
use Momentum\Modal\Tests\Stubs\ExampleController;
use Momentum\Modal\Tests\Stubs\ExampleMiddleware;
use function Pest\Laravel\from;
Expand Down Expand Up @@ -61,8 +63,8 @@

from($fromURL)
->get(route('users.tweets.show', [$user, $tweet]), [
'X-Inertia' => true,
'X-Inertia-Modal-Redirect' => $fromURL,
Header::INERTIA => true,
ModalHeader::REDIRECT => $fromURL,
])
->assertSuccessful()
->assertJsonPath('component', 'Home')
Expand Down Expand Up @@ -94,8 +96,8 @@

from($fromURL)
->get(route('users.tweets.show', [$user, $tweet]), [
'X-Inertia' => true,
'X-Inertia-Modal-Redirect' => $fromURL,
Header::INERTIA => true,
ModalHeader::REDIRECT => $fromURL,
])
->assertJsonPath('component', 'Users/Show')
->assertJsonPath('props.page', '3')
Expand Down