Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.github export-ignore
/tests export-ignore
phpunit.xml export-ignore
7 changes: 5 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: ['8.1', '8.2', '8.3', '8.4', '8.5']
php: ['8.2', '8.3', '8.4', '8.5']

steps:
- name: Checkout code
Expand All @@ -30,7 +30,10 @@ jobs:
coverage: none

- name: Install Composer dependencies
run: composer install --prefer-dist --no-interaction --no-progress
run: composer update --prefer-dist --no-interaction --no-progress

- name: Run Pint
run: vendor/bin/pint --test

- name: Run tests
run: vendor/bin/pest
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor
composer.lock
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@
"forum": "https://discourse.roots.io"
},
"require": {
"php": ">=8.1",
"php": ">=8.2",
"composer/installers": "~1.0 || ~2.0"
},
"require-dev": {
"laravel/pint": "^1.20.0 <1.21"
"laravel/pint": "^1.20",
"pestphp/pest": "^2.36 || ^3.0 || ^4.0"
},
"config": {
"allow-plugins": {
"composer/installers": true
"composer/installers": true,
"pestphp/pest-plugin": true
}
}
}
234 changes: 0 additions & 234 deletions composer.lock

This file was deleted.

17 changes: 17 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="tests/bootstrap.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<file>wp-stage-switcher.php</file>
</include>
</source>
</phpunit>
1 change: 1 addition & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
11 changes: 11 additions & 0 deletions tests/Support/FakeAdminBar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class WP_Admin_Bar
{
public array $menus = [];

public function add_menu(array $args): void
{
$this->menus[$args['id']] = $args;
}
}
74 changes: 74 additions & 0 deletions tests/Unit/RelativeUriTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

use Roots\StageSwitcher\StageSwitcher;

afterEach(function () {
global $_test_site_url, $_test_filter_overrides;
$_test_site_url = null;
$_test_filter_overrides = [];
unset($_SERVER['REQUEST_URI']);
});

function callRelativeUri(StageSwitcher $instance, string $request_uri): string
{
$method = new ReflectionMethod($instance, 'relative_uri');

return $method->invoke($instance, $request_uri);
}

function switcher(string $siteUrl = 'http://example.test/wp', array $stages = []): StageSwitcher
{
global $_test_site_url;
$_test_site_url = $siteUrl;

$instance = new StageSwitcher;

$reflection = new ReflectionProperty(StageSwitcher::class, 'stages');
$reflection->setValue($instance, $stages);

return $instance;
}

it('strips subfolder prefix from request path', function () {
expect(callRelativeUri(switcher(), '/wp/about'))->toBe('/about');
});

it('strips exact base path to root', function () {
expect(callRelativeUri(switcher(), '/wp'))->toBe('/');
});

it('does not strip partial segment matches', function () {
expect(callRelativeUri(switcher(), '/wp-json/wp/v2/posts'))->toBe('/wp-json/wp/v2/posts');
});

it('preserves query strings', function () {
expect(callRelativeUri(switcher(), '/wp/about?foo=bar'))->toBe('/about?foo=bar');
});

it('returns request uri unchanged when no prefix matches', function () {
expect(callRelativeUri(switcher('http://example.test'), '/about'))->toBe('/about');
});

it('falls back to ENVIRONMENTS config path', function () {
$switcher = switcher(
'http://example.test',
['development' => 'http://example.test/app'],
);

expect(callRelativeUri($switcher, '/app/about'))->toBe('/about');
});

it('keeps root path as root', function () {
expect(callRelativeUri(switcher(), '/'))->toBe('/');
});

it('assembles correct target URL with query string', function () {
$_SERVER['REQUEST_URI'] = '/wp/about?foo=bar';

$adminBar = new WP_Admin_Bar;
$switcher = new StageSwitcher;
$switcher->admin_bar_stage_switcher($adminBar);

expect($adminBar->menus['stage_staging']['href'])->toBe('https://staging.example.com/about?foo=bar');
expect($adminBar->menus['stage_production']['href'])->toBe('https://example.com/about?foo=bar');
});
Loading