PHP Testing Tutorial: Test Doubles - PHPUnit Mocks and Stubs

preview_player
Показать описание
These are ESSENTIAL PHP testing skills! Learn how to replace PHP objects with mock objects so that you can test your code in isolation. Learn the difference between PHPUnit Stubs and PHPUnit MockObjects and all of the cool things you can do with them to make testing easier.

💻Source code here:

🆓 Join GaryClarke Tech and claim your $49 Docker + PHP course for FREE here:

⭐This is a sample from my Testing PHP course. Mocking is put to practical use in the full course:

👇Follow me on Social Media:

💕Show support:

Chapters:
0:00 Introduction to test doubles
1:45 Install PHPUnit and setup autoloading
3:50 Create a test file
5:17 Create a mock object
8:35 Set mock expectations
11:45 Faking method-returned values
13:12 Default mock method return values
16:35 Mocking consecutive method returns
20:38 Mock throwing exceptions
23:47 Complex mock returns using callbacks
28:20 Test correct method args passed to mocked method
30:30 Passing multiple args to mocked method
33:50 Complex mock method args using callbacks
40:13 Custom mocks using the Mock Builder
Рекомендации по теме
Комментарии
Автор

I have adhd and have trouble with getting comfortable with tutorial styles and formats. Your video here is just bliss to me. Thank you thank you!
Please don't stop making videos!

sofascialistadankulamegado
Автор

This is the best video tutorial on the internet talking about mocks in PHP, just amazing. Thank you.

anasselmoussaoui
Автор

This is actually an amazing subject that I’m currently doing

micasntna
Автор

You explain the Mock in unittest that very easy understanding, you have reproduced difference use cases are very important to understand!!
Thank you so much!

PaulKeeWong
Автор

Thanks for this great video.

So basically when we type hint the return type of the method that we're testing, and we call it on the mocked object we'll get null casted to that type.

obadaalzidi
Автор

this is a very informative video, please don't stop making videos about php because your way of explaining is very good, god bless you

wpwilds
Автор

i want to hit the like button for every test passed. Thanks, the best tutorial available on this subject.

samhk
Автор

This is great. I know and use it now, but wish I had known sooner

lmartyn
Автор

Thanks for video. I have one question: how can i test the private or protected methods of class which return void?

МухаметджинТимур
Автор

Hello would you please explain how to mock protect method and return specific value

wisamalkhatib
Автор

We have a PDO class at work that takes care of creating a PDO db instance. Is it possible to mock that?

NoahNobody
Автор

What you do @Gary Clarke - is defenatly amazing, I'm watching carefully tutorials where the key feature is to make "TESTS", and have no idea what the test are really needed for? Maded to check, is sometching that we anyway overriding each time will bring proper value? I dont know, are test are bringed to life just because to keep programers at work ???

qkenkenu
Автор

I have a trait which is used for handling HTTP requests. Is it possible to mock and test a trait? Really like your video and have been able to apply it so far for other areas of my app, but now I've hit a wall where I'm unsure how to handle traits. It's worth noting that I have a get() method which calls a protected makeRequest() method inside it - is it possible to test nested method calls? Again, can't think how this would work

markwilkinson