Unable to mock function to return an empty array.
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Stale
- Tech stack
- php
- Domain
- testing-qa
Research direction
Start with the reduced test in tests/php/SubscriptionsTest.php and run phpunit --filter test_return_empty_array under the reported PHP and PHPUnit versions. Trace the mocked call through the WP_Mock test path shown in the failure, especially vendor/10up/wp_mock/php/WP_Mock/Tools/TestCase.php. Done means the test receives an empty array rather than NULL and retains the configured return type.
Written by the indexing model from the issue text.
Description
Describe the bug
In Distributor, I am attempting to mock a function to return an empty array:
However, the return value is coming back as NULL.
As an alternative approach, I've tried setting the return attribute as a callable function(){ return []; } but the same issue occurs.
To Reproduce
- As a reduced test case, create the test:
public function test_return_empty_array() { $post_id = 1; \WP_Mock::userFunction( '\Distributor\Utils\peters_test_function', [ 'return' => [], ] ); $this->assertSame( [], \Distributor\Utils\peters_test_function( $post_id ) ); } - Run
phpunit --filter test_return_empty_array - PHP Unit will throw a failure:
PHPUnit 8.5.26 #StandWithUkraine F 1 / 1 (100%) Time: 2.11 seconds, Memory: 18.00 MB There was 1 failure: 1) Distributor\SubscriptionsTest::test_return_empty_array Failed asserting that null is identical to Array &0 (). /vagrant/content/plugins/distributor/tests/php/SubscriptionsTest.php:18 /vagrant/content/plugins/distributor/vendor/10up/wp_mock/php/WP_Mock/Tools/TestCase.php:307 phpvfscomposer:///vagrant/content/plugins/distributor/vendor/phpunit/phpunit/phpunit:97
Expected behavior
Returned values retain type.
Screenshots
If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
- PHPUnit 8.5.26
- PHP 7.4.30
- OS: Ubuntu 22.04 LTS
Additional context
- Dominant language
- PHP
- Stars
- 714
- Forks
- 72
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from 10up/wp_mock
-
`expectActionAdded( array( Functions::type( class-string ), 'method' ) )` fails when same method name used twicePossibly taken @faisalahammad claimed this 126 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 50/100
-
PHPCS reporting error on dependent library nikic/php-parser, hamcrest/hamcrest-phpPossibly taken @faisalahammad claimed this 126 days ago. OpenDependencies
Difficulty 4/5 3-5 days Newbie friendliness 25/100
-
Request: control Strict Mode per testPossibly taken @BrianHenryIE claimed this 311 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 45/100
-
Pass in Closure for HooksPossibly taken @faisalahammad claimed this 126 days ago. OpenEnhancement Question
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Dependencies PHPUnit
Difficulty 5/5 Over a week Newbie friendliness 30/100
Similar issues
-
wpvc
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
10up/autoshare-for-twitter#438 ·
-
wpvc
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
10up/Ad-Refresh-Control#240 ·
-
wpvc
Difficulty 1/5 1-3 hours Newbie friendliness 62/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Backend bug severity: low
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
RunningJoke/allipunkte#74 ·