Created
April 30, 2024 09:14
-
-
Save leecolarelli/8a77cfa6c7b35f741363e8f6d3d688ee to your computer and use it in GitHub Desktop.
Example of building a request with route parameters in Pest/PHPUnit test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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