Skip to content

tests: Use mock web server - #281

Open
jtojnar wants to merge 1 commit into
microformats:mainfrom
jtojnar:tests-local-fetch
Open

jtojnar wants to merge 1 commit into
microformats:mainfrom
jtojnar:tests-local-fetch

Conversation

@jtojnar

@jtojnar jtojnar commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

@jtojnar
jtojnar force-pushed the tests-local-fetch branch from 5f28c51 to ba9499d Compare June 25, 2026 23:57
@gRegorLove gRegorLove added this to the 0.6.1 milestone Oct 31, 2026
@jtojnar
jtojnar force-pushed the tests-local-fetch branch 5 times, most recently from 33058d6 to f2f2a05 Compare November 1, 2026 20:53
@jtojnar jtojnar mentioned this pull request Nov 2, 2026
This will allow the tests to work offline or if the servers have an outage,
and they will not break if the site changes.
@jtojnar

jtojnar commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Re: the discussion from https://chat.indieweb.org/microformats/2026-11-09

@JKingweb wrote:

It's what I use, for what it's worth. It's a library which starts and stops the basic built-in PHP server on-demand. It certainly beats relying on an external resource not changing, and it adds no added dependencies since it's just using what's built in PHP. The library does require the sockets PHP extension, but this is only required if you're not specifying a port number for the server.

For tests, you do not want to specify a port number, since you have no way of knowing which ports are free.

But the extension will only be needed for development and developers can easily install it when missing from their environment. We already use require-dev so Composer will not prevent consumers from installing mf2/mf2 even if they lack sockets extension.

@Zegnat wrote:

Interesting. In one of my current projects I fake the HTTP request through dependency injection instead, requires no separate processes: https://github.com/Zegnat/php-webmention-endpoint-discovery/blob/main/tests/FakeHttp.php - I think you could do something similar in php-mf2 with https://github.com/php-mock/php-mock-phpunit to override curl.

That does appear to work:

$curlExec = $this->getFunctionMock('Mf2', 'curl_exec');

$curlExec
	->expects($this->exactly(2))
	->willReturn(
		file_get_contents(__DIR__ . '/waterpigs.co.uk_index.html'),
		file_get_contents(__DIR__ . '/waterpigs.co.uk_photo.jpg')
	);

$curlGetinfo = $this->getFunctionMock('Mf2', 'curl_getinfo');

$curlGetinfo
	->expects($this->exactly(2))
	->willReturn(
		['content_type' => 'text/html', 'url' => 'http://waterpigs.co.uk/'],
		['content_type' => 'image/jpeg', 'url' => 'http://waterpigs.co.uk/photo.jpg']
	);

Though I find replacing random functions called internally by the system under test far less elegant than using it as is and controlling the inputs.

@Zegnat wrote:

But as long as it is still easy to run the tests, the mock server will work just as well. But I do not think ext-sockets is enabled by default in PHP, is it?

At least Arch, Debian, Fedora and NixOS all include sockets by default.

And since tests will only be run by developers/CI, they can easily install it, if they use a stripped down environment for development for some reason.

What does the test result in when it is not enabled? Will it mark the test as skipped per PHPUnit, fail it, or do something else?

Running composer install in the project will fail:

Details
php-mf2 $ composer install
No composer.lock file present. Updating dependencies to latest instead of installing from lock file. See https://getcomposer.org/install for more information.
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires donatj/mock-webserver ^2.10 -> satisfiable by donatj/mock-webserver[v2.10.0, v2.10.1].
    - donatj/mock-webserver[v2.10.0, ..., v2.10.1] require ext-sockets * -> it is missing from your system. Install or enable PHP's sockets extension.

To enable extensions, verify that they are enabled in your .ini files:
    - /nix/store/bbbcw1zff9dmqkdwffvhghww4ysci23c-php-with-extensions-8.5.12/lib/php.ini
You can also run `php --ini` in a terminal to see which files are used by PHP in CLI mode.
Alternatively, you can run Composer with `--ignore-platform-req=ext-sockets` to temporarily ignore these required extensions.

If the user decides to override it with --ignore-platform-req=ext-sockets, the two tests in question will fail:

Error: Call to undefined function donatj\MockWebServer\socket_create()

vendor/donatj/mock-webserver/src/MockWebServer.php:293
vendor/donatj/mock-webserver/src/MockWebServer.php:49
tests/Mf2/ParserTest.php:275

But again, this should not be an obstacle in practice.

@JKingweb wrote:

Unfortunately it's a hard dependency in composer.json, so composer install will fail before you even get to running the tests. For my application I got around by adding provides:ext-sockets, but a library can't do that (because there is no provides-dev section in the Composer schema). I'll need to figure out a workaround (or use something else) soon, though, as I'll shortly be facing the same problem.

Am I missing some use case where you would want to run tests and be unable to install the extension?

@pfefferle wrote:

these are two very small wrappers for curl… I think a mock server to simply test a GET request is a bit overkill!? aside from that, I would prefer the build in PHP server over the mock server. this is less dependencies…

I previously wrote a small wrapper around the built-in PHP server. Then we found donatj/mock-webserver, which is exactly the same thing, only slightly cleaner written. Either way, the dependency is trivial compared to PHPUnit.

@pfefferle wrote:

maybe we could use github pages… if github is down, the tests won’t work either 😉

Please no more increasing dependency on GitHub. We should be striving to build Indie Web, not being beholden to corporations. Especially when GitHub is has been increasingly unreliable lately.

I develop software from my local checkout, benefitting from git being decentralized VCS. So, as long as I obtained dependencies prior to GitHub crumbling, I can continue to run tests just fine.

@jtojnar

jtojnar commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

I tried to move this PR to the top of the stack but that would cause testRelativeURLResolvedWithFinalURL to fail in all the other PRs in the stack.

The website used by the test changed again, now it passes ["value"=>"https://aaronparecki.com/img/1240x,q60/2014/12/23/5/photo.jpeg", "alt"=>""] to parse_url instead of just the URL string, which is a type error.

I could update the test again but this PR solves it once and for all in better way IMO so I moved it back to the bottom.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants