Unable to mock function to return an empty array.

オープン
#173 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
php
領域
testing-qa

調査の方向性

tests/php/SubscriptionsTest.php の縮小したテストから始め、報告されている PHP および PHPUnit のバージョンで phpunit --filter test_return_empty_array を実行します。失敗に示されている WP_Mock のテストパス、特に vendor/10up/wp_mock/php/WP_Mock/Tools/TestCase.php を通じてモックされた呼び出しを追跡します。テストが NULL ではなく空の配列を受け取り、設定された戻り値の型を維持すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

Describe the bug
In Distributor, I am attempting to mock a function to return an empty array:

https://github.com/10up/distributor/blob/74ba9c5110de1dfa3415e76eab7d7ffc87b19e49/tests/php/SubscriptionsTest.php#L321-L325

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

  1. 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 ) );
     }
    
  2. Run phpunit --filter test_return_empty_array
  3. 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

主要言語
PHP
スター
714
フォーク
72
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

10up/wp_mock のほかの issue

10up/wp_mock の issue をすべて見る

似ている issue

PHP の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。