Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static methods are evil, then how come Laravel uses so much static calls?

Tags:

php

tdd

laravel

If you have been into test-driven driven development especially in PHP, you probably know that static methods are hard to test. Mocking static calls have been deprecated with PHPUnit.

How to do guys mock static calls? And if statics are bad for TDD, how Laravel tests all its static calls?

like image 576
Hyder B. Avatar asked Oct 18 '25 04:10

Hyder B.


1 Answers

Yes, Laravel uses a lot of facades, but you can easily test everything. I'd recommend you to read a "Laravel Testing Decoded" by Jeffrey Way, it will answer all of your questions.

If you don't want to use facades, you don't have to, because Laravel offers a lot of global helpers (like redirect() for Redirect::, auth() for Auth::, session() for Session:: etc). Also, you can always use DI:

public function index(User $user)
{
    $users = $user->all();

Instead of:

$users = User::all();
like image 173
Alexey Mezenin Avatar answered Oct 20 '25 18:10

Alexey Mezenin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!