This is a skeleton project to work on coding katas using PHP and PHPUnit. Follow the instructions below to set up the project and run the tests.
- PHP 7.4 or higher
- Composer
- VSCode (optional, for debugging with Xdebug)
-
Clone the repository
git clone https://github.com/zendyani/php-basic-skeleton.git cd php-basic-skeleton
-
Install dependencies
Make sure you have Composer installed, then run:
composer install
src/
: This directory contains the source code of your project.tests/
: This directory contains the test cases for your project.
To run the tests, use the following command:
composer test
You can also generate a code coverage report by running:
vendor/bin/phpunit --coverage-html coverage
The coverage report will be generated in the coverage/
directory.
To check and fix coding standards, use the following command:
composer lint
To perform static analysis on the code, use the following command:
composer analyse
To enable Xdebug for debugging, ensure that your php.ini
has the correct configuration. Here is an example configuration for Xdebug 3.x:
[xdebug]
zend_extension=xdebug
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=localhost
xdebug.client_port=9003
Make sure your debugging client (IDE) is set up to listen on the same port (9003). Below is an example configuration for VSCode:
VSCode launch.json
example:
- Open your VSCode workspace and create or open a
.vscode
directory. - Create a
launch.json
file in the.vscode
directory with the following content:{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Listen for Xdebug", "type": "php", "request": "launch", "port": [ 9003 ] } ] }
- Open the Run and Debug panel by clicking the play icon in the sidebar or pressing
Ctrl+Shift+D
. - Select "Listen for XDebug" from the configuration dropdown at the top.
- Click the green play button or press
F5
to start listening for Xdebug connections.
After starting the Xdebug listener in VSCode, you can run your tests with Xdebug enabled:
composer test
If you don't need Xdebug for running your PHPUnit tests, you can disable it during test execution:
XDEBUG_MODE=off ./vendor/bin/phpunit
Alternatively, you can modify the phpunit.xml
configuration to not set the XDEBUG_MODE
environment variable.
To create a new test case:
- Create a new file in the
tests/
directory with the name of the class you want to test, followed byTest.php
. For example, if you are testing a class namedCalculator
, create a file namedCalculatorTest.php
. - Inside the test file, create a class that extends
PHPUnit\Framework\TestCase
. - Write test methods inside the test class. Each test method should be prefixed with the word
test
.
Example test file:
<?php
use PHPUnit\Framework\TestCase;
class CalculatorTest extends TestCase
{
public function testAddition()
{
$calculator = new Calculator();
$this->assertEquals(4, $calculator->add(2, 2));
}
}
Feel free to fork this repository and create pull requests. If you find any issues, please open an issue on GitHub.
This project is licensed under the MIT License. See the LICENSE file for details.