Example project to demonstrate how to use PHPUnit and Selenium with certain test cases that you need to implement.
- Docker
- IDE / Text editor
Get a clone from GitHub or alternatively just download codes as a zip archive and extract those to your computer.
git clone https://github.com/tarlepp/how-to-use-phpunit.git
After you've got the codes you need to install required dependencies via docker with following command:
docker run -v $(pwd):/app composer install
Note that this command must be run on the same directory where you cloned or unzipped project source files.
Also note that installation is made via composer container - so no need for extra installations to get that working.
- Within commands use
%cd%
(in PowerShell use${PWD}
) instead of$(pwd)
- Check that you've activated shared drives, open Docker settings from your tray and open
Shared Drives
- If you're using eg. VirtualBox or VMware you need to disable
Hyper-V
manually if/when you want to use those again
To start all necessary containers you need to run following command.
docker-compose up -d
This command will download and build all containers at first runtime - and it will take some time.
After previous command is run without errors, you can run phpunit
inside
container with following command.
docker-compose exec phpunit ./bin/phpunit
You will get test output to your shell where you'll see all possible errors and other information about those tests.
Note that at first time you run this command it will install necessary packages for PhpUnit.
You can run this command at any time you want to run tests again
Implement all tests. See next chapter for instructions.
When your containers are running all code changes are synced automatically to containers - so you don't need to worried about that at all.
Application source code lives under ./src
directory. Note that application
is not a real application - just simple classes that you need to test.
Application itself is built with Symfony framework.
You can find all the tests under ./tests
directory. Tests are divided to
three (3) categories:
- Functional
- Integration
- Unit
By default there is base structure for each test that you need to implement by yourself.
After you have run the tests you can see actual code coverage report under
./build/report/
directory - just open that index.html
file with your
favorite browser to see if you missed something in your tests.
Within Selenium tests it's quite usefully to actually see what browser is actually doing - no worries this is also covered.
For this you need a VNC Viewer so download and install it first.
Next step is to change default password for VNC connection with following command:
docker-compose exec chrome x11vnc -storepasswd <your_password_here> /home/seluser/.vnc/passwd
After that open VNC Viewer and connect to 0.0.0.0:5900
There's few useful commands you can use to manage with containers.
# get shell access to running container
docker-compose exec phpunit /bin/bash
# stop containers
docker-compose down
Need more? Just run docker
or docker-compose
and you will be provided a
list of available commands or give online documentation
a change.
Copyright (c) 2018 Tarmo Leppänen