Skip to content

Commit

Permalink
✅ Add routing integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
retlehs committed Dec 16, 2024
1 parent f0d76ae commit 21643b8
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .devcontainer/config/web/default.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
server {
listen 80 default;
listen [::]:80;
listen 8080 default_server;
listen [::]:8080 default_server;

# listen 443 ssl;
# listen [::]:443 ssl ipv6only=on;
Expand All @@ -11,7 +13,7 @@ server {
add_header X-Content-Type-Options "nosniff";
charset utf-8;

server_name web.local web;
server_name _;
root /roots/app/public;
index index.php index.html;

Expand Down
3 changes: 3 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ services:
image: nginx:latest
ports:
- '${FORWARD_WEB_PORT:-8080}:80'

expose:
- '8080'
environment:
- NGINX_ENTRYPOINT_WORKER_PROCESSES_AUTOTUNE=1
volumes:
Expand Down
26 changes: 26 additions & 0 deletions tests/Integration/Routing/RoutingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Roots\Acorn\Tests\Integration\Routing;

use GuzzleHttp\Client;

uses(RoutingTestCase::class);

it('should return a 200 status code for WordPress homepage', function () {
$client = new Client([
'verify' => false,
]);

$response = $client->request('GET', 'http://web:8080/');
expect($response->getStatusCode())->toBe(200);
});

it('should return a 200 status code for Acorn 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())->toBe('Howdy');
});
72 changes: 72 additions & 0 deletions tests/Integration/Routing/RoutingTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Roots\Acorn\Tests\Integration\Routing;

use Mockery\Adapter\Phpunit\MockeryTestCase;
use Illuminate\Foundation\Testing\Concerns\MakesHttpRequests;
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);
}
}
18 changes: 18 additions & 0 deletions tests/Test/Concerns/SupportsRouting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Roots\Acorn\Tests\Test\Concerns;

use Roots\Acorn\Application;
use Illuminate\Support\Facades\Route;

trait SupportsRouting
{
protected function setUpRouting()
{
// The application will be booted by the mu-plugin
$this->app = Application::getInstance();

// Set up facades
Route::setFacadeApplication($this->app);
}
}

0 comments on commit 21643b8

Please sign in to comment.