Hi Team,
pimcore v 6.6
I’m trying to unit test by codeception, I have installed codception and tests/ folder created under my project directory. But when im calling the service function getting below error
Error attached:
**[PHPUnit\Framework\Exception] Use of undefined constant PIMCORE_PROJECT_ROOT - assumed 'PIMCORE_PROJECT_ROOT' (this will throw an Error in a future version of PHP)
src/GenericBundle/Service/PdfImportService.php:31**
File name for test is **PdfImportServiceTest.php**
public function testGetFilesArray()
{
$pdfService = new PdfImportService();
$this->assertFalse($pdfService->getFilesArray());
}
I have added bootstrap.php
file inside tests/
path. Code is here:
<?php
// tests/bootstrap.php
include "./vendor/autoload.php";
\Pimcore\Bootstrap::setProjectRoot();
\Pimcore\Bootstrap::bootstrap();
Added another file named codeception.dist.yml
:
namespace: Tests
support_namespace: Support
actor_suffix: Tester
paths:
tests: .
output: ./_output
data: ./Support/Data
support: ./Support
envs: ./_envs
settings:
bootstrap: _bootstrap.php
colors: true
params:
- env
extensions:
enabled:
- Codeception\Extension\RunFailed
When I’m executing this test case, it is calling constructor of actual service file and there we have defined constant path for the file, but this test case is not working. Actual function from the backend is working
In some services im getting get container null error… Please check the link for error.
I followed pimcore unit testing document + symfony documnet + codception document. But it didn’t work for me. I’m stuck with this from 3 days.
Please let me know how to resolve it.
Thanks in advance
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Hi there,
It looks like you are encountering two main issues while setting up unit testing with Codeception in your Pimcore v6.6 project:
Undefined Constant
PIMCORE_PROJECT_ROOT
Error: This error suggests that the constantPIMCORE_PROJECT_ROOT
is not defined in the context of your test environment. This constant is usually defined in Pimcore’s bootstrap process. To resolve this:tests/bootstrap.php
file correctly initializes the Pimcore environment. The call to\Pimcore\Bootstrap::setProjectRoot();
should definePIMCORE_PROJECT_ROOT
. Make sure this method is being invoked correctly and that it can determine the project root path.codeception.dist.yml
, thebootstrap
setting should point to your bootstrap file. Make sure it’s correctly set tobootstrap: bootstrap.php
(assuming your bootstrap file istests/bootstrap.php
).Service Container Null Error:
PdfImportService
. If it relies on services from the container, consider passing these dependencies through the constructor or setters in your test case.Here’s a revised approach to setting up your test environment:
Revise
bootstrap.php
: Make sure it initializes the Pimcore environment correctly. This involves setting the project root and bootstrapping Pimcore:Update
codeception.dist.yml
: Ensure it points to your bootstrap file correctly:Modify Test Case for Dependency Injection: Mock or provide the necessary dependencies for your
PdfImportService
in the test case:Run Tests: Execute your tests and see if the issues persist.
If problems continue, add debug statements in your bootstrap file and service constructor to ensure the environment is set up correctly and dependencies are being resolved.
Let me know how it goes!
Best,
Bobby