Skip to content

Commit ba9499d

Browse files
committed
tests: Use mock web server
This will allow the tests to work offline or if the servers have an outage, and they will not break if the site changes.
1 parent 89c7dda commit ba9499d

6 files changed

Lines changed: 1690 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ Pull requests very welcome, please try to maintain stylistic, structural and nam
357357

358358
### Testing
359359

360-
There are currently two separate test suites: one, in `tests/Mf2`, is written in phpunit, containing many microformats parsing examples as well as internal parser tests and regression tests for specific issues over php-mf2’s history. Run it with `./vendor/bin/phpunit`. If you do not have a live internet connection, you can exclude tests that depend on it: `./vendor/bin/phpunit --exclude-group internet`.
360+
There are currently two separate test suites: one, in `tests/Mf2`, is written in phpunit, containing many microformats parsing examples as well as internal parser tests and regression tests for specific issues over php-mf2’s history. Run it with `./vendor/bin/phpunit`.
361361

362362
The other, in `tests/test-suite`, is a custom test harness which hooks up php-mf2 to the cross-platform [microformats test suite](https://github.com/microformats/tests). To run these tests you must first install the tests with `./composer.phar install`. Each test consists of a HTML file and a corresponding JSON file, and the suite can be run with `php ./tests/test-suite/test-suite.php`.
363363

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
}
2222
},
2323
"require-dev": {
24+
"donatj/mock-webserver": "^2.10",
2425
"mf2/tests": "dev-master#e9e2b905821ba0a5b59dab1a8eaf40634ce9cd49",
2526
"squizlabs/php_codesniffer": "^3.10.2",
2627
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",

tests/Mf2/ParserTest.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Mf2\Parser\Test;
44

5+
use donatj\MockWebServer\MockWebServer;
6+
use donatj\MockWebServer\Response as MockWebServerResponse;
57
use Mf2\Parser;
68
use Mf2;
79
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
@@ -266,16 +268,30 @@ public function testParsesNestedMicroformatsWithClassnamesInAnyOrder() {
266268
$this->assertEquals('Name', $output['items'][0]['properties']['in-reply-to'][0]['properties']['name'][0]);
267269
}
268270

269-
/**
270-
* @group internet
271-
*/
272271
public function testFetchMicroformats() {
273-
$mf = Mf2\fetch('http://waterpigs.co.uk/');
272+
$server = new MockWebServer();
273+
$server->start();
274+
275+
// Obtained from http://waterpigs.co.uk/
276+
$indexUrl = $server->setResponseOfPath(
277+
'/',
278+
new MockWebServerResponse(file_get_contents(__DIR__ . '/waterpigs.co.uk_index.html'), [], 200)
279+
);
280+
281+
// Obtained from http://waterpigs.co.uk/photo.jpg
282+
$photoUrl = $server->setResponseOfPath(
283+
'/photo.jpg',
284+
new MockWebServerResponse(file_get_contents(__DIR__ . '/waterpigs.co.uk_photo.jpg'), ['content-type: image/jpeg'], 200)
285+
);
286+
287+
$mf = Mf2\fetch($indexUrl);
274288
$this->assertArrayHasKey('items', $mf);
275289

276-
$mf = Mf2\fetch('http://waterpigs.co.uk/photo.jpg', null, $curlInfo);
290+
$mf = Mf2\fetch($photoUrl, null, $curlInfo);
277291
$this->assertNull($mf);
278292
$this->assertStringContainsString('jpeg', $curlInfo['content_type']);
293+
294+
$server->stop();
279295
}
280296

281297
/**
@@ -381,22 +397,27 @@ public function testApplyTransformationToSrcset() {
381397

382398
/**
383399
* @see https://github.com/indieweb/php-mf2/issues/84
384-
* @group internet
385-
*
386-
* 2024-04-07: The final photo URL in this test changed over time.
387-
* Updated it to only check that the final URL's hostname was correct,
388-
* not the full photo URL.
389400
*/
390401
public function testRelativeURLResolvedWithFinalURL() {
391-
$mf = Mf2\fetch('http://aaron.pk/4Zn5');
402+
$server = new MockWebServer();
403+
$server->start();
404+
405+
// Obtained from http://aaron.pk/4Zn5
406+
$url = $server->setResponseOfPath(
407+
'/4Zn5',
408+
new MockWebServerResponse(file_get_contents(__DIR__ . '/aaron.pk_4Zn5.html'), [], 200)
409+
);
410+
411+
$mf = Mf2\fetch($url);
412+
413+
$server->stop();
392414

393415
$this->assertArrayHasKey('photo', $mf['items'][0]['properties']);
394416

395417
$hostname = parse_url($mf['items'][0]['properties']['photo'][0], PHP_URL_HOST);
396-
$this->assertEquals('aaronparecki.com', $hostname);
418+
$this->assertEquals('127.0.0.1', $hostname);
397419

398-
// previous assertion: not the photo URL changed over time
399-
// $this->assertEquals('https://aaronparecki.com/img/1240x0/2014/12/23/5/photo.jpeg', $mf['items'][0]['properties']['photo'][0]);
420+
$this->assertEquals($server->getServerRoot() . '/img/1240x,q60/2014/12/23/5/photo.jpeg', $mf['items'][0]['properties']['photo'][0]);
400421
}
401422

402423
public function testScriptTagContentsRemovedFromTextValue() {

0 commit comments

Comments
 (0)