Skip to content

Commit 5f28c51

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 5f28c51

5 files changed

Lines changed: 1689 additions & 6 deletions

File tree

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 & 6 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;
@@ -270,12 +272,29 @@ public function testParsesNestedMicroformatsWithClassnamesInAnyOrder() {
270272
* @group internet
271273
*/
272274
public function testFetchMicroformats() {
273-
$mf = Mf2\fetch('http://waterpigs.co.uk/');
275+
$server = new MockWebServer();
276+
$server->start();
277+
278+
// Obtained from http://waterpigs.co.uk/
279+
$indexUrl = $server->setResponseOfPath(
280+
'/',
281+
new MockWebServerResponse(file_get_contents(__DIR__ . '/waterpigs.co.uk_index.html'), [], 200)
282+
);
283+
284+
// Obtained from http://waterpigs.co.uk/photo.jpg
285+
$photoUrl = $server->setResponseOfPath(
286+
'/photo.jpg',
287+
new MockWebServerResponse(file_get_contents(__DIR__ . '/waterpigs.co.uk_photo.jpg'), ['content-type: image/jpeg'], 200)
288+
);
289+
290+
$mf = Mf2\fetch($indexUrl);
274291
$this->assertArrayHasKey('items', $mf);
275292

276-
$mf = Mf2\fetch('http://waterpigs.co.uk/photo.jpg', null, $curlInfo);
293+
$mf = Mf2\fetch($photoUrl, null, $curlInfo);
277294
$this->assertNull($mf);
278295
$this->assertStringContainsString('jpeg', $curlInfo['content_type']);
296+
297+
$server->stop();
279298
}
280299

281300
/**
@@ -388,15 +407,25 @@ public function testApplyTransformationToSrcset() {
388407
* not the full photo URL.
389408
*/
390409
public function testRelativeURLResolvedWithFinalURL() {
391-
$mf = Mf2\fetch('http://aaron.pk/4Zn5');
410+
$server = new MockWebServer();
411+
$server->start();
412+
413+
// Obtained from http://aaron.pk/4Zn5
414+
$url = $server->setResponseOfPath(
415+
'/4Zn5',
416+
new MockWebServerResponse(file_get_contents(__DIR__ . '/aaron.pk_4Zn5.html'), [], 200)
417+
);
418+
419+
$mf = Mf2\fetch($url);
420+
421+
$server->stop();
392422

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

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

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]);
428+
$this->assertEquals($server->getServerRoot() . '/img/1240x,q60/2014/12/23/5/photo.jpeg', $mf['items'][0]['properties']['photo'][0]);
400429
}
401430

402431
public function testScriptTagContentsRemovedFromTextValue() {

0 commit comments

Comments
 (0)