Unable to mock function to return an empty array.

Aperta
#173 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
php
Ambito
testing-qa

Direzione di ricerca

Inizia con il test ridotto in tests/php/SubscriptionsTest.php ed esegui phpunit --filter test_return_empty_array con le versioni di PHP e PHPUnit indicate. Segui la chiamata mockata attraverso il percorso di test WP_Mock mostrato nell’errore, in particolare vendor/10up/wp_mock/php/WP_Mock/Tools/TestCase.php. Il lavoro è completato quando il test riceve un array vuoto anziché NULL e mantiene il tipo di ritorno configurato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
PHP
Stelle
714
Fork
72
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di 10up/wp_mock

Tutte le issue di 10up/wp_mock

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.