Setup PHP Unit Testing with Composer
33sQuick, practical setup guide for PHP developers wanting to add unit testing.
▶ Play ClipThis video demonstrates how to set up PHPUnit testing with Composer for a simple date format conversion service. The tutorial covers project initialization, installing PHPUnit, writing a basic test, and running tests via both the command line and Composer scripts.
Initialize a Composer project with autoload mapping for the src directory.
Ensure the service namespace matches the autoload configuration in composer.json.
Install phpunit/phpunit as a dev dependency using Composer.
Create a test namespace and a test class extending PHPUnit\Framework\TestCase.
Write a test method that intentionally fails to verify PHPUnit is working.
Run the test using php vendor/bin/phpunit tests and confirm the failure message.
Add a 'test' script in composer.json to simplify running tests with phpunit and --testdox flag.
The video successfully demonstrates setting up PHPUnit with Composer, writing a basic test, and running it efficiently. Future videos will cover IDE integration.
"Title accurately describes the content: setting up PHP testing with Composer and PHPUnit."
What command initializes a Composer project with autoload mapping to src?
composer init --quiet --name="date-service" --autoload="src"
0:09
How do you install PHPUnit as a dev dependency?
composer require phpunit/phpunit --dev
1:06
What is the naming convention for a test class?
The test class should have the method or class name you want to test with 'Test' appended (uppercase T).
1:39
What is the naming convention for a test method?
Test methods should start with lowercase 'test' followed by the test name.
2:02
How do you run PHPUnit tests from the command line?
php vendor/bin/phpunit tests
2:21
How can you shorten the test command using Composer?
Add a script in composer.json: "test": "php vendor/bin/phpunit tests --testdox"
2:47
Composer Init Command
Shows the exact command to set up a PHP project with autoloading.
0:09PHPUnit Dev Dependency
Demonstrates installing PHPUnit only for development environment.
1:06Intentionally Failing Test
Verifies PHPUnit is working by writing a test that fails with a custom message.
2:08Composer Script Shortcut
Optimizes workflow by creating a composer test script with --testdox flag.
2:47[00:00] Today I will walk you through writing Unittest,
[00:01] date format to a different format.
[00:09] So let's start the setup by opening the terminal
[00:09] I will put the quite flag and let's name our project
[00:17] and we add the autoload flag with a and we write the src.
[00:23] This will allows us to import our service
[00:29] because our service is in the src directory.
[00:33] This command will create a vendor directory
[00:38] our dependencies and project configuration.
[00:41] it wrote the name as we wrote in our command
[00:48] the src directory, now let's navigate to our
[00:53] namespace.
[00:54] let's correct the namespace like it is defined
[00:59] Let's navigate back to our composer.json to
[01:06] testing, we can use the terminal composer
[01:12] dependency is only required in development
[01:16] successfully installed.
[01:17] let's create a namespace for our test by copying
[01:26] and the directory should be the "src" a non
[01:32] our IDE. and let us create a class into it
[01:39] As a naming convention the test class should
[01:44] like to test and test with uppercase T.
[01:52] create the class.
[01:53] First let me clean up the import.
[01:56] and then I will write a test method, as a
[02:02] with lower case test and the name of our test.
[02:08] The purpose of this video is to successfully
[02:14] therefor lets purposly fail our test and write
[02:21] let's open the terminal and run the test using
[02:28] and as you see our test failed successfully,
[02:33] our message that we provided is here.
[02:36] And there is our message.
[02:37] very nice our test seams to be working, one
[02:43] our test command because it is long!.
[02:47] we can do that in the composer.json using
[02:52] to the value that will be executed when the
[03:00] vendor/bin/phpunit" then let's add our directory
[03:06] the test in the console we add the --testdox.
[03:10] And that's it, let's run now our new command
[03:13] our command seams to call the correct value
[03:19] Ladies and Gentleman this was how to setup
[03:26] phpunit command and the composer test, next
[03:31] run out test in the IDE, I hope you enjoyed
⚡ Saved you time reading this? Transcribe any YouTube video for free — no signup needed.