All contributions

spiral/testing

Spiral Testing

A testing toolkit for applications on the Spiral PHP framework: fake HTTP requests, console commands, the scaffolder and the container.

Language
PHP
GitHub stars
7
Merged changes
3
Project site
spiral.dev

What we changed

withActor() keeps the auth transports configured in the app, so a test with a fake user no longer ends in a 500 error. Console and scaffolder output assertions ignore line-ending differences, so text with CRLF passes the same way as with LF.

What this work shows

  • HTTP auth in Spiral (TransportRegistry)
  • PHPUnit assertions independent of line endings
  • Console command and scaffolder tests
What was broken

withActor() ended in a 500 error

Faking a logged-in user with withActor() replaced the whole TransportRegistry with a single transport named testing. An app with its own transport, such as header, got an "Undefined auth transport header" error from AuthTransportMiddleware.

How we fixed it

Transport names are kept

The fake registry takes over the names of all transports configured in the app and maps each one to the fake header transport. We added a test with a custom transport and a type annotation for Psalm.

#91

What was broken

The console assertion depended on line endings

assertConsoleCommandOutputContainsStrings() compared text exactly, so an expected string with \r\n did not match output with \n, and the other way round. An issue in the project asked for an assertion that ignores line endings.

How we fixed it

A comparison that ignores line endings

The assertion now uses PHPUnit's assertStringContainsStringIgnoringLineEndings. We added a test command, multiline, that prints two lines, and a test that expects them with \r\n.

#94

What was broken

The scaffolder had the same problem

assertScaffolderCommandSame() and assertScaffolderCommandContains() checked the command output with a plain assertStringContainsString. Text with different line endings failed even when the content was the same.

How we fixed it

Both scaffolder assertions ignore line endings

Both output assertions in InteractsWithScaffolder now use the comparison that ignores line endings. New tests generate a filter with create:filter and expect the message with \r\n.

#95

What it taught us

A fake in a testing toolkit has to keep the application's configuration rather than overwrite it, or the test fails where the real app works. Our text assertions ignore line endings, so a test result does not depend on the operating system.

More about this technology: PHP

Other projects we write code for

Describe your project.

A free quote comes back within an hour during working hours.