Problem/Motivation

I'm trying to test a service that uses the connection service.
Call:

$query->execute()->fetchAll();

Test

TestHelpers::service('database', (new ConnectionStub()));
    TestHelpers::service('cache.data', (new CacheBackendInterfaceStub()));
    $customColors = TestHelpers::service('custom_colors.get_colors', initService: TRUE);

Note that the cacheBackendInterfaceStub is a partial stub from #3442210: What is the proper way to inject the cache backend?

Error

There was 1 error:

1) Drupal\Tests\custom_colors\Unit\customColorsTest::testGetColors
Error: Call to a member function fetchAll() on array

/var/www/html/web/modules/custom/custom_colors/src/customColors.php:70
/var/www/html/web/modules/custom/custom_colors/tests/src/Unit/customColorsTest.php:40
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestResult.php:729
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestSuite.php:685
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestSuite.php:685
/var/www/html/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:651
/var/www/html/vendor/phpunit/phpunit/src/TextUI/Command.php:146
/var/www/html/vendor/phpunit/phpunit/src/TextUI/Command.php:99
/var/www/html/vendor/bin/phpunit:122

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

nicxvan created an issue. See original summary.

Murz made their first commit to this issue’s fork.

murz’s picture

Status: Active » Needs review

Yeah, by default the ConnectionStub always returns the array, that is wrong.
I fixed this by returning a mock of PDOStatement for select and integers for insert and delete actions.
See the MR.

Please test it and report if it works well for you.

Actually, to test your code, you should manually make a stub of the execute response, using this approach:

    $database = TestHelpers::service('database');
    $yourExpectedResult = [1, 2, 3];
    $executeMock = $this->createMock(\PDOStatement::class);
    $executeMock->method('fetchAll')->willReturn($yourExpectedResult);
    $database->stubSetExecuteHandler(function () use ($executeMock) {
      return $executeMock;
    }, 'select');
    $result = $database->select('table1')->execute()->fetchAll();
    $this->assertEquals($yourExpectedResult, $result);

  • Murz committed 276e6f06 on 1.4.x
    Issue #3447490: ConnectionStub throws error when fetchAll is called on...
murz’s picture

I tested and merged this. If it will not help to you, please reopen the issue.

murz’s picture

Status: Needs review » Fixed

  • Murz committed 109992a6 on 1.4.x
    Issue #3447490 by Murz, nicxvan: ConnectionStub throws error when...
nicxvan’s picture

I'm no longer getting this error, I added some more detail in #3442210: What is the proper way to inject the cache backend? with what I am experiencing now.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

nicxvan’s picture