Skip to content

Return a string from the mocked recursiveTagParse#101

Merged
malberts merged 1 commit into
masterfrom
fix/parser-mock-string-return
Jun 12, 2026
Merged

Return a string from the mocked recursiveTagParse#101
malberts merged 1 commit into
masterfrom
fix/parser-mock-string-return

Conversation

@malberts

@malberts malberts commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

Why

MediaWiki master declares Parser::recursiveTagParse() and recursiveTagParseFully()
with a string return type (the parameter stays untyped). The component tests mock these
with returnArgument(0), echoing the input verbatim. When a component parses an attribute
value that is a bool or int, the mock returns that non-string and PHPUnit's typed mock
throws:

TypeError: Mock_Parser::recursiveTagParse(): Return value must be of type string, true returned

Why an attribute value is a bool

This is not an unrealistic fixture. ParserRequest builds its attribute array from what the
parser passes in, and for the parser-function syntax a valueless option is stored as a PHP
true (an empty one as false) by ParserRequest::getKeyValuePairFrom(). So
[ 'disabled' => true ] is exactly what {{#bootstrap_button:text|disabled}} produces; the
tag-extension syntax (<bootstrap_button disabled>) yields a string instead. Both reach
recursiveTagParse().

In production the real recursiveTagParse() coerces such a value and returns a string, so it
works. The mock's returnArgument(0) did not coerce, which is the only reason the typed
return now fails.

What

Make the recursiveTagParse / recursiveTagParseFully mocks return (string)$text, so the
mock behaves like the real parser and honours the string return contract. The realistic
bool/int inputs are kept and no assertions change (true stays truthy as "1", 0 stays
falsy as "0"). The renderErrorMessage mock is left as-is.

Verification

Test-only change. On MediaWiki master the component tests that hit the TypeError now pass;
the older rows are unaffected, since the cast is a no-op for the string inputs they use.

MediaWiki master declares Parser::recursiveTagParse() and
recursiveTagParseFully() with a `string` return type. The component test
mocks stub them with returnArgument(0), echoing the input verbatim. When a
component passes a non-string attribute value (a bool or int) to the parser,
the mock returns that non-string and PHPUnit's typed mock throws "Return
value must be of type string".

Cast the echoed value to string so the mocks honour the method's contract.
Behaviour is unchanged for string inputs, and the renderErrorMessage mock is
left as-is.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@malberts malberts marked this pull request as ready for review June 12, 2026 10:34
@malberts malberts merged commit 391390b into master Jun 12, 2026
3 of 6 checks passed
@malberts malberts deleted the fix/parser-mock-string-return branch June 12, 2026 11:48
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.

1 participant