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

Add RunnableTestCase to run fixed code in a test #3089

Merged
merged 1 commit into from
Mar 29, 2020

Conversation

paslandau
Copy link
Contributor

@paslandau paslandau commented Mar 28, 2020

  • added RunnableTestCase::assertOriginalAndFixedFileYieldSameResult($file)
    that takes a fixture file and expects fixture classes that implement the
    new RunnableInterface which exposes a run() method
  • the fixture class is dynamically renamed to avoid naming conflicts
    and loaded via eval()
  • fixtures that don't implement the RunnableInterface are ignored
    otherwise the run() method is called on the original class as well
    as on the fixed one and the output is expected to be equal
    (via assertSame()) by default
  • the default assertion behavior can be changed via $assertion callable

@paslandau
Copy link
Contributor Author

@TomasVotruba
Is there anything else apart from composer run complete-check that I can run locally to verify the code? Was kinda suprised to see the GH actions fail because everything looked fine locally :)

@TomasVotruba
Copy link
Member

The composer command doesn't include all the actions.
Could you add missing scripts there?

@paslandau
Copy link
Contributor Author

paslandau commented Mar 28, 2020

Could you add missing scripts there?

Had a quick look at the .github/workflows but I don't quite understand the setup and I don't feel comfortable changing the composer script without fully understanding the consequences.

Is this something that you can provide quickly on master?

Happy to fix any open issues in my PR as long as I get the corresponding feedback locally as the feedback loop via github actions only takes too long for that :/

@paslandau paslandau force-pushed the run_fixtures_in_tests branch from 7939a04 to 23485f2 Compare March 28, 2020 19:59
@TomasVotruba
Copy link
Member

It's usually the last run in each workflow.
E.g.

- run: composer check-fixtures

I might get into it in 1-2 hours and update the composer.json with full check

@TomasVotruba
Copy link
Member

TomasVotruba commented Mar 28, 2020

It seems most of issues are gone.

To apply Rector, just run:

composer rector-ci-fix

It's seems there is a bug in one old Rector rule though, I'll check it.

EDIT: fixed 17f9efc

@paslandau
Copy link
Contributor Author

paslandau commented Mar 29, 2020

How long does a composer complete-check now take for you? Running all rector sets is pretty slow for me and actually just failed with

[...]
Set "safe07" is OK
Set "shopware55" is OK
Set "shopware56" is OK
Set "silverstripe" is OK

  [Symfony\Component\Process\Exception\ProcessTimedOutException]
  The process "php ci/run_all_sets.php" exceeded the timeout of 300 seconds.

(Running in Docker on a Windows 10 Host)

@paslandau paslandau force-pushed the run_fixtures_in_tests branch from 23485f2 to b4889a6 Compare March 29, 2020 08:45
@TomasVotruba
Copy link
Member

I never run it locally, so I don't know.

How could be the performance improved there?

$loadable = (string) Strings::replace($loadable, '#\\s*namespace.*;#', '');
$loadable = (string) Strings::replace($loadable, '#class\\s+(\\S*)\\s+#', sprintf('class %s ', $className));
eval($loadable);
if (is_a($className, RunnableInterface::class, true)) {
Copy link
Member

Choose a reason for hiding this comment

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

This should be right under $className = $this->getTemporaryClassName(); with early reaturn,
as it can skipp all the Strings::replace()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't think that would work.
is_a will only be able to recognize the class after eval has made it available to php

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I didn't notice. Is there a way to handle this withou eval()?
That opens big black door.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, to be fair you already have an unchecked require_once in \Rector\Core\Testing\PHPUnit\FixtureSplitter::splitContentToOriginalFileAndExpectedFile (which is basically the same) :)

Only way to work around this is to split the "orignal" file and the "fixed" files into separate files and also use different class names and proper autoloading. However, this clashes with the current way fixtures are written and checked (e.g. it won't be possible to do a full "equality" check of the contents any longer because the class names will be different - which I think is very helpful).

If you keep the "goal" in mind (i.e. "reuse existing test files and simply add an additional layer of validation), I'm afraid there is no way around using something that dymically injects code.

- added RunnableTestCase::assertOriginalAndFixedFileYieldSameResult($file)
that takes a fixture file and expects fixture classes that implement the
new RunnableInterface which exposes a run() method
- the fixture class is dynamically renamed to avoid naming conflicts
and loaded via eval()
- fixtures that don't implement the RunnableInterface are ignored
otherwise the run() method is called on the original class as well
as on the fixed one and the output is expected to be equal
(via assertSame)
@paslandau paslandau force-pushed the run_fixtures_in_tests branch from b4889a6 to 016124b Compare March 29, 2020 12:09
@paslandau
Copy link
Contributor Author

@TomasVotruba
anything still open/unclear here?

@TomasVotruba
Copy link
Member

Looks good me to me 👍

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