Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNT Handle LogicExceptions when calling setI18nLocale() #11576

Merged

Conversation

emteknetnz
Copy link
Member

@emteknetnz emteknetnz commented Jan 23, 2025

Issue silverstripe/.github#357

Fixes https://github.com/silverstripe/silverstripe-behat-extension/actions/runs/12938868907/job/36089987699?pr=295#step:12:101

1) SilverStripe\BehatExtension\Tests\SilverStripeContextTest::testGetRegionObjThrowsExceptionOnUnknownSelector LogicException: getItemPath returned null for SilverStripe\BehatExtension\Tests\SilverStripeContextTest.

SilverStripeContextTest does some weird stuff with mocking. In these sorts of scenarios I'm assuming the type of unit testing that we're running won't be reliant upon having to set the en_US locale in order to get the test to always pass.

Kitchen sink CI run with this PR - https://github.com/creative-commoners/recipe-kitchen-sink/actions/runs/12979001917

Copy link
Member

@GuySartorelli GuySartorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like SilverStripeContextTest is the only situation we expect this to fail - I'd be much more comfortable with a if ($this instanceof SilverStripeContextTest) { return; } at the top of the method.

@emteknetnz emteknetnz force-pushed the pulls/5/logicexception branch from 7869f1a to 4daf118 Compare January 23, 2025 23:54
@emteknetnz
Copy link
Member Author

emteknetnz commented Jan 23, 2025

Updated. I've used a string classname rather than importing SilverStripeContextTest and class_exists() and SilverStripeContextTest::class syntax because behat-extension in not a required module, and nor should it be IMO

src/Dev/SapphireTest.php Outdated Show resolved Hide resolved
// LogicException when calling getCurrentRelativePath(), and we can be sure
// do not require the locale to be set to en_US
$excluded = [
'SilverStripe\\BehatExtension\\Tests\\SilverStripeContextTest',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a use statement - mostly because when searching for FQCN (e.g. if the namespace changes) we're likely to put SilverStripe\BehatExtension\Tests\SilverStripeContextTest in the IDE, not double \\

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that a use statement doesn't require that the class exists. It just is a reference for the IDE to know that when it sees the used name, it should expand to the FQCN.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it to single \ rather than double \\ PHP doesn't mind which way you do it, I just prefer the double

I'm not going to import the class as what this PR is already doing is bad enough, importing a subclass from an optional module into the base class is just gross

Copy link
Member

@GuySartorelli GuySartorelli Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should discuss this in person to get us on the same page about it, because to me a hardcoded string is the gross thing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@emteknetnz emteknetnz Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

importing a subclass from an optional module into the base class is just gross

IMO we just shouldn't do this. it's pure code smell. The string FQCN is less offensive, though I still don't actually like it.

Those examples you've provide are a different scenario where they are all optional modules (unlike framework, which is the base module), and the classes with the class_exists() checks are all classes that becomes available on the condition of another optional module (a sibling module) being installed

Non gross solution here is to revert it back to how it was before and just catch a LogicException

OR, if you're concered that's too blunt

Add some new protected API into SapphireTest protected bool $doUpdateI8nLocale = true (or something named similarly), set that to false in that behat-extension unit test, and then (maybe, not sure if needed) add a conflict in silverstripe/framework composer.json for silverstripe/behat-extensions so that the --prefer-lowest build doesn't break

Let me know what solution you'd like (excluding importing the FCQN :-) )

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If by "importing the FQCN" you mean using a use statement, then we need to discuss in person because that's the solution I'd like.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we'll never agree on that for some reason (I really really don't understand your perspective on this at all)..... so if we absolutely can't do that for some obscure reason, the protected property would be preferable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have updated PR, and create additional PR for behat-extension

src/Dev/SapphireTest.php Outdated Show resolved Hide resolved
@emteknetnz emteknetnz force-pushed the pulls/5/logicexception branch 2 times, most recently from e420dff to 5ad5cb4 Compare January 24, 2025 00:32
Copy link
Member

@GuySartorelli GuySartorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@GuySartorelli GuySartorelli merged commit 79129e3 into silverstripe:5 Jan 27, 2025
17 checks passed
@GuySartorelli GuySartorelli deleted the pulls/5/logicexception branch January 27, 2025 00:00
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