Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save leecolarelli/8a77cfa6c7b35f741363e8f6d3d688ee to your computer and use it in GitHub Desktop.

Select an option

Save leecolarelli/8a77cfa6c7b35f741363e8f6d3d688ee to your computer and use it in GitHub Desktop.
Example of building a request with route parameters in Pest/PHPUnit test
<?php
namespace Tests\Http\Middleware;
use App\Http\Middleware\VerifyBigcommerceStoreRequest;
use App\Models\Store\Store;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Route as Router;
use Illuminate\Support\Facades\Schema;
use ReflectionException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Tests\TestCase;
/**
* verifyReferrerMatchesBigcommerceStoreUrl()
*
* @var TestCase $this
*/
it(
'can verify that the referrer matches the bigcommerce store url',
/** @throws ReflectionException */
function (): void {
Schema::disableForeignKeyConstraints();
app()->detectEnvironment(fn () => 'production');
$store = Store::factory()->createOne(['owner_id' => 1, 'hash' => 'abc123', 'url' => 'https://test-store.com']);
$routeName = 'api.bigcommerce.stores.customers.store';
$route = Router::getRoutes()->getByName($routeName);
$fullUrl = route($routeName, 'abc123');
$method = $route->methods()[0];
$uri = $route->uri;
$request = Request::create($fullUrl, 'POST', [], [], [], ['HTTP_REFERER' => 'https://test-store.com/']);
$request->setRouteResolver(fn () => (new Route($method, $uri, []))->bind($request));
$this->invokeNonPublicMethod(
new VerifyBigcommerceStoreRequest(),
'verifyReferrerMatchesBigcommerceStoreUrl',
[$request]
);
}
)->throwsNoExceptions();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment