I’m currently developing a Laravel package and writing tests for my API routes using Pest and orchestra/testbench. However, I’m encountering an issue where my routes cannot be found when running the test.
Can someone
Here’s the route I’m trying to test: /api/my-web/matches
<?php
use IlluminateSupportFacadesConfig;
test('', function () {
Config::set([
"football.route_prefix" => "api/football",
"football.middlewares" => []
]);
$response = $this->getJson('/api/my-web/matches');
dd($response->json());
});
Error:
array:1 [
"message" => "The route /api/my-web/matches could not be found."
]
Pest setup:
<?php
/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnitFrameworkTestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/
// uses(TestsTestCase::class)->in('Feature');
use AlexFootballtestsFootballTestCase;
uses(FootballTestCase::class)->in(__DIR__);
/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/
expect()->extend('toBeOne', function () {
return $this->toBe(1);
});
/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/
function something()
{
// ..
}
Package TestCase:
<?php
namespace AlexFootballtests;
use AlexFootballFootballServiceProvider;
use OrchestraTestbenchTestCase;
class FootballTestCase extends TestCase
{
protected function getPackageProviders($app): array
{
return [
FootballServiceProvider::class,
];
}
}
PhpUnit.xml:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Package">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>src/</directory>
</include>
</source>
</phpunit>
ServiceProvider:
<?php
namespace AlexFootball;
use IlluminateSupportServiceProvider;
class FootballServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot(): void {
//publish config
$this->publishes([
__DIR__ . '/../config/football.php' => config_path('football.php'),
], "football.config");
//load api routes
$this->loadRoutesFrom(__DIR__ . '/../routes/api.php');
}
/**
* Register the application services.
*
* @return void
*/
public function register() {}
}
Any help on what might be causing this issue and how to resolve it would be greatly appreciated. If someone could possibly assist me in troubleshooting this, I would be very thankful. Thank you!