1

Background I: *.phpt in phpunit

I recently read an article about *.phpt support in PhpUnit:
https://www.moxio.com/blog/32/start-testing-with-phpt-tests-in-phpunit

The big advantages here:

  • Supported out of the box with phpunit, no need for custom code to discover and deal with those files.
  • Common format that everybody seems to agree upon.
  • One can directly specify a specific *.phpt as a cli parameter.

A typical *.phpt file would look like this:

--TEST--
Basic arithmetic - addition
--FILE--
<?php
var_dump(42 + 1);
?>
--EXPECT--
int(43)

Background II: Self-updating fixtures

Earlier than that, I read about unit tests that can update (= overwrite) their own fixture files if a cli variable is present:
https://tomasvotruba.com/blog/2020/07/20/how-to-update-hundreds-of-test-fixtures-with-single-phpunit-run/

The following cli command would then overwrite the "expected" part in fixtures for failing tests:

$ UPDATE_TESTS=1 ./vendor/bin/phpunit

One would then review the git diff, and either accept the changes as desired functionality change, or fix the behavior.

The format of fixture files proposed in this article is very similar to the *.phpt file format, but in the end the format can be completely arbitrary and project-specific.

Benefits:

  • Start with empty "--EXPECT--" section, and have this part generated automatically.
  • Automatically update tests after behavior change, e.g. after a bug was fixed, or when BC-breaking changes were introduced.
  • Frequently update fixtures during early development, when behavior is not really stable yet.
  • No more copy/paste from cli diff output.

Disadvantages:

  • No built-in support in phpunit. Requires custom code. This does not scale well, if I want to do it in multiple projects.
  • One cannot target the *.phpt file as a cli parameter.

Question

Is there a built-in way to auto-update those *.phpt test files with phpunit?
Or should I perhaps use something other than phpunit?

donquixote
  • 4,877
  • 3
  • 31
  • 54
  • Just a quick update: I am currently using self-updating tests that are _not_ using the .phpt format but that write to yml files. This does not solve the problem described in this QA where I specifically asked for self-updating phpt tests. So let's keep this open. – donquixote Oct 15 '21 at 14:18

0 Answers0