Skip to content

Commit d0eb3a0

Browse files
committed
✅ Move tests to Pest
1 parent 4c3dd56 commit d0eb3a0

File tree

8 files changed

+151
-85
lines changed

8 files changed

+151
-85
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ jobs:
2828
- name: Install composer dependencies
2929
run: composer install --optimize-autoloader
3030

31+
- name: Execute the Composer lint script
32+
run: composer run-script lint
33+
3134
- name: Build prefixed vendor
3235
run: composer run build
3336

3437
- name: Install wp-playground CLI
3538
run: npm install -g @wp-playground/cli
3639

37-
3840
- name: Start wp-playground with blueprint
3941
run: |
4042
echo "Starting wp-playground server in background..."
@@ -61,72 +63,8 @@ jobs:
6163
# Wait for WordPress to be ready
6264
sleep 10
6365
64-
- name: Test single post markdown output
65-
run: |
66-
echo "Testing single post with ?format=markdown"
67-
68-
# Check if server is responding
69-
echo "Checking server status..."
70-
curl -v "http://localhost:9400/" || echo "Server not responding"
71-
72-
echo "Testing markdown endpoint..."
73-
response=$(curl -s "http://localhost:9400/hello-world/?format=markdown")
74-
echo "Response received: $response"
75-
76-
if [[ "$response" == *"# Hello world"* ]] && [[ "$response" == *"WordPress"* ]]; then
77-
echo "✅ Single post markdown query parameter test passed"
78-
else
79-
echo "❌ Single post markdown query parameter test failed"
80-
exit 1
81-
fi
82-
83-
- name: Test single post with Accept header
84-
run: |
85-
echo "Testing single post with Accept: text/markdown header"
86-
response=$(curl -s -H "Accept: text/markdown" "http://localhost:9400/hello-world/")
87-
88-
if [[ "$response" == *"# Hello world"* ]] && [[ "$response" == *"WordPress"* ]]; then
89-
echo "✅ Single post Accept header test passed"
90-
else
91-
echo "❌ Single post Accept header test failed"
92-
exit 1
93-
fi
94-
95-
- name: Test main feed markdown
96-
run: |
97-
echo "Testing main feed with ?format=markdown"
98-
response=$(curl -s "http://localhost:9400/feed/?format=markdown")
99-
100-
if [[ "$response" == *"Markdown Feed"* ]] && [[ "$response" == *"Hello world"* ]]; then
101-
echo "✅ Main feed markdown test passed"
102-
else
103-
echo "❌ Main feed markdown test failed"
104-
exit 1
105-
fi
106-
107-
- name: Test dedicated markdown feed
108-
run: |
109-
echo "Testing dedicated /feed/markdown/ endpoint"
110-
response=$(curl -s "http://localhost:9400/feed/markdown/")
111-
112-
if [[ "$response" == *"Markdown Feed"* ]] && [[ "$response" == *"Hello world"* ]]; then
113-
echo "✅ Dedicated markdown feed test passed"
114-
else
115-
echo "❌ Dedicated markdown feed test failed"
116-
exit 1
117-
fi
118-
119-
- name: Test content type headers
120-
run: |
121-
echo "Testing Content-Type headers"
122-
content_type=$(curl -s -I "http://localhost:9400/hello-world/?format=markdown" | grep -i "content-type")
123-
124-
if [[ "$content_type" == *"text/markdown"* ]]; then
125-
echo "✅ Content-Type header test passed"
126-
else
127-
echo "❌ Content-Type header test failed"
128-
exit 1
129-
fi
66+
- name: Run Pest tests
67+
run: composer run test
13068

13169
- name: Cleanup
13270
if: always()

README.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ A WordPress plugin that returns post content in Markdown format when requested w
66

77
PHP 8.1+
88

9+
## Installation
10+
11+
### via Composer
12+
13+
```bash
14+
composer require roots/post-content-to-markdown
15+
```
16+
17+
### Manual
18+
19+
1. Download the [latest release](https://github.com/roots/post-content-to-markdown/releases)
20+
2. Place in `wp-content/plugins/post-content-to-markdown/`
21+
3. Activate via wp-admin or WP-CLI
22+
923
## Usage
1024

1125
### Accept headers (ideal for LLMs)
@@ -107,22 +121,6 @@ The plugin automatically adds a `<atom:link>` element to your site's RSS feed, a
107121
<atom:link href="https://example.com/feed/markdown/" rel="alternate" type="text/markdown" />
108122
```
109123

110-
## Installation
111-
112-
### via Composer
113-
114-
```bash
115-
composer require roots/post-content-to-markdown
116-
```
117-
118-
### Manual
119-
120-
1. Download the [latest release](https://github.com/roots/post-content-to-markdown/releases)
121-
2. Place in `wp-content/plugins/post-content-to-markdown/`
122-
3. Activate via wp-admin or WP-CLI
123-
124-
**Note:** After activation, you may need to flush rewrite rules by visiting Settings → Permalinks and clicking "Save Changes" if the `/feed/markdown/` endpoint doesn't work immediately.
125-
126124
## Filters
127125

128126
The plugin provides several filters for customization:

composer.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,22 @@
1212
}
1313
],
1414
"scripts": {
15-
"build": "vendor/bin/php-scoper add-prefix --force && cd vendor_prefixed && composer dump-autoload"
15+
"build": "vendor/bin/php-scoper add-prefix --force && cd vendor_prefixed && composer dump-autoload",
16+
"lint": "vendor/bin/pint",
17+
"test": "vendor/bin/pest"
1618
},
1719
"require": {
20+
"php": "^8.1",
1821
"league/html-to-markdown": "^5.1"
1922
},
2023
"require-dev": {
21-
"humbug/php-scoper": "0.18.7"
24+
"humbug/php-scoper": "0.18.7",
25+
"laravel/pint": "^1.20",
26+
"pestphp/pest": "^2.36"
27+
},
28+
"config": {
29+
"allow-plugins": {
30+
"pestphp/pest-plugin": true
31+
}
2232
}
2333
}

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "post-content-to-markdown",
3+
"private": true,
4+
"scripts": {
5+
"playground": "npx @wp-playground/cli server --mount=\"$(pwd):/wordpress/wp-content/plugins/post-content-to-markdown\" --blueprint=blueprint.json"
6+
}
7+
}

phpunit.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
>
7+
<testsuites>
8+
<testsuite name="Feature">
9+
<directory>tests/Feature</directory>
10+
</testsuite>
11+
</testsuites>
12+
<source>
13+
<include>
14+
<directory>.</directory>
15+
</include>
16+
<exclude>
17+
<directory>vendor</directory>
18+
<directory>vendor_prefixed</directory>
19+
<directory>tests</directory>
20+
</exclude>
21+
</source>
22+
</phpunit>

post-content-to-markdown.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Description: Serve post content as Markdown via Accept headers or query parameters.
66
* Version: 1.2.0
77
* Author: roots.io
8+
* Requires PHP: 8.1
89
*/
910

1011
namespace PostContentToMarkdown;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
test('wordpress site loads', function () {
4+
$response = makeRequest('/');
5+
6+
expect($response['body'])->not->toBeEmpty();
7+
});
8+
9+
test('single post returns markdown with query parameter', function () {
10+
$response = makeRequest('/hello-world/?format=markdown');
11+
12+
expect($response['body'])
13+
->toContain('# Hello world')
14+
->toContain('WordPress');
15+
});
16+
17+
test('single post returns markdown with Accept header', function () {
18+
$response = makeRequest('/hello-world/', [
19+
'Accept' => 'text/markdown',
20+
]);
21+
22+
expect($response['body'])
23+
->toContain('# Hello world')
24+
->toContain('WordPress');
25+
});
26+
27+
test('main feed returns markdown with query parameter', function () {
28+
$response = makeRequest('/feed/?format=markdown');
29+
30+
expect($response['body'])
31+
->toContain('Markdown Feed')
32+
->toContain('Hello world');
33+
});
34+
35+
test('dedicated markdown feed endpoint works', function () {
36+
$response = makeRequest('/feed/markdown/');
37+
38+
expect($response['body'])
39+
->toContain('Markdown Feed')
40+
->toContain('Hello world');
41+
});
42+
43+
test('content type header is set to text/markdown', function () {
44+
$response = makeRequest('/hello-world/?format=markdown');
45+
46+
expect($response['headers'])
47+
->toHaveKey('content-type')
48+
->and($response['headers']['content-type'])
49+
->toContain('text/markdown');
50+
});

tests/Pest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* Make an HTTP request to the WordPress Playground instance
5+
*/
6+
function makeRequest(string $path, array $headers = []): array
7+
{
8+
$url = 'http://localhost:9400'.$path;
9+
10+
$context_options = [
11+
'http' => [
12+
'method' => 'GET',
13+
'header' => implode("\r\n", array_map(
14+
fn ($key, $value) => "$key: $value",
15+
array_keys($headers),
16+
array_values($headers)
17+
)),
18+
'ignore_errors' => true,
19+
],
20+
];
21+
22+
$context = stream_context_create($context_options);
23+
$response = @file_get_contents($url, false, $context);
24+
25+
// Parse response headers
26+
$responseHeaders = [];
27+
if (isset($http_response_header)) {
28+
foreach ($http_response_header as $header) {
29+
if (str_contains($header, ':')) {
30+
[$key, $value] = explode(':', $header, 2);
31+
$responseHeaders[strtolower(trim($key))] = trim($value);
32+
}
33+
}
34+
}
35+
36+
return [
37+
'body' => $response !== false ? $response : '',
38+
'headers' => $responseHeaders,
39+
];
40+
}

0 commit comments

Comments
 (0)