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

Injection in ViewHelper is problematic for phpunit functionnal tests #85

Closed
asakurayoh opened this issue Oct 29, 2024 · 7 comments · Fixed by #87
Closed

Injection in ViewHelper is problematic for phpunit functionnal tests #85

asakurayoh opened this issue Oct 29, 2024 · 7 comments · Fixed by #87

Comments

@asakurayoh
Copy link

Hi.
Not sure if there is a problem on how I test my things, but I get this error when running my functionnal tests (with TYPO3 testing-framework):
Error: Typed property Praetorius\ViteAssetCollector\ViewHelpers\Asset\ViteViewHelper::$viteService must not be accessed before initialization

I'm in TYPO3 v12.

I use "executeFrontendSubRequest" to call my pages to get the body and test against the result.

@s2b
Copy link
Owner

s2b commented Oct 29, 2024

Thank you for this report. Unfortunately, I currently don't have a similar setup to test this. It seems to be a problem with dependency injection in the sub-request. So first some general questions:

  • do you use the latest version of the testing-framework (probably ^8 for TYPO3 v12)?
  • do you set any special configuration in your test cases?
  • do other ViewHelpers with DI work, like <f:asset.script />, which injects AssetCollector?
  • do other ViewHelpers that use TYPO3 services work, like <f:image />, which uses GeneralUtility::makeInstance(ImageService::class)?

@asakurayoh
Copy link
Author

  • Yes, I have updated the testing-framework to 8.2.3 to validate there was not a bug already fixed.
  • It's in a demo site pretty simple, no special configuration.
  • I have tested with <f:asset.css> and use a css in my sitepackage. (it also inject AssetCollector) and not problems.
  • I have also use f:image, and no problem too.

My setup is a very simple simple from which we start our project, nothing fancy.
My tests really just check that a text is show on an VERY basic page (It really just examples of tests).
I've just reviewed some core tests and I seem to do it correctly. I've validate your ViewHelper and the injection and has it is from a ViewHelperInterface, normally the injection is activated (public: true). So not sure where is the problem :/

Thanks!

@s2b
Copy link
Owner

s2b commented Oct 30, 2024

And the extension is loaded in your test using testExtensionsToLoad?

@asakurayoh
Copy link
Author

asakurayoh commented Oct 30, 2024

Ho. I didn't think of that!
Yeah, now t hat work.

I now have Praetorius\ViteAssetCollector\Exception\ViteException: Vite manifest file "_assets/vite/.vite/manifest.json" was resolved to "/var/www/html/public/typo3temp/var/tests/functional-ae0ef2a/_assets/vite/.vite/manifest.json" and cannot be opened. but that is understandable...

I suppose I will just create a mock manifest.json for my functionnal testsé

Thanks for the help!

@asakurayoh
Copy link
Author

asakurayoh commented Oct 30, 2024

Ok, I finally got it to work.
Here how I did it, if it help someone:

  1. In the functionnal test, add the extension vite_asset_collector to $testExtensionsToLoad
  2. Create a fixture for the manifest.json file in Tests/Fixtures/manifest.json:
{
    "Main.entry.js": {
        "css": [],
        "file": "",
        "isEntry": true,
        "src": ""
    }
}
  1. Use $pathsToProvideInTestInstance to copy the manifest:
protected array $pathsToProvideInTestInstance = [
    'typo3conf/ext/site_package/Tests/Fixtures/manifest.json' => 'fileadmin/Fixtures/manifest.json',
];
  1. Set the extension configuration to use this manifest:
$this->get(ExtensionConfiguration::class)->set('vite_asset_collector', [
    'useDevServer' => '0',
    'devServerUri' => 'https://localhost:5173',
    'defaultManifest' => 'fileadmin/Fixtures/manifest.json',
]);
  1. Now the frontend will load the manifest.json in Fixtures without real files in it, but it works.

I don't think there is a "good" way to fix this with the actual code. Something in the viewhelper that could just ignore everything if set; like a "ignore" configuration?

Thanks.

@s2b
Copy link
Owner

s2b commented Oct 30, 2024

You could also enable the dev server, then probably no files need to exist.

@asakurayoh
Copy link
Author

You could also enable the dev server, then probably no files need to exist.

Yeah, you're right!

Just this is enough:

  1. In the functionnal test, add the extension vite_asset_collector to $testExtensionsToLoad
  2. Set the extension configuration to use this manifest:
$this->get(ExtensionConfiguration::class)->set('vite_asset_collector', [
    'useDevServer' => '1',
]);

s2b added a commit that referenced this issue Oct 31, 2024
s2b added a commit that referenced this issue Oct 31, 2024
s2b added a commit that referenced this issue Oct 31, 2024
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 a pull request may close this issue.

2 participants