Unit testing static methods in PHP

preview_player
Показать описание

You'll probably come across some articles and opinions that say static methods can't be tested, and that using static methods should be avoided. While there are problems with static methods, as far as testing goes, this isn't true - you can test static methods. The problem with static methods comes when trying to mock them. In this video we'll look at an example of how to test a static method.
Рекомендации по теме
Комментарии
Автор

Hello Dave, glad you've created such video on this subject. I have the following code:


public function getExcelDataByFilePath(string $filePath): array
{
/** @var Xlsx $reader */
$reader =

$spreadsheet = $reader->load($filePath);

return
}


How can I test the IOFactory::createReader() call? Thanks!

dandan-jsir
Автор

Having ran into this issue it hasn't been my concern of trying to test my static methods specifically but actually writing tests for my code that then use those static methods. Which If I am then wanting to test now becomes a mess because I can't mock the static method. So that being the case what is the benefit of using a static method in the first place? Do you have recommendations on how to navigate testings those methods that implement calls to static methods?

BrassKneecap
visit shbcf.ru