Question

Unit test cases in pimcore, getting error while executing test cases

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

https://imgur.com/a/S2i833s

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


Submit an answer


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!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
December 13, 2023

Hi there,

It looks like you are encountering two main issues while setting up unit testing with Codeception in your Pimcore v6.6 project:

  1. Undefined Constant PIMCORE_PROJECT_ROOT Error: This error suggests that the constant PIMCORE_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:

    • Bootstrap File: Ensure your tests/bootstrap.php file correctly initializes the Pimcore environment. The call to \Pimcore\Bootstrap::setProjectRoot(); should define PIMCORE_PROJECT_ROOT. Make sure this method is being invoked correctly and that it can determine the project root path.
    • Test Configuration File: In codeception.dist.yml, the bootstrap setting should point to your bootstrap file. Make sure it’s correctly set to bootstrap: bootstrap.php (assuming your bootstrap file is tests/bootstrap.php).
  2. Service Container Null Error:

    • This error usually occurs when the service dependencies are not correctly injected or when the test environment doesn’t have access to the Symfony service container.
    • Mocking or Service Container Initialization: If your service depends on other services or parameters from the container, you need to mock these dependencies in your test or initialize the service container. You can use Pimcore’s testing tools or Symfony’s framework for testing to set up a test kernel and access the service container.
    • Dependency Injection: Review how dependencies are injected into 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:

  1. Revise bootstrap.php: Make sure it initializes the Pimcore environment correctly. This involves setting the project root and bootstrapping Pimcore:

    // tests/bootstrap.php
    define('PIMCORE_PROJECT_ROOT', realpath(__DIR__ . '/..'));
    include PIMCORE_PROJECT_ROOT . '/vendor/autoload.php';
    
    \Pimcore\Bootstrap::setProjectRoot(PIMCORE_PROJECT_ROOT);
    \Pimcore\Bootstrap::bootstrap();
    
  2. Update codeception.dist.yml: Ensure it points to your bootstrap file correctly:

    settings:
        bootstrap: bootstrap.php
        # Other settings...
    
  3. Modify Test Case for Dependency Injection: Mock or provide the necessary dependencies for your PdfImportService in the test case:

    public function testGetFilesArray()
    {
        // Mock dependencies or create them as needed
        $dependency = $this->createMock(DependencyClass::class);
        // Configure your mocks as needed
    
        $pdfService = new PdfImportService($dependency);
        $this->assertFalse($pdfService->getFilesArray());
    }
    
  4. 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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.