-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ Add routing integration tests (#422)
Co-authored-by: QWp6t <[email protected]>
- Loading branch information
Showing
7 changed files
with
204 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
name: Integration | ||
|
||
on: [push, pull_request, workflow_dispatch] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Copy .env file | ||
working-directory: .devcontainer | ||
run: cp config/app/.env.example .env | ||
|
||
- name: Start dev container and test | ||
uses: devcontainers/[email protected] | ||
with: | ||
configFile: .devcontainer/devcontainer.json | ||
runCmd: | | ||
cd /roots/app | ||
composer install | ||
# ??? | ||
composer remove roots/acorn | ||
composer require roots/acorn --no-interaction | ||
composer require --dev nunomaduro/collision | ||
composer require --dev spatie/laravel-ignition | ||
# ??? | ||
while ! mysqladmin ping -h"database" --silent; do | ||
sleep 1 | ||
done | ||
wp core install --url=http://web:8080 --title=Acorn --admin_user=admin --admin_password=admin [email protected] --skip-email --allow-root | ||
wp acorn optimize:clear | ||
cd /roots/acorn | ||
composer install | ||
composer run-script test tests/Integration/Routing | ||
- name: Verify routes | ||
run: | | ||
curl -s http://localhost:8080/test/ | grep "Howdy" |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Roots\Acorn\Tests\Integration\Routing; | ||
|
||
use GuzzleHttp\Client; | ||
|
||
uses(RoutingTestCase::class)->group('integration'); | ||
|
||
expect()->extend('toHaveBodyClass', function (string $class) { | ||
preg_match('/<body[^>]*class=["\']([^"\']*)["\']/', $this->value, $matches); | ||
expect($matches)->toHaveCount(2, 'No body tag with class attribute found'); | ||
expect($matches[1])->toContain($class); | ||
|
||
return $this; | ||
}); | ||
|
||
it('handles the test route', function () { | ||
$client = new Client([ | ||
'verify' => false, | ||
]); | ||
|
||
$response = $client->request('GET', 'http://web:8080/test/'); | ||
expect($response->getStatusCode())->toBe(200); | ||
expect((string) $response->getBody())->toContain('Howdy'); | ||
}); | ||
|
||
it('does not intercept WordPress admin routes', function () { | ||
$client = new Client([ | ||
'verify' => false, | ||
'allow_redirects' => false, | ||
]); | ||
|
||
$response = $client->request('GET', 'http://web:8080/wp/wp-admin/'); | ||
expect($response->getStatusCode())->toBe(302); | ||
expect($response->getHeader('Location')[0])->toContain('wp-login.php'); | ||
}); | ||
|
||
it('handles non-existent routes with 404', function () { | ||
$client = new Client([ | ||
'verify' => false, | ||
'http_errors' => false, | ||
]); | ||
|
||
$response = $client->request('GET', 'http://web:8080/non-existent-'.time()); | ||
expect($response->getStatusCode())->toBe(404); | ||
expect((string) $response->getBody())->toContain('Page not found'); | ||
expect((string) $response->getBody())->toHaveBodyClass('error404'); | ||
}); | ||
|
||
it('handles default homepage', function () { | ||
$client = new Client([ | ||
'verify' => false, | ||
]); | ||
|
||
$response = $client->request('GET', 'http://web:8080/'); | ||
expect($response->getStatusCode())->toBe(200); | ||
expect((string) $response->getBody())->toHaveBodyClass('home'); | ||
}); | ||
|
||
it('handles WordPress REST API routes', function () { | ||
$client = new Client([ | ||
'verify' => false, | ||
]); | ||
|
||
$response = $client->request('GET', 'http://web:8080/wp-json/'); | ||
expect($response->getStatusCode())->toBe(200); | ||
|
||
$data = json_decode((string) $response->getBody(), true); | ||
expect($data)->toHaveKey('name'); | ||
expect($data)->toHaveKey('url'); | ||
}); | ||
|
||
it('handles WordPress search route', function () { | ||
$client = new Client([ | ||
'verify' => false, | ||
]); | ||
|
||
$response = $client->request('GET', 'http://web:8080/search/test/'); | ||
expect($response->getStatusCode())->toBe(200); | ||
expect((string) $response->getBody())->toHaveBodyClass('search'); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Roots\Acorn\Tests\Integration\Routing; | ||
|
||
use Illuminate\Foundation\Testing\Concerns\MakesHttpRequests; | ||
use Mockery\Adapter\Phpunit\MockeryTestCase; | ||
use Roots\Acorn\Tests\Test\Concerns\SupportsGlobalStubs; | ||
use Roots\Acorn\Tests\Test\Concerns\SupportsScopedFixtures; | ||
|
||
class RoutingTestCase extends MockeryTestCase | ||
{ | ||
use MakesHttpRequests; | ||
use SupportsGlobalStubs; | ||
use SupportsScopedFixtures; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->clearStubs(); | ||
|
||
// Ensure routes directory exists | ||
if (! is_dir('/roots/app/public/routes')) { | ||
mkdir('/roots/app/public/routes', 0777, true); | ||
} | ||
|
||
// Create web.php routes file | ||
$webRoutes = <<<'PHP' | ||
<?php | ||
use Illuminate\Support\Facades\Route; | ||
Route::middleware(['web'])->group(function () { | ||
Route::get('/test', fn() => 'Howdy')->name('test'); | ||
}); | ||
PHP; | ||
|
||
file_put_contents('/roots/app/public/routes/web.php', $webRoutes); | ||
|
||
// Ensure mu-plugins directory exists | ||
if (! is_dir('/roots/app/public/content/mu-plugins')) { | ||
mkdir('/roots/app/public/content/mu-plugins', 0777, true); | ||
} | ||
|
||
// Create or update the Acorn boot mu-plugin | ||
$bootPlugin = <<<'PHP' | ||
<?php | ||
/* | ||
Plugin Name: Acorn Boot | ||
*/ | ||
use Roots\Acorn\Application; | ||
use Roots\Acorn\Configuration\Exceptions; | ||
use Roots\Acorn\Configuration\Middleware; | ||
add_action('after_setup_theme', function () { | ||
Application::configure() | ||
->withMiddleware(function (Middleware $middleware) { | ||
// | ||
}) | ||
->withExceptions(function (Exceptions $exceptions) { | ||
// | ||
}) | ||
->withRouting( | ||
web: '/roots/app/public/routes/web.php' | ||
) | ||
->boot(); | ||
}, 0); | ||
PHP; | ||
|
||
file_put_contents('/roots/app/public/content/mu-plugins/01-acorn-boot.php', $bootPlugin); | ||
} | ||
} |