Closed (fixed)
Project:
Test Helpers
Version:
1.4.x-dev
Component:
Code
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
16 May 2024 at 01:59 UTC
Updated:
13 Feb 2025 at 17:43 UTC
Jump to comment: Most recent
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
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
Comment #4
murzYeah, 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:
Comment #6
murzI tested and merged this. If it will not help to you, please reopen the issue.
Comment #7
murzComment #10
nicxvan commentedI'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.
Comment #12
nicxvan commented