orchestral / testbench

Laravel Testing Helper for Packages Development
https://packages.tools/testbench
MIT License
2.1k stars 136 forks source link

Issues with Cache Table and Database Driver When Upgrading to Laravel 11 #402

Closed ItsReddi closed 6 months ago

ItsReddi commented 6 months ago

Description:

After upgrading my package dependencies and codebase to Laravel 11, I encountered an issue when running my tests. The error message indicates a missing cache table:

SQLSTATE[HY000]: General error: 1 no such table: cache (Connection: testing, SQL: select * from "cache" where "key" = :user:3:1:35fe716e453c3d731d5c2366d2cb8 limit 1)

Additional Context:

Upon investigation, I found that Laravel 11 changed the default cache store from 'file' to 'database'. To address this, I updated my TestCase (which extends Orchestra\Testbench\TestCase) to explicitly set the cache store to 'file':

protected function defineEnvironment($app)
{
    $app['config']->set('cache.default', 'file');
}

This change resolved the issue locally, but when running tests in GitHub Actions, I encounter a different error related to the database driver:

Generating autoload files
> @php vendor/bin/testbench package:discover --ansi

In Connection.php line 813:
  could not find driver (Connection: testing, SQL: PRAGMA foreign_keys = OFF;)

In Connector.php line 65:
  could not find driver

Script @php vendor/bin/testbench package:discover --ansi handling the post-autoload-dump event returned with error code 1

PHPUnit:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" bootstrap="tests/bootstrap.php" colors="true" stopOnFailure="false">
  <coverage>
    <report>
      <html outputDirectory="coverage-report" lowUpperBound="35" highLowerBound="70"/>
      <text outputFile="php://stdout" showUncoveredFiles="true"/>
    </report>
  </coverage>
  <testsuites>
    <testsuite name="Test Suite">
      <directory suffix="Test.php">./tests</directory>
    </testsuite>
  </testsuites>
  <logging/>
  <source>
    <include>
      <directory suffix=".php">./src</directory>
    </include>
  </source>
</phpunit>

At this juncture, I find myself perplexed and seeking guidance. Upon encountering this error, I revisited the testbench-core Laravel skeleton provided by your package, expecting that the cache.default configuration would already be aligned with Laravel 11's expectations. It appears, however, that an explicit setting on my part should not have been necessary, as this configuration is presumably handled within the package itself. (I do not have any testbench.yaml) This discrepancy has left me unsure of the correct course of action.

I would appreciate any guidance on resolving these issue.

crynobone commented 6 months ago

Need a way to replicate the issue.

ItsReddi commented 6 months ago

I created a repository please have look here: https://github.com/ItsReddi/laravel-testbench-issue-402/pull/1 That would be the cache issue. ( https://github.com/ItsReddi/laravel-testbench-issue-402/actions/runs/8463227380 )

I also tried to reproduce could not find driver (Connection: testing, SQL: PRAGMA foreign_keys = OFF;) Without any luck but i think it is the same issue that the cache issue is. See: https://github.com/ItsReddi/laravel-testbench-issue-402/pull/3

crynobone commented 6 months ago

@ItsReddi The repository doesn't have the same issue as described in the first thread:

Generating autoload files
> @php vendor/bin/testbench package:discover --ansi

In Connection.php line 813:
  could not find driver (Connection: testing, SQL: PRAGMA foreign_keys = OFF;)

In Connector.php line 65:
  could not find driver

Script @php vendor/bin/testbench package:discover --ansi handling the post-autoload-dump event returned with error code 1
ItsReddi commented 6 months ago

That is correct:

I also tried to reproduce could not find driver (Connection: testing, SQL: PRAGMA foreign_keys = OFF;) Without any luck but i think it is the same issue that the cache issue is. See: ItsReddi/laravel-testbench-issue-402#3

Isn't the cache issue mentioned & shown the same underlying issue?

Description:

After upgrading my package dependencies and codebase to Laravel 11, I encountered an issue when running my tests. The error message indicates a missing cache table:

SQLSTATE[HY000]: General error: 1 no such table: cache (Connection: testing, SQL: select * from "cache" where "key" = :user:3:1:35fe716e453c3d731d5c2366d2cb8 limit 1)

Additional Context:

Upon investigation, I found that Laravel 11 changed the default cache store from 'file' to 'database'. To address this, I updated my TestCase (which extends Orchestra\Testbench\TestCase) to explicitly set the cache store to 'file':

My guess is that orchestral-core laravel skeleton is not used and because of that, cache is failing and discover tries to connect to non existent mysql.

crynobone commented 6 months ago

I need to understand what's triggering cache via package:discover command, in the repository above you explicitly use cache. 2 different usage.

ItsReddi commented 6 months ago

I think the Package discover is not failing because of cache. But the Test itself is failing because of cache, that should not be the case or? (since orchestral-core, defaulting to file)

https://github.com/ItsReddi/laravel-testbench-issue-402/actions/runs/8463227380


But i try to reproduce the discover error too now. I think it has to do something with eloquent. (and because core skeleton is not used it tries to use a database), so same underlying problem as the cache stuff.

crynobone commented 6 months ago

By default, nothing will execute cache in package:discover unless it's been requested. In your tests you still can manually set cache.driver to file but this can't be done via package:discover

ItsReddi commented 6 months ago

We misunderstand us, cache issue has nothing todo with, discover.

In your tests you still can manually set cache.driver to file

Yes but should that be needed? https://github.com/orchestral/testbench-core/blob/3b94cc71f00b9de01597aec5ae962ade106dc5e8/laravel/config/cache.php#L18

I try to reproduce the discover issue now, it is maybe clearer then.

crynobone commented 6 months ago

All commands using php vendor/bin/testbench will load an .env based of https://github.com/orchestral/testbench-core/blob/3b94cc71f00b9de01597aec5ae962ade106dc5e8/laravel/.env.example#L39

ItsReddi commented 6 months ago

Okay sorry for wasting your time. It appears the root of the problem lies with self-hosted action runners within a Kubernetes cluster. For reasons yet to be determined, pdo_sqlite is not loaded exclusively on PHP 8.2, while all other PHP versions function as expected.

Interestingly, on GitHub's hosted runners, everything operates smoothly, including on PHP 8.2.