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

Throw exception if in-memory database when running tests #212

Merged
merged 2 commits into from
Nov 6, 2023
Merged

Conversation

MarioRadu
Copy link
Member

No description provided.

Copy link

codecov bot commented Nov 3, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (2e5c0f1) 71.02% compared to head (9803c56) 71.02%.

Additional details and impacted files
@@            Coverage Diff            @@
##                4.0     #212   +/-   ##
=========================================
  Coverage     71.02%   71.02%           
  Complexity      652      652           
=========================================
  Files           109      109           
  Lines          2454     2454           
=========================================
  Hits           1743     1743           
  Misses          711      711           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@alexmerlin alexmerlin left a comment

Choose a reason for hiding this comment

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

The below is not a change request, just my insights on this case.

I would do something like this:

    /**
     * @throws NotFoundExceptionInterface
     * @throws ContainerExceptionInterface
     */
    public function setUp(): void
    {
        $this->enableTestMode();

        $this->initContainer();
        $this->initApp();
        $this->initPipeline();
        $this->initRoutes();

        $this->ensureTestMode();

        if (method_exists($this, 'runMigrations')) {
            $this->runMigrations();
        }
        if (method_exists($this, 'runSeeders')) {
            $this->runSeeders();
        }
    }

    /**
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    protected function ensureTestMode(): void
    {
        if (! $this->isTestMode()) {
            throw new RuntimeException(
                'You are running tests, but test mode is NOT enabled. Did you forget to create local.test.php?'
            );
        }

        if (! $this->getEntityManager()->getConnection()->getParams()['memory'] ?? false) {
            throw new RuntimeException(
                'You are running tests in a non memory database. Did you forget to create local.test.php?'
            );
        }
    }

Note:

  • I added ['memory'] ?? false because depending on the connection, the memory key might not be available (for example, with the real connection seems to return null)
  • $this->getEntityManager()->getConnection()->getParams() is not a reliable solution because getParams is an internal method. Unfortunately the connection does not seem to offer a clean solution so another approach might be checking the config value under doctrine->connection->orm_default->params. The problem with this if there are somehow multiple connections used in a project, then we might look at the wrong config values (maybe it's not an actual issue, I'm just thinking out loud 😃 ).

@arhimede arhimede merged commit 5520c60 into 4.0 Nov 6, 2023
12 checks passed
@alexmerlin alexmerlin deleted the issue-211 branch November 7, 2023 06:11
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.

None yet

3 participants