Mô tả

If you are a PHP developer and you want to learn how to write better code, with fewer bugs, you will need to know how to write unit tests.

Learn how to unit test your PHP code using the PHPUnit testing framework.

  • Learn what unit tests are

  • Learn how to write unit tests using PHPUnit

  • Build a test suite for PHP functions and classes

  • Make test methods dependent on each other

  • Use test doubles: mocks and stubs, including how to use Mockery

  • Learn how and why you should use dependency injection

  • Learn test-driven development (TDD)

  • Learn how to test protected and private methods and properties (and whether you actually should or not!)

  • Learn how to test abstract classes

A skill that will enable you to write higher quality code, in a shorter time, with better documentation and fewer bugs.

Learning how to write unit tests for your code will make you write better code. With automated tests, you can find bugs more easily, and if you break something you'll notice straight away.

Many experienced developers write unit tests for their code. It's a repeatable way of checking that the code works as intended, and also serves as documentation. Many popular PHP projects are unit tested, such as WordPress and Moodle, and if you use frameworks such as Laravel or Symfony, these provide tight integration with the PHPUnit testing framework.

Content and Overview

How do you currently test your code? Maybe you run your code in a browser, use var_dump a lot and so on. The problem with this is that you only tend to test new code - you don't go back and test old code. What if you add some new code that breaks the old code?

With automated unit tests, you have more control. If your new code breaks something, you'll notice straight away. You can fix bugs more easily, thereby reducing development time.

Tests are also a form of documentation, just like comments: they serve to show why the code has been written that way.

  • Suitable for all PHP developers, you'll start by learning the basics of unit testing PHP code, including how to use assertions.

  • Then you'll learn how to test functions and classes, and how to configure the PHPUnit framework.

  • Next you'll learn how to use test dependencies, fixtures and how to test for exceptions.

  • You'll also see how test doubles work - mock objects and stub methods.

  • Then you'll learn how to write code using test-driven development - writing the tests first, then the code.

  • You'll also learn how to test protected and private class methods and properties, and whether this is actually a good idea or not.

  • Testing static methods is covered in detail, including several methods for dealing with static dependencies.

When you complete the course you'll be able to write unit tests for your existing PHP code, and develop new code using test-driven development.

Complete with all the code shown in the lectures, you'll be able to work alongside the instructor and will receive a verifiable certificate of completion upon finishing the course.

Also, at all times throughout the course you have access to the instructor in the Q&A section to ask for help with any topic related to the course.

Enrol now and add unit testing to your PHP skillset!

Bạn sẽ học được gì

Write automated tests for your functions and classes

Write better code with less bugs, in less time

Develop code using test-driven development

Know that the code you write works how you want it to

Have improved documentation for your code

Get support directly from the instructor

Yêu cầu

  • You must know PHP, including object-oriented code
  • Be happy running commands in the terminal / command line on your computer
  • Ideally have PHP and Composer installed, but a brief overview of installing these will be covered

Nội dung khoá học

10 sections

Introduction and project setup

5 lectures
Introduction
01:46
Install PHP and Composer (Windows)
02:19
Install PHP and Composer (macOS)
01:40
Install PHP and Composer (Linux)
01:31
Create a project folder and install PHPUnit
01:57

Unit testing with PHPUnit: the basics

7 lectures
Write and run your first test: an introduction to assertions
04:15
Test a function: using multiple assertions
03:00
Test that incorrect results are not returned: using multiple test methods
02:45
Test a class: fixing bugs through testing
04:30
How to name your test methods
03:04
Unit testing with PHPUnit: the basics
6 questions
Write your first PHPUnit test method
1 question

Configuring the PHPUnit test environment

4 lectures
The PHPUnit test runner: specify different options when running tests
03:03
Configure PHPUnit: the XML configuration file
04:05
Autoload classes being tested using Composer
03:58
Configuring the PHPUnit test environment
3 questions

Test dependencies, fixtures and exceptions

8 lectures
Unit test a queue class
04:16
Test dependencies: make one test method dependent on another
03:02
Fixtures: set up the known state of the tests using setUp and tearDown
04:38
Easily add a new test method using the test fixture
02:07
Share fixtures between tests for resource-intensive data
04:08
Testing exceptions: expecting code to throw an exception
04:09
Test dependencies, fixtures and exceptions
5 questions
Test fixtures
1 question

Test doubles: mocks and stubs

5 lectures
Test doubles: create mock objects to remove dependencies on external resources
05:35
Dependency injection: inject objects that a class depends on
05:21
Test object interactions: verify how a dependency is used
04:46
Customise the creation of the mock object: the getMockBuilder method
05:22
Test doubles: mocks and stubs
5 questions

Mockery - an alternative mocking framework with a human-readable syntax

5 lectures
Mock a dependency that doesn't exist yet
04:11
Using Mockery: installation and integration with PHPUnit
02:21
Using Mockery to mock a dependency that doesn't exist yet
03:38
PHPUnit vs Mockery: Returning different values on subsequent method calls
05:46
Mockery Spies: Make assertions on a call after the event
04:37

Test-driven development

7 lectures
Test-driven development: set up a TDD project
03:09
Write the test first, then write the code to make it pass
03:16
Write just enough code to make the test pass
03:19
Add tests and refactor code until the tests pass
03:14
Add more tests and application code functionality
03:49
Another test, more functionality, and more refactoring
02:55
Data providers: provide a test method with a collection of data
05:50

Testing non-public methods and properties and abstract classes

6 lectures
Testing non-public methods
03:09
Testing protected methods using inheritance
03:17
Testing private methods using reflection
02:22
Testing private methods with arguments using reflection
02:43
Testing protected and private attributes
01:56
Testing abstract classes
04:47

Testing static methods

5 lectures
Testing static methods
02:52
The problem with static methods: dependencies and mocking
04:07
Option 1: Refactor code to remove the static method
03:37
Option 2: Pass the dependency as a callable
04:29
Option 3: Use Mockery alias mocks to stub the static method call
02:37

Conclusion

2 lectures
Conclusion
01:01
Bonus Lecture: discount PHP hosting
00:03

Đánh giá của học viên

Chưa có đánh giá
Course Rating
5
0%
4
0%
3
0%
2
0%
1
0%

Bình luận khách hàng

Viết Bình Luận

Bạn đánh giá khoá học này thế nào?

image

Đăng ký get khoá học Udemy - Unica - Gitiho giá chỉ 50k!

Get khoá học giá rẻ ngay trước khi bị fix.